mirror of https://github.com/libsdl-org/SDL
coreaudio: Move from AudioUnits to AudioQueues.
AudioQueues are available in Mac OS X 10.5 and later (and iOS 2.0 and later). Their API is much more clear (and if you don't mind the threading tapdance to get its own CFRunLoop) much easier to use in general for our purposes. As an added benefit: they seemlessly deal with format conversion in ways AudioUnits don't: for example, my MacBook Pro's built-in microphone won't capture at 8000Hz and the AudioUnit version wouldn't resample to hide this fact; the AudioQueue version, however, can handle this.
This commit is contained in:
parent
3b53304a94
commit
0265d3af9b
|
@ -29,6 +29,7 @@
|
||||||
#include "../SDL_sysaudio.h"
|
#include "../SDL_sysaudio.h"
|
||||||
#include "SDL_coreaudio.h"
|
#include "SDL_coreaudio.h"
|
||||||
#include "SDL_assert.h"
|
#include "SDL_assert.h"
|
||||||
|
#include "../../thread/SDL_systhread.h"
|
||||||
|
|
||||||
#define DEBUG_COREAUDIO 0
|
#define DEBUG_COREAUDIO 0
|
||||||
|
|
||||||
|
@ -288,42 +289,20 @@ static void update_audio_session()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* The CoreAudio callback */
|
/* The AudioQueue callback */
|
||||||
static OSStatus
|
static void
|
||||||
outputCallback(void *inRefCon,
|
outputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer)
|
||||||
AudioUnitRenderActionFlags * ioActionFlags,
|
|
||||||
const AudioTimeStamp * inTimeStamp,
|
|
||||||
UInt32 inBusNumber, UInt32 inNumberFrames,
|
|
||||||
AudioBufferList * ioData)
|
|
||||||
{
|
{
|
||||||
SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
|
SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData;
|
||||||
AudioBuffer *abuf;
|
|
||||||
UInt32 remaining, len;
|
|
||||||
void *ptr;
|
|
||||||
UInt32 i;
|
|
||||||
|
|
||||||
/* Only do anything if audio is enabled and not paused */
|
|
||||||
if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
|
if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
|
||||||
for (i = 0; i < ioData->mNumberBuffers; i++) {
|
/* Supply silence if audio is enabled and not paused */
|
||||||
abuf = &ioData->mBuffers[i];
|
SDL_memset(inBuffer->mAudioData, this->spec.silence, inBuffer->mAudioDataBytesCapacity);
|
||||||
SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
|
} else {
|
||||||
}
|
UInt32 remaining = inBuffer->mAudioDataBytesCapacity;
|
||||||
return 0;
|
Uint8 *ptr = (Uint8 *) inBuffer->mAudioData;
|
||||||
}
|
|
||||||
|
|
||||||
/* No SDL conversion should be needed here, ever, since we accept
|
|
||||||
any input format in OpenAudio, and leave the conversion to CoreAudio.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
SDL_assert(!this->convert.needed);
|
|
||||||
SDL_assert(this->spec.channels == ioData->mNumberChannels);
|
|
||||||
*/
|
|
||||||
|
|
||||||
for (i = 0; i < ioData->mNumberBuffers; i++) {
|
|
||||||
abuf = &ioData->mBuffers[i];
|
|
||||||
remaining = abuf->mDataByteSize;
|
|
||||||
ptr = abuf->mData;
|
|
||||||
while (remaining > 0) {
|
while (remaining > 0) {
|
||||||
|
UInt32 len;
|
||||||
if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
|
if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
|
||||||
/* Generate the data */
|
/* Generate the data */
|
||||||
SDL_LockMutex(this->mixer_lock);
|
SDL_LockMutex(this->mixer_lock);
|
||||||
|
@ -334,51 +313,39 @@ outputCallback(void *inRefCon,
|
||||||
}
|
}
|
||||||
|
|
||||||
len = this->hidden->bufferSize - this->hidden->bufferOffset;
|
len = this->hidden->bufferSize - this->hidden->bufferOffset;
|
||||||
if (len > remaining)
|
if (len > remaining) {
|
||||||
len = remaining;
|
len = remaining;
|
||||||
|
}
|
||||||
SDL_memcpy(ptr, (char *)this->hidden->buffer +
|
SDL_memcpy(ptr, (char *)this->hidden->buffer +
|
||||||
this->hidden->bufferOffset, len);
|
this->hidden->bufferOffset, len);
|
||||||
ptr = (char *)ptr + len;
|
ptr = ptr + len;
|
||||||
remaining -= len;
|
remaining -= len;
|
||||||
this->hidden->bufferOffset += len;
|
this->hidden->bufferOffset += len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return noErr;
|
if (!SDL_AtomicGet(&this->hidden->shutdown)) {
|
||||||
}
|
AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL);
|
||||||
|
|
||||||
static OSStatus
|
|
||||||
inputCallback(void *inRefCon,
|
|
||||||
AudioUnitRenderActionFlags *ioActionFlags,
|
|
||||||
const AudioTimeStamp *inTimeStamp,
|
|
||||||
UInt32 inBusNumber, UInt32 inNumberFrames,
|
|
||||||
AudioBufferList *ioData)
|
|
||||||
{
|
|
||||||
AudioBufferList bufferList;
|
|
||||||
SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
|
|
||||||
if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
|
|
||||||
return noErr; /* just drop this if we're not accepting input. */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bufferList.mNumberBuffers = 1;
|
inBuffer->mAudioDataByteSize = inBuffer->mAudioDataBytesCapacity;
|
||||||
bufferList.mBuffers[0].mData = NULL;
|
}
|
||||||
|
|
||||||
const OSStatus err = AudioUnitRender(this->hidden->audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &bufferList);
|
static void
|
||||||
|
inputCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer,
|
||||||
if (err == noErr) {
|
const AudioTimeStamp *inStartTime, UInt32 inNumberPacketDescriptions,
|
||||||
const AudioBuffer *abuf = &bufferList.mBuffers[0];
|
const AudioStreamPacketDescription *inPacketDescs )
|
||||||
UInt32 remaining = abuf->mDataByteSize;
|
{
|
||||||
const Uint8 *ptr = (const Uint8 *) abuf->mData;
|
SDL_AudioDevice *this = (SDL_AudioDevice *) inUserData;
|
||||||
|
if (SDL_AtomicGet(&this->enabled) && !SDL_AtomicGet(&this->paused)) { /* ignore unless we're active. */
|
||||||
/* No SDL conversion should be needed here, ever, since we accept
|
const Uint8 *ptr = (const Uint8 *) inBuffer->mAudioData;
|
||||||
any input format in OpenAudio, and leave the conversion to CoreAudio.
|
UInt32 remaining = inBuffer->mAudioDataByteSize;
|
||||||
*/
|
|
||||||
while (remaining > 0) {
|
while (remaining > 0) {
|
||||||
UInt32 len = this->hidden->bufferSize - this->hidden->bufferOffset;
|
UInt32 len = this->hidden->bufferSize - this->hidden->bufferOffset;
|
||||||
if (len > remaining)
|
if (len > remaining) {
|
||||||
len = remaining;
|
len = remaining;
|
||||||
|
}
|
||||||
|
|
||||||
/* !!! FIXME: why are we copying here? just pass the buffer to the callback? */
|
|
||||||
SDL_memcpy((char *)this->hidden->buffer + this->hidden->bufferOffset, ptr, len);
|
SDL_memcpy((char *)this->hidden->buffer + this->hidden->bufferOffset, ptr, len);
|
||||||
ptr += len;
|
ptr += len;
|
||||||
remaining -= len;
|
remaining -= len;
|
||||||
|
@ -386,15 +353,16 @@ inputCallback(void *inRefCon,
|
||||||
|
|
||||||
if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
|
if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
|
||||||
SDL_LockMutex(this->mixer_lock);
|
SDL_LockMutex(this->mixer_lock);
|
||||||
(*this->spec.callback)(this->spec.userdata,
|
(*this->spec.callback)(this->spec.userdata, this->hidden->buffer, this->hidden->bufferSize);
|
||||||
this->hidden->buffer, this->hidden->bufferSize);
|
|
||||||
SDL_UnlockMutex(this->mixer_lock);
|
SDL_UnlockMutex(this->mixer_lock);
|
||||||
this->hidden->bufferOffset = 0;
|
this->hidden->bufferOffset = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return noErr;
|
if (!SDL_AtomicGet(&this->hidden->shutdown)) {
|
||||||
|
AudioQueueEnqueueBuffer(this->hidden->audioQueue, inBuffer, 0, NULL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -439,30 +407,35 @@ device_unplugged(AudioObjectID devid, UInt32 num_addr, const AudioObjectProperty
|
||||||
static void
|
static void
|
||||||
COREAUDIO_CloseDevice(_THIS)
|
COREAUDIO_CloseDevice(_THIS)
|
||||||
{
|
{
|
||||||
const int iscapture = this->iscapture;
|
const SDL_bool iscapture = this->iscapture;
|
||||||
|
int i;
|
||||||
|
|
||||||
if (this->hidden->audioUnitOpened) {
|
/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
|
||||||
const AudioUnitElement output_bus = 0;
|
/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
|
||||||
const AudioUnitElement input_bus = 1;
|
#if MACOSX_COREAUDIO
|
||||||
const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
|
/* Fire a callback if the device stops being "alive" (disconnected, etc). */
|
||||||
AURenderCallbackStruct callback;
|
|
||||||
|
|
||||||
#if MACOSX_COREAUDIO
|
|
||||||
/* Unregister our disconnect callback. */
|
|
||||||
AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
|
AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* stop processing the audio unit */
|
if (this->hidden->thread) {
|
||||||
AudioOutputUnitStop(this->hidden->audioUnit);
|
SDL_AtomicSet(&this->hidden->shutdown, 1);
|
||||||
|
SDL_WaitThread(this->hidden->thread, NULL);
|
||||||
/* Remove the input callback */
|
|
||||||
SDL_zero(callback);
|
|
||||||
AudioUnitSetProperty(this->hidden->audioUnit,
|
|
||||||
iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
|
|
||||||
kAudioUnitScope_Global, bus, &callback, sizeof(callback));
|
|
||||||
AudioComponentInstanceDispose(this->hidden->audioUnit);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this->hidden->audioQueue) {
|
||||||
|
for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
|
||||||
|
if (this->hidden->audioBuffer[i]) {
|
||||||
|
AudioQueueFreeBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AudioQueueDispose(this->hidden->audioQueue, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this->hidden->ready_semaphore) {
|
||||||
|
SDL_DestroySemaphore(this->hidden->ready_semaphore);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_free(this->hidden->thread_error);
|
||||||
SDL_free(this->hidden->buffer);
|
SDL_free(this->hidden->buffer);
|
||||||
SDL_free(this->hidden);
|
SDL_free(this->hidden);
|
||||||
|
|
||||||
|
@ -530,103 +503,44 @@ prepare_device(_THIS, void *handle, int iscapture)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int
|
static int
|
||||||
prepare_audiounit(_THIS, void *handle, int iscapture,
|
prepare_audioqueue(_THIS)
|
||||||
const AudioStreamBasicDescription * strdesc)
|
|
||||||
{
|
{
|
||||||
OSStatus result = noErr;
|
const AudioStreamBasicDescription *strdesc = &this->hidden->strdesc;
|
||||||
AURenderCallbackStruct callback;
|
const int iscapture = this->iscapture;
|
||||||
AudioComponentDescription desc;
|
OSStatus result;
|
||||||
AudioComponent comp = NULL;
|
int i;
|
||||||
const AudioUnitElement output_bus = 0;
|
|
||||||
const AudioUnitElement input_bus = 1;
|
|
||||||
|
|
||||||
#if MACOSX_COREAUDIO
|
SDL_assert(CFRunLoopGetCurrent() != NULL);
|
||||||
if (!prepare_device(this, handle, iscapture)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SDL_zero(desc);
|
if (iscapture) {
|
||||||
desc.componentType = kAudioUnitType_Output;
|
result = AudioQueueNewInput(strdesc, inputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue);
|
||||||
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
|
CHECK_RESULT("AudioQueueNewInput");
|
||||||
|
} else {
|
||||||
#if MACOSX_COREAUDIO
|
result = AudioQueueNewOutput(strdesc, outputCallback, this, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &this->hidden->audioQueue);
|
||||||
desc.componentSubType = iscapture ? kAudioUnitSubType_HALOutput : kAudioUnitSubType_DefaultOutput;
|
CHECK_RESULT("AudioQueueNewOutput");
|
||||||
#else
|
|
||||||
desc.componentSubType = kAudioUnitSubType_RemoteIO;
|
|
||||||
#endif
|
|
||||||
comp = AudioComponentFindNext(NULL, &desc);
|
|
||||||
|
|
||||||
if (comp == NULL) {
|
|
||||||
SDL_SetError("Couldn't find requested CoreAudio component");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Open & initialize the audio unit */
|
|
||||||
result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
|
|
||||||
CHECK_RESULT("AudioComponentInstanceNew");
|
|
||||||
|
|
||||||
this->hidden->audioUnitOpened = SDL_TRUE;
|
|
||||||
|
|
||||||
if (iscapture) { /* have to do EnableIO only for capture devices. */
|
|
||||||
UInt32 enable = 1;
|
|
||||||
result = AudioUnitSetProperty(this->hidden->audioUnit,
|
|
||||||
kAudioOutputUnitProperty_EnableIO,
|
|
||||||
kAudioUnitScope_Input, input_bus,
|
|
||||||
&enable, sizeof (enable));
|
|
||||||
CHECK_RESULT
|
|
||||||
("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO input bus)");
|
|
||||||
|
|
||||||
enable = 0;
|
|
||||||
result = AudioUnitSetProperty(this->hidden->audioUnit,
|
|
||||||
kAudioOutputUnitProperty_EnableIO,
|
|
||||||
kAudioUnitScope_Output, output_bus,
|
|
||||||
&enable, sizeof (enable));
|
|
||||||
CHECK_RESULT
|
|
||||||
("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO output bus)");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#if MACOSX_COREAUDIO
|
#if MACOSX_COREAUDIO
|
||||||
/* this is always on the output_bus, even for capture devices. */
|
{
|
||||||
result = AudioUnitSetProperty(this->hidden->audioUnit,
|
const AudioObjectPropertyAddress prop = {
|
||||||
kAudioOutputUnitProperty_CurrentDevice,
|
kAudioDevicePropertyDeviceUID,
|
||||||
kAudioUnitScope_Global, output_bus,
|
iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
|
||||||
&this->hidden->deviceID,
|
kAudioObjectPropertyElementMaster
|
||||||
sizeof(AudioDeviceID));
|
};
|
||||||
CHECK_RESULT
|
CFStringRef devuid;
|
||||||
("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
|
UInt32 devuidsize = sizeof (devuid);
|
||||||
|
result = AudioObjectGetPropertyData(this->hidden->deviceID, &prop, 0, NULL, &devuidsize, &devuid);
|
||||||
|
CHECK_RESULT("AudioObjectGetPropertyData (kAudioDevicePropertyDeviceUID)");
|
||||||
|
result = AudioQueueSetProperty(this->hidden->audioQueue, kAudioQueueProperty_CurrentDevice, &devuid, devuidsize);
|
||||||
|
CHECK_RESULT("AudioQueueSetProperty (kAudioQueueProperty_CurrentDevice)");
|
||||||
|
|
||||||
|
/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
|
||||||
|
/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
|
||||||
|
/* Fire a callback if the device stops being "alive" (disconnected, etc). */
|
||||||
|
AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Set the data format of the audio unit. */
|
|
||||||
result = AudioUnitSetProperty(this->hidden->audioUnit,
|
|
||||||
kAudioUnitProperty_StreamFormat,
|
|
||||||
iscapture ? kAudioUnitScope_Output : kAudioUnitScope_Input,
|
|
||||||
iscapture ? input_bus : output_bus,
|
|
||||||
strdesc, sizeof (*strdesc));
|
|
||||||
CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
|
|
||||||
|
|
||||||
if (iscapture) { /* only need to do this for capture devices. */
|
|
||||||
const UInt32 yes = 1;
|
|
||||||
result = AudioUnitSetProperty(this->hidden->audioUnit,
|
|
||||||
kAudioUnitProperty_ShouldAllocateBuffer,
|
|
||||||
kAudioUnitScope_Output,
|
|
||||||
input_bus, &yes, sizeof (yes));
|
|
||||||
CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_ShouldAllocateBuffer)");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set the audio callback */
|
|
||||||
SDL_zero(callback);
|
|
||||||
callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
|
|
||||||
callback.inputProcRefCon = this;
|
|
||||||
|
|
||||||
result = AudioUnitSetProperty(this->hidden->audioUnit,
|
|
||||||
iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
|
|
||||||
kAudioUnitScope_Global,
|
|
||||||
iscapture ? input_bus : output_bus,
|
|
||||||
&callback, sizeof (callback));
|
|
||||||
CHECK_RESULT
|
|
||||||
("AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)");
|
|
||||||
|
|
||||||
/* Calculate the final parameters for this audio specification */
|
/* Calculate the final parameters for this audio specification */
|
||||||
SDL_CalculateAudioSpec(&this->spec);
|
SDL_CalculateAudioSpec(&this->spec);
|
||||||
|
|
||||||
|
@ -640,29 +554,54 @@ prepare_audiounit(_THIS, void *handle, int iscapture,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = AudioUnitInitialize(this->hidden->audioUnit);
|
for (i = 0; i < SDL_arraysize(this->hidden->audioBuffer); i++) {
|
||||||
CHECK_RESULT("AudioUnitInitialize");
|
result = AudioQueueAllocateBuffer(this->hidden->audioQueue, this->spec.size, &this->hidden->audioBuffer[i]);
|
||||||
|
CHECK_RESULT("AudioQueueAllocateBuffer");
|
||||||
|
SDL_memset(this->hidden->audioBuffer[i]->mAudioData, this->spec.silence, this->hidden->audioBuffer[i]->mAudioDataBytesCapacity);
|
||||||
|
this->hidden->audioBuffer[i]->mAudioDataByteSize = this->hidden->audioBuffer[i]->mAudioDataBytesCapacity;
|
||||||
|
result = AudioQueueEnqueueBuffer(this->hidden->audioQueue, this->hidden->audioBuffer[i], 0, NULL);
|
||||||
|
CHECK_RESULT("AudioQueueEnqueueBuffer");
|
||||||
|
}
|
||||||
|
|
||||||
/* Finally, start processing of the audio unit */
|
result = AudioQueueStart(this->hidden->audioQueue, NULL);
|
||||||
result = AudioOutputUnitStart(this->hidden->audioUnit);
|
CHECK_RESULT("AudioQueueStart");
|
||||||
CHECK_RESULT("AudioOutputUnitStart");
|
|
||||||
|
|
||||||
/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
|
|
||||||
/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
|
|
||||||
#if MACOSX_COREAUDIO
|
|
||||||
/* Fire a callback if the device stops being "alive" (disconnected, etc). */
|
|
||||||
AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/* We're running! */
|
/* We're running! */
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
audioqueue_thread(void *arg)
|
||||||
|
{
|
||||||
|
SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
|
||||||
|
const int rc = prepare_audioqueue(this);
|
||||||
|
if (!rc) {
|
||||||
|
this->hidden->thread_error = SDL_strdup(SDL_GetError());
|
||||||
|
SDL_SemPost(this->hidden->ready_semaphore);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* init was successful, alert parent thread and start running... */
|
||||||
|
SDL_SemPost(this->hidden->ready_semaphore);
|
||||||
|
while (!SDL_AtomicGet(&this->hidden->shutdown)) {
|
||||||
|
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.10, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this->iscapture) { /* just stop immediately for capture devices. */
|
||||||
|
AudioQueueStop(this->hidden->audioQueue, 1);
|
||||||
|
} else { /* Drain off any pending playback. */
|
||||||
|
AudioQueueStop(this->hidden->audioQueue, 0);
|
||||||
|
const CFTimeInterval secs = (((this->spec.size / (SDL_AUDIO_BITSIZE(this->spec.format) / 8)) / this->spec.channels) / ((CFTimeInterval) this->spec.freq)) * 2.0;
|
||||||
|
CFRunLoopRunInMode(kCFRunLoopDefaultMode, secs, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
||||||
{
|
{
|
||||||
AudioStreamBasicDescription strdesc;
|
AudioStreamBasicDescription *strdesc;
|
||||||
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
|
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
|
||||||
int valid_datatype = 0;
|
int valid_datatype = 0;
|
||||||
|
|
||||||
|
@ -674,6 +613,8 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
||||||
}
|
}
|
||||||
SDL_zerop(this->hidden);
|
SDL_zerop(this->hidden);
|
||||||
|
|
||||||
|
strdesc = &this->hidden->strdesc;
|
||||||
|
|
||||||
if (iscapture) {
|
if (iscapture) {
|
||||||
open_capture_devices++;
|
open_capture_devices++;
|
||||||
} else {
|
} else {
|
||||||
|
@ -682,12 +623,12 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
||||||
update_audio_session();
|
update_audio_session();
|
||||||
|
|
||||||
/* Setup a AudioStreamBasicDescription with the requested format */
|
/* Setup a AudioStreamBasicDescription with the requested format */
|
||||||
SDL_zero(strdesc);
|
SDL_zerop(strdesc);
|
||||||
strdesc.mFormatID = kAudioFormatLinearPCM;
|
strdesc->mFormatID = kAudioFormatLinearPCM;
|
||||||
strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
|
strdesc->mFormatFlags = kLinearPCMFormatFlagIsPacked;
|
||||||
strdesc.mChannelsPerFrame = this->spec.channels;
|
strdesc->mChannelsPerFrame = this->spec.channels;
|
||||||
strdesc.mSampleRate = this->spec.freq;
|
strdesc->mSampleRate = this->spec.freq;
|
||||||
strdesc.mFramesPerPacket = 1;
|
strdesc->mFramesPerPacket = 1;
|
||||||
|
|
||||||
while ((!valid_datatype) && (test_format)) {
|
while ((!valid_datatype) && (test_format)) {
|
||||||
this->spec.format = test_format;
|
this->spec.format = test_format;
|
||||||
|
@ -704,14 +645,14 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
||||||
case AUDIO_F32LSB:
|
case AUDIO_F32LSB:
|
||||||
case AUDIO_F32MSB:
|
case AUDIO_F32MSB:
|
||||||
valid_datatype = 1;
|
valid_datatype = 1;
|
||||||
strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
|
strdesc->mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
|
||||||
if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
|
if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
|
||||||
strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
strdesc->mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
|
||||||
|
|
||||||
if (SDL_AUDIO_ISFLOAT(this->spec.format))
|
if (SDL_AUDIO_ISFLOAT(this->spec.format))
|
||||||
strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
|
strdesc->mFormatFlags |= kLinearPCMFormatFlagIsFloat;
|
||||||
else if (SDL_AUDIO_ISSIGNED(this->spec.format))
|
else if (SDL_AUDIO_ISSIGNED(this->spec.format))
|
||||||
strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
|
strdesc->mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -720,16 +661,37 @@ COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
||||||
return SDL_SetError("Unsupported audio format");
|
return SDL_SetError("Unsupported audio format");
|
||||||
}
|
}
|
||||||
|
|
||||||
strdesc.mBytesPerFrame =
|
strdesc->mBytesPerFrame = strdesc->mBitsPerChannel * strdesc->mChannelsPerFrame / 8;
|
||||||
strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
|
strdesc->mBytesPerPacket = strdesc->mBytesPerFrame * strdesc->mFramesPerPacket;
|
||||||
strdesc.mBytesPerPacket =
|
|
||||||
strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
|
|
||||||
|
|
||||||
if (!prepare_audiounit(this, handle, iscapture, &strdesc)) {
|
#if MACOSX_COREAUDIO
|
||||||
return -1; /* prepare_audiounit() will call SDL_SetError()... */
|
if (!prepare_device(this, handle, iscapture)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* This has to init in a new thread so it can get its own CFRunLoop. :/ */
|
||||||
|
SDL_AtomicSet(&this->hidden->shutdown, 0);
|
||||||
|
this->hidden->ready_semaphore = SDL_CreateSemaphore(0);
|
||||||
|
if (!this->hidden->ready_semaphore) {
|
||||||
|
return -1; /* oh well. */
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0; /* good to go. */
|
this->hidden->thread = SDL_CreateThreadInternal(audioqueue_thread, "AudioQueue thread", 512 * 1024, this);
|
||||||
|
if (!this->hidden->thread) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SemWait(this->hidden->ready_semaphore);
|
||||||
|
SDL_DestroySemaphore(this->hidden->ready_semaphore);
|
||||||
|
this->hidden->ready_semaphore = NULL;
|
||||||
|
|
||||||
|
if ((this->hidden->thread != NULL) && (this->hidden->thread_error != NULL)) {
|
||||||
|
SDL_SetError("%s", this->hidden->thread_error);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (this->hidden->thread != NULL) ? 0 : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -760,6 +722,7 @@ COREAUDIO_Init(SDL_AudioDriverImpl * impl)
|
||||||
/* Set category to ambient sound so that other music continues playing.
|
/* Set category to ambient sound so that other music continues playing.
|
||||||
You can change this at runtime in your own code if you need different
|
You can change this at runtime in your own code if you need different
|
||||||
behavior. If this is common, we can add an SDL hint for this.
|
behavior. If this is common, we can add an SDL hint for this.
|
||||||
|
!!! FIXME: do this when a device is opened, and deinitialize when all devices close.
|
||||||
*/
|
*/
|
||||||
/* !!! FIXME: move this to AVAudioSession. This is deprecated, and the new version is available as of (ancient!) iOS 3.0 */
|
/* !!! FIXME: move this to AVAudioSession. This is deprecated, and the new version is available as of (ancient!) iOS 3.0 */
|
||||||
AudioSessionInitialize(NULL, NULL, NULL, nil);
|
AudioSessionInitialize(NULL, NULL, NULL, nil);
|
||||||
|
|
|
@ -32,10 +32,9 @@
|
||||||
#if MACOSX_COREAUDIO
|
#if MACOSX_COREAUDIO
|
||||||
#include <CoreAudio/CoreAudio.h>
|
#include <CoreAudio/CoreAudio.h>
|
||||||
#include <CoreServices/CoreServices.h>
|
#include <CoreServices/CoreServices.h>
|
||||||
#else
|
|
||||||
#include <AudioToolbox/AudioToolbox.h>
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <AudioToolbox/AudioToolbox.h>
|
||||||
#include <AudioUnit/AudioUnit.h>
|
#include <AudioUnit/AudioUnit.h>
|
||||||
|
|
||||||
/* Hidden "this" pointer for the audio functions */
|
/* Hidden "this" pointer for the audio functions */
|
||||||
|
@ -43,11 +42,16 @@
|
||||||
|
|
||||||
struct SDL_PrivateAudioData
|
struct SDL_PrivateAudioData
|
||||||
{
|
{
|
||||||
AudioUnit audioUnit;
|
SDL_Thread *thread;
|
||||||
SDL_bool audioUnitOpened;
|
AudioQueueRef audioQueue;
|
||||||
|
AudioQueueBufferRef audioBuffer[2];
|
||||||
void *buffer;
|
void *buffer;
|
||||||
UInt32 bufferOffset;
|
UInt32 bufferOffset;
|
||||||
UInt32 bufferSize;
|
UInt32 bufferSize;
|
||||||
|
AudioStreamBasicDescription strdesc;
|
||||||
|
SDL_sem *ready_semaphore;
|
||||||
|
char *thread_error;
|
||||||
|
SDL_atomic_t shutdown;
|
||||||
#if MACOSX_COREAUDIO
|
#if MACOSX_COREAUDIO
|
||||||
AudioDeviceID deviceID;
|
AudioDeviceID deviceID;
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue