Commit b2540abd authored by Ryan C. Gordon's avatar Ryan C. Gordon

Merged Mac OS X and iOS audio targets.

--HG--
rename : src/audio/macosx/SDL_coreaudio.c => src/audio/coreaudio/SDL_coreaudio.c
rename : src/audio/macosx/SDL_coreaudio.h => src/audio/coreaudio/SDL_coreaudio.h
parent cd8da309
......@@ -123,6 +123,8 @@
04FFAB9612E23BDC00BA343D /* SDL_blendmode.h in Headers */ = {isa = PBXBuildFile; fileRef = 04FFAB9312E23BDC00BA343D /* SDL_blendmode.h */; settings = {ATTRIBUTES = (Public, ); }; };
04FFAB9812E23BDC00BA343D /* SDL_shape.h in Headers */ = {isa = PBXBuildFile; fileRef = 04FFAB9512E23BDC00BA343D /* SDL_shape.h */; settings = {ATTRIBUTES = (Public, ); }; };
22C905CD13A22646003FE4E4 /* SDL_hints_c.h in Headers */ = {isa = PBXBuildFile; fileRef = 22C905CC13A22646003FE4E4 /* SDL_hints_c.h */; };
56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.c in Sources */ = {isa = PBXBuildFile; fileRef = 56EA86F913E9EC2B002E47EB /* SDL_coreaudio.c */; };
56EA86FC13E9EC2B002E47EB /* SDL_coreaudio.h in Headers */ = {isa = PBXBuildFile; fileRef = 56EA86FA13E9EC2B002E47EB /* SDL_coreaudio.h */; };
56ED04E1118A8EE200A56AA6 /* SDL_power.c in Sources */ = {isa = PBXBuildFile; fileRef = 56ED04E0118A8EE200A56AA6 /* SDL_power.c */; };
56ED04E3118A8EFD00A56AA6 /* SDL_syspower.m in Sources */ = {isa = PBXBuildFile; fileRef = 56ED04E2118A8EFD00A56AA6 /* SDL_syspower.m */; };
FD24846D0E5655AE0021E198 /* SDL_uikitkeyboard.h in Headers */ = {isa = PBXBuildFile; fileRef = FD24846B0E5655AE0021E198 /* SDL_uikitkeyboard.h */; };
......@@ -191,8 +193,6 @@
FD65267F0DE8FCDD002AD96B /* SDL_thread.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA150DD52EDC00FB1D6B /* SDL_thread.c */; };
FD6526800DE8FCDD002AD96B /* SDL_timer.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA2E0DD52EDC00FB1D6B /* SDL_timer.c */; };
FD6526810DE8FCDD002AD96B /* SDL_systimer.c in Sources */ = {isa = PBXBuildFile; fileRef = FD99BA310DD52EDC00FB1D6B /* SDL_systimer.c */; };
FD689EFB0E26E57800F90B21 /* SDL_coreaudio_iphone.c in Sources */ = {isa = PBXBuildFile; fileRef = FD689EF90E26E57800F90B21 /* SDL_coreaudio_iphone.c */; };
FD689EFC0E26E57800F90B21 /* SDL_coreaudio_iphone.h in Headers */ = {isa = PBXBuildFile; fileRef = FD689EFA0E26E57800F90B21 /* SDL_coreaudio_iphone.h */; };
FD689F030E26E5B600F90B21 /* SDL_sysjoystick.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F000E26E5B600F90B21 /* SDL_sysjoystick.m */; };
FD689F040E26E5B600F90B21 /* SDLUIAccelerationDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = FD689F010E26E5B600F90B21 /* SDLUIAccelerationDelegate.h */; };
FD689F050E26E5B600F90B21 /* SDLUIAccelerationDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = FD689F020E26E5B600F90B21 /* SDLUIAccelerationDelegate.m */; };
......@@ -378,6 +378,8 @@
04FFAB9312E23BDC00BA343D /* SDL_blendmode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_blendmode.h; path = ../../include/SDL_blendmode.h; sourceTree = SOURCE_ROOT; };
04FFAB9512E23BDC00BA343D /* SDL_shape.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_shape.h; path = ../../include/SDL_shape.h; sourceTree = SOURCE_ROOT; };
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>"; };
56EA86F913E9EC2B002E47EB /* SDL_coreaudio.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = SDL_coreaudio.c; path = coreaudio/SDL_coreaudio.c; sourceTree = "<group>"; };
56EA86FA13E9EC2B002E47EB /* SDL_coreaudio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SDL_coreaudio.h; path = coreaudio/SDL_coreaudio.h; sourceTree = "<group>"; };
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; };
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; };
FD0BBFEF0E3933DD00D833B1 /* SDL_uikitview.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_uikitview.h; sourceTree = "<group>"; };
......@@ -392,8 +394,6 @@
FD5F9D1F0E0E08B3008E885B /* SDL_joystick_c.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_joystick_c.h; sourceTree = "<group>"; };
FD5F9D200E0E08B3008E885B /* SDL_sysjoystick.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_sysjoystick.h; sourceTree = "<group>"; };
FD6526630DE8FCCB002AD96B /* libSDL.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDL.a; sourceTree = BUILT_PRODUCTS_DIR; };
FD689EF90E26E57800F90B21 /* SDL_coreaudio_iphone.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = SDL_coreaudio_iphone.c; sourceTree = "<group>"; };
FD689EFA0E26E57800F90B21 /* SDL_coreaudio_iphone.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_coreaudio_iphone.h; sourceTree = "<group>"; };
FD689F000E26E5B600F90B21 /* SDL_sysjoystick.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_sysjoystick.m; sourceTree = "<group>"; };
FD689F010E26E5B600F90B21 /* SDLUIAccelerationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDLUIAccelerationDelegate.h; sourceTree = "<group>"; };
FD689F020E26E5B600F90B21 /* SDLUIAccelerationDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDLUIAccelerationDelegate.m; sourceTree = "<group>"; };
......@@ -785,6 +785,15 @@
name = Frameworks;
sourceTree = "<group>";
};
56EA86F813E9EBF9002E47EB /* coreaudio */ = {
isa = PBXGroup;
children = (
56EA86F913E9EC2B002E47EB /* SDL_coreaudio.c */,
56EA86FA13E9EC2B002E47EB /* SDL_coreaudio.h */,
);
name = coreaudio;
sourceTree = "<group>";
};
56ED04DE118A8E9A00A56AA6 /* power */ = {
isa = PBXGroup;
children = (
......@@ -828,15 +837,6 @@
path = ../../src/joystick;
sourceTree = SOURCE_ROOT;
};
FD689EF80E26E57800F90B21 /* iphoneos */ = {
isa = PBXGroup;
children = (
FD689EF90E26E57800F90B21 /* SDL_coreaudio_iphone.c */,
FD689EFA0E26E57800F90B21 /* SDL_coreaudio_iphone.h */,
);
path = iphoneos;
sourceTree = "<group>";
};
FD689EFF0E26E5B600F90B21 /* iphoneos */ = {
isa = PBXGroup;
children = (
......@@ -983,7 +983,7 @@
FD99B8FB0DD52EDC00FB1D6B /* audio */ = {
isa = PBXGroup;
children = (
FD689EF80E26E57800F90B21 /* iphoneos */,
56EA86F813E9EBF9002E47EB /* coreaudio */,
FD99B91C0DD52EDC00FB1D6B /* dummy */,
FD99B9440DD52EDC00FB1D6B /* SDL_audio.c */,
FD99B9450DD52EDC00FB1D6B /* SDL_audio_c.h */,
......@@ -1196,7 +1196,6 @@
FDA686000DF244C800F98A1A /* SDL_nullvideo.h in Headers */,
FD5F9D300E0E08B3008E885B /* SDL_joystick_c.h in Headers */,
FD5F9D310E0E08B3008E885B /* SDL_sysjoystick.h in Headers */,
FD689EFC0E26E57800F90B21 /* SDL_coreaudio_iphone.h in Headers */,
FD689F040E26E5B600F90B21 /* SDLUIAccelerationDelegate.h in Headers */,
FD689F1C0E26E5D900F90B21 /* SDL_uikitevents.h in Headers */,
FD689F1E0E26E5D900F90B21 /* SDL_uikitopengles.h in Headers */,
......@@ -1245,6 +1244,7 @@
04BAC09C1300C1290055DE28 /* SDL_assert_c.h in Headers */,
0417A516130C988500578583 /* SDL_keycode.h in Headers */,
22C905CD13A22646003FE4E4 /* SDL_hints_c.h in Headers */,
56EA86FC13E9EC2B002E47EB /* SDL_coreaudio.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -1433,7 +1433,6 @@
FDA685FB0DF244C800F98A1A /* SDL_nullevents.c in Sources */,
FDA685FF0DF244C800F98A1A /* SDL_nullvideo.c in Sources */,
FD5F9D2F0E0E08B3008E885B /* SDL_joystick.c in Sources */,
FD689EFB0E26E57800F90B21 /* SDL_coreaudio_iphone.c in Sources */,
FD689F030E26E5B600F90B21 /* SDL_sysjoystick.m in Sources */,
FD689F050E26E5B600F90B21 /* SDLUIAccelerationDelegate.m in Sources */,
FD689F1D0E26E5D900F90B21 /* SDL_uikitevents.m in Sources */,
......@@ -1474,6 +1473,7 @@
0402A85812FE70C600CECEE3 /* SDL_render_gles2.c in Sources */,
0402A85912FE70C600CECEE3 /* SDL_shaders_gles2.c in Sources */,
04BAC09D1300C1290055DE28 /* SDL_log.c in Sources */,
56EA86FB13E9EC2B002E47EB /* SDL_coreaudio.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......
......@@ -1139,7 +1139,7 @@
children = (
04BDFD8712E6671700899322 /* disk */,
04BDFD9312E6671700899322 /* dummy */,
04BDFD9F12E6671700899322 /* macosx */,
04BDFD9F12E6671700899322 /* coreaudio */,
04BDFDB412E6671700899322 /* SDL_audio.c */,
04BDFDB512E6671700899322 /* SDL_audio_c.h */,
04BDFDB612E6671700899322 /* SDL_audiocvt.c */,
......@@ -1174,13 +1174,13 @@
path = dummy;
sourceTree = "<group>";
};
04BDFD9F12E6671700899322 /* macosx */ = {
04BDFD9F12E6671700899322 /* coreaudio */ = {
isa = PBXGroup;
children = (
04BDFDA012E6671700899322 /* SDL_coreaudio.c */,
04BDFDA112E6671700899322 /* SDL_coreaudio.h */,
);
path = macosx;
path = coreaudio;
sourceTree = "<group>";
};
04BDFDD312E6671700899322 /* cpuinfo */ = {
......
......@@ -2306,7 +2306,7 @@ AC_HELP_STRING([--enable-render-d3d], [enable the Direct3D render driver [[defau
# Set up files for the audio library
if test x$enable_audio = xyes; then
AC_DEFINE(SDL_AUDIO_DRIVER_COREAUDIO, 1, [ ])
SOURCES="$SOURCES $srcdir/src/audio/macosx/*.c"
SOURCES="$SOURCES $srcdir/src/audio/coreaudio/*.c"
have_audio=yes
fi
# Set up files for the joystick library
......
......@@ -58,7 +58,6 @@ extern AudioBootStrap WINMM_bootstrap;
extern AudioBootStrap PAUDIO_bootstrap;
extern AudioBootStrap BEOSAUDIO_bootstrap;
extern AudioBootStrap COREAUDIO_bootstrap;
extern AudioBootStrap COREAUDIOIPHONE_bootstrap;
extern AudioBootStrap SNDMGR_bootstrap;
extern AudioBootStrap DISKAUD_bootstrap;
extern AudioBootStrap DUMMYAUD_bootstrap;
......@@ -116,9 +115,6 @@ static const AudioBootStrap *const bootstrap[] = {
#if SDL_AUDIO_DRIVER_COREAUDIO
&COREAUDIO_bootstrap,
#endif
#if SDL_AUDIO_DRIVER_COREAUDIOIPHONE
&COREAUDIOIPHONE_bootstrap,
#endif
#if SDL_AUDIO_DRIVER_DISK
&DISKAUD_bootstrap,
#endif
......
......@@ -20,12 +20,15 @@
*/
#include "SDL_config.h"
#if MACOSX_COREAUDIO
#include <CoreAudio/CoreAudio.h>
#include <CoreServices/CoreServices.h>
#include <AudioUnit/AudioUnit.h>
#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5
#include <AudioUnit/AUNTComponent.h>
#endif
#endif
#include <AudioUnit/AudioUnit.h>
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
......@@ -34,6 +37,16 @@
#define DEBUG_COREAUDIO 0
static void COREAUDIO_CloseDevice(_THIS);
#define CHECK_RESULT(msg) \
if (result != noErr) { \
COREAUDIO_CloseDevice(this); \
SDL_SetError("CoreAudio error (%s): %d", msg, (int) result); \
return 0; \
}
#if MACOSX_COREAUDIO
typedef void (*addDevFn)(const char *name, AudioDeviceID devId, void *data);
static void
......@@ -172,6 +185,62 @@ COREAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
build_device_list(iscapture, addToDevList, addfn);
}
static int
find_device_by_name(_THIS, const char *devname, int iscapture)
{
AudioDeviceID devid = 0;
OSStatus result = noErr;
UInt32 size = 0;
UInt32 alive = 0;
pid_t pid = 0;
if (devname == NULL) {
size = sizeof(AudioDeviceID);
const AudioHardwarePropertyID propid =
((iscapture) ? kAudioHardwarePropertyDefaultInputDevice :
kAudioHardwarePropertyDefaultOutputDevice);
result = AudioHardwareGetProperty(propid, &size, &devid);
CHECK_RESULT("AudioHardwareGetProperty (default device)");
} else {
FindDevIdData data;
SDL_zero(data);
data.findname = devname;
build_device_list(iscapture, findDevId, &data);
if (!data.found) {
SDL_SetError("CoreAudio: No such audio device.");
return 0;
}
devid = data.devId;
}
size = sizeof(alive);
result = AudioDeviceGetProperty(devid, 0, iscapture,
kAudioDevicePropertyDeviceIsAlive,
&size, &alive);
CHECK_RESULT
("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)");
if (!alive) {
SDL_SetError("CoreAudio: requested device exists, but isn't alive.");
return 0;
}
size = sizeof(pid);
result = AudioDeviceGetProperty(devid, 0, iscapture,
kAudioDevicePropertyHogMode, &size, &pid);
/* some devices don't support this property, so errors are fine here. */
if ((result == noErr) && (pid != -1)) {
SDL_SetError("CoreAudio: requested device is being hogged.");
return 0;
}
this->hidden->deviceID = devid;
return 1;
}
#endif
/* The CoreAudio callback */
static OSStatus
outputCallback(void *inRefCon,
......@@ -272,7 +341,11 @@ COREAUDIO_CloseDevice(_THIS)
scope, bus, &callback,
sizeof(callback));
/* !!! FIXME: how does iOS free this? */
#if MACOSX_COREAUDIO
CloseComponent(this->hidden->audioUnit);
#endif
this->hidden->audioUnitOpened = 0;
}
SDL_free(this->hidden->buffer);
......@@ -282,105 +355,65 @@ COREAUDIO_CloseDevice(_THIS)
}
#define CHECK_RESULT(msg) \
if (result != noErr) { \
COREAUDIO_CloseDevice(this); \
SDL_SetError("CoreAudio error (%s): %d", msg, (int) result); \
return 0; \
}
static int
find_device_by_name(_THIS, const char *devname, int iscapture)
{
AudioDeviceID devid = 0;
OSStatus result = noErr;
UInt32 size = 0;
UInt32 alive = 0;
pid_t pid = 0;
if (devname == NULL) {
size = sizeof(AudioDeviceID);
const AudioHardwarePropertyID propid =
((iscapture) ? kAudioHardwarePropertyDefaultInputDevice :
kAudioHardwarePropertyDefaultOutputDevice);
result = AudioHardwareGetProperty(propid, &size, &devid);
CHECK_RESULT("AudioHardwareGetProperty (default device)");
} else {
FindDevIdData data;
SDL_zero(data);
data.findname = devname;
build_device_list(iscapture, findDevId, &data);
if (!data.found) {
SDL_SetError("CoreAudio: No such audio device.");
return 0;
}
devid = data.devId;
}
size = sizeof(alive);
result = AudioDeviceGetProperty(devid, 0, iscapture,
kAudioDevicePropertyDeviceIsAlive,
&size, &alive);
CHECK_RESULT
("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)");
if (!alive) {
SDL_SetError("CoreAudio: requested device exists, but isn't alive.");
return 0;
}
size = sizeof(pid);
result = AudioDeviceGetProperty(devid, 0, iscapture,
kAudioDevicePropertyHogMode, &size, &pid);
/* some devices don't support this property, so errors are fine here. */
if ((result == noErr) && (pid != -1)) {
SDL_SetError("CoreAudio: requested device is being hogged.");
return 0;
}
this->hidden->deviceID = devid;
return 1;
}
static int
prepare_audiounit(_THIS, const char *devname, int iscapture,
const AudioStreamBasicDescription * strdesc)
{
OSStatus result = noErr;
AURenderCallbackStruct callback;
#if MACOSX_COREAUDIO
ComponentDescription desc;
Component comp = NULL;
#else
AudioComponentDescription desc;
AudioComponent comp = NULL;
#endif
const AudioUnitElement output_bus = 0;
const AudioUnitElement input_bus = 1;
const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
kAudioUnitScope_Input);
#if MACOSX_COREAUDIO
if (!find_device_by_name(this, devname, iscapture)) {
SDL_SetError("Couldn't find requested CoreAudio device");
return 0;
}
SDL_memset(&desc, '\0', sizeof(ComponentDescription));
#endif
SDL_zero(desc);
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
#if MACOSX_COREAUDIO
desc.componentSubType = kAudioUnitSubType_DefaultOutput;
comp = FindNextComponent(NULL, &desc);
#else
desc.componentSubType = kAudioUnitSubType_RemoteIO; /* !!! FIXME: ? */
comp = AudioComponentFindNext(NULL, &desc);
#endif
if (comp == NULL) {
SDL_SetError("Couldn't find requested CoreAudio component");
return 0;
}
/* Open & initialize the audio unit */
#if MACOSX_COREAUDIO
result = OpenAComponent(comp, &this->hidden->audioUnit);
CHECK_RESULT("OpenAComponent");
#else
/*
AudioComponentInstanceNew only available on iPhone OS 2.0 and Mac OS X 10.6
We can't use OpenAComponent on iPhone because it is not present
*/
result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
CHECK_RESULT("AudioComponentInstanceNew");
#endif
this->hidden->audioUnitOpened = 1;
#if MACOSX_COREAUDIO
result = AudioUnitSetProperty(this->hidden->audioUnit,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global, 0,
......@@ -388,6 +421,7 @@ prepare_audiounit(_THIS, const char *devname, int iscapture,
sizeof(AudioDeviceID));
CHECK_RESULT
("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
#endif
/* Set the data format of the audio unit. */
result = AudioUnitSetProperty(this->hidden->audioUnit,
......@@ -498,16 +532,22 @@ static int
COREAUDIO_Init(SDL_AudioDriverImpl * impl)
{
/* Set the function pointers */
impl->DetectDevices = COREAUDIO_DetectDevices;
impl->OpenDevice = COREAUDIO_OpenDevice;
impl->CloseDevice = COREAUDIO_CloseDevice;
#if MACOSX_COREAUDIO
impl->DetectDevices = COREAUDIO_DetectDevices;
#else
impl->OnlyHasDefaultOutputDevice = 1;
#endif
impl->ProvidesOwnCallbackThread = 1;
return 1; /* this audio target is available. */
}
AudioBootStrap COREAUDIO_bootstrap = {
"coreaudio", "Mac OS X CoreAudio", COREAUDIO_Init, 0
"coreaudio", "CoreAudio", COREAUDIO_Init, 0
};
/* vi: set ts=4 sw=4 expandtab: */
......@@ -25,6 +25,10 @@
#include "../SDL_sysaudio.h"
#if !defined(__IPHONEOS__)
#define MACOSX_COREAUDIO 1
#endif
/* Hidden "this" pointer for the audio functions */
#define _THIS SDL_AudioDevice *this
......@@ -35,7 +39,9 @@ struct SDL_PrivateAudioData
void *buffer;
UInt32 bufferOffset;
UInt32 bufferSize;
#if MACOSX_COREAUDIO
AudioDeviceID deviceID;
#endif
};
#endif /* _SDL_coreaudio_h */
......
/*
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#include <AudioUnit/AudioUnit.h>
#include "SDL_audio.h"
#include "../SDL_audio_c.h"
#include "../SDL_sysaudio.h"
#include "SDL_coreaudio_iphone.h"
#define DEBUG_COREAUDIO 0
/* The CoreAudio callback */
static OSStatus
outputCallback(void *inRefCon,
AudioUnitRenderActionFlags * ioActionFlags,
const AudioTimeStamp * inTimeStamp,
UInt32 inBusNumber, UInt32 inNumberFrames,
AudioBufferList * ioDataList)
{
SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
AudioBuffer *ioData = &ioDataList->mBuffers[0];
UInt32 remaining, len;
void *ptr;
/* Is there ever more than one buffer, and what do you do with it? */
if (ioDataList->mNumberBuffers != 1) {
return noErr;
}
/* Only do anything if audio is enabled and not paused */
if (!this->enabled || this->paused) {
SDL_memset(ioData->mData, this->spec.silence, ioData->mDataByteSize);
return 0;
}
/* No SDL conversion should be needed here, ever, since we accept
any input format in OpenAudio, and leave the conversion to CoreAudio.
*/
/*
assert(!this->convert.needed);
assert(this->spec.channels == ioData->mNumberChannels);
*/
remaining = ioData->mDataByteSize;
ptr = ioData->mData;
while (remaining > 0) {
if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
/* Generate the data */
SDL_memset(this->hidden->buffer, this->spec.silence,
this->hidden->bufferSize);
SDL_mutexP(this->mixer_lock);
(*this->spec.callback) (this->spec.userdata, this->hidden->buffer,
this->hidden->bufferSize);
SDL_mutexV(this->mixer_lock);
this->hidden->bufferOffset = 0;
}
len = this->hidden->bufferSize - this->hidden->bufferOffset;
if (len > remaining)
len = remaining;
SDL_memcpy(ptr,
(char *) this->hidden->buffer + this->hidden->bufferOffset,
len);
ptr = (char *) ptr + len;
remaining -= len;
this->hidden->bufferOffset += len;
}
return 0;
}
static OSStatus
inputCallback(void *inRefCon,
AudioUnitRenderActionFlags * ioActionFlags,
const AudioTimeStamp * inTimeStamp,
UInt32 inBusNumber, UInt32 inNumberFrames,
AudioBufferList * ioData)
{
//err = AudioUnitRender(afr->fAudioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, afr->fAudioBuffer);
// !!! FIXME: write me!
return noErr;
}
static void
COREAUDIO_CloseDevice(_THIS)
{
if (this->hidden != NULL) {
if (this->hidden->audioUnitOpened) {
OSStatus result = noErr;
AURenderCallbackStruct callback;
const AudioUnitElement output_bus = 0;
const AudioUnitElement input_bus = 1;
const int iscapture = this->iscapture;
const AudioUnitElement bus =
((iscapture) ? input_bus : output_bus);
const AudioUnitScope scope =
((iscapture) ? kAudioUnitScope_Output :
kAudioUnitScope_Input);
/* stop processing the audio unit */
result = AudioOutputUnitStop(this->hidden->audioUnit);
/* Remove the input callback */
SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
result = AudioUnitSetProperty(this->hidden->audioUnit,
kAudioUnitProperty_SetRenderCallback,
scope, bus, &callback,
sizeof(callback));
//CloseComponent(this->hidden->audioUnit);
this->hidden->audioUnitOpened = 0;
}
SDL_free(this->hidden->buffer);
SDL_free(this->hidden);
this->hidden = NULL;
}
}
#define CHECK_RESULT(msg) \
if (result != noErr) { \
COREAUDIO_CloseDevice(this); \
SDL_SetError("CoreAudio error (%s): %d", msg, result); \
return 0; \
}
static int
prepare_audiounit(_THIS, const char *devname, int iscapture,
const AudioStreamBasicDescription * strdesc)
{
OSStatus result = noErr;
AURenderCallbackStruct callback;
AudioComponentDescription desc;
AudioComponent comp = NULL;
UInt32 enableIO = 0;
const AudioUnitElement output_bus = 0;
const AudioUnitElement input_bus = 1;
const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
kAudioUnitScope_Input);
SDL_memset(&desc, '\0', sizeof(AudioComponentDescription));
desc.componentType = kAudioUnitType_Output;
desc.componentSubType = kAudioUnitSubType_RemoteIO;
desc.componentManufacturer = kAudioUnitManufacturer_Apple;
comp = AudioComponentFindNext(NULL, &desc);
if (comp == NULL) {
SDL_SetError("Couldn't find requested CoreAudio component");
return 0;
}
/* Open & initialize the audio unit */
/*
AudioComponentInstanceNew only available on iPhone OS 2.0 and Mac OS X 10.6
We can't use OpenAComponent on iPhone because it is not present
*/
result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
CHECK_RESULT("AudioComponentInstanceNew");
this->hidden->audioUnitOpened = 1;
// !!! FIXME: this is wrong?
enableIO = ((iscapture) ? 1 : 0);
result = AudioUnitSetProperty(this->hidden->audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Input, input_bus,
&enableIO, sizeof(enableIO));
CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO input)");
// !!! FIXME: this is wrong?
enableIO = ((iscapture) ? 0 : 1);
result = AudioUnitSetProperty(this->hidden->audioUnit,
kAudioOutputUnitProperty_EnableIO,
kAudioUnitScope_Output, output_bus,
&enableIO, sizeof(enableIO));
CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO output)");
/*result = AudioUnitSetProperty(this->hidden->audioUnit,
kAudioOutputUnitProperty_CurrentDevice,
kAudioUnitScope_Global, 0,
&this->hidden->deviceID,
sizeof(AudioDeviceID));
CHECK_RESULT("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)"); */
/* Set the data format of the audio unit. */
result = AudioUnitSetProperty(this->hidden->audioUnit,
kAudioUnitProperty_StreamFormat,
scope, bus, strdesc, sizeof(*strdesc));
CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
/* Set the audio callback */
SDL_memset(&callback, '\0', sizeof(AURenderCallbackStruct));
callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
callback.inputProcRefCon = this;
result = AudioUnitSetProperty(this->hidden->audioUnit,
kAudioUnitProperty_SetRenderCallback,
scope, bus, &callback, sizeof(callback));
CHECK_RESULT
("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)");
/* Calculate the final parameters for this audio specification */
SDL_CalculateAudioSpec(&this->spec);
/* Allocate a sample buffer */
this->hidden->bufferOffset = this->hidden->bufferSize = this->spec.size;
this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
result = AudioUnitInitialize(this->hidden->audioUnit);
CHECK_RESULT("AudioUnitInitialize");
/* Finally, start processing of the audio unit */
result = AudioOutputUnitStart(this->hidden->audioUnit);
CHECK_RESULT("AudioOutputUnitStart");
/* We're running! */
return 1;
}
static int
COREAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
{
AudioStreamBasicDescription strdesc;
SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
int valid_datatype = 0;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
SDL_malloc((sizeof *this->hidden));
if (this->hidden == NULL) {
SDL_OutOfMemory();
return (0);
}
SDL_memset(this->hidden, 0, (sizeof *this->hidden));
/* Setup a AudioStreamBasicDescription with the requested format */
SDL_memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
strdesc.mFormatID = kAudioFormatLinearPCM;
strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
strdesc.mChannelsPerFrame = this->spec.channels;
strdesc.mSampleRate = this->spec.freq;
strdesc.mFramesPerPacket = 1;
while ((!valid_datatype) && (test_format)) {
this->spec.format = test_format;
/* Just a list of valid SDL formats, so people don't pass junk here. */
switch (test_format) {
case AUDIO_U8:
case AUDIO_S8:
case AUDIO_U16LSB:
case AUDIO_S16LSB:
case AUDIO_U16MSB:
case AUDIO_S16MSB:
case AUDIO_S32LSB:
case AUDIO_S32MSB:
case AUDIO_F32LSB:
case AUDIO_F32MSB:
valid_datatype = 1;
strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
if (SDL_AUDIO_ISFLOAT(this->spec.format))
strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
else if (SDL_AUDIO_ISSIGNED(this->spec.format))
strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
break;
}
}
if (!valid_datatype) { /* shouldn't happen, but just in case... */
COREAUDIO_CloseDevice(this);
SDL_SetError("Unsupported audio format");
return 0;
}
strdesc.mBytesPerFrame =
strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
strdesc.mBytesPerPacket =
strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
COREAUDIO_CloseDevice(this);
return 0; /* prepare_audiounit() will call SDL_SetError()... */
}
return 1; /* good to go. */
}
static int
COREAUDIO_Init(SDL_AudioDriverImpl * impl)
{
/* Set the function pointers */
impl->OpenDevice = COREAUDIO_OpenDevice;
impl->CloseDevice = COREAUDIO_CloseDevice;
impl->ProvidesOwnCallbackThread = 1;
/* added for iPhone */
impl->OnlyHasDefaultInputDevice = 1;
impl->OnlyHasDefaultOutputDevice = 1;
impl->HasCaptureSupport = 0; /* still needs to be written */
return 1; /* this audio target is available. */
}
AudioBootStrap COREAUDIOIPHONE_bootstrap = {
"coreaudio-iphoneos", "SDL CoreAudio (iPhone OS) audio driver",
COREAUDIO_Init, 0
};
/* vi: set ts=4 sw=4 expandtab: */
/*
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"
#ifndef _SDL_coreaudio_h
#define _SDL_coreaudio_h
#include "../SDL_sysaudio.h"
/* Hidden "this" pointer for the audio functions */
#define _THIS SDL_AudioDevice *this
struct SDL_PrivateAudioData
{
AudioUnit audioUnit;
int audioUnitOpened;
void *buffer;
UInt32 bufferOffset;
UInt32 bufferSize;
// AudioDeviceID deviceID;
};
#endif /* _SDL_coreaudio_h */
/* vi: set ts=4 sw=4 expandtab: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment