19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <CoreAudio/CoreAudio.h> 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <CoreServices/CoreServices.h> 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <AudioUnit/AudioUnit.h> 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if MAC_OS_X_VERSION_MAX_ALLOWED <= 1050 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <AudioUnit/AUNTComponent.h> 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h" 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h" 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysaudio.h" 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_coreaudio.h" 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */ 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Core_OpenAudio(_THIS, SDL_AudioSpec *spec); 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Core_WaitAudio(_THIS); 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Core_PlayAudio(_THIS); 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *Core_GetAudioBuf(_THIS); 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Core_CloseAudio(_THIS); 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */ 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void) 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(1); 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device) 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(device->hidden); 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(device); 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex) 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioDevice *this; 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Initialize all variables that we clean on shutdown */ 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this, 0, (sizeof *this)); 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->hidden = (struct SDL_PrivateAudioData *) 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_malloc((sizeof *this->hidden)); 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (this == NULL) || (this->hidden == NULL) ) { 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_OutOfMemory(); 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( this ) { 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(this); 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the function pointers */ 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->OpenAudio = Core_OpenAudio; 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->WaitAudio = Core_WaitAudio; 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->PlayAudio = Core_PlayAudio; 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->GetAudioBuf = Core_GetAudioBuf; 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->CloseAudio = Core_CloseAudio; 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall this->free = Audio_DeleteDevice; 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return this; 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap COREAUDIO_bootstrap = { 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall "coreaudio", "Mac OS X CoreAudio", 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Audio_Available, Audio_CreateDevice 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}; 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The CoreAudio callback */ 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic OSStatus audioCallback (void *inRefCon, 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall AudioUnitRenderActionFlags *ioActionFlags, 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const AudioTimeStamp *inTimeStamp, 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall UInt32 inBusNumber, 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall UInt32 inNumberFrames, 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall AudioBufferList *ioData) 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_AudioDevice *this = (SDL_AudioDevice *)inRefCon; 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall UInt32 remaining, len; 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall AudioBuffer *abuf; 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall void *ptr; 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall UInt32 i; 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Only do anything if audio is enabled and not paused */ 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( ! this->enabled || this->paused ) { 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i = 0; i < ioData->mNumberBuffers; i++) { 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall abuf = &ioData->mBuffers[i]; 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize); 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return 0; 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* No SDL conversion should be needed here, ever, since we accept 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall any input format in OpenAudio, and leave the conversion to CoreAudio. 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(!this->convert.needed); 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall assert(this->spec.channels == ioData->mNumberChannels); 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */ 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for (i = 0; i < ioData->mNumberBuffers; i++) { 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall abuf = &ioData->mBuffers[i]; 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall remaining = abuf->mDataByteSize; 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ptr = abuf->mData; 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while (remaining > 0) { 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (bufferOffset >= bufferSize) { 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Generate the data */ 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memset(buffer, this->spec.silence, bufferSize); 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_mutexP(this->mixer_lock); 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (*this->spec.callback)(this->spec.userdata, 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall buffer, bufferSize); 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_mutexV(this->mixer_lock); 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall bufferOffset = 0; 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = bufferSize - bufferOffset; 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (len > remaining) 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall len = remaining; 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_memcpy(ptr, (char *)buffer + bufferOffset, len); 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ptr = (char *)ptr + len; 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall remaining -= len; 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall bufferOffset += len; 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return 0; 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Dummy functions -- we don't use thread-based audio */ 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Core_WaitAudio(_THIS) 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Core_PlayAudio(_THIS) 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint8 *Core_GetAudioBuf(_THIS) 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(NULL); 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Core_CloseAudio(_THIS) 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus result; 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct AURenderCallbackStruct callback; 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* stop processing the audio unit */ 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = AudioOutputUnitStop (outputAudioUnit); 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (result != noErr) { 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Core_CloseAudio: AudioOutputUnitStop"); 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Remove the input callback */ 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall callback.inputProc = 0; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall callback.inputProcRefCon = 0; 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = AudioUnitSetProperty (outputAudioUnit, 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kAudioUnitProperty_SetRenderCallback, 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kAudioUnitScope_Input, 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 0, 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &callback, 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sizeof(callback)); 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (result != noErr) { 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Core_CloseAudio: AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)"); 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = CloseComponent(outputAudioUnit); 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (result != noErr) { 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Core_CloseAudio: CloseComponent"); 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return; 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_free(buffer); 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define CHECK_RESULT(msg) \ 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (result != noErr) { \ 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Failed to start CoreAudio: " msg); \ 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return -1; \ 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint Core_OpenAudio(_THIS, SDL_AudioSpec *spec) 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall OSStatus result = noErr; 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Component comp; 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ComponentDescription desc; 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall struct AURenderCallbackStruct callback; 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall AudioStreamBasicDescription requestedDesc; 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Setup a AudioStreamBasicDescription with the requested format */ 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mFormatID = kAudioFormatLinearPCM; 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked; 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mChannelsPerFrame = spec->channels; 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mSampleRate = spec->freq; 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mBitsPerChannel = spec->format & 0xFF; 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (spec->format & 0x8000) 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger; 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (spec->format & 0x1000) 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian; 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mFramesPerPacket = 1; 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mBytesPerFrame = requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8; 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall requestedDesc.mBytesPerPacket = requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket; 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Locate the default output audio unit */ 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall desc.componentType = kAudioUnitType_Output; 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall desc.componentSubType = kAudioUnitSubType_DefaultOutput; 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall desc.componentManufacturer = kAudioUnitManufacturer_Apple; 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall desc.componentFlags = 0; 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall desc.componentFlagsMask = 0; 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall comp = FindNextComponent (NULL, &desc); 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (comp == NULL) { 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError ("Failed to start CoreAudio: FindNextComponent returned NULL"); 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return -1; 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Open & initialize the default output audio unit */ 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = OpenAComponent (comp, &outputAudioUnit); 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CHECK_RESULT("OpenAComponent") 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = AudioUnitInitialize (outputAudioUnit); 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CHECK_RESULT("AudioUnitInitialize") 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the input format of the audio unit. */ 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = AudioUnitSetProperty (outputAudioUnit, 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kAudioUnitProperty_StreamFormat, 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kAudioUnitScope_Input, 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 0, 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &requestedDesc, 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sizeof (requestedDesc)); 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)") 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set the audio callback */ 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall callback.inputProc = audioCallback; 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall callback.inputProcRefCon = this; 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = AudioUnitSetProperty (outputAudioUnit, 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kAudioUnitProperty_SetRenderCallback, 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall kAudioUnitScope_Input, 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 0, 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall &callback, 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall sizeof(callback)); 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)") 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Calculate the final parameters for this audio specification */ 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_CalculateAudioSpec(spec); 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Allocate a sample buffer */ 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall bufferOffset = bufferSize = this->spec.size; 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall buffer = SDL_malloc(bufferSize); 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Finally, start processing of the audio unit */ 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall result = AudioOutputUnitStart (outputAudioUnit); 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall CHECK_RESULT("AudioOutputUnitStart") 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We're running! */ 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(1); 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 292