Merged Mac OS X and iOS audio targets.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 04 Aug 2011 00:45:09 -0400
changeset 5596d4b2f6543074
parent 5595 2cf94ca4ad24
child 5597 c19d7122cb1a
Merged Mac OS X and iOS audio targets.
Xcode-iPhoneOS/SDL/SDLiPhoneOS.xcodeproj/project.pbxproj
Xcode/SDL/SDL.xcodeproj/project.pbxproj
configure.in
src/audio/SDL_audio.c
src/audio/coreaudio/SDL_coreaudio.c
src/audio/coreaudio/SDL_coreaudio.h
src/audio/iphoneos/SDL_coreaudio_iphone.c
src/audio/iphoneos/SDL_coreaudio_iphone.h
src/audio/macosx/SDL_coreaudio.c
src/audio/macosx/SDL_coreaudio.h
     1.1 --- a/Xcode-iPhoneOS/SDL/SDLiPhoneOS.xcodeproj/project.pbxproj	Tue Jul 26 14:20:22 2011 -0700
     1.2 +++ b/Xcode-iPhoneOS/SDL/SDLiPhoneOS.xcodeproj/project.pbxproj	Thu Aug 04 00:45:09 2011 -0400
     1.3 @@ -123,6 +123,8 @@
     1.4  		04FFAB9612E23BDC00BA343D /* SDL_blendmode.h in Headers */ = {isa = PBXBuildFile; fileRef = 04FFAB9312E23BDC00BA343D /* SDL_blendmode.h */; settings = {ATTRIBUTES = (Public, ); }; };
     1.5  		04FFAB9812E23BDC00BA343D /* SDL_shape.h in Headers */ = {isa = PBXBuildFile; fileRef = 04FFAB9512E23BDC00BA343D /* SDL_shape.h */; settings = {ATTRIBUTES = (Public, ); }; };
     1.6  		22C905CD13A22646003FE4E4 /* SDL_hints_c.h in Headers */ = {isa = PBXBuildFile; fileRef = 22C905CC13A22646003FE4E4 /* SDL_hints_c.h */; };
     1.7 +		56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.c in Sources */ = {isa = PBXBuildFile; fileRef = 56EA86F913E9EC2B002E47EB /* SDL_coreaudio.c */; };
     1.8 +		56EA86FC13E9EC2B002E47EB /* SDL_coreaudio.h in Headers */ = {isa = PBXBuildFile; fileRef = 56EA86FA13E9EC2B002E47EB /* SDL_coreaudio.h */; };
     1.9  		56ED04E1118A8EE200A56AA6 /* SDL_power.c in Sources */ = {isa = PBXBuildFile; fileRef = 56ED04E0118A8EE200A56AA6 /* SDL_power.c */; };
    1.10  		56ED04E3118A8EFD00A56AA6 /* SDL_syspower.m in Sources */ = {isa = PBXBuildFile; fileRef = 56ED04E2118A8EFD00A56AA6 /* SDL_syspower.m */; };
    1.11  		FD24846D0E5655AE0021E198 /* SDL_uikitkeyboard.h in Headers */ = {isa = PBXBuildFile; fileRef = FD24846B0E5655AE0021E198 /* SDL_uikitkeyboard.h */; };
    1.12 @@ -191,8 +193,6 @@
    1.13  		FD65267F0DE8FCDD002AD96B /* SDL_thread.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA150DD52EDC00FB1D6B /* SDL_thread.c */; };
    1.14  		FD6526800DE8FCDD002AD96B /* SDL_timer.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA2E0DD52EDC00FB1D6B /* SDL_timer.c */; };
    1.15  		FD6526810DE8FCDD002AD96B /* SDL_systimer.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA310DD52EDC00FB1D6B /* SDL_systimer.c */; };
    1.16 -		FD689EFB0E26E57800F90B21 /* SDL_coreaudio_iphone.c in Sources */ = {isa = PBXBuildFile; fileRef = FD689EF90E26E57800F90B21 /* SDL_coreaudio_iphone.c */; };
    1.17 -		FD689EFC0E26E57800F90B21 /* SDL_coreaudio_iphone.h in Headers */ = {isa = PBXBuildFile; fileRef = FD689EFA0E26E57800F90B21 /* SDL_coreaudio_iphone.h */; };
    1.18  		FD689F030E26E5B600F90B21 /* SDL_sysjoystick.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F000E26E5B600F90B21 /* SDL_sysjoystick.m */; };
    1.19  		FD689F040E26E5B600F90B21 /* SDLUIAccelerationDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = FD689F010E26E5B600F90B21 /* SDLUIAccelerationDelegate.h */; };
    1.20  		FD689F050E26E5B600F90B21 /* SDLUIAccelerationDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F020E26E5B600F90B21 /* SDLUIAccelerationDelegate.m */; };
    1.21 @@ -378,6 +378,8 @@
    1.22  		04FFAB9312E23BDC00BA343D /* SDL_blendmode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_blendmode.h; path = ../../include/SDL_blendmode.h; sourceTree = SOURCE_ROOT; };
    1.23  		04FFAB9512E23BDC00BA343D /* SDL_shape.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_shape.h; path = ../../include/SDL_shape.h; sourceTree = SOURCE_ROOT; };
    1.24  		22C905CC13A22646003FE4E4 /* SDL_hints_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_hints_c.h; path = ../../src/SDL_hints_c.h; sourceTree = "<group>"; };
    1.25 +		56EA86F913E9EC2B002E47EB /* SDL_coreaudio.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDL_coreaudio.c; path = coreaudio/SDL_coreaudio.c; sourceTree = "<group>"; };
    1.26 +		56EA86FA13E9EC2B002E47EB /* SDL_coreaudio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_coreaudio.h; path = coreaudio/SDL_coreaudio.h; sourceTree = "<group>"; };
    1.27  		56ED04E0118A8EE200A56AA6 /* SDL_power.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDL_power.c; path = ../../src/power/SDL_power.c; sourceTree = SOURCE_ROOT; };
    1.28  		56ED04E2118A8EFD00A56AA6 /* SDL_syspower.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SDL_syspower.m; path = ../../src/power/uikit/SDL_syspower.m; sourceTree = SOURCE_ROOT; };
    1.29  		FD0BBFEF0E3933DD00D833B1 /* SDL_uikitview.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitview.h; sourceTree = "<group>"; };
    1.30 @@ -392,8 +394,6 @@
    1.31  		FD5F9D1F0E0E08B3008E885B /* SDL_joystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_joystick_c.h; sourceTree = "<group>"; };
    1.32  		FD5F9D200E0E08B3008E885B /* SDL_sysjoystick.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sysjoystick.h; sourceTree = "<group>"; };
    1.33  		FD6526630DE8FCCB002AD96B /* libSDL.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDL.a; sourceTree = BUILT_PRODUCTS_DIR; };
    1.34 -		FD689EF90E26E57800F90B21 /* SDL_coreaudio_iphone.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_coreaudio_iphone.c; sourceTree = "<group>"; };
    1.35 -		FD689EFA0E26E57800F90B21 /* SDL_coreaudio_iphone.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_coreaudio_iphone.h; sourceTree = "<group>"; };
    1.36  		FD689F000E26E5B600F90B21 /* SDL_sysjoystick.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_sysjoystick.m; sourceTree = "<group>"; };
    1.37  		FD689F010E26E5B600F90B21 /* SDLUIAccelerationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDLUIAccelerationDelegate.h; sourceTree = "<group>"; };
    1.38  		FD689F020E26E5B600F90B21 /* SDLUIAccelerationDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDLUIAccelerationDelegate.m; sourceTree = "<group>"; };
    1.39 @@ -785,6 +785,15 @@
    1.40  			name = Frameworks;
    1.41  			sourceTree = "<group>";
    1.42  		};
    1.43 +		56EA86F813E9EBF9002E47EB /* coreaudio */ = {
    1.44 +			isa = PBXGroup;
    1.45 +			children = (
    1.46 +				56EA86F913E9EC2B002E47EB /* SDL_coreaudio.c */,
    1.47 +				56EA86FA13E9EC2B002E47EB /* SDL_coreaudio.h */,
    1.48 +			);
    1.49 +			name = coreaudio;
    1.50 +			sourceTree = "<group>";
    1.51 +		};
    1.52  		56ED04DE118A8E9A00A56AA6 /* power */ = {
    1.53  			isa = PBXGroup;
    1.54  			children = (
    1.55 @@ -828,15 +837,6 @@
    1.56  			path = ../../src/joystick;
    1.57  			sourceTree = SOURCE_ROOT;
    1.58  		};
    1.59 -		FD689EF80E26E57800F90B21 /* iphoneos */ = {
    1.60 -			isa = PBXGroup;
    1.61 -			children = (
    1.62 -				FD689EF90E26E57800F90B21 /* SDL_coreaudio_iphone.c */,
    1.63 -				FD689EFA0E26E57800F90B21 /* SDL_coreaudio_iphone.h */,
    1.64 -			);
    1.65 -			path = iphoneos;
    1.66 -			sourceTree = "<group>";
    1.67 -		};
    1.68  		FD689EFF0E26E5B600F90B21 /* iphoneos */ = {
    1.69  			isa = PBXGroup;
    1.70  			children = (
    1.71 @@ -983,7 +983,7 @@
    1.72  		FD99B8FB0DD52EDC00FB1D6B /* audio */ = {
    1.73  			isa = PBXGroup;
    1.74  			children = (
    1.75 -				FD689EF80E26E57800F90B21 /* iphoneos */,
    1.76 +				56EA86F813E9EBF9002E47EB /* coreaudio */,
    1.77  				FD99B91C0DD52EDC00FB1D6B /* dummy */,
    1.78  				FD99B9440DD52EDC00FB1D6B /* SDL_audio.c */,
    1.79  				FD99B9450DD52EDC00FB1D6B /* SDL_audio_c.h */,
    1.80 @@ -1196,7 +1196,6 @@
    1.81  				FDA686000DF244C800F98A1A /* SDL_nullvideo.h in Headers */,
    1.82  				FD5F9D300E0E08B3008E885B /* SDL_joystick_c.h in Headers */,
    1.83  				FD5F9D310E0E08B3008E885B /* SDL_sysjoystick.h in Headers */,
    1.84 -				FD689EFC0E26E57800F90B21 /* SDL_coreaudio_iphone.h in Headers */,
    1.85  				FD689F040E26E5B600F90B21 /* SDLUIAccelerationDelegate.h in Headers */,
    1.86  				FD689F1C0E26E5D900F90B21 /* SDL_uikitevents.h in Headers */,
    1.87  				FD689F1E0E26E5D900F90B21 /* SDL_uikitopengles.h in Headers */,
    1.88 @@ -1245,6 +1244,7 @@
    1.89  				04BAC09C1300C1290055DE28 /* SDL_assert_c.h in Headers */,
    1.90  				0417A516130C988500578583 /* SDL_keycode.h in Headers */,
    1.91  				22C905CD13A22646003FE4E4 /* SDL_hints_c.h in Headers */,
    1.92 +				56EA86FC13E9EC2B002E47EB /* SDL_coreaudio.h in Headers */,
    1.93  			);
    1.94  			runOnlyForDeploymentPostprocessing = 0;
    1.95  		};
    1.96 @@ -1433,7 +1433,6 @@
    1.97  				FDA685FB0DF244C800F98A1A /* SDL_nullevents.c in Sources */,
    1.98  				FDA685FF0DF244C800F98A1A /* SDL_nullvideo.c in Sources */,
    1.99  				FD5F9D2F0E0E08B3008E885B /* SDL_joystick.c in Sources */,
   1.100 -				FD689EFB0E26E57800F90B21 /* SDL_coreaudio_iphone.c in Sources */,
   1.101  				FD689F030E26E5B600F90B21 /* SDL_sysjoystick.m in Sources */,
   1.102  				FD689F050E26E5B600F90B21 /* SDLUIAccelerationDelegate.m in Sources */,
   1.103  				FD689F1D0E26E5D900F90B21 /* SDL_uikitevents.m in Sources */,
   1.104 @@ -1474,6 +1473,7 @@
   1.105  				0402A85812FE70C600CECEE3 /* SDL_render_gles2.c in Sources */,
   1.106  				0402A85912FE70C600CECEE3 /* SDL_shaders_gles2.c in Sources */,
   1.107  				04BAC09D1300C1290055DE28 /* SDL_log.c in Sources */,
   1.108 +				56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.c in Sources */,
   1.109  			);
   1.110  			runOnlyForDeploymentPostprocessing = 0;
   1.111  		};
     2.1 --- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj	Tue Jul 26 14:20:22 2011 -0700
     2.2 +++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj	Thu Aug 04 00:45:09 2011 -0400
     2.3 @@ -1139,7 +1139,7 @@
     2.4  			children = (
     2.5  				04BDFD8712E6671700899322 /* disk */,
     2.6  				04BDFD9312E6671700899322 /* dummy */,
     2.7 -				04BDFD9F12E6671700899322 /* macosx */,
     2.8 +				04BDFD9F12E6671700899322 /* coreaudio */,
     2.9  				04BDFDB412E6671700899322 /* SDL_audio.c */,
    2.10  				04BDFDB512E6671700899322 /* SDL_audio_c.h */,
    2.11  				04BDFDB612E6671700899322 /* SDL_audiocvt.c */,
    2.12 @@ -1174,13 +1174,13 @@
    2.13  			path = dummy;
    2.14  			sourceTree = "<group>";
    2.15  		};
    2.16 -		04BDFD9F12E6671700899322 /* macosx */ = {
    2.17 +		04BDFD9F12E6671700899322 /* coreaudio */ = {
    2.18  			isa = PBXGroup;
    2.19  			children = (
    2.20  				04BDFDA012E6671700899322 /* SDL_coreaudio.c */,
    2.21  				04BDFDA112E6671700899322 /* SDL_coreaudio.h */,
    2.22  			);
    2.23 -			path = macosx;
    2.24 +			path = coreaudio;
    2.25  			sourceTree = "<group>";
    2.26  		};
    2.27  		04BDFDD312E6671700899322 /* cpuinfo */ = {
     3.1 --- a/configure.in	Tue Jul 26 14:20:22 2011 -0700
     3.2 +++ b/configure.in	Thu Aug 04 00:45:09 2011 -0400
     3.3 @@ -2306,7 +2306,7 @@
     3.4          # Set up files for the audio library
     3.5          if test x$enable_audio = xyes; then
     3.6              AC_DEFINE(SDL_AUDIO_DRIVER_COREAUDIO, 1, [ ])
     3.7 -            SOURCES="$SOURCES $srcdir/src/audio/macosx/*.c"
     3.8 +            SOURCES="$SOURCES $srcdir/src/audio/coreaudio/*.c"
     3.9              have_audio=yes
    3.10          fi
    3.11          # Set up files for the joystick library
     4.1 --- a/src/audio/SDL_audio.c	Tue Jul 26 14:20:22 2011 -0700
     4.2 +++ b/src/audio/SDL_audio.c	Thu Aug 04 00:45:09 2011 -0400
     4.3 @@ -58,7 +58,6 @@
     4.4  extern AudioBootStrap PAUDIO_bootstrap;
     4.5  extern AudioBootStrap BEOSAUDIO_bootstrap;
     4.6  extern AudioBootStrap COREAUDIO_bootstrap;
     4.7 -extern AudioBootStrap COREAUDIOIPHONE_bootstrap;
     4.8  extern AudioBootStrap SNDMGR_bootstrap;
     4.9  extern AudioBootStrap DISKAUD_bootstrap;
    4.10  extern AudioBootStrap DUMMYAUD_bootstrap;
    4.11 @@ -116,9 +115,6 @@
    4.12  #if SDL_AUDIO_DRIVER_COREAUDIO
    4.13      &COREAUDIO_bootstrap,
    4.14  #endif
    4.15 -#if SDL_AUDIO_DRIVER_COREAUDIOIPHONE
    4.16 -    &COREAUDIOIPHONE_bootstrap,
    4.17 -#endif
    4.18  #if SDL_AUDIO_DRIVER_DISK
    4.19      &DISKAUD_bootstrap,
    4.20  #endif
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/audio/coreaudio/SDL_coreaudio.c	Thu Aug 04 00:45:09 2011 -0400
     5.3 @@ -0,0 +1,553 @@
     5.4 +/*
     5.5 +  Simple DirectMedia Layer
     5.6 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     5.7 +
     5.8 +  This software is provided 'as-is', without any express or implied
     5.9 +  warranty.  In no event will the authors be held liable for any damages
    5.10 +  arising from the use of this software.
    5.11 +
    5.12 +  Permission is granted to anyone to use this software for any purpose,
    5.13 +  including commercial applications, and to alter it and redistribute it
    5.14 +  freely, subject to the following restrictions:
    5.15 +
    5.16 +  1. The origin of this software must not be misrepresented; you must not
    5.17 +     claim that you wrote the original software. If you use this software
    5.18 +     in a product, an acknowledgment in the product documentation would be
    5.19 +     appreciated but is not required.
    5.20 +  2. Altered source versions must be plainly marked as such, and must not be
    5.21 +     misrepresented as being the original software.
    5.22 +  3. This notice may not be removed or altered from any source distribution.
    5.23 +*/
    5.24 +#include "SDL_config.h"
    5.25 +
    5.26 +#if MACOSX_COREAUDIO
    5.27 +#include <CoreAudio/CoreAudio.h>
    5.28 +#include <CoreServices/CoreServices.h>
    5.29 +#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
    5.30 +#include <AudioUnit/AUNTComponent.h>
    5.31 +#endif
    5.32 +#endif
    5.33 +
    5.34 +#include <AudioUnit/AudioUnit.h>
    5.35 +
    5.36 +#include "SDL_audio.h"
    5.37 +#include "../SDL_audio_c.h"
    5.38 +#include "../SDL_sysaudio.h"
    5.39 +#include "SDL_coreaudio.h"
    5.40 +
    5.41 +#define DEBUG_COREAUDIO 0
    5.42 +
    5.43 +static void COREAUDIO_CloseDevice(_THIS);
    5.44 +
    5.45 +#define CHECK_RESULT(msg) \
    5.46 +    if (result != noErr) { \
    5.47 +        COREAUDIO_CloseDevice(this); \
    5.48 +        SDL_SetError("CoreAudio error (%s): %d", msg, (int) result); \
    5.49 +        return 0; \
    5.50 +    }
    5.51 +
    5.52 +#if MACOSX_COREAUDIO
    5.53 +typedef void (*addDevFn)(const char *name, AudioDeviceID devId, void *data);
    5.54 +
    5.55 +static void
    5.56 +addToDevList(const char *name, AudioDeviceID devId, void *data)
    5.57 +{
    5.58 +    SDL_AddAudioDevice addfn = (SDL_AddAudioDevice) data;
    5.59 +    addfn(name);
    5.60 +}
    5.61 +
    5.62 +typedef struct
    5.63 +{
    5.64 +    const char *findname;
    5.65 +    AudioDeviceID devId;
    5.66 +    int found;
    5.67 +} FindDevIdData;
    5.68 +
    5.69 +static void
    5.70 +findDevId(const char *name, AudioDeviceID devId, void *_data)
    5.71 +{
    5.72 +    FindDevIdData *data = (FindDevIdData *) _data;
    5.73 +    if (!data->found) {
    5.74 +        if (SDL_strcmp(name, data->findname) == 0) {
    5.75 +            data->found = 1;
    5.76 +            data->devId = devId;
    5.77 +        }
    5.78 +    }
    5.79 +}
    5.80 +
    5.81 +static void
    5.82 +build_device_list(int iscapture, addDevFn addfn, void *addfndata)
    5.83 +{
    5.84 +    Boolean outWritable = 0;
    5.85 +    OSStatus result = noErr;
    5.86 +    UInt32 size = 0;
    5.87 +    AudioDeviceID *devs = NULL;
    5.88 +    UInt32 i = 0;
    5.89 +    UInt32 max = 0;
    5.90 +
    5.91 +    result = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
    5.92 +                                          &size, &outWritable);
    5.93 +
    5.94 +    if (result != kAudioHardwareNoError)
    5.95 +        return;
    5.96 +
    5.97 +    devs = (AudioDeviceID *) alloca(size);
    5.98 +    if (devs == NULL)
    5.99 +        return;
   5.100 +
   5.101 +    result = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
   5.102 +                                      &size, devs);
   5.103 +    if (result != kAudioHardwareNoError)
   5.104 +        return;
   5.105 +
   5.106 +    max = size / sizeof (AudioDeviceID);
   5.107 +    for (i = 0; i < max; i++) {
   5.108 +        CFStringRef cfstr = NULL;
   5.109 +        char *ptr = NULL;
   5.110 +        AudioDeviceID dev = devs[i];
   5.111 +        AudioBufferList *buflist = NULL;
   5.112 +        int usable = 0;
   5.113 +        CFIndex len = 0;
   5.114 +
   5.115 +        result = AudioDeviceGetPropertyInfo(dev, 0, iscapture,
   5.116 +                                            kAudioDevicePropertyStreamConfiguration,
   5.117 +                                            &size, &outWritable);
   5.118 +        if (result != noErr)
   5.119 +            continue;
   5.120 +
   5.121 +        buflist = (AudioBufferList *) SDL_malloc(size);
   5.122 +        if (buflist == NULL)
   5.123 +            continue;
   5.124 +
   5.125 +        result = AudioDeviceGetProperty(dev, 0, iscapture,
   5.126 +                                        kAudioDevicePropertyStreamConfiguration,
   5.127 +                                        &size, buflist);
   5.128 +
   5.129 +        if (result == noErr) {
   5.130 +            UInt32 j;
   5.131 +            for (j = 0; j < buflist->mNumberBuffers; j++) {
   5.132 +                if (buflist->mBuffers[j].mNumberChannels > 0) {
   5.133 +                    usable = 1;
   5.134 +                    break;
   5.135 +                }
   5.136 +            }
   5.137 +        }
   5.138 +
   5.139 +        SDL_free(buflist);
   5.140 +
   5.141 +        if (!usable)
   5.142 +            continue;
   5.143 +
   5.144 +        size = sizeof(CFStringRef);
   5.145 +        result = AudioDeviceGetProperty(dev, 0, iscapture,
   5.146 +                                        kAudioDevicePropertyDeviceNameCFString,
   5.147 +                                        &size, &cfstr);
   5.148 +
   5.149 +        if (result != kAudioHardwareNoError)
   5.150 +            continue;
   5.151 +
   5.152 +        len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr),
   5.153 +                                                kCFStringEncodingUTF8);
   5.154 +
   5.155 +        ptr = (char *) SDL_malloc(len + 1);
   5.156 +        usable = ((ptr != NULL) &&
   5.157 +                  (CFStringGetCString
   5.158 +                   (cfstr, ptr, len + 1, kCFStringEncodingUTF8)));
   5.159 +
   5.160 +        CFRelease(cfstr);
   5.161 +
   5.162 +        if (usable) {
   5.163 +            len = strlen(ptr);
   5.164 +            /* Some devices have whitespace at the end...trim it. */
   5.165 +            while ((len > 0) && (ptr[len - 1] == ' ')) {
   5.166 +                len--;
   5.167 +            }
   5.168 +            usable = (len > 0);
   5.169 +        }
   5.170 +
   5.171 +        if (usable) {
   5.172 +            ptr[len] = '\0';
   5.173 +
   5.174 +#if DEBUG_COREAUDIO
   5.175 +            printf("COREAUDIO: Found %s device #%d: '%s' (devid %d)\n",
   5.176 +                   ((iscapture) ? "capture" : "output"),
   5.177 +                   (int) *devCount, ptr, (int) dev);
   5.178 +#endif
   5.179 +            addfn(ptr, dev, addfndata);
   5.180 +        }
   5.181 +        SDL_free(ptr);  /* addfn() would have copied the string. */
   5.182 +    }
   5.183 +}
   5.184 +
   5.185 +static void
   5.186 +COREAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
   5.187 +{
   5.188 +    build_device_list(iscapture, addToDevList, addfn);
   5.189 +}
   5.190 +
   5.191 +static int
   5.192 +find_device_by_name(_THIS, const char *devname, int iscapture)
   5.193 +{
   5.194 +    AudioDeviceID devid = 0;
   5.195 +    OSStatus result = noErr;
   5.196 +    UInt32 size = 0;
   5.197 +    UInt32 alive = 0;
   5.198 +    pid_t pid = 0;
   5.199 +
   5.200 +    if (devname == NULL) {
   5.201 +        size = sizeof(AudioDeviceID);
   5.202 +        const AudioHardwarePropertyID propid =
   5.203 +            ((iscapture) ? kAudioHardwarePropertyDefaultInputDevice :
   5.204 +             kAudioHardwarePropertyDefaultOutputDevice);
   5.205 +
   5.206 +        result = AudioHardwareGetProperty(propid, &size, &devid);
   5.207 +        CHECK_RESULT("AudioHardwareGetProperty (default device)");
   5.208 +    } else {
   5.209 +        FindDevIdData data;
   5.210 +        SDL_zero(data);
   5.211 +        data.findname = devname;
   5.212 +        build_device_list(iscapture, findDevId, &data);
   5.213 +        if (!data.found) {
   5.214 +            SDL_SetError("CoreAudio: No such audio device.");
   5.215 +            return 0;
   5.216 +        }
   5.217 +        devid = data.devId;
   5.218 +    }
   5.219 +
   5.220 +    size = sizeof(alive);
   5.221 +    result = AudioDeviceGetProperty(devid, 0, iscapture,
   5.222 +                                    kAudioDevicePropertyDeviceIsAlive,
   5.223 +                                    &size, &alive);
   5.224 +    CHECK_RESULT
   5.225 +        ("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)");
   5.226 +
   5.227 +    if (!alive) {
   5.228 +        SDL_SetError("CoreAudio: requested device exists, but isn't alive.");
   5.229 +        return 0;
   5.230 +    }
   5.231 +
   5.232 +    size = sizeof(pid);
   5.233 +    result = AudioDeviceGetProperty(devid, 0, iscapture,
   5.234 +                                    kAudioDevicePropertyHogMode, &size, &pid);
   5.235 +
   5.236 +    /* some devices don't support this property, so errors are fine here. */
   5.237 +    if ((result == noErr) && (pid != -1)) {
   5.238 +        SDL_SetError("CoreAudio: requested device is being hogged.");
   5.239 +        return 0;
   5.240 +    }
   5.241 +
   5.242 +    this->hidden->deviceID = devid;
   5.243 +    return 1;
   5.244 +}
   5.245 +#endif
   5.246 +
   5.247 +/* The CoreAudio callback */
   5.248 +static OSStatus
   5.249 +outputCallback(void *inRefCon,
   5.250 +               AudioUnitRenderActionFlags * ioActionFlags,
   5.251 +               const AudioTimeStamp * inTimeStamp,
   5.252 +               UInt32 inBusNumber, UInt32 inNumberFrames,
   5.253 +               AudioBufferList * ioData)
   5.254 +{
   5.255 +    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
   5.256 +    AudioBuffer *abuf;
   5.257 +    UInt32 remaining, len;
   5.258 +    void *ptr;
   5.259 +    UInt32 i;
   5.260 +
   5.261 +    /* Only do anything if audio is enabled and not paused */
   5.262 +    if (!this->enabled || this->paused) {
   5.263 +        for (i = 0; i < ioData->mNumberBuffers; i++) {
   5.264 +            abuf = &ioData->mBuffers[i];
   5.265 +            SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
   5.266 +        }
   5.267 +        return 0;
   5.268 +    }
   5.269 +
   5.270 +    /* No SDL conversion should be needed here, ever, since we accept
   5.271 +       any input format in OpenAudio, and leave the conversion to CoreAudio.
   5.272 +     */
   5.273 +    /*
   5.274 +       assert(!this->convert.needed);
   5.275 +       assert(this->spec.channels == ioData->mNumberChannels);
   5.276 +     */
   5.277 +
   5.278 +    for (i = 0; i < ioData->mNumberBuffers; i++) {
   5.279 +        abuf = &ioData->mBuffers[i];
   5.280 +        remaining = abuf->mDataByteSize;
   5.281 +        ptr = abuf->mData;
   5.282 +        while (remaining > 0) {
   5.283 +            if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
   5.284 +                /* Generate the data */
   5.285 +                SDL_memset(this->hidden->buffer, this->spec.silence,
   5.286 +                           this->hidden->bufferSize);
   5.287 +                SDL_mutexP(this->mixer_lock);
   5.288 +                (*this->spec.callback)(this->spec.userdata,
   5.289 +                            this->hidden->buffer, this->hidden->bufferSize);
   5.290 +                SDL_mutexV(this->mixer_lock);
   5.291 +                this->hidden->bufferOffset = 0;
   5.292 +            }
   5.293 +
   5.294 +            len = this->hidden->bufferSize - this->hidden->bufferOffset;
   5.295 +            if (len > remaining)
   5.296 +                len = remaining;
   5.297 +            SDL_memcpy(ptr, (char *)this->hidden->buffer +
   5.298 +                       this->hidden->bufferOffset, len);
   5.299 +            ptr = (char *)ptr + len;
   5.300 +            remaining -= len;
   5.301 +            this->hidden->bufferOffset += len;
   5.302 +        }
   5.303 +    }
   5.304 +
   5.305 +    return 0;
   5.306 +}
   5.307 +
   5.308 +static OSStatus
   5.309 +inputCallback(void *inRefCon,
   5.310 +              AudioUnitRenderActionFlags * ioActionFlags,
   5.311 +              const AudioTimeStamp * inTimeStamp,
   5.312 +              UInt32 inBusNumber, UInt32 inNumberFrames,
   5.313 +              AudioBufferList * ioData)
   5.314 +{
   5.315 +    //err = AudioUnitRender(afr->fAudioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, afr->fAudioBuffer);
   5.316 +    // !!! FIXME: write me!
   5.317 +    return noErr;
   5.318 +}
   5.319 +
   5.320 +
   5.321 +static void
   5.322 +COREAUDIO_CloseDevice(_THIS)
   5.323 +{
   5.324 +    if (this->hidden != NULL) {
   5.325 +        if (this->hidden->audioUnitOpened) {
   5.326 +            OSStatus result = noErr;
   5.327 +            AURenderCallbackStruct callback;
   5.328 +            const AudioUnitElement output_bus = 0;
   5.329 +            const AudioUnitElement input_bus = 1;
   5.330 +            const int iscapture = this->iscapture;
   5.331 +            const AudioUnitElement bus =
   5.332 +                ((iscapture) ? input_bus : output_bus);
   5.333 +            const AudioUnitScope scope =
   5.334 +                ((iscapture) ? kAudioUnitScope_Output :
   5.335 +                 kAudioUnitScope_Input);
   5.336 +
   5.337 +            /* stop processing the audio unit */
   5.338 +            result = AudioOutputUnitStop(this->hidden->audioUnit);
   5.339 +
   5.340 +            /* Remove the input callback */
   5.341 +            SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   5.342 +            result = AudioUnitSetProperty(this->hidden->audioUnit,
   5.343 +                                          kAudioUnitProperty_SetRenderCallback,
   5.344 +                                          scope, bus, &callback,
   5.345 +                                          sizeof(callback));
   5.346 +
   5.347 +            /* !!! FIXME: how does iOS free this? */
   5.348 +            #if MACOSX_COREAUDIO
   5.349 +            CloseComponent(this->hidden->audioUnit);
   5.350 +            #endif
   5.351 +
   5.352 +            this->hidden->audioUnitOpened = 0;
   5.353 +        }
   5.354 +        SDL_free(this->hidden->buffer);
   5.355 +        SDL_free(this->hidden);
   5.356 +        this->hidden = NULL;
   5.357 +    }
   5.358 +}
   5.359 +
   5.360 +
   5.361 +static int
   5.362 +prepare_audiounit(_THIS, const char *devname, int iscapture,
   5.363 +                  const AudioStreamBasicDescription * strdesc)
   5.364 +{
   5.365 +    OSStatus result = noErr;
   5.366 +    AURenderCallbackStruct callback;
   5.367 +#if MACOSX_COREAUDIO
   5.368 +    ComponentDescription desc;
   5.369 +    Component comp = NULL;
   5.370 +#else
   5.371 +    AudioComponentDescription desc;
   5.372 +    AudioComponent comp = NULL;
   5.373 +#endif
   5.374 +    const AudioUnitElement output_bus = 0;
   5.375 +    const AudioUnitElement input_bus = 1;
   5.376 +    const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
   5.377 +    const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
   5.378 +                                  kAudioUnitScope_Input);
   5.379 +
   5.380 +#if MACOSX_COREAUDIO
   5.381 +    if (!find_device_by_name(this, devname, iscapture)) {
   5.382 +        SDL_SetError("Couldn't find requested CoreAudio device");
   5.383 +        return 0;
   5.384 +    }
   5.385 +#endif
   5.386 +    
   5.387 +    SDL_zero(desc);
   5.388 +    desc.componentType = kAudioUnitType_Output;
   5.389 +    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
   5.390 +
   5.391 +#if MACOSX_COREAUDIO
   5.392 +    desc.componentSubType = kAudioUnitSubType_DefaultOutput;
   5.393 +    comp = FindNextComponent(NULL, &desc);
   5.394 +#else
   5.395 +    desc.componentSubType = kAudioUnitSubType_RemoteIO;  /* !!! FIXME: ? */
   5.396 +    comp = AudioComponentFindNext(NULL, &desc);
   5.397 +#endif
   5.398 +
   5.399 +    if (comp == NULL) {
   5.400 +        SDL_SetError("Couldn't find requested CoreAudio component");
   5.401 +        return 0;
   5.402 +    }
   5.403 +
   5.404 +    /* Open & initialize the audio unit */
   5.405 +#if MACOSX_COREAUDIO
   5.406 +    result = OpenAComponent(comp, &this->hidden->audioUnit);
   5.407 +    CHECK_RESULT("OpenAComponent");
   5.408 +#else
   5.409 +    /*
   5.410 +       AudioComponentInstanceNew only available on iPhone OS 2.0 and Mac OS X 10.6
   5.411 +       We can't use OpenAComponent on iPhone because it is not present
   5.412 +     */
   5.413 +    result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
   5.414 +    CHECK_RESULT("AudioComponentInstanceNew");
   5.415 +#endif
   5.416 +
   5.417 +    this->hidden->audioUnitOpened = 1;
   5.418 +
   5.419 +#if MACOSX_COREAUDIO
   5.420 +    result = AudioUnitSetProperty(this->hidden->audioUnit,
   5.421 +                                  kAudioOutputUnitProperty_CurrentDevice,
   5.422 +                                  kAudioUnitScope_Global, 0,
   5.423 +                                  &this->hidden->deviceID,
   5.424 +                                  sizeof(AudioDeviceID));
   5.425 +    CHECK_RESULT
   5.426 +        ("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
   5.427 +#endif
   5.428 +
   5.429 +    /* Set the data format of the audio unit. */
   5.430 +    result = AudioUnitSetProperty(this->hidden->audioUnit,
   5.431 +                                  kAudioUnitProperty_StreamFormat,
   5.432 +                                  scope, bus, strdesc, sizeof(*strdesc));
   5.433 +    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
   5.434 +
   5.435 +    /* Set the audio callback */
   5.436 +    SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   5.437 +    callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
   5.438 +    callback.inputProcRefCon = this;
   5.439 +    result = AudioUnitSetProperty(this->hidden->audioUnit,
   5.440 +                                  kAudioUnitProperty_SetRenderCallback,
   5.441 +                                  scope, bus, &callback, sizeof(callback));
   5.442 +    CHECK_RESULT
   5.443 +        ("AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)");
   5.444 +
   5.445 +    /* Calculate the final parameters for this audio specification */
   5.446 +    SDL_CalculateAudioSpec(&this->spec);
   5.447 +
   5.448 +    /* Allocate a sample buffer */
   5.449 +    this->hidden->bufferOffset = this->hidden->bufferSize = this->spec.size;
   5.450 +    this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
   5.451 +
   5.452 +    result = AudioUnitInitialize(this->hidden->audioUnit);
   5.453 +    CHECK_RESULT("AudioUnitInitialize");
   5.454 +
   5.455 +    /* Finally, start processing of the audio unit */
   5.456 +    result = AudioOutputUnitStart(this->hidden->audioUnit);
   5.457 +    CHECK_RESULT("AudioOutputUnitStart");
   5.458 +
   5.459 +    /* We're running! */
   5.460 +    return 1;
   5.461 +}
   5.462 +
   5.463 +
   5.464 +static int
   5.465 +COREAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
   5.466 +{
   5.467 +    AudioStreamBasicDescription strdesc;
   5.468 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   5.469 +    int valid_datatype = 0;
   5.470 +
   5.471 +    /* Initialize all variables that we clean on shutdown */
   5.472 +    this->hidden = (struct SDL_PrivateAudioData *)
   5.473 +        SDL_malloc((sizeof *this->hidden));
   5.474 +    if (this->hidden == NULL) {
   5.475 +        SDL_OutOfMemory();
   5.476 +        return (0);
   5.477 +    }
   5.478 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   5.479 +
   5.480 +    /* Setup a AudioStreamBasicDescription with the requested format */
   5.481 +    SDL_memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
   5.482 +    strdesc.mFormatID = kAudioFormatLinearPCM;
   5.483 +    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
   5.484 +    strdesc.mChannelsPerFrame = this->spec.channels;
   5.485 +    strdesc.mSampleRate = this->spec.freq;
   5.486 +    strdesc.mFramesPerPacket = 1;
   5.487 +
   5.488 +    while ((!valid_datatype) && (test_format)) {
   5.489 +        this->spec.format = test_format;
   5.490 +        /* Just a list of valid SDL formats, so people don't pass junk here. */
   5.491 +        switch (test_format) {
   5.492 +        case AUDIO_U8:
   5.493 +        case AUDIO_S8:
   5.494 +        case AUDIO_U16LSB:
   5.495 +        case AUDIO_S16LSB:
   5.496 +        case AUDIO_U16MSB:
   5.497 +        case AUDIO_S16MSB:
   5.498 +        case AUDIO_S32LSB:
   5.499 +        case AUDIO_S32MSB:
   5.500 +        case AUDIO_F32LSB:
   5.501 +        case AUDIO_F32MSB:
   5.502 +            valid_datatype = 1;
   5.503 +            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
   5.504 +            if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
   5.505 +                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
   5.506 +
   5.507 +            if (SDL_AUDIO_ISFLOAT(this->spec.format))
   5.508 +                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
   5.509 +            else if (SDL_AUDIO_ISSIGNED(this->spec.format))
   5.510 +                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
   5.511 +            break;
   5.512 +        }
   5.513 +    }
   5.514 +
   5.515 +    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
   5.516 +        COREAUDIO_CloseDevice(this);
   5.517 +        SDL_SetError("Unsupported audio format");
   5.518 +        return 0;
   5.519 +    }
   5.520 +
   5.521 +    strdesc.mBytesPerFrame =
   5.522 +        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
   5.523 +    strdesc.mBytesPerPacket =
   5.524 +        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
   5.525 +
   5.526 +    if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
   5.527 +        COREAUDIO_CloseDevice(this);
   5.528 +        return 0;               /* prepare_audiounit() will call SDL_SetError()... */
   5.529 +    }
   5.530 +
   5.531 +    return 1;                   /* good to go. */
   5.532 +}
   5.533 +
   5.534 +static int
   5.535 +COREAUDIO_Init(SDL_AudioDriverImpl * impl)
   5.536 +{
   5.537 +    /* Set the function pointers */
   5.538 +    impl->OpenDevice = COREAUDIO_OpenDevice;
   5.539 +    impl->CloseDevice = COREAUDIO_CloseDevice;
   5.540 +
   5.541 +#if MACOSX_COREAUDIO
   5.542 +    impl->DetectDevices = COREAUDIO_DetectDevices;
   5.543 +#else
   5.544 +    impl->OnlyHasDefaultOutputDevice = 1;
   5.545 +#endif
   5.546 +    
   5.547 +    impl->ProvidesOwnCallbackThread = 1;
   5.548 +
   5.549 +    return 1;   /* this audio target is available. */
   5.550 +}
   5.551 +
   5.552 +AudioBootStrap COREAUDIO_bootstrap = {
   5.553 +    "coreaudio", "CoreAudio", COREAUDIO_Init, 0
   5.554 +};
   5.555 +
   5.556 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/audio/coreaudio/SDL_coreaudio.h	Thu Aug 04 00:45:09 2011 -0400
     6.3 @@ -0,0 +1,48 @@
     6.4 +/*
     6.5 +  Simple DirectMedia Layer
     6.6 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     6.7 +
     6.8 +  This software is provided 'as-is', without any express or implied
     6.9 +  warranty.  In no event will the authors be held liable for any damages
    6.10 +  arising from the use of this software.
    6.11 +
    6.12 +  Permission is granted to anyone to use this software for any purpose,
    6.13 +  including commercial applications, and to alter it and redistribute it
    6.14 +  freely, subject to the following restrictions:
    6.15 +
    6.16 +  1. The origin of this software must not be misrepresented; you must not
    6.17 +     claim that you wrote the original software. If you use this software
    6.18 +     in a product, an acknowledgment in the product documentation would be
    6.19 +     appreciated but is not required.
    6.20 +  2. Altered source versions must be plainly marked as such, and must not be
    6.21 +     misrepresented as being the original software.
    6.22 +  3. This notice may not be removed or altered from any source distribution.
    6.23 +*/
    6.24 +#include "SDL_config.h"
    6.25 +
    6.26 +#ifndef _SDL_coreaudio_h
    6.27 +#define _SDL_coreaudio_h
    6.28 +
    6.29 +#include "../SDL_sysaudio.h"
    6.30 +
    6.31 +#if !defined(__IPHONEOS__)
    6.32 +#define MACOSX_COREAUDIO 1
    6.33 +#endif
    6.34 +
    6.35 +/* Hidden "this" pointer for the audio functions */
    6.36 +#define _THIS	SDL_AudioDevice *this
    6.37 +
    6.38 +struct SDL_PrivateAudioData
    6.39 +{
    6.40 +    AudioUnit audioUnit;
    6.41 +    int audioUnitOpened;
    6.42 +    void *buffer;
    6.43 +    UInt32 bufferOffset;
    6.44 +    UInt32 bufferSize;
    6.45 +#if MACOSX_COREAUDIO
    6.46 +    AudioDeviceID deviceID;
    6.47 +#endif
    6.48 +};
    6.49 +
    6.50 +#endif /* _SDL_coreaudio_h */
    6.51 +/* vi: set ts=4 sw=4 expandtab: */
     7.1 --- a/src/audio/iphoneos/SDL_coreaudio_iphone.c	Tue Jul 26 14:20:22 2011 -0700
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,333 +0,0 @@
     7.4 -/*
     7.5 -  Simple DirectMedia Layer
     7.6 -  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     7.7 -
     7.8 -  This software is provided 'as-is', without any express or implied
     7.9 -  warranty.  In no event will the authors be held liable for any damages
    7.10 -  arising from the use of this software.
    7.11 -
    7.12 -  Permission is granted to anyone to use this software for any purpose,
    7.13 -  including commercial applications, and to alter it and redistribute it
    7.14 -  freely, subject to the following restrictions:
    7.15 -
    7.16 -  1. The origin of this software must not be misrepresented; you must not
    7.17 -     claim that you wrote the original software. If you use this software
    7.18 -     in a product, an acknowledgment in the product documentation would be
    7.19 -     appreciated but is not required.
    7.20 -  2. Altered source versions must be plainly marked as such, and must not be
    7.21 -     misrepresented as being the original software.
    7.22 -  3. This notice may not be removed or altered from any source distribution.
    7.23 -*/
    7.24 -#include "SDL_config.h"
    7.25 -
    7.26 -#include <AudioUnit/AudioUnit.h>
    7.27 -
    7.28 -#include "SDL_audio.h"
    7.29 -#include "../SDL_audio_c.h"
    7.30 -#include "../SDL_sysaudio.h"
    7.31 -#include "SDL_coreaudio_iphone.h"
    7.32 -
    7.33 -#define DEBUG_COREAUDIO 0
    7.34 -
    7.35 -/* The CoreAudio callback */
    7.36 -static OSStatus
    7.37 -outputCallback(void *inRefCon,
    7.38 -               AudioUnitRenderActionFlags * ioActionFlags,
    7.39 -               const AudioTimeStamp * inTimeStamp,
    7.40 -               UInt32 inBusNumber, UInt32 inNumberFrames,
    7.41 -               AudioBufferList * ioDataList)
    7.42 -{
    7.43 -    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
    7.44 -    AudioBuffer *ioData = &ioDataList->mBuffers[0];
    7.45 -    UInt32 remaining, len;
    7.46 -    void *ptr;
    7.47 -
    7.48 -    /* Is there ever more than one buffer, and what do you do with it? */
    7.49 -    if (ioDataList->mNumberBuffers != 1) {
    7.50 -        return noErr;
    7.51 -    }
    7.52 -
    7.53 -    /* Only do anything if audio is enabled and not paused */
    7.54 -    if (!this->enabled || this->paused) {
    7.55 -        SDL_memset(ioData->mData, this->spec.silence, ioData->mDataByteSize);
    7.56 -        return 0;
    7.57 -    }
    7.58 -
    7.59 -    /* No SDL conversion should be needed here, ever, since we accept
    7.60 -       any input format in OpenAudio, and leave the conversion to CoreAudio.
    7.61 -     */
    7.62 -    /*
    7.63 -       assert(!this->convert.needed);
    7.64 -       assert(this->spec.channels == ioData->mNumberChannels);
    7.65 -     */
    7.66 -
    7.67 -    remaining = ioData->mDataByteSize;
    7.68 -    ptr = ioData->mData;
    7.69 -    while (remaining > 0) {
    7.70 -        if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
    7.71 -            /* Generate the data */
    7.72 -            SDL_memset(this->hidden->buffer, this->spec.silence,
    7.73 -                       this->hidden->bufferSize);
    7.74 -            SDL_mutexP(this->mixer_lock);
    7.75 -            (*this->spec.callback) (this->spec.userdata, this->hidden->buffer,
    7.76 -                                    this->hidden->bufferSize);
    7.77 -            SDL_mutexV(this->mixer_lock);
    7.78 -            this->hidden->bufferOffset = 0;
    7.79 -        }
    7.80 -
    7.81 -        len = this->hidden->bufferSize - this->hidden->bufferOffset;
    7.82 -        if (len > remaining)
    7.83 -            len = remaining;
    7.84 -        SDL_memcpy(ptr,
    7.85 -                   (char *) this->hidden->buffer + this->hidden->bufferOffset,
    7.86 -                   len);
    7.87 -        ptr = (char *) ptr + len;
    7.88 -        remaining -= len;
    7.89 -        this->hidden->bufferOffset += len;
    7.90 -    }
    7.91 -
    7.92 -    return 0;
    7.93 -}
    7.94 -
    7.95 -static OSStatus
    7.96 -inputCallback(void *inRefCon,
    7.97 -              AudioUnitRenderActionFlags * ioActionFlags,
    7.98 -              const AudioTimeStamp * inTimeStamp,
    7.99 -              UInt32 inBusNumber, UInt32 inNumberFrames,
   7.100 -              AudioBufferList * ioData)
   7.101 -{
   7.102 -    //err = AudioUnitRender(afr->fAudioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, afr->fAudioBuffer);
   7.103 -    // !!! FIXME: write me!
   7.104 -    return noErr;
   7.105 -}
   7.106 -
   7.107 -
   7.108 -static void
   7.109 -COREAUDIO_CloseDevice(_THIS)
   7.110 -{
   7.111 -    if (this->hidden != NULL) {
   7.112 -        if (this->hidden->audioUnitOpened) {
   7.113 -            OSStatus result = noErr;
   7.114 -            AURenderCallbackStruct callback;
   7.115 -            const AudioUnitElement output_bus = 0;
   7.116 -            const AudioUnitElement input_bus = 1;
   7.117 -            const int iscapture = this->iscapture;
   7.118 -            const AudioUnitElement bus =
   7.119 -                ((iscapture) ? input_bus : output_bus);
   7.120 -            const AudioUnitScope scope =
   7.121 -                ((iscapture) ? kAudioUnitScope_Output :
   7.122 -                 kAudioUnitScope_Input);
   7.123 -
   7.124 -            /* stop processing the audio unit */
   7.125 -            result = AudioOutputUnitStop(this->hidden->audioUnit);
   7.126 -
   7.127 -            /* Remove the input callback */
   7.128 -            SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   7.129 -            result = AudioUnitSetProperty(this->hidden->audioUnit,
   7.130 -                                          kAudioUnitProperty_SetRenderCallback,
   7.131 -                                          scope, bus, &callback,
   7.132 -                                          sizeof(callback));
   7.133 -
   7.134 -            //CloseComponent(this->hidden->audioUnit);
   7.135 -            this->hidden->audioUnitOpened = 0;
   7.136 -        }
   7.137 -        SDL_free(this->hidden->buffer);
   7.138 -        SDL_free(this->hidden);
   7.139 -        this->hidden = NULL;
   7.140 -    }
   7.141 -}
   7.142 -
   7.143 -
   7.144 -#define CHECK_RESULT(msg) \
   7.145 -    if (result != noErr) { \
   7.146 -        COREAUDIO_CloseDevice(this); \
   7.147 -        SDL_SetError("CoreAudio error (%s): %d", msg, result); \
   7.148 -        return 0; \
   7.149 -    }
   7.150 -
   7.151 -static int
   7.152 -prepare_audiounit(_THIS, const char *devname, int iscapture,
   7.153 -                  const AudioStreamBasicDescription * strdesc)
   7.154 -{
   7.155 -    OSStatus result = noErr;
   7.156 -    AURenderCallbackStruct callback;
   7.157 -    AudioComponentDescription desc;
   7.158 -    AudioComponent comp = NULL;
   7.159 -
   7.160 -    UInt32 enableIO = 0;
   7.161 -    const AudioUnitElement output_bus = 0;
   7.162 -    const AudioUnitElement input_bus = 1;
   7.163 -    const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
   7.164 -    const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
   7.165 -                                  kAudioUnitScope_Input);
   7.166 -
   7.167 -    SDL_memset(&desc, '\0', sizeof(AudioComponentDescription));
   7.168 -    desc.componentType = kAudioUnitType_Output;
   7.169 -    desc.componentSubType = kAudioUnitSubType_RemoteIO;
   7.170 -    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
   7.171 -
   7.172 -    comp = AudioComponentFindNext(NULL, &desc);
   7.173 -    if (comp == NULL) {
   7.174 -        SDL_SetError("Couldn't find requested CoreAudio component");
   7.175 -        return 0;
   7.176 -    }
   7.177 -
   7.178 -    /* Open & initialize the audio unit */
   7.179 -    /*
   7.180 -       AudioComponentInstanceNew only available on iPhone OS 2.0 and Mac OS X 10.6  
   7.181 -       We can't use OpenAComponent on iPhone because it is not present
   7.182 -     */
   7.183 -    result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
   7.184 -    CHECK_RESULT("AudioComponentInstanceNew");
   7.185 -
   7.186 -    this->hidden->audioUnitOpened = 1;
   7.187 -
   7.188 -    // !!! FIXME: this is wrong?
   7.189 -    enableIO = ((iscapture) ? 1 : 0);
   7.190 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   7.191 -                                  kAudioOutputUnitProperty_EnableIO,
   7.192 -                                  kAudioUnitScope_Input, input_bus,
   7.193 -                                  &enableIO, sizeof(enableIO));
   7.194 -    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO input)");
   7.195 -
   7.196 -    // !!! FIXME: this is wrong?
   7.197 -    enableIO = ((iscapture) ? 0 : 1);
   7.198 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   7.199 -                                  kAudioOutputUnitProperty_EnableIO,
   7.200 -                                  kAudioUnitScope_Output, output_bus,
   7.201 -                                  &enableIO, sizeof(enableIO));
   7.202 -    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO output)");
   7.203 -
   7.204 -    /*result = AudioUnitSetProperty(this->hidden->audioUnit,
   7.205 -       kAudioOutputUnitProperty_CurrentDevice,
   7.206 -       kAudioUnitScope_Global, 0,
   7.207 -       &this->hidden->deviceID,
   7.208 -       sizeof(AudioDeviceID));
   7.209 -
   7.210 -       CHECK_RESULT("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)"); */
   7.211 -
   7.212 -    /* Set the data format of the audio unit. */
   7.213 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   7.214 -                                  kAudioUnitProperty_StreamFormat,
   7.215 -                                  scope, bus, strdesc, sizeof(*strdesc));
   7.216 -    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
   7.217 -
   7.218 -    /* Set the audio callback */
   7.219 -    SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   7.220 -    callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
   7.221 -    callback.inputProcRefCon = this;
   7.222 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   7.223 -                                  kAudioUnitProperty_SetRenderCallback,
   7.224 -                                  scope, bus, &callback, sizeof(callback));
   7.225 -    CHECK_RESULT
   7.226 -        ("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)");
   7.227 -
   7.228 -    /* Calculate the final parameters for this audio specification */
   7.229 -    SDL_CalculateAudioSpec(&this->spec);
   7.230 -
   7.231 -    /* Allocate a sample buffer */
   7.232 -    this->hidden->bufferOffset = this->hidden->bufferSize = this->spec.size;
   7.233 -    this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
   7.234 -
   7.235 -    result = AudioUnitInitialize(this->hidden->audioUnit);
   7.236 -    CHECK_RESULT("AudioUnitInitialize");
   7.237 -
   7.238 -    /* Finally, start processing of the audio unit */
   7.239 -    result = AudioOutputUnitStart(this->hidden->audioUnit);
   7.240 -    CHECK_RESULT("AudioOutputUnitStart");
   7.241 -    /* We're running! */
   7.242 -    return 1;
   7.243 -}
   7.244 -
   7.245 -static int
   7.246 -COREAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
   7.247 -{
   7.248 -    AudioStreamBasicDescription strdesc;
   7.249 -    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   7.250 -    int valid_datatype = 0;
   7.251 -
   7.252 -    /* Initialize all variables that we clean on shutdown */
   7.253 -    this->hidden = (struct SDL_PrivateAudioData *)
   7.254 -        SDL_malloc((sizeof *this->hidden));
   7.255 -    if (this->hidden == NULL) {
   7.256 -        SDL_OutOfMemory();
   7.257 -        return (0);
   7.258 -    }
   7.259 -    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   7.260 -
   7.261 -    /* Setup a AudioStreamBasicDescription with the requested format */
   7.262 -    SDL_memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
   7.263 -    strdesc.mFormatID = kAudioFormatLinearPCM;
   7.264 -    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
   7.265 -    strdesc.mChannelsPerFrame = this->spec.channels;
   7.266 -    strdesc.mSampleRate = this->spec.freq;
   7.267 -    strdesc.mFramesPerPacket = 1;
   7.268 -
   7.269 -    while ((!valid_datatype) && (test_format)) {
   7.270 -        this->spec.format = test_format;
   7.271 -        /* Just a list of valid SDL formats, so people don't pass junk here. */
   7.272 -        switch (test_format) {
   7.273 -        case AUDIO_U8:
   7.274 -        case AUDIO_S8:
   7.275 -        case AUDIO_U16LSB:
   7.276 -        case AUDIO_S16LSB:
   7.277 -        case AUDIO_U16MSB:
   7.278 -        case AUDIO_S16MSB:
   7.279 -        case AUDIO_S32LSB:
   7.280 -        case AUDIO_S32MSB:
   7.281 -        case AUDIO_F32LSB:
   7.282 -        case AUDIO_F32MSB:
   7.283 -            valid_datatype = 1;
   7.284 -            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
   7.285 -            if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
   7.286 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
   7.287 -
   7.288 -            if (SDL_AUDIO_ISFLOAT(this->spec.format))
   7.289 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
   7.290 -            else if (SDL_AUDIO_ISSIGNED(this->spec.format))
   7.291 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
   7.292 -            break;
   7.293 -        }
   7.294 -    }
   7.295 -
   7.296 -    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
   7.297 -        COREAUDIO_CloseDevice(this);
   7.298 -        SDL_SetError("Unsupported audio format");
   7.299 -        return 0;
   7.300 -    }
   7.301 -
   7.302 -    strdesc.mBytesPerFrame =
   7.303 -        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
   7.304 -    strdesc.mBytesPerPacket =
   7.305 -        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
   7.306 -
   7.307 -    if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
   7.308 -        COREAUDIO_CloseDevice(this);
   7.309 -        return 0;               /* prepare_audiounit() will call SDL_SetError()... */
   7.310 -    }
   7.311 -
   7.312 -    return 1;                   /* good to go. */
   7.313 -}
   7.314 -
   7.315 -static int
   7.316 -COREAUDIO_Init(SDL_AudioDriverImpl * impl)
   7.317 -{
   7.318 -    /* Set the function pointers */
   7.319 -    impl->OpenDevice = COREAUDIO_OpenDevice;
   7.320 -    impl->CloseDevice = COREAUDIO_CloseDevice;
   7.321 -    impl->ProvidesOwnCallbackThread = 1;
   7.322 -
   7.323 -    /* added for iPhone */
   7.324 -    impl->OnlyHasDefaultInputDevice = 1;
   7.325 -    impl->OnlyHasDefaultOutputDevice = 1;
   7.326 -    impl->HasCaptureSupport = 0;        /* still needs to be written */
   7.327 -
   7.328 -    return 1;   /* this audio target is available. */
   7.329 -}
   7.330 -
   7.331 -AudioBootStrap COREAUDIOIPHONE_bootstrap = {
   7.332 -    "coreaudio-iphoneos", "SDL CoreAudio (iPhone OS) audio driver",
   7.333 -    COREAUDIO_Init, 0
   7.334 -};
   7.335 -
   7.336 -/* vi: set ts=4 sw=4 expandtab: */
     8.1 --- a/src/audio/iphoneos/SDL_coreaudio_iphone.h	Tue Jul 26 14:20:22 2011 -0700
     8.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.3 @@ -1,42 +0,0 @@
     8.4 -/*
     8.5 -  Simple DirectMedia Layer
     8.6 -  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     8.7 -
     8.8 -  This software is provided 'as-is', without any express or implied
     8.9 -  warranty.  In no event will the authors be held liable for any damages
    8.10 -  arising from the use of this software.
    8.11 -
    8.12 -  Permission is granted to anyone to use this software for any purpose,
    8.13 -  including commercial applications, and to alter it and redistribute it
    8.14 -  freely, subject to the following restrictions:
    8.15 -
    8.16 -  1. The origin of this software must not be misrepresented; you must not
    8.17 -     claim that you wrote the original software. If you use this software
    8.18 -     in a product, an acknowledgment in the product documentation would be
    8.19 -     appreciated but is not required.
    8.20 -  2. Altered source versions must be plainly marked as such, and must not be
    8.21 -     misrepresented as being the original software.
    8.22 -  3. This notice may not be removed or altered from any source distribution.
    8.23 -*/
    8.24 -#include "SDL_config.h"
    8.25 -
    8.26 -#ifndef _SDL_coreaudio_h
    8.27 -#define _SDL_coreaudio_h
    8.28 -
    8.29 -#include "../SDL_sysaudio.h"
    8.30 -
    8.31 -/* Hidden "this" pointer for the audio functions */
    8.32 -#define _THIS	SDL_AudioDevice *this
    8.33 -
    8.34 -struct SDL_PrivateAudioData
    8.35 -{
    8.36 -    AudioUnit audioUnit;
    8.37 -    int audioUnitOpened;
    8.38 -    void *buffer;
    8.39 -    UInt32 bufferOffset;
    8.40 -    UInt32 bufferSize;
    8.41 -    //  AudioDeviceID deviceID;
    8.42 -};
    8.43 -
    8.44 -#endif /* _SDL_coreaudio_h */
    8.45 -/* vi: set ts=4 sw=4 expandtab: */
     9.1 --- a/src/audio/macosx/SDL_coreaudio.c	Tue Jul 26 14:20:22 2011 -0700
     9.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.3 @@ -1,513 +0,0 @@
     9.4 -/*
     9.5 -  Simple DirectMedia Layer
     9.6 -  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     9.7 -
     9.8 -  This software is provided 'as-is', without any express or implied
     9.9 -  warranty.  In no event will the authors be held liable for any damages
    9.10 -  arising from the use of this software.
    9.11 -
    9.12 -  Permission is granted to anyone to use this software for any purpose,
    9.13 -  including commercial applications, and to alter it and redistribute it
    9.14 -  freely, subject to the following restrictions:
    9.15 -
    9.16 -  1. The origin of this software must not be misrepresented; you must not
    9.17 -     claim that you wrote the original software. If you use this software
    9.18 -     in a product, an acknowledgment in the product documentation would be
    9.19 -     appreciated but is not required.
    9.20 -  2. Altered source versions must be plainly marked as such, and must not be
    9.21 -     misrepresented as being the original software.
    9.22 -  3. This notice may not be removed or altered from any source distribution.
    9.23 -*/
    9.24 -#include "SDL_config.h"
    9.25 -
    9.26 -#include <CoreAudio/CoreAudio.h>
    9.27 -#include <CoreServices/CoreServices.h>
    9.28 -#include <AudioUnit/AudioUnit.h>
    9.29 -#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
    9.30 -#include <AudioUnit/AUNTComponent.h>
    9.31 -#endif
    9.32 -
    9.33 -#include "SDL_audio.h"
    9.34 -#include "../SDL_audio_c.h"
    9.35 -#include "../SDL_sysaudio.h"
    9.36 -#include "SDL_coreaudio.h"
    9.37 -
    9.38 -#define DEBUG_COREAUDIO 0
    9.39 -
    9.40 -typedef void (*addDevFn)(const char *name, AudioDeviceID devId, void *data);
    9.41 -
    9.42 -static void
    9.43 -addToDevList(const char *name, AudioDeviceID devId, void *data)
    9.44 -{
    9.45 -    SDL_AddAudioDevice addfn = (SDL_AddAudioDevice) data;
    9.46 -    addfn(name);
    9.47 -}
    9.48 -
    9.49 -typedef struct
    9.50 -{
    9.51 -    const char *findname;
    9.52 -    AudioDeviceID devId;
    9.53 -    int found;
    9.54 -} FindDevIdData;
    9.55 -
    9.56 -static void
    9.57 -findDevId(const char *name, AudioDeviceID devId, void *_data)
    9.58 -{
    9.59 -    FindDevIdData *data = (FindDevIdData *) _data;
    9.60 -    if (!data->found) {
    9.61 -        if (SDL_strcmp(name, data->findname) == 0) {
    9.62 -            data->found = 1;
    9.63 -            data->devId = devId;
    9.64 -        }
    9.65 -    }
    9.66 -}
    9.67 -
    9.68 -static void
    9.69 -build_device_list(int iscapture, addDevFn addfn, void *addfndata)
    9.70 -{
    9.71 -    Boolean outWritable = 0;
    9.72 -    OSStatus result = noErr;
    9.73 -    UInt32 size = 0;
    9.74 -    AudioDeviceID *devs = NULL;
    9.75 -    UInt32 i = 0;
    9.76 -    UInt32 max = 0;
    9.77 -
    9.78 -    result = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
    9.79 -                                          &size, &outWritable);
    9.80 -
    9.81 -    if (result != kAudioHardwareNoError)
    9.82 -        return;
    9.83 -
    9.84 -    devs = (AudioDeviceID *) alloca(size);
    9.85 -    if (devs == NULL)
    9.86 -        return;
    9.87 -
    9.88 -    result = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
    9.89 -                                      &size, devs);
    9.90 -    if (result != kAudioHardwareNoError)
    9.91 -        return;
    9.92 -
    9.93 -    max = size / sizeof (AudioDeviceID);
    9.94 -    for (i = 0; i < max; i++) {
    9.95 -        CFStringRef cfstr = NULL;
    9.96 -        char *ptr = NULL;
    9.97 -        AudioDeviceID dev = devs[i];
    9.98 -        AudioBufferList *buflist = NULL;
    9.99 -        int usable = 0;
   9.100 -        CFIndex len = 0;
   9.101 -
   9.102 -        result = AudioDeviceGetPropertyInfo(dev, 0, iscapture,
   9.103 -                                            kAudioDevicePropertyStreamConfiguration,
   9.104 -                                            &size, &outWritable);
   9.105 -        if (result != noErr)
   9.106 -            continue;
   9.107 -
   9.108 -        buflist = (AudioBufferList *) SDL_malloc(size);
   9.109 -        if (buflist == NULL)
   9.110 -            continue;
   9.111 -
   9.112 -        result = AudioDeviceGetProperty(dev, 0, iscapture,
   9.113 -                                        kAudioDevicePropertyStreamConfiguration,
   9.114 -                                        &size, buflist);
   9.115 -
   9.116 -        if (result == noErr) {
   9.117 -            UInt32 j;
   9.118 -            for (j = 0; j < buflist->mNumberBuffers; j++) {
   9.119 -                if (buflist->mBuffers[j].mNumberChannels > 0) {
   9.120 -                    usable = 1;
   9.121 -                    break;
   9.122 -                }
   9.123 -            }
   9.124 -        }
   9.125 -
   9.126 -        SDL_free(buflist);
   9.127 -
   9.128 -        if (!usable)
   9.129 -            continue;
   9.130 -
   9.131 -        size = sizeof(CFStringRef);
   9.132 -        result = AudioDeviceGetProperty(dev, 0, iscapture,
   9.133 -                                        kAudioDevicePropertyDeviceNameCFString,
   9.134 -                                        &size, &cfstr);
   9.135 -
   9.136 -        if (result != kAudioHardwareNoError)
   9.137 -            continue;
   9.138 -
   9.139 -        len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr),
   9.140 -                                                kCFStringEncodingUTF8);
   9.141 -
   9.142 -        ptr = (char *) SDL_malloc(len + 1);
   9.143 -        usable = ((ptr != NULL) &&
   9.144 -                  (CFStringGetCString
   9.145 -                   (cfstr, ptr, len + 1, kCFStringEncodingUTF8)));
   9.146 -
   9.147 -        CFRelease(cfstr);
   9.148 -
   9.149 -        if (usable) {
   9.150 -            len = strlen(ptr);
   9.151 -            /* Some devices have whitespace at the end...trim it. */
   9.152 -            while ((len > 0) && (ptr[len - 1] == ' ')) {
   9.153 -                len--;
   9.154 -            }
   9.155 -            usable = (len > 0);
   9.156 -        }
   9.157 -
   9.158 -        if (usable) {
   9.159 -            ptr[len] = '\0';
   9.160 -
   9.161 -#if DEBUG_COREAUDIO
   9.162 -            printf("COREAUDIO: Found %s device #%d: '%s' (devid %d)\n",
   9.163 -                   ((iscapture) ? "capture" : "output"),
   9.164 -                   (int) *devCount, ptr, (int) dev);
   9.165 -#endif
   9.166 -            addfn(ptr, dev, addfndata);
   9.167 -        }
   9.168 -        SDL_free(ptr);  /* addfn() would have copied the string. */
   9.169 -    }
   9.170 -}
   9.171 -
   9.172 -static void
   9.173 -COREAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
   9.174 -{
   9.175 -    build_device_list(iscapture, addToDevList, addfn);
   9.176 -}
   9.177 -
   9.178 -/* The CoreAudio callback */
   9.179 -static OSStatus
   9.180 -outputCallback(void *inRefCon,
   9.181 -               AudioUnitRenderActionFlags * ioActionFlags,
   9.182 -               const AudioTimeStamp * inTimeStamp,
   9.183 -               UInt32 inBusNumber, UInt32 inNumberFrames,
   9.184 -               AudioBufferList * ioData)
   9.185 -{
   9.186 -    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
   9.187 -    AudioBuffer *abuf;
   9.188 -    UInt32 remaining, len;
   9.189 -    void *ptr;
   9.190 -    UInt32 i;
   9.191 -
   9.192 -    /* Only do anything if audio is enabled and not paused */
   9.193 -    if (!this->enabled || this->paused) {
   9.194 -        for (i = 0; i < ioData->mNumberBuffers; i++) {
   9.195 -            abuf = &ioData->mBuffers[i];
   9.196 -            SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
   9.197 -        }
   9.198 -        return 0;
   9.199 -    }
   9.200 -
   9.201 -    /* No SDL conversion should be needed here, ever, since we accept
   9.202 -       any input format in OpenAudio, and leave the conversion to CoreAudio.
   9.203 -     */
   9.204 -    /*
   9.205 -       assert(!this->convert.needed);
   9.206 -       assert(this->spec.channels == ioData->mNumberChannels);
   9.207 -     */
   9.208 -
   9.209 -    for (i = 0; i < ioData->mNumberBuffers; i++) {
   9.210 -        abuf = &ioData->mBuffers[i];
   9.211 -        remaining = abuf->mDataByteSize;
   9.212 -        ptr = abuf->mData;
   9.213 -        while (remaining > 0) {
   9.214 -            if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
   9.215 -                /* Generate the data */
   9.216 -                SDL_memset(this->hidden->buffer, this->spec.silence,
   9.217 -                           this->hidden->bufferSize);
   9.218 -                SDL_mutexP(this->mixer_lock);
   9.219 -                (*this->spec.callback)(this->spec.userdata,
   9.220 -                            this->hidden->buffer, this->hidden->bufferSize);
   9.221 -                SDL_mutexV(this->mixer_lock);
   9.222 -                this->hidden->bufferOffset = 0;
   9.223 -            }
   9.224 -
   9.225 -            len = this->hidden->bufferSize - this->hidden->bufferOffset;
   9.226 -            if (len > remaining)
   9.227 -                len = remaining;
   9.228 -            SDL_memcpy(ptr, (char *)this->hidden->buffer +
   9.229 -                       this->hidden->bufferOffset, len);
   9.230 -            ptr = (char *)ptr + len;
   9.231 -            remaining -= len;
   9.232 -            this->hidden->bufferOffset += len;
   9.233 -        }
   9.234 -    }
   9.235 -
   9.236 -    return 0;
   9.237 -}
   9.238 -
   9.239 -static OSStatus
   9.240 -inputCallback(void *inRefCon,
   9.241 -              AudioUnitRenderActionFlags * ioActionFlags,
   9.242 -              const AudioTimeStamp * inTimeStamp,
   9.243 -              UInt32 inBusNumber, UInt32 inNumberFrames,
   9.244 -              AudioBufferList * ioData)
   9.245 -{
   9.246 -    //err = AudioUnitRender(afr->fAudioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, afr->fAudioBuffer);
   9.247 -    // !!! FIXME: write me!
   9.248 -    return noErr;
   9.249 -}
   9.250 -
   9.251 -
   9.252 -static void
   9.253 -COREAUDIO_CloseDevice(_THIS)
   9.254 -{
   9.255 -    if (this->hidden != NULL) {
   9.256 -        if (this->hidden->audioUnitOpened) {
   9.257 -            OSStatus result = noErr;
   9.258 -            AURenderCallbackStruct callback;
   9.259 -            const AudioUnitElement output_bus = 0;
   9.260 -            const AudioUnitElement input_bus = 1;
   9.261 -            const int iscapture = this->iscapture;
   9.262 -            const AudioUnitElement bus =
   9.263 -                ((iscapture) ? input_bus : output_bus);
   9.264 -            const AudioUnitScope scope =
   9.265 -                ((iscapture) ? kAudioUnitScope_Output :
   9.266 -                 kAudioUnitScope_Input);
   9.267 -
   9.268 -            /* stop processing the audio unit */
   9.269 -            result = AudioOutputUnitStop(this->hidden->audioUnit);
   9.270 -
   9.271 -            /* Remove the input callback */
   9.272 -            SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   9.273 -            result = AudioUnitSetProperty(this->hidden->audioUnit,
   9.274 -                                          kAudioUnitProperty_SetRenderCallback,
   9.275 -                                          scope, bus, &callback,
   9.276 -                                          sizeof(callback));
   9.277 -
   9.278 -            CloseComponent(this->hidden->audioUnit);
   9.279 -            this->hidden->audioUnitOpened = 0;
   9.280 -        }
   9.281 -        SDL_free(this->hidden->buffer);
   9.282 -        SDL_free(this->hidden);
   9.283 -        this->hidden = NULL;
   9.284 -    }
   9.285 -}
   9.286 -
   9.287 -
   9.288 -#define CHECK_RESULT(msg) \
   9.289 -    if (result != noErr) { \
   9.290 -        COREAUDIO_CloseDevice(this); \
   9.291 -        SDL_SetError("CoreAudio error (%s): %d", msg, (int) result); \
   9.292 -        return 0; \
   9.293 -    }
   9.294 -
   9.295 -static int
   9.296 -find_device_by_name(_THIS, const char *devname, int iscapture)
   9.297 -{
   9.298 -    AudioDeviceID devid = 0;
   9.299 -    OSStatus result = noErr;
   9.300 -    UInt32 size = 0;
   9.301 -    UInt32 alive = 0;
   9.302 -    pid_t pid = 0;
   9.303 -
   9.304 -    if (devname == NULL) {
   9.305 -        size = sizeof(AudioDeviceID);
   9.306 -        const AudioHardwarePropertyID propid =
   9.307 -            ((iscapture) ? kAudioHardwarePropertyDefaultInputDevice :
   9.308 -             kAudioHardwarePropertyDefaultOutputDevice);
   9.309 -
   9.310 -        result = AudioHardwareGetProperty(propid, &size, &devid);
   9.311 -        CHECK_RESULT("AudioHardwareGetProperty (default device)");
   9.312 -    } else {
   9.313 -        FindDevIdData data;
   9.314 -        SDL_zero(data);
   9.315 -        data.findname = devname;
   9.316 -        build_device_list(iscapture, findDevId, &data);
   9.317 -        if (!data.found) {
   9.318 -            SDL_SetError("CoreAudio: No such audio device.");
   9.319 -            return 0;
   9.320 -        }
   9.321 -        devid = data.devId;
   9.322 -    }
   9.323 -
   9.324 -    size = sizeof(alive);
   9.325 -    result = AudioDeviceGetProperty(devid, 0, iscapture,
   9.326 -                                    kAudioDevicePropertyDeviceIsAlive,
   9.327 -                                    &size, &alive);
   9.328 -    CHECK_RESULT
   9.329 -        ("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)");
   9.330 -
   9.331 -    if (!alive) {
   9.332 -        SDL_SetError("CoreAudio: requested device exists, but isn't alive.");
   9.333 -        return 0;
   9.334 -    }
   9.335 -
   9.336 -    size = sizeof(pid);
   9.337 -    result = AudioDeviceGetProperty(devid, 0, iscapture,
   9.338 -                                    kAudioDevicePropertyHogMode, &size, &pid);
   9.339 -
   9.340 -    /* some devices don't support this property, so errors are fine here. */
   9.341 -    if ((result == noErr) && (pid != -1)) {
   9.342 -        SDL_SetError("CoreAudio: requested device is being hogged.");
   9.343 -        return 0;
   9.344 -    }
   9.345 -
   9.346 -    this->hidden->deviceID = devid;
   9.347 -    return 1;
   9.348 -}
   9.349 -
   9.350 -
   9.351 -static int
   9.352 -prepare_audiounit(_THIS, const char *devname, int iscapture,
   9.353 -                  const AudioStreamBasicDescription * strdesc)
   9.354 -{
   9.355 -    OSStatus result = noErr;
   9.356 -    AURenderCallbackStruct callback;
   9.357 -    ComponentDescription desc;
   9.358 -    Component comp = NULL;
   9.359 -    const AudioUnitElement output_bus = 0;
   9.360 -    const AudioUnitElement input_bus = 1;
   9.361 -    const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
   9.362 -    const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
   9.363 -                                  kAudioUnitScope_Input);
   9.364 -
   9.365 -    if (!find_device_by_name(this, devname, iscapture)) {
   9.366 -        SDL_SetError("Couldn't find requested CoreAudio device");
   9.367 -        return 0;
   9.368 -    }
   9.369 -
   9.370 -    SDL_memset(&desc, '\0', sizeof(ComponentDescription));
   9.371 -    desc.componentType = kAudioUnitType_Output;
   9.372 -    desc.componentSubType = kAudioUnitSubType_DefaultOutput;
   9.373 -    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
   9.374 -
   9.375 -    comp = FindNextComponent(NULL, &desc);
   9.376 -    if (comp == NULL) {
   9.377 -        SDL_SetError("Couldn't find requested CoreAudio component");
   9.378 -        return 0;
   9.379 -    }
   9.380 -
   9.381 -    /* Open & initialize the audio unit */
   9.382 -    result = OpenAComponent(comp, &this->hidden->audioUnit);
   9.383 -    CHECK_RESULT("OpenAComponent");
   9.384 -
   9.385 -    this->hidden->audioUnitOpened = 1;
   9.386 -
   9.387 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   9.388 -                                  kAudioOutputUnitProperty_CurrentDevice,
   9.389 -                                  kAudioUnitScope_Global, 0,
   9.390 -                                  &this->hidden->deviceID,
   9.391 -                                  sizeof(AudioDeviceID));
   9.392 -    CHECK_RESULT
   9.393 -        ("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
   9.394 -
   9.395 -    /* Set the data format of the audio unit. */
   9.396 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   9.397 -                                  kAudioUnitProperty_StreamFormat,
   9.398 -                                  scope, bus, strdesc, sizeof(*strdesc));
   9.399 -    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
   9.400 -
   9.401 -    /* Set the audio callback */
   9.402 -    SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
   9.403 -    callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
   9.404 -    callback.inputProcRefCon = this;
   9.405 -    result = AudioUnitSetProperty(this->hidden->audioUnit,
   9.406 -                                  kAudioUnitProperty_SetRenderCallback,
   9.407 -                                  scope, bus, &callback, sizeof(callback));
   9.408 -    CHECK_RESULT
   9.409 -        ("AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)");
   9.410 -
   9.411 -    /* Calculate the final parameters for this audio specification */
   9.412 -    SDL_CalculateAudioSpec(&this->spec);
   9.413 -
   9.414 -    /* Allocate a sample buffer */
   9.415 -    this->hidden->bufferOffset = this->hidden->bufferSize = this->spec.size;
   9.416 -    this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
   9.417 -
   9.418 -    result = AudioUnitInitialize(this->hidden->audioUnit);
   9.419 -    CHECK_RESULT("AudioUnitInitialize");
   9.420 -
   9.421 -    /* Finally, start processing of the audio unit */
   9.422 -    result = AudioOutputUnitStart(this->hidden->audioUnit);
   9.423 -    CHECK_RESULT("AudioOutputUnitStart");
   9.424 -
   9.425 -    /* We're running! */
   9.426 -    return 1;
   9.427 -}
   9.428 -
   9.429 -
   9.430 -static int
   9.431 -COREAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
   9.432 -{
   9.433 -    AudioStreamBasicDescription strdesc;
   9.434 -    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   9.435 -    int valid_datatype = 0;
   9.436 -
   9.437 -    /* Initialize all variables that we clean on shutdown */
   9.438 -    this->hidden = (struct SDL_PrivateAudioData *)
   9.439 -        SDL_malloc((sizeof *this->hidden));
   9.440 -    if (this->hidden == NULL) {
   9.441 -        SDL_OutOfMemory();
   9.442 -        return (0);
   9.443 -    }
   9.444 -    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   9.445 -
   9.446 -    /* Setup a AudioStreamBasicDescription with the requested format */
   9.447 -    SDL_memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
   9.448 -    strdesc.mFormatID = kAudioFormatLinearPCM;
   9.449 -    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
   9.450 -    strdesc.mChannelsPerFrame = this->spec.channels;
   9.451 -    strdesc.mSampleRate = this->spec.freq;
   9.452 -    strdesc.mFramesPerPacket = 1;
   9.453 -
   9.454 -    while ((!valid_datatype) && (test_format)) {
   9.455 -        this->spec.format = test_format;
   9.456 -        /* Just a list of valid SDL formats, so people don't pass junk here. */
   9.457 -        switch (test_format) {
   9.458 -        case AUDIO_U8:
   9.459 -        case AUDIO_S8:
   9.460 -        case AUDIO_U16LSB:
   9.461 -        case AUDIO_S16LSB:
   9.462 -        case AUDIO_U16MSB:
   9.463 -        case AUDIO_S16MSB:
   9.464 -        case AUDIO_S32LSB:
   9.465 -        case AUDIO_S32MSB:
   9.466 -        case AUDIO_F32LSB:
   9.467 -        case AUDIO_F32MSB:
   9.468 -            valid_datatype = 1;
   9.469 -            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
   9.470 -            if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
   9.471 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
   9.472 -
   9.473 -            if (SDL_AUDIO_ISFLOAT(this->spec.format))
   9.474 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
   9.475 -            else if (SDL_AUDIO_ISSIGNED(this->spec.format))
   9.476 -                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
   9.477 -            break;
   9.478 -        }
   9.479 -    }
   9.480 -
   9.481 -    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
   9.482 -        COREAUDIO_CloseDevice(this);
   9.483 -        SDL_SetError("Unsupported audio format");
   9.484 -        return 0;
   9.485 -    }
   9.486 -
   9.487 -    strdesc.mBytesPerFrame =
   9.488 -        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
   9.489 -    strdesc.mBytesPerPacket =
   9.490 -        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
   9.491 -
   9.492 -    if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
   9.493 -        COREAUDIO_CloseDevice(this);
   9.494 -        return 0;               /* prepare_audiounit() will call SDL_SetError()... */
   9.495 -    }
   9.496 -
   9.497 -    return 1;                   /* good to go. */
   9.498 -}
   9.499 -
   9.500 -static int
   9.501 -COREAUDIO_Init(SDL_AudioDriverImpl * impl)
   9.502 -{
   9.503 -    /* Set the function pointers */
   9.504 -    impl->DetectDevices = COREAUDIO_DetectDevices;
   9.505 -    impl->OpenDevice = COREAUDIO_OpenDevice;
   9.506 -    impl->CloseDevice = COREAUDIO_CloseDevice;
   9.507 -    impl->ProvidesOwnCallbackThread = 1;
   9.508 -
   9.509 -    return 1;   /* this audio target is available. */
   9.510 -}
   9.511 -
   9.512 -AudioBootStrap COREAUDIO_bootstrap = {
   9.513 -    "coreaudio", "Mac OS X CoreAudio", COREAUDIO_Init, 0
   9.514 -};
   9.515 -
   9.516 -/* vi: set ts=4 sw=4 expandtab: */
    10.1 --- a/src/audio/macosx/SDL_coreaudio.h	Tue Jul 26 14:20:22 2011 -0700
    10.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.3 @@ -1,42 +0,0 @@
    10.4 -/*
    10.5 -  Simple DirectMedia Layer
    10.6 -  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
    10.7 -
    10.8 -  This software is provided 'as-is', without any express or implied
    10.9 -  warranty.  In no event will the authors be held liable for any damages
   10.10 -  arising from the use of this software.
   10.11 -
   10.12 -  Permission is granted to anyone to use this software for any purpose,
   10.13 -  including commercial applications, and to alter it and redistribute it
   10.14 -  freely, subject to the following restrictions:
   10.15 -
   10.16 -  1. The origin of this software must not be misrepresented; you must not
   10.17 -     claim that you wrote the original software. If you use this software
   10.18 -     in a product, an acknowledgment in the product documentation would be
   10.19 -     appreciated but is not required.
   10.20 -  2. Altered source versions must be plainly marked as such, and must not be
   10.21 -     misrepresented as being the original software.
   10.22 -  3. This notice may not be removed or altered from any source distribution.
   10.23 -*/
   10.24 -#include "SDL_config.h"
   10.25 -
   10.26 -#ifndef _SDL_coreaudio_h
   10.27 -#define _SDL_coreaudio_h
   10.28 -
   10.29 -#include "../SDL_sysaudio.h"
   10.30 -
   10.31 -/* Hidden "this" pointer for the audio functions */
   10.32 -#define _THIS	SDL_AudioDevice *this
   10.33 -
   10.34 -struct SDL_PrivateAudioData
   10.35 -{
   10.36 -    AudioUnit audioUnit;
   10.37 -    int audioUnitOpened;
   10.38 -    void *buffer;
   10.39 -    UInt32 bufferOffset;
   10.40 -    UInt32 bufferSize;
   10.41 -    AudioDeviceID deviceID;
   10.42 -};
   10.43 -
   10.44 -#endif /* _SDL_coreaudio_h */
   10.45 -/* vi: set ts=4 sw=4 expandtab: */