src/audio/coreaudio/SDL_coreaudio.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 05 Jul 2012 12:16:44 -0400
changeset 6352 a9bcd26e7105
parent 6281 e46d6f4b469e
child 6413 701a0c0d70d0
permissions -rwxr-xr-x
Removed unneeded audio buffer memset() for consistent behavior on all platforms.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 5643
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
    21
#include "SDL_config.h"
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#include "SDL_audio.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    23
#include "../SDL_audio_c.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    24
#include "../SDL_sysaudio.h"
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include "SDL_coreaudio.h"
6281
e46d6f4b469e Replaced some assert macros with SDL_assert.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
    26
#include "SDL_assert.h"
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    28
#define DEBUG_COREAUDIO 0
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    30
static void COREAUDIO_CloseDevice(_THIS);
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    31
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    32
#define CHECK_RESULT(msg) \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    33
    if (result != noErr) { \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    34
        COREAUDIO_CloseDevice(this); \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    35
        SDL_SetError("CoreAudio error (%s): %d", msg, (int) result); \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    36
        return 0; \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    37
    }
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    38
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    39
#if MACOSX_COREAUDIO
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    40
typedef void (*addDevFn)(const char *name, AudioDeviceID devId, void *data);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
    42
static void
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    43
addToDevList(const char *name, AudioDeviceID devId, void *data)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
{
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    45
    SDL_AddAudioDevice addfn = (SDL_AddAudioDevice) data;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    46
    addfn(name);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
}
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    49
typedef struct
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    50
{
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    51
    const char *findname;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    52
    AudioDeviceID devId;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    53
    int found;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    54
} FindDevIdData;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    55
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    56
static void
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    57
findDevId(const char *name, AudioDeviceID devId, void *_data)
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    58
{
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    59
    FindDevIdData *data = (FindDevIdData *) _data;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    60
    if (!data->found) {
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    61
        if (SDL_strcmp(name, data->findname) == 0) {
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    62
            data->found = 1;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    63
            data->devId = devId;
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    64
        }
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    65
    }
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    66
}
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    67
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    68
static void
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    69
build_device_list(int iscapture, addDevFn addfn, void *addfndata)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    71
    OSStatus result = noErr;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    72
    UInt32 size = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    73
    AudioDeviceID *devs = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    74
    UInt32 i = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    75
    UInt32 max = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    76
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    77
    AudioObjectPropertyAddress addr = {
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    78
        kAudioHardwarePropertyDevices,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    79
        kAudioObjectPropertyScopeGlobal,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    80
        kAudioObjectPropertyElementMaster
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    81
    };
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    82
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    83
    result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &addr,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    84
                                            0, NULL, &size);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    85
    if (result != kAudioHardwareNoError)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    86
        return;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    87
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    88
    devs = (AudioDeviceID *) alloca(size);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    89
    if (devs == NULL)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    90
        return;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    91
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    92
    result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
    93
                                        0, NULL, &size, devs);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    94
    if (result != kAudioHardwareNoError)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    95
        return;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    96
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    97
    max = size / sizeof (AudioDeviceID);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    98
    for (i = 0; i < max; i++) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    99
        CFStringRef cfstr = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   100
        char *ptr = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   101
        AudioDeviceID dev = devs[i];
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   102
        AudioBufferList *buflist = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   103
        int usable = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   104
        CFIndex len = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   105
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   106
        addr.mScope = iscapture ? kAudioDevicePropertyScopeInput :
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   107
                        kAudioDevicePropertyScopeOutput;
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   108
        addr.mSelector = kAudioDevicePropertyStreamConfiguration;
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   109
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   110
        result = AudioObjectGetPropertyDataSize(dev, &addr, 0, NULL, &size);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   111
        if (result != noErr)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   112
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   113
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   114
        buflist = (AudioBufferList *) SDL_malloc(size);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   115
        if (buflist == NULL)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   116
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   117
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   118
        result = AudioObjectGetPropertyData(dev, &addr, 0, NULL,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   119
                                            &size, buflist);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   121
        if (result == noErr) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   122
            UInt32 j;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   123
            for (j = 0; j < buflist->mNumberBuffers; j++) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   124
                if (buflist->mBuffers[j].mNumberChannels > 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   125
                    usable = 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   126
                    break;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   127
                }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   128
            }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   129
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   130
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   131
        SDL_free(buflist);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   132
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   133
        if (!usable)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   134
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   135
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   136
        addr.mSelector = kAudioObjectPropertyName;
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   137
        size = sizeof (CFStringRef);
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   138
        result = AudioObjectGetPropertyData(dev, &addr, 0, NULL, &size, &cfstr);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   139
        if (result != kAudioHardwareNoError)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   140
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   141
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   142
        len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr),
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   143
                                                kCFStringEncodingUTF8);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   144
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   145
        ptr = (char *) SDL_malloc(len + 1);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   146
        usable = ((ptr != NULL) &&
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   147
                  (CFStringGetCString
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   148
                   (cfstr, ptr, len + 1, kCFStringEncodingUTF8)));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   149
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   150
        CFRelease(cfstr);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   151
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   152
        if (usable) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   153
            len = strlen(ptr);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   154
            /* Some devices have whitespace at the end...trim it. */
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   155
            while ((len > 0) && (ptr[len - 1] == ' ')) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   156
                len--;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   157
            }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   158
            usable = (len > 0);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   159
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   160
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   161
        if (usable) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   162
            ptr[len] = '\0';
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   163
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   164
#if DEBUG_COREAUDIO
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   165
            printf("COREAUDIO: Found %s device #%d: '%s' (devid %d)\n",
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   166
                   ((iscapture) ? "capture" : "output"),
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   167
                   (int) *devCount, ptr, (int) dev);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   168
#endif
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   169
            addfn(ptr, dev, addfndata);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   170
        }
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   171
        SDL_free(ptr);  /* addfn() would have copied the string. */
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
    }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
}
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   175
static void
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   176
COREAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   177
{
5593
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   178
    build_device_list(iscapture, addToDevList, addfn);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   179
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   180
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   181
static int
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   182
find_device_by_name(_THIS, const char *devname, int iscapture)
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   183
{
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   184
    AudioDeviceID devid = 0;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   185
    OSStatus result = noErr;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   186
    UInt32 size = 0;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   187
    UInt32 alive = 0;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   188
    pid_t pid = 0;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   189
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   190
    AudioObjectPropertyAddress addr = {
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   191
        0,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   192
        kAudioObjectPropertyScopeGlobal,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   193
        kAudioObjectPropertyElementMaster
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   194
    };
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   195
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   196
    if (devname == NULL) {
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   197
        size = sizeof (AudioDeviceID);
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   198
        addr.mSelector =
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   199
            ((iscapture) ? kAudioHardwarePropertyDefaultInputDevice :
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   200
            kAudioHardwarePropertyDefaultOutputDevice);
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   201
        result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   202
                                            0, NULL, &size, &devid);
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   203
        CHECK_RESULT("AudioHardwareGetProperty (default device)");
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   204
    } else {
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   205
        FindDevIdData data;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   206
        SDL_zero(data);
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   207
        data.findname = devname;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   208
        build_device_list(iscapture, findDevId, &data);
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   209
        if (!data.found) {
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   210
            SDL_SetError("CoreAudio: No such audio device.");
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   211
            return 0;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   212
        }
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   213
        devid = data.devId;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   214
    }
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   215
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   216
    addr.mSelector = kAudioDevicePropertyDeviceIsAlive;
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   217
    addr.mScope = iscapture ? kAudioDevicePropertyScopeInput :
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   218
                    kAudioDevicePropertyScopeOutput;
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   219
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   220
    size = sizeof (alive);
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   221
    result = AudioObjectGetPropertyData(devid, &addr, 0, NULL, &size, &alive);
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   222
    CHECK_RESULT
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   223
        ("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)");
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   224
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   225
    if (!alive) {
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   226
        SDL_SetError("CoreAudio: requested device exists, but isn't alive.");
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   227
        return 0;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   228
    }
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   229
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   230
    addr.mSelector = kAudioDevicePropertyHogMode;
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   231
    size = sizeof (pid);
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   232
    result = AudioObjectGetPropertyData(devid, &addr, 0, NULL, &size, &pid);
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   233
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   234
    /* some devices don't support this property, so errors are fine here. */
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   235
    if ((result == noErr) && (pid != -1)) {
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   236
        SDL_SetError("CoreAudio: requested device is being hogged.");
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   237
        return 0;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   238
    }
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   239
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   240
    this->hidden->deviceID = devid;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   241
    return 1;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   242
}
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   243
#endif
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   244
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   245
/* The CoreAudio callback */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   246
static OSStatus
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   247
outputCallback(void *inRefCon,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   248
               AudioUnitRenderActionFlags * ioActionFlags,
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   249
               const AudioTimeStamp * inTimeStamp,
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   250
               UInt32 inBusNumber, UInt32 inNumberFrames,
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   251
               AudioBufferList * ioData)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   253
    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   254
    AudioBuffer *abuf;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   255
    UInt32 remaining, len;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   256
    void *ptr;
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   257
    UInt32 i;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   258
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   259
    /* Only do anything if audio is enabled and not paused */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   260
    if (!this->enabled || this->paused) {
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   261
        for (i = 0; i < ioData->mNumberBuffers; i++) {
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   262
            abuf = &ioData->mBuffers[i];
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   263
            SDL_memset(abuf->mData, this->spec.silence, abuf->mDataByteSize);
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   264
        }
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   265
        return 0;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   267
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
    /* No SDL conversion should be needed here, ever, since we accept
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
       any input format in OpenAudio, and leave the conversion to CoreAudio.
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   270
     */
1338
604d73db6802 Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents: 1336
diff changeset
   271
    /*
6281
e46d6f4b469e Replaced some assert macros with SDL_assert.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   272
       SDL_assert(!this->convert.needed);
e46d6f4b469e Replaced some assert macros with SDL_assert.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   273
       SDL_assert(this->spec.channels == ioData->mNumberChannels);
1338
604d73db6802 Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents: 1336
diff changeset
   274
     */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   275
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   276
    for (i = 0; i < ioData->mNumberBuffers; i++) {
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   277
        abuf = &ioData->mBuffers[i];
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   278
        remaining = abuf->mDataByteSize;
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   279
        ptr = abuf->mData;
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   280
        while (remaining > 0) {
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   281
            if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   282
                /* Generate the data */
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   283
                SDL_mutexP(this->mixer_lock);
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   284
                (*this->spec.callback)(this->spec.userdata,
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   285
                            this->hidden->buffer, this->hidden->bufferSize);
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   286
                SDL_mutexV(this->mixer_lock);
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   287
                this->hidden->bufferOffset = 0;
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   288
            }
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   289
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   290
            len = this->hidden->bufferSize - this->hidden->bufferOffset;
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   291
            if (len > remaining)
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   292
                len = remaining;
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   293
            SDL_memcpy(ptr, (char *)this->hidden->buffer +
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   294
                       this->hidden->bufferOffset, len);
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   295
            ptr = (char *)ptr + len;
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   296
            remaining -= len;
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   297
            this->hidden->bufferOffset += len;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
        }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   299
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   300
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
    return 0;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
}
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   303
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   304
static OSStatus
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   305
inputCallback(void *inRefCon,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   306
              AudioUnitRenderActionFlags * ioActionFlags,
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   307
              const AudioTimeStamp * inTimeStamp,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   308
              UInt32 inBusNumber, UInt32 inNumberFrames,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   309
              AudioBufferList * ioData)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   310
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   311
    //err = AudioUnitRender(afr->fAudioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, afr->fAudioBuffer);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   312
    // !!! FIXME: write me!
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   313
    return noErr;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   314
}
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   315
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   316
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   317
static void
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   318
COREAUDIO_CloseDevice(_THIS)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   320
    if (this->hidden != NULL) {
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   321
        if (this->hidden->audioUnitOpened) {
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   322
            OSStatus result = noErr;
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   323
            AURenderCallbackStruct callback;
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   324
            const AudioUnitElement output_bus = 0;
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   325
            const AudioUnitElement input_bus = 1;
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   326
            const int iscapture = this->iscapture;
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   327
            const AudioUnitElement bus =
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   328
                ((iscapture) ? input_bus : output_bus);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   329
            const AudioUnitScope scope =
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   330
                ((iscapture) ? kAudioUnitScope_Output :
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   331
                 kAudioUnitScope_Input);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   332
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   333
            /* stop processing the audio unit */
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   334
            result = AudioOutputUnitStop(this->hidden->audioUnit);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   335
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   336
            /* Remove the input callback */
6352
a9bcd26e7105 Removed unneeded audio buffer memset() for consistent behavior on all platforms.
Sam Lantinga <slouken@libsdl.org>
parents: 6281
diff changeset
   337
            SDL_memset(&callback, 0, sizeof(AURenderCallbackStruct));
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   338
            result = AudioUnitSetProperty(this->hidden->audioUnit,
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   339
                                          kAudioUnitProperty_SetRenderCallback,
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   340
                                          scope, bus, &callback,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   341
                                          sizeof(callback));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   342
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   343
            /* !!! FIXME: how does iOS free this? */
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   344
            #if MACOSX_COREAUDIO
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   345
            CloseComponent(this->hidden->audioUnit);
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   346
            #endif
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   347
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   348
            this->hidden->audioUnitOpened = 0;
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   349
        }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   350
        SDL_free(this->hidden->buffer);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   351
        SDL_free(this->hidden);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   352
        this->hidden = NULL;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   354
}
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   355
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   357
static int
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   358
prepare_audiounit(_THIS, const char *devname, int iscapture,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   359
                  const AudioStreamBasicDescription * strdesc)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   360
{
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   361
    OSStatus result = noErr;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   362
    AURenderCallbackStruct callback;
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   363
#if MACOSX_COREAUDIO
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   364
    ComponentDescription desc;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   365
    Component comp = NULL;
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   366
#else
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   367
    AudioComponentDescription desc;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   368
    AudioComponent comp = NULL;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   369
#endif
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   370
    const AudioUnitElement output_bus = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   371
    const AudioUnitElement input_bus = 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   372
    const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   373
    const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   374
                                  kAudioUnitScope_Input);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   375
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   376
#if MACOSX_COREAUDIO
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   377
    if (!find_device_by_name(this, devname, iscapture)) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   378
        SDL_SetError("Couldn't find requested CoreAudio device");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   379
        return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   380
    }
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   381
#endif
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   382
    
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   383
    SDL_zero(desc);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   384
    desc.componentType = kAudioUnitType_Output;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   385
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   386
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   387
#if MACOSX_COREAUDIO
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   388
    desc.componentSubType = kAudioUnitSubType_DefaultOutput;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   389
    comp = FindNextComponent(NULL, &desc);
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   390
#else
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   391
    desc.componentSubType = kAudioUnitSubType_RemoteIO;  /* !!! FIXME: ? */
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   392
    comp = AudioComponentFindNext(NULL, &desc);
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   393
#endif
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   394
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   395
    if (comp == NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   396
        SDL_SetError("Couldn't find requested CoreAudio component");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   397
        return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   398
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   399
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   400
    /* Open & initialize the audio unit */
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   401
#if MACOSX_COREAUDIO
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   402
    result = OpenAComponent(comp, &this->hidden->audioUnit);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   403
    CHECK_RESULT("OpenAComponent");
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   404
#else
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   405
    /*
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   406
       AudioComponentInstanceNew only available on iPhone OS 2.0 and Mac OS X 10.6
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   407
       We can't use OpenAComponent on iPhone because it is not present
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   408
     */
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   409
    result = AudioComponentInstanceNew(comp, &this->hidden->audioUnit);
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   410
    CHECK_RESULT("AudioComponentInstanceNew");
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   411
#endif
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   412
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   413
    this->hidden->audioUnitOpened = 1;
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   414
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   415
#if MACOSX_COREAUDIO
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   416
    result = AudioUnitSetProperty(this->hidden->audioUnit,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   417
                                  kAudioOutputUnitProperty_CurrentDevice,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   418
                                  kAudioUnitScope_Global, 0,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   419
                                  &this->hidden->deviceID,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   420
                                  sizeof(AudioDeviceID));
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   421
    CHECK_RESULT
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   422
        ("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   423
#endif
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   424
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   425
    /* Set the data format of the audio unit. */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   426
    result = AudioUnitSetProperty(this->hidden->audioUnit,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   427
                                  kAudioUnitProperty_StreamFormat,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   428
                                  scope, bus, strdesc, sizeof(*strdesc));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   429
    CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   430
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   431
    /* Set the audio callback */
6352
a9bcd26e7105 Removed unneeded audio buffer memset() for consistent behavior on all platforms.
Sam Lantinga <slouken@libsdl.org>
parents: 6281
diff changeset
   432
    SDL_memset(&callback, 0, sizeof(AURenderCallbackStruct));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   433
    callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   434
    callback.inputProcRefCon = this;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   435
    result = AudioUnitSetProperty(this->hidden->audioUnit,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   436
                                  kAudioUnitProperty_SetRenderCallback,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   437
                                  scope, bus, &callback, sizeof(callback));
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   438
    CHECK_RESULT
3617
023aa9699baa Merged r4713:4714 from branches/SDL-1.2: CoreAudio 64-bit & Snow Leopard fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3242
diff changeset
   439
        ("AudioUnitSetProperty (kAudioUnitProperty_SetRenderCallback)");
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   440
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   441
    /* Calculate the final parameters for this audio specification */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   442
    SDL_CalculateAudioSpec(&this->spec);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   443
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   444
    /* Allocate a sample buffer */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   445
    this->hidden->bufferOffset = this->hidden->bufferSize = this->spec.size;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   446
    this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   447
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   448
    result = AudioUnitInitialize(this->hidden->audioUnit);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   449
    CHECK_RESULT("AudioUnitInitialize");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   450
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   451
    /* Finally, start processing of the audio unit */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   452
    result = AudioOutputUnitStart(this->hidden->audioUnit);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   453
    CHECK_RESULT("AudioOutputUnitStart");
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   454
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   455
    /* We're running! */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   456
    return 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   457
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   458
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   459
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   460
static int
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   461
COREAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   462
{
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   463
    AudioStreamBasicDescription strdesc;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   464
    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   465
    int valid_datatype = 0;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   466
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   467
    /* Initialize all variables that we clean on shutdown */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   468
    this->hidden = (struct SDL_PrivateAudioData *)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   469
        SDL_malloc((sizeof *this->hidden));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   470
    if (this->hidden == NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   471
        SDL_OutOfMemory();
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   472
        return (0);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   473
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   474
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   475
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   476
    /* Setup a AudioStreamBasicDescription with the requested format */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   477
    SDL_memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   478
    strdesc.mFormatID = kAudioFormatLinearPCM;
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   479
    strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   480
    strdesc.mChannelsPerFrame = this->spec.channels;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   481
    strdesc.mSampleRate = this->spec.freq;
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   482
    strdesc.mFramesPerPacket = 1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   483
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   484
    while ((!valid_datatype) && (test_format)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   485
        this->spec.format = test_format;
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   486
        /* Just a list of valid SDL formats, so people don't pass junk here. */
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   487
        switch (test_format) {
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   488
        case AUDIO_U8:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   489
        case AUDIO_S8:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   490
        case AUDIO_U16LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   491
        case AUDIO_S16LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   492
        case AUDIO_U16MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   493
        case AUDIO_S16MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   494
        case AUDIO_S32LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   495
        case AUDIO_S32MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   496
        case AUDIO_F32LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   497
        case AUDIO_F32MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   498
            valid_datatype = 1;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   499
            strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   500
            if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   501
                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsBigEndian;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   502
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   503
            if (SDL_AUDIO_ISFLOAT(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   504
                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   505
            else if (SDL_AUDIO_ISSIGNED(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   506
                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   507
            break;
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   508
        }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   509
    }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   510
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   511
    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   512
        COREAUDIO_CloseDevice(this);
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   513
        SDL_SetError("Unsupported audio format");
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   514
        return 0;
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   515
    }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   516
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   517
    strdesc.mBytesPerFrame =
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   518
        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   519
    strdesc.mBytesPerPacket =
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   520
        strdesc.mBytesPerFrame * strdesc.mFramesPerPacket;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   521
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   522
    if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
2057
c2fed8bb6700 Oh for crying out loud...
Ryan C. Gordon <icculus@icculus.org>
parents: 2055
diff changeset
   523
        COREAUDIO_CloseDevice(this);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   524
        return 0;               /* prepare_audiounit() will call SDL_SetError()... */
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   525
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   526
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   527
    return 1;                   /* good to go. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   528
}
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   529
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   530
static int
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   531
COREAUDIO_Init(SDL_AudioDriverImpl * impl)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   532
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   533
    /* Set the function pointers */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   534
    impl->OpenDevice = COREAUDIO_OpenDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   535
    impl->CloseDevice = COREAUDIO_CloseDevice;
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   536
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   537
#if MACOSX_COREAUDIO
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   538
    impl->DetectDevices = COREAUDIO_DetectDevices;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   539
#else
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   540
    impl->OnlyHasDefaultOutputDevice = 1;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   541
#endif
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   542
    
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   543
    impl->ProvidesOwnCallbackThread = 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   544
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   545
    return 1;   /* this audio target is available. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   546
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   547
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   548
AudioBootStrap COREAUDIO_bootstrap = {
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   549
    "coreaudio", "CoreAudio", COREAUDIO_Init, 0
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   550
};
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   551
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
   552
/* vi: set ts=4 sw=4 expandtab: */