Mac OS X audio backend now supports int32/float32.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 01 Sep 2006 05:31:36 +0000
changeset 2003506851d3efa4
parent 2002 7590aaf89a60
child 2004 c27292a690b7
Mac OS X audio backend now supports int32/float32.
src/audio/macosx/SDL_coreaudio.c
     1.1 --- a/src/audio/macosx/SDL_coreaudio.c	Fri Sep 01 00:04:07 2006 +0000
     1.2 +++ b/src/audio/macosx/SDL_coreaudio.c	Fri Sep 01 05:31:36 2006 +0000
     1.3 @@ -207,25 +207,56 @@
     1.4      Component comp;
     1.5      ComponentDescription desc;
     1.6      struct AudioUnitInputCallback callback;
     1.7 -    AudioStreamBasicDescription requestedDesc;
     1.8 +    AudioStreamBasicDescription desc;
     1.9 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(spec->format);
    1.10 +    int valid_datatype = 0;
    1.11  
    1.12      /* Setup a AudioStreamBasicDescription with the requested format */
    1.13 -    requestedDesc.mFormatID = kAudioFormatLinearPCM;
    1.14 -    requestedDesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
    1.15 -    requestedDesc.mChannelsPerFrame = spec->channels;
    1.16 -    requestedDesc.mSampleRate = spec->freq;
    1.17 +    memset(&desc, '\0', sizeof (AudioStreamBasicDescription));
    1.18 +    desc.mFormatID = kAudioFormatLinearPCM;
    1.19 +    desc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
    1.20 +    desc.mChannelsPerFrame = spec->channels;
    1.21 +    desc.mSampleRate = spec->freq;
    1.22 +    desc.mFramesPerPacket = 1;
    1.23  
    1.24 -    requestedDesc.mBitsPerChannel = spec->format & 0xFF;
    1.25 -    if (spec->format & 0x8000)
    1.26 -        requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
    1.27 -    if (spec->format & 0x1000)
    1.28 -        requestedDesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
    1.29 +    while ((!valid_datatype) && (test_format)) {
    1.30 +        spec->format = test_format;
    1.31 +        desc.mFormatFlags = 0;
    1.32 +        /* Just a list of valid SDL formats, so people don't pass junk here. */
    1.33 +        switch (test_format) {
    1.34 +            case AUDIO_U8:
    1.35 +            case AUDIO_S8:
    1.36 +            case AUDIO_U16LSB:
    1.37 +            case AUDIO_S16LSB:
    1.38 +            case AUDIO_U16MSB:
    1.39 +            case AUDIO_S16MSB:
    1.40 +            case AUDIO_S32LSB:
    1.41 +            case AUDIO_S32MSB:
    1.42 +            case AUDIO_F32LSB:
    1.43 +            case AUDIO_F32MSB:
    1.44 +                valid_datatype = 1;
    1.45 +                desc.mBitsPerChannel = SDL_AUDIO_BITSIZE(spec->format);
    1.46  
    1.47 -    requestedDesc.mFramesPerPacket = 1;
    1.48 -    requestedDesc.mBytesPerFrame =
    1.49 -        requestedDesc.mBitsPerChannel * requestedDesc.mChannelsPerFrame / 8;
    1.50 -    requestedDesc.mBytesPerPacket =
    1.51 -        requestedDesc.mBytesPerFrame * requestedDesc.mFramesPerPacket;
    1.52 +                if (SDL_AUDIO_ISFLOAT(spec->format))
    1.53 +                    desc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
    1.54 +                else if (SDL_AUDIO_ISSIGNED(spec->format))
    1.55 +                    desc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
    1.56 +
    1.57 +                if (SDL_AUDIO_ISBIGENDIAN(spec->format))
    1.58 +                    desc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
    1.59 +                break;
    1.60 +        }
    1.61 +    }
    1.62 +
    1.63 +    if (!valid_datatype) {  /* shouldn't happen, but just in case... */
    1.64 +        SDL_SetError("Unsupported audio format");
    1.65 +        return (-1);
    1.66 +    }
    1.67 +
    1.68 +    desc.mBytesPerFrame =
    1.69 +        desc.mBitsPerChannel * desc.mChannelsPerFrame / 8;
    1.70 +    desc.mBytesPerPacket =
    1.71 +        desc.mBytesPerFrame * desc.mFramesPerPacket;
    1.72  
    1.73  
    1.74      /* Locate the default output audio unit */
    1.75 @@ -252,7 +283,7 @@
    1.76                                        kAudioUnitProperty_StreamFormat,
    1.77                                        kAudioUnitScope_Input,
    1.78                                        0,
    1.79 -                                      &requestedDesc, sizeof(requestedDesc));
    1.80 +                                      &desc, sizeof (desc));
    1.81      CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)")
    1.82          /* Set the audio callback */
    1.83          callback.inputProc = audioCallback;