2009-09-24 21:35:06 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
#include "CoreAudioSoundStream.h"
|
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
typedef struct internal
|
|
|
|
{
|
|
|
|
AudioUnit audioUnit;
|
|
|
|
short realtimeBuffer[1024 * 1024];
|
|
|
|
};
|
2009-09-24 21:35:06 +00:00
|
|
|
|
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
OSStatus callback(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) {
|
|
|
|
|
|
|
|
UInt32 remaining, len;
|
|
|
|
void *ptr;
|
|
|
|
AudioBuffer *data;
|
|
|
|
|
|
|
|
internal *soundStruct = (internal *)inRefCon;
|
|
|
|
|
|
|
|
data = &ioData->mBuffers[0];
|
|
|
|
len = data->mDataByteSize;
|
|
|
|
ptr = data->mData;
|
|
|
|
|
|
|
|
memcpy(ptr, soundStruct->realtimeBuffer, len);
|
2009-09-24 21:35:06 +00:00
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
return 0;
|
2009-09-24 21:35:06 +00:00
|
|
|
}
|
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-24 21:35:06 +00:00
|
|
|
void CoreAudioSound::SoundLoop()
|
|
|
|
{
|
|
|
|
CoreAudioInit();
|
|
|
|
|
|
|
|
}
|
|
|
|
CoreAudioSound::CoreAudioSound(CMixer *mixer) : SoundStream(mixer)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CoreAudioSound::~CoreAudioSound()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
bool CoreAudioSound::CoreAudioInit()
|
|
|
|
{
|
2009-10-23 17:10:27 +00:00
|
|
|
ComponentDescription desc;
|
|
|
|
OSStatus err;
|
|
|
|
UInt32 enableIO;
|
|
|
|
AURenderCallbackStruct callback_struct;
|
|
|
|
UInt32 shouldAllocateBuffer = 1;
|
|
|
|
AudioStreamBasicDescription format;
|
2009-09-24 21:35:06 +00:00
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
internal *soundStruct = (internal *)malloc(sizeof(internal));
|
|
|
|
|
|
|
|
desc.componentType = kAudioUnitType_Output;
|
|
|
|
desc.componentSubType = kAudioUnitSubType_HALOutput;
|
|
|
|
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
|
|
|
|
|
|
|
Component component = FindNextComponent(NULL, &desc);
|
|
|
|
if (component == NULL)
|
|
|
|
printf("error finding component\n");
|
2009-09-24 21:35:06 +00:00
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
err = OpenAComponent(component, &soundStruct->audioUnit);
|
|
|
|
if (err)
|
|
|
|
printf("error opening audio component\n");
|
|
|
|
|
|
|
|
//enable output device
|
|
|
|
enableIO = 1;
|
|
|
|
AudioUnitSetProperty(soundStruct->audioUnit,
|
|
|
|
kAudioOutputUnitProperty_EnableIO,
|
|
|
|
kAudioUnitScope_Output,
|
|
|
|
0,
|
|
|
|
&enableIO,
|
|
|
|
sizeof(enableIO));
|
2009-09-24 21:35:06 +00:00
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
AudioUnitSetProperty(soundStruct->audioUnit, kAudioUnitProperty_ShouldAllocateBuffer, kAudioUnitScope_Global, 1, &shouldAllocateBuffer, sizeof(shouldAllocateBuffer));
|
|
|
|
if (err)
|
|
|
|
printf("error while allocate audiounit buffer\n");
|
2009-09-24 21:35:06 +00:00
|
|
|
|
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
format.mBitsPerChannel = 16;
|
|
|
|
format.mChannelsPerFrame = 2;
|
|
|
|
format.mBytesPerPacket = sizeof(float);
|
|
|
|
format.mBytesPerFrame = sizeof(float);
|
|
|
|
format.mFormatFlags = kAudioFormatFlagsNativeFloatPacked | kLinearPCMFormatFlagIsNonInterleaved;
|
|
|
|
format.mFormatID = kAudioFormatLinearPCM;
|
|
|
|
format.mFramesPerPacket = 1;
|
|
|
|
format.mSampleRate = m_mixer->GetSampleRate();
|
2009-09-24 21:35:06 +00:00
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
//set format to output scope
|
|
|
|
err = AudioUnitSetProperty(soundStruct->audioUnit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Output, 1, &format, sizeof(AudioStreamBasicDescription));
|
|
|
|
if (err)
|
|
|
|
printf("error when setting output format\n");
|
|
|
|
|
2009-09-24 21:35:06 +00:00
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
callback_struct.inputProc = callback;
|
|
|
|
callback_struct.inputProcRefCon = soundStruct;
|
2009-09-24 21:35:06 +00:00
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
err = AudioUnitSetProperty(soundStruct->audioUnit, kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Global, 0, &callback_struct, sizeof(callback_struct));
|
|
|
|
if (err)
|
|
|
|
printf("error when setting output callback\n");
|
|
|
|
|
|
|
|
err = AudioUnitInitialize(soundStruct->audioUnit);
|
|
|
|
if (err)
|
|
|
|
printf("error when initiaising audiounit\n");
|
|
|
|
|
|
|
|
err = AudioOutputUnitStart(soundStruct->audioUnit);
|
|
|
|
if (err)
|
|
|
|
printf("error when stating audiounit\n");
|
|
|
|
|
|
|
|
while(!threadData)
|
|
|
|
{
|
|
|
|
m_mixer->Mix(soundStruct->realtimeBuffer, 2048);
|
|
|
|
}
|
|
|
|
|
2009-09-24 21:35:06 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
void *coreAudioThread(void *args)
|
|
|
|
{
|
|
|
|
|
|
|
|
((CoreAudioSound *)args)->SoundLoop();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-24 21:35:06 +00:00
|
|
|
bool CoreAudioSound::Start()
|
|
|
|
{
|
2009-10-23 17:10:27 +00:00
|
|
|
|
|
|
|
soundSyncEvent.Init();
|
|
|
|
|
|
|
|
thread = new Common::Thread(coreAudioThread, (void *)this);
|
|
|
|
|
2009-09-24 21:35:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CoreAudioSound::Stop()
|
|
|
|
{
|
2009-10-23 17:10:27 +00:00
|
|
|
delete thread;
|
|
|
|
thread = NULL;
|
|
|
|
|
2009-09-24 21:35:06 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
void CoreAudioSound::Update()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
|
2009-10-23 17:10:27 +00:00
|
|
|
}
|