These files contain the audio support for iPhone. They are based on the CoreAudio audio driver for Mac OS X. The principle difference is that the iPhone doesn't seem to have a concept of audio devices ... it just has special units for audio in and audio out. Also had to change some functions to versions which seem to only exist on iPhone and will apparently exist in Mac OS X 10.6(!) gsoc2008_iphone
authorHolmes Futrell <hfutrell@umail.ucsb.edu>
Fri, 18 Jul 2008 17:53:54 +0000
branchgsoc2008_iphone
changeset 2364e321b52dee8f
parent 2363 49b243db2e04
child 2365 478fa831910f
These files contain the audio support for iPhone. They are based on the CoreAudio audio driver for Mac OS X. The principle difference is that the iPhone doesn't seem to have a concept of audio devices ... it just has special units for audio in and audio out. Also had to change some functions to versions which seem to only exist on iPhone and will apparently exist in Mac OS X 10.6(!)

There is currently no audio recording support -- my iPod Touch doesn't have a microphone to test this with.
src/audio/iphoneos/SDL_coreaudio_iphone.c
src/audio/iphoneos/SDL_coreaudio_iphone.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/iphoneos/SDL_coreaudio_iphone.c	Fri Jul 18 17:53:54 2008 +0000
     1.3 @@ -0,0 +1,339 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#include <AudioUnit/AudioUnit.h>
    1.28 +
    1.29 +#include "SDL_audio.h"
    1.30 +#include "../SDL_audio_c.h"
    1.31 +#include "../SDL_sysaudio.h"
    1.32 +#include "SDL_coreaudio_iphone.h"
    1.33 +
    1.34 +#define DEBUG_COREAUDIO 0
    1.35 +
    1.36 +static void
    1.37 +COREAUDIO_Deinitialize(void)
    1.38 +{
    1.39 +}
    1.40 +
    1.41 +/* The CoreAudio callback */
    1.42 +static OSStatus
    1.43 +outputCallback(void *inRefCon,
    1.44 +               AudioUnitRenderActionFlags * ioActionFlags,
    1.45 +               const AudioTimeStamp * inTimeStamp,
    1.46 +               UInt32 inBusNumber, UInt32 inNumberFrames,
    1.47 +               AudioBufferList * ioDataList)
    1.48 +{
    1.49 +    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
    1.50 +    AudioBuffer *ioData = &ioDataList->mBuffers[0];
    1.51 +    UInt32 remaining, len;
    1.52 +    void *ptr;
    1.53 +
    1.54 +    /* Is there ever more than one buffer, and what do you do with it? */
    1.55 +    if (ioDataList->mNumberBuffers != 1) {
    1.56 +        return noErr;
    1.57 +    }
    1.58 +
    1.59 +    /* Only do anything if audio is enabled and not paused */
    1.60 +    if (!this->enabled || this->paused) {
    1.61 +        SDL_memset(ioData->mData, this->spec.silence, ioData->mDataByteSize);
    1.62 +        return 0;
    1.63 +    }
    1.64 +
    1.65 +    /* No SDL conversion should be needed here, ever, since we accept
    1.66 +       any input format in OpenAudio, and leave the conversion to CoreAudio.
    1.67 +	*/
    1.68 +    /*
    1.69 +       assert(!this->convert.needed);
    1.70 +       assert(this->spec.channels == ioData->mNumberChannels);
    1.71 +     */
    1.72 +
    1.73 +    remaining = ioData->mDataByteSize;
    1.74 +    ptr = ioData->mData;
    1.75 +    while (remaining > 0) {
    1.76 +        if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
    1.77 +            /* Generate the data */
    1.78 +            SDL_memset(this->hidden->buffer, this->spec.silence,
    1.79 +                       this->hidden->bufferSize);
    1.80 +            SDL_mutexP(this->mixer_lock);
    1.81 +            (*this->spec.callback) (this->spec.userdata, this->hidden->buffer,
    1.82 +                                    this->hidden->bufferSize);
    1.83 +            SDL_mutexV(this->mixer_lock);
    1.84 +            this->hidden->bufferOffset = 0;
    1.85 +        }
    1.86 +
    1.87 +        len = this->hidden->bufferSize - this->hidden->bufferOffset;
    1.88 +        if (len > remaining)
    1.89 +            len = remaining;
    1.90 +        SDL_memcpy(ptr,
    1.91 +                   (char *) this->hidden->buffer + this->hidden->bufferOffset,
    1.92 +                   len);
    1.93 +        ptr = (char *) ptr + len;
    1.94 +        remaining -= len;
    1.95 +        this->hidden->bufferOffset += len;
    1.96 +    }
    1.97 +
    1.98 +    return 0;
    1.99 +}
   1.100 +
   1.101 +static OSStatus
   1.102 +inputCallback(void *inRefCon,
   1.103 +              AudioUnitRenderActionFlags * ioActionFlags,
   1.104 +              const AudioTimeStamp * inTimeStamp,
   1.105 +              UInt32 inBusNumber, UInt32 inNumberFrames,
   1.106 +              AudioBufferList * ioData)
   1.107 +{
   1.108 +    //err = AudioUnitRender(afr->fAudioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, afr->fAudioBuffer);
   1.109 +    // !!! FIXME: write me!
   1.110 +    return noErr;
   1.111 +}
   1.112 +
   1.113 +
   1.114 +static void
   1.115 +COREAUDIO_CloseDevice(_THIS)
   1.116 +{
   1.117 +    if (this->hidden != NULL) {
   1.118 +        if (this->hidden->audioUnitOpened) {
   1.119 +            OSStatus result = noErr;
   1.120 +            AURenderCallbackStruct callback;
   1.121 +            const AudioUnitElement output_bus = 0;
   1.122 +            const AudioUnitElement input_bus = 1;
   1.123 +            const int iscapture = this->iscapture;
   1.124 +            const AudioUnitElement bus =
   1.125 +                ((iscapture) ? input_bus : output_bus);
   1.126 +            const AudioUnitScope scope =
   1.127 +                ((iscapture) ? kAudioUnitScope_Output :
   1.128 +                 kAudioUnitScope_Input);
   1.129 +
   1.130 +            /* stop processing the audio unit */
   1.131 +            result = AudioOutputUnitStop(this->hidden->audioUnit);
   1.132 +
   1.133 +            /* Remove the input callback */
   1.134 +            SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   1.135 +            result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.136 +                                          kAudioUnitProperty_SetRenderCallback,
   1.137 +                                          scope, bus, &callback,
   1.138 +                                          sizeof(callback));
   1.139 +
   1.140 +            //CloseComponent(this->hidden->audioUnit);
   1.141 +            this->hidden->audioUnitOpened = 0;
   1.142 +        }
   1.143 +        SDL_free(this->hidden->buffer);
   1.144 +        SDL_free(this->hidden);
   1.145 +        this->hidden = NULL;
   1.146 +    }
   1.147 +}
   1.148 +
   1.149 +
   1.150 +#define CHECK_RESULT(msg) \
   1.151 +    if (result != noErr) { \
   1.152 +        COREAUDIO_CloseDevice(this); \
   1.153 +        SDL_SetError("CoreAudio error (%s): %d", msg, result); \
   1.154 +        return 0; \
   1.155 +    }
   1.156 +
   1.157 +static int
   1.158 +prepare_audiounit(_THIS, const char *devname, int iscapture,
   1.159 +                  const AudioStreamBasicDescription * strdesc)
   1.160 +{
   1.161 +    OSStatus result = noErr;
   1.162 +    AURenderCallbackStruct callback;
   1.163 +    AudioComponentDescription desc;
   1.164 +    AudioComponent comp = NULL;
   1.165 +
   1.166 +    UInt32 enableIO = 0;
   1.167 +    const AudioUnitElement output_bus = 0;
   1.168 +    const AudioUnitElement input_bus = 1;
   1.169 +    const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
   1.170 +    const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
   1.171 +                                  kAudioUnitScope_Input);
   1.172 +
   1.173 +    SDL_memset(&desc, '\0', sizeof(AudioComponentDescription));
   1.174 +    desc.componentType = kAudioUnitType_Output;
   1.175 +	desc.componentSubType = kAudioUnitSubType_RemoteIO;
   1.176 +	desc.componentManufacturer = kAudioUnitManufacturer_Apple;
   1.177 +
   1.178 +    comp = AudioComponentFindNext(NULL, &desc);
   1.179 +    if (comp == NULL) {
   1.180 +        SDL_SetError("Couldn't find requested CoreAudio component");
   1.181 +        return 0;
   1.182 +    }
   1.183 +
   1.184 +    /* Open & initialize the audio unit */
   1.185 +	/*
   1.186 +		AudioComponentInstanceNew only available on iPhone OS 2.0 and Mac OS X 10.6	
   1.187 +		We can't use OpenAComponent on iPhone because it is not present
   1.188 +	*/
   1.189 +	result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
   1.190 +	CHECK_RESULT("AudioComponentInstanceNew");
   1.191 +
   1.192 +    this->hidden->audioUnitOpened = 1;
   1.193 +
   1.194 +    // !!! FIXME: this is wrong?
   1.195 +    enableIO = ((iscapture) ? 1 : 0);
   1.196 +    result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.197 +                                  kAudioOutputUnitProperty_EnableIO,
   1.198 +                                  kAudioUnitScope_Input, input_bus,
   1.199 +                                  &enableIO, sizeof(enableIO));
   1.200 +    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO input)");
   1.201 +
   1.202 +    // !!! FIXME: this is wrong?
   1.203 +    enableIO = ((iscapture) ? 0 : 1);
   1.204 +    result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.205 +                                  kAudioOutputUnitProperty_EnableIO,
   1.206 +                                  kAudioUnitScope_Output, output_bus,
   1.207 +                                  &enableIO, sizeof(enableIO));
   1.208 +    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO output)");
   1.209 +
   1.210 +    /*result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.211 +                                  kAudioOutputUnitProperty_CurrentDevice,
   1.212 +                                  kAudioUnitScope_Global, 0,
   1.213 +                                  &this->hidden->deviceID,
   1.214 +                                  sizeof(AudioDeviceID));
   1.215 +   
   1.216 +	CHECK_RESULT("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");*/
   1.217 +
   1.218 +    /* Set the data format of the audio unit. */
   1.219 +    result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.220 +                                  kAudioUnitProperty_StreamFormat,
   1.221 +                                  scope, bus, strdesc, sizeof(*strdesc));
   1.222 +    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
   1.223 +
   1.224 +    /* Set the audio callback */
   1.225 +    SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   1.226 +    callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
   1.227 +    callback.inputProcRefCon = this;
   1.228 +    result = AudioUnitSetProperty(this->hidden->audioUnit,
   1.229 +                                  kAudioUnitProperty_SetRenderCallback,
   1.230 +                                  scope, bus, &callback, sizeof(callback));
   1.231 +    CHECK_RESULT
   1.232 +        ("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)");
   1.233 +
   1.234 +    /* Calculate the final parameters for this audio specification */
   1.235 +    SDL_CalculateAudioSpec(&this->spec);
   1.236 +
   1.237 +    /* Allocate a sample buffer */
   1.238 +    this->hidden->bufferOffset = this->hidden->bufferSize = this->spec.size;
   1.239 +    this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
   1.240 +
   1.241 +    result = AudioUnitInitialize(this->hidden->audioUnit);
   1.242 +    CHECK_RESULT("AudioUnitInitialize");
   1.243 +
   1.244 +    /* Finally, start processing of the audio unit */
   1.245 +    result = AudioOutputUnitStart(this->hidden->audioUnit);
   1.246 +    CHECK_RESULT("AudioOutputUnitStart");	
   1.247 +    /* We're running! */
   1.248 +    return 1;
   1.249 +}
   1.250 +
   1.251 +static int
   1.252 +COREAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
   1.253 +{
   1.254 +    AudioStreamBasicDescription strdesc;
   1.255 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   1.256 +    int valid_datatype = 0;
   1.257 +
   1.258 +    /* Initialize all variables that we clean on shutdown */
   1.259 +    this->hidden = (struct SDL_PrivateAudioData *)
   1.260 +        SDL_malloc((sizeof *this->hidden));
   1.261 +    if (this->hidden == NULL) {
   1.262 +        SDL_OutOfMemory();
   1.263 +        return (0);
   1.264 +    }
   1.265 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   1.266 +
   1.267 +    /* Setup a AudioStreamBasicDescription with the requested format */
   1.268 +    SDL_memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
   1.269 +    strdesc.mFormatID = kAudioFormatLinearPCM;
   1.270 +    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
   1.271 +    strdesc.mChannelsPerFrame = this->spec.channels;
   1.272 +    strdesc.mSampleRate = this->spec.freq;
   1.273 +    strdesc.mFramesPerPacket = 1;
   1.274 +
   1.275 +    while ((!valid_datatype) && (test_format)) {
   1.276 +        this->spec.format = test_format;
   1.277 +        /* Just a list of valid SDL formats, so people don't pass junk here. */
   1.278 +        switch (test_format) {
   1.279 +        case AUDIO_U8:
   1.280 +        case AUDIO_S8:
   1.281 +        case AUDIO_U16LSB:
   1.282 +        case AUDIO_S16LSB:
   1.283 +        case AUDIO_U16MSB:
   1.284 +        case AUDIO_S16MSB:
   1.285 +        case AUDIO_S32LSB:
   1.286 +        case AUDIO_S32MSB:
   1.287 +        case AUDIO_F32LSB:
   1.288 +        case AUDIO_F32MSB:
   1.289 +            valid_datatype = 1;
   1.290 +            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
   1.291 +            if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
   1.292 +                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
   1.293 +
   1.294 +            if (SDL_AUDIO_ISFLOAT(this->spec.format))
   1.295 +                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
   1.296 +            else if (SDL_AUDIO_ISSIGNED(this->spec.format))
   1.297 +                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
   1.298 +            break;
   1.299 +        }
   1.300 +    }
   1.301 +
   1.302 +    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
   1.303 +        COREAUDIO_CloseDevice(this);
   1.304 +        SDL_SetError("Unsupported audio format");
   1.305 +        return 0;
   1.306 +    }
   1.307 +
   1.308 +    strdesc.mBytesPerFrame =
   1.309 +        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
   1.310 +    strdesc.mBytesPerPacket =
   1.311 +        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
   1.312 +
   1.313 +    if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
   1.314 +        COREAUDIO_CloseDevice(this);
   1.315 +        return 0;               /* prepare_audiounit() will call SDL_SetError()... */
   1.316 +    }
   1.317 +
   1.318 +    return 1;                   /* good to go. */
   1.319 +}
   1.320 +
   1.321 +static int
   1.322 +COREAUDIO_Init(SDL_AudioDriverImpl * impl)
   1.323 +{
   1.324 +    /* Set the function pointers */
   1.325 +    impl->OpenDevice = COREAUDIO_OpenDevice;
   1.326 +    impl->CloseDevice = COREAUDIO_CloseDevice;
   1.327 +    impl->Deinitialize = COREAUDIO_Deinitialize;
   1.328 +    impl->ProvidesOwnCallbackThread = 1;
   1.329 +	
   1.330 +	/* added for iPhone */
   1.331 +	impl->OnlyHasDefaultInputDevice  = 1;
   1.332 +	impl->OnlyHasDefaultOutputDevice = 1;
   1.333 +	impl->HasCaptureSupport = 0; /* still needs to be written */
   1.334 +	
   1.335 +    return 1;
   1.336 +}
   1.337 +
   1.338 +AudioBootStrap COREAUDIOIPHONE_bootstrap = {
   1.339 +    "coreaudio-iphoneos", "SDL CoreAudio (iPhone OS) audio driver", COREAUDIO_Init, 0
   1.340 +};
   1.341 +
   1.342 +/* vi: set ts=4 sw=4 expandtab: */
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/audio/iphoneos/SDL_coreaudio_iphone.h	Fri Jul 18 17:53:54 2008 +0000
     2.3 @@ -0,0 +1,43 @@
     2.4 +/*
     2.5 +    SDL - Simple DirectMedia Layer
     2.6 +    Copyright (C) 1997-2006 Sam Lantinga
     2.7 +
     2.8 +    This library is free software; you can redistribute it and/or
     2.9 +    modify it under the terms of the GNU Lesser General Public
    2.10 +    License as published by the Free Software Foundation; either
    2.11 +    version 2.1 of the License, or (at your option) any later version.
    2.12 +
    2.13 +    This library is distributed in the hope that it will be useful,
    2.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 +    Lesser General Public License for more details.
    2.17 +
    2.18 +    You should have received a copy of the GNU Lesser General Public
    2.19 +    License along with this library; if not, write to the Free Software
    2.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    2.21 +
    2.22 +    Sam Lantinga
    2.23 +    slouken@libsdl.org
    2.24 +*/
    2.25 +#include "SDL_config.h"
    2.26 +
    2.27 +#ifndef _SDL_coreaudio_h
    2.28 +#define _SDL_coreaudio_h
    2.29 +
    2.30 +#include "../SDL_sysaudio.h"
    2.31 +
    2.32 +/* Hidden "this" pointer for the audio functions */
    2.33 +#define _THIS	SDL_AudioDevice *this
    2.34 +
    2.35 +struct SDL_PrivateAudioData
    2.36 +{
    2.37 +    AudioUnit audioUnit;
    2.38 +    int audioUnitOpened;
    2.39 +    void *buffer;
    2.40 +    UInt32 bufferOffset;
    2.41 +    UInt32 bufferSize;
    2.42 +  //  AudioDeviceID deviceID;
    2.43 +};
    2.44 +
    2.45 +#endif /* _SDL_coreaudio_h */
    2.46 +/* vi: set ts=4 sw=4 expandtab: */