src/audio/coreaudio/SDL_coreaudio.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 09 Aug 2016 16:58:06 -0400
changeset 10255 77a266c9c786
parent 10240 0ba7106e9a6d
permissions -rw-r--r--
Added a FIXME.
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
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 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
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
    21
#include "../../SDL_internal.h"
9332
e29fec41a2c6 Added missing guards.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8657
diff changeset
    22
e29fec41a2c6 Added missing guards.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8657
diff changeset
    23
#if SDL_AUDIO_DRIVER_COREAUDIO
e29fec41a2c6 Added missing guards.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8657
diff changeset
    24
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
    25
/* !!! FIXME: clean out some of the macro salsa in here. */
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
    26
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#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
    28
#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
    29
#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
    30
#include "SDL_coreaudio.h"
6281
e46d6f4b469e Replaced some assert macros with SDL_assert.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
    31
#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
    32
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    33
#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
    34
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    35
#define CHECK_RESULT(msg) \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    36
    if (result != noErr) { \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    37
        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
    38
        return 0; \
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    39
    }
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    40
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
    41
#if MACOSX_COREAUDIO
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    42
static const AudioObjectPropertyAddress devlist_address = {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    43
    kAudioHardwarePropertyDevices,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    44
    kAudioObjectPropertyScopeGlobal,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    45
    kAudioObjectPropertyElementMaster
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    46
};
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    47
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    48
typedef void (*addDevFn)(const char *name, const int iscapture, AudioDeviceID devId, void *data);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    49
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    50
typedef struct AudioDeviceList
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    51
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    52
    AudioDeviceID devid;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    53
    SDL_bool alive;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    54
    struct AudioDeviceList *next;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    55
} AudioDeviceList;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    56
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    57
static AudioDeviceList *output_devs = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    58
static AudioDeviceList *capture_devs = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    59
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    60
static SDL_bool
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    61
add_to_internal_dev_list(const int iscapture, AudioDeviceID devId)
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    62
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    63
    AudioDeviceList *item = (AudioDeviceList *) SDL_malloc(sizeof (AudioDeviceList));
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    64
    if (item == NULL) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    65
        return SDL_FALSE;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    66
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    67
    item->devid = devId;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    68
    item->alive = SDL_TRUE;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    69
    item->next = iscapture ? capture_devs : output_devs;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    70
    if (iscapture) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    71
        capture_devs = item;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    72
    } else {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    73
        output_devs = item;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    74
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    75
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    76
    return SDL_TRUE;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    77
}
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
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
    79
static void
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    80
addToDevList(const char *name, const int iscapture, 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
    81
{
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    82
    if (add_to_internal_dev_list(iscapture, devId)) {
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
    83
        SDL_AddAudioDevice(iscapture, name, (void *) ((size_t) devId));
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
    84
    }
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    85
}
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    86
ab22ca13c47f Cleaned up audio device detection. Cleared out a lot of cut-and-paste.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    87
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
    88
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
    89
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    90
    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
    91
    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
    92
    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
    93
    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
    94
    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
    95
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    96
    result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
    97
                                            &devlist_address, 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
    98
    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
    99
        return;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   100
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   101
    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
   102
    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
   103
        return;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   104
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   105
    result = AudioObjectGetPropertyData(kAudioObjectSystemObject,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   106
                                        &devlist_address, 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
   107
    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
   108
        return;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   109
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
   110
    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
   111
    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
   112
        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
   113
        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
   114
        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
   115
        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
   116
        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
   117
        CFIndex len = 0;
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   118
        const AudioObjectPropertyAddress addr = {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   119
            kAudioDevicePropertyStreamConfiguration,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   120
            iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   121
            kAudioObjectPropertyElementMaster
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   122
        };
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   123
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   124
        const AudioObjectPropertyAddress nameaddr = {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   125
            kAudioObjectPropertyName,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   126
            iscapture ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   127
            kAudioObjectPropertyElementMaster
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   128
        };
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   129
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   130
        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
   131
        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
   132
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   133
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   134
        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
   135
        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
   136
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   137
5643
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,
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   139
                                            &size, buflist);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   141
        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
   142
            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
   143
            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
   144
                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
   145
                    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
   146
                    break;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   147
                }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   148
            }
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
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   151
        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
   152
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   153
        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
   154
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   155
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   156
5643
4c286789c40f Cleaned out functions deprecated in Mac OS X 10.6 SDK.
Ryan C. Gordon <icculus@icculus.org>
parents: 5605
diff changeset
   157
        size = sizeof (CFStringRef);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   158
        result = AudioObjectGetPropertyData(dev, &nameaddr, 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
   159
        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
   160
            continue;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   161
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   162
        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
   163
                                                kCFStringEncodingUTF8);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   164
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   165
        ptr = (char *) SDL_malloc(len + 1);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   166
        usable = ((ptr != NULL) &&
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   167
                  (CFStringGetCString
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   168
                   (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
   169
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   170
        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
   171
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   172
        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
   173
            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
   174
            /* Some devices have whitespace at the end...trim it. */
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   175
            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
   176
                len--;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   177
            }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   178
            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
   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
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
   181
        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
   182
            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
   183
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   184
#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
   185
            printf("COREAUDIO: Found %s device #%d: '%s' (devid %d)\n",
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   186
                   ((iscapture) ? "capture" : "output"),
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   187
                   (int) i, ptr, (int) dev);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   188
#endif
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   189
            addfn(ptr, iscapture, 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
   190
        }
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
   191
        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
   192
    }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
}
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
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
   195
static void
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   196
free_audio_device_list(AudioDeviceList **list)
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   197
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   198
    AudioDeviceList *item = *list;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   199
    while (item) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   200
        AudioDeviceList *next = item->next;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   201
        SDL_free(item);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   202
        item = next;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   203
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   204
    *list = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   205
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   206
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   207
static void
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   208
COREAUDIO_DetectDevices(void)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   209
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   210
    build_device_list(SDL_TRUE, addToDevList, NULL);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   211
    build_device_list(SDL_FALSE, addToDevList, NULL);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   212
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   213
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   214
static void
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   215
build_device_change_list(const char *name, const int iscapture, AudioDeviceID devId, void *data)
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   216
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   217
    AudioDeviceList **list = (AudioDeviceList **) data;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   218
    AudioDeviceList *item;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   219
    for (item = *list; item != NULL; item = item->next) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   220
        if (item->devid == devId) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   221
            item->alive = SDL_TRUE;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   222
            return;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   223
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   224
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   225
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   226
    add_to_internal_dev_list(iscapture, devId);  /* new device, add it. */
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   227
    SDL_AddAudioDevice(iscapture, name, (void *) ((size_t) devId));
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   228
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   229
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   230
static void
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   231
reprocess_device_list(const int iscapture, AudioDeviceList **list)
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   232
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   233
    AudioDeviceList *item;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   234
    AudioDeviceList *prev = NULL;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   235
    for (item = *list; item != NULL; item = item->next) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   236
        item->alive = SDL_FALSE;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   237
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   238
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   239
    build_device_list(iscapture, build_device_change_list, list);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   240
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   241
    /* free items in the list that aren't still alive. */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   242
    item = *list;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   243
    while (item != NULL) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   244
        AudioDeviceList *next = item->next;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   245
        if (item->alive) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   246
            prev = item;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   247
        } else {
9399
a684dbd185c7 SDL_RemoveAudioDevice() should specify capture vs output.
Ryan C. Gordon <icculus@icculus.org>
parents: 9394
diff changeset
   248
            SDL_RemoveAudioDevice(iscapture, (void *) ((size_t) item->devid));
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   249
            if (prev) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   250
                prev->next = item->next;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   251
            } else {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   252
                *list = item->next;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   253
            }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   254
            SDL_free(item);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   255
        }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   256
        item = next;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   257
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   258
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   259
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   260
/* this is called when the system's list of available audio devices changes. */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   261
static OSStatus
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   262
device_list_changed(AudioObjectID systemObj, UInt32 num_addr, const AudioObjectPropertyAddress *addrs, void *data)
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   263
{
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   264
    reprocess_device_list(SDL_TRUE, &capture_devs);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   265
    reprocess_device_list(SDL_FALSE, &output_devs);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   266
    return 0;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   267
}
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   268
#endif
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   269
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   270
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   271
static int open_playback_devices = 0;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   272
static int open_capture_devices = 0;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   273
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   274
static void update_audio_session()
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   275
{
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   276
#if !MACOSX_COREAUDIO
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   277
    /* !!! FIXME: move this to AVAudioSession. This is deprecated, and the new version is available as of (ancient!) iOS 3.0 */
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   278
    UInt32 category;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   279
    if (open_playback_devices && open_capture_devices) {
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   280
        category = kAudioSessionCategory_PlayAndRecord;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   281
    } else if (open_capture_devices) {
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   282
        category = kAudioSessionCategory_RecordAudio;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   283
    } else {  /* nothing open, or just playing audio. */
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   284
        category = kAudioSessionCategory_AmbientSound;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   285
    }
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   286
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof (UInt32), &category);
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   287
#endif
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   288
}
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   289
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   290
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   291
/* 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
   292
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
   293
outputCallback(void *inRefCon,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   294
               AudioUnitRenderActionFlags * ioActionFlags,
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   295
               const AudioTimeStamp * inTimeStamp,
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   296
               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
   297
               AudioBufferList * ioData)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
{
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
   299
    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
   300
    AudioBuffer *abuf;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   301
    UInt32 remaining, len;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   302
    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
   303
    UInt32 i;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   305
    /* Only do anything if audio is enabled and not paused */
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10214
diff changeset
   306
    if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&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
   307
        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
   308
            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
   309
            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
   310
        }
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   311
        return 0;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   312
    }
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
   313
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   314
    /* 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
   315
       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
   316
     */
1338
604d73db6802 Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents: 1336
diff changeset
   317
    /*
6281
e46d6f4b469e Replaced some assert macros with SDL_assert.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   318
       SDL_assert(!this->convert.needed);
e46d6f4b469e Replaced some assert macros with SDL_assert.
Ryan C. Gordon <icculus@icculus.org>
parents: 6138
diff changeset
   319
       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
   320
     */
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
   321
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
   322
    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
   323
        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
   324
        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
   325
        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
   326
        while (remaining > 0) {
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   327
            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
   328
                /* Generate the data */
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   329
                SDL_LockMutex(this->mixer_lock);
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
   330
                (*this->spec.callback)(this->spec.userdata,
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   331
                            this->hidden->buffer, this->hidden->bufferSize);
6977
b73d51026c68 Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   332
                SDL_UnlockMutex(this->mixer_lock);
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   333
                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
   334
            }
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
   335
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   336
            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
   337
            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
   338
                len = remaining;
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   339
            SDL_memcpy(ptr, (char *)this->hidden->buffer +
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   340
                       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
   341
            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
   342
            remaining -= len;
3636
e474a820daeb Patched to compile.
Ryan C. Gordon <icculus@icculus.org>
parents: 3617
diff changeset
   343
            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
   344
        }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   345
    }
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
   346
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   347
    return noErr;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   348
}
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
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
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
   351
inputCallback(void *inRefCon,
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   352
              AudioUnitRenderActionFlags *ioActionFlags,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   353
              const AudioTimeStamp *inTimeStamp,
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
              UInt32 inBusNumber, UInt32 inNumberFrames,
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   355
              AudioBufferList *ioData)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
{
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   357
    SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10214
diff changeset
   358
    if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   359
        return noErr;  /* just drop this if we're not accepting input. */
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   360
    }
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   361
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   362
    const OSStatus err = AudioUnitRender(this->hidden->audioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, &this->hidden->captureBufferList);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   363
    SDL_assert(this->hidden->captureBufferList.mNumberBuffers == 1);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   364
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   365
    if (err == noErr) {
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   366
        const AudioBuffer *abuf = &this->hidden->captureBufferList.mBuffers[0];
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   367
        UInt32 remaining = abuf->mDataByteSize;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   368
        const Uint8 *ptr = (const Uint8 *) abuf->mData;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   369
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   370
        /* No SDL conversion should be needed here, ever, since we accept
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   371
           any input format in OpenAudio, and leave the conversion to CoreAudio.
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   372
         */
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   373
        while (remaining > 0) {
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   374
            UInt32 len = this->hidden->bufferSize - this->hidden->bufferOffset;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   375
            if (len > remaining)
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   376
                len = remaining;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   377
10255
77a266c9c786 Added a FIXME.
Ryan C. Gordon <icculus@icculus.org>
parents: 10240
diff changeset
   378
            /* !!! FIXME: why are we copying here? just pass the buffer to the callback? */
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   379
            SDL_memcpy((char *)this->hidden->buffer + this->hidden->bufferOffset, ptr, len);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   380
            ptr += len;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   381
            remaining -= len;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   382
            this->hidden->bufferOffset += len;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   383
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   384
            if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   385
                SDL_LockMutex(this->mixer_lock);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   386
                (*this->spec.callback)(this->spec.userdata,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   387
                            this->hidden->buffer, this->hidden->bufferSize);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   388
                SDL_UnlockMutex(this->mixer_lock);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   389
                this->hidden->bufferOffset = 0;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   390
            }
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   391
        }
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   392
    }
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   393
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   394
    return noErr;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   395
}
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   396
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   397
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   398
#if MACOSX_COREAUDIO
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   399
static const AudioObjectPropertyAddress alive_address =
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   400
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   401
    kAudioDevicePropertyDeviceIsAlive,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   402
    kAudioObjectPropertyScopeGlobal,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   403
    kAudioObjectPropertyElementMaster
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   404
};
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   405
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   406
static OSStatus
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   407
device_unplugged(AudioObjectID devid, UInt32 num_addr, const AudioObjectPropertyAddress *addrs, void *data)
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   408
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   409
    SDL_AudioDevice *this = (SDL_AudioDevice *) data;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   410
    SDL_bool dead = SDL_FALSE;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   411
    UInt32 isAlive = 1;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   412
    UInt32 size = sizeof (isAlive);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   413
    OSStatus error;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   414
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10214
diff changeset
   415
    if (!SDL_AtomicGet(&this->enabled)) {
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   416
        return 0;  /* already known to be dead. */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   417
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   418
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   419
    error = AudioObjectGetPropertyData(this->hidden->deviceID, &alive_address,
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   420
                                       0, NULL, &size, &isAlive);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   421
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   422
    if (error == kAudioHardwareBadDeviceError) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   423
        dead = SDL_TRUE;  /* device was unplugged. */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   424
    } else if ((error == kAudioHardwareNoError) && (!isAlive)) {
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   425
        dead = SDL_TRUE;  /* device died in some other way. */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   426
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   427
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   428
    if (dead) {
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   429
        SDL_OpenedAudioDeviceDisconnected(this);
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   430
    }
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   431
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   432
    return 0;
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   433
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   434
#endif
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   435
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   436
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
   437
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
   438
{
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   439
    const int iscapture = this->iscapture;
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   440
    if (this->hidden->audioUnitOpened) {
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   441
        #if MACOSX_COREAUDIO
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   442
        /* Unregister our disconnect callback. */
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   443
        AudioObjectRemovePropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   444
        #endif
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   445
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   446
        AURenderCallbackStruct callback;
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   447
        const AudioUnitElement output_bus = 0;
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   448
        const AudioUnitElement input_bus = 1;
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   449
        const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   450
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   451
        /* stop processing the audio unit */
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   452
        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
   453
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   454
        /* Remove the input callback */
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   455
        SDL_zero(callback);
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   456
        AudioUnitSetProperty(this->hidden->audioUnit,
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   457
                             iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   458
                             kAudioUnitScope_Global, bus, &callback, sizeof(callback));
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   459
        AudioComponentInstanceDispose(this->hidden->audioUnit);
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   460
    }
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   461
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   462
    SDL_free(this->hidden->captureBufferList.mBuffers[0].mData);
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   463
    SDL_free(this->hidden->buffer);
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   464
    SDL_free(this->hidden);
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   465
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   466
    if (iscapture) {
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   467
        open_capture_devices--;
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   468
    } else {
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   469
        open_playback_devices--;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   470
    }
10237
9530fc07da6c audio: Clean up some CloseDevice() interface details.
Ryan C. Gordon <icculus@icculus.org>
parents: 10227
diff changeset
   471
    update_audio_session();
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   472
}
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   473
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   474
#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
   475
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   476
prepare_device(_THIS, void *handle, int iscapture)
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   477
{
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   478
    AudioDeviceID devid = (AudioDeviceID) ((size_t) handle);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   479
    OSStatus result = noErr;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   480
    UInt32 size = 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   481
    UInt32 alive = 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   482
    pid_t pid = 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   483
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   484
    AudioObjectPropertyAddress addr = {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   485
        0,
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   486
        kAudioObjectPropertyScopeGlobal,
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   487
        kAudioObjectPropertyElementMaster
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   488
    };
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   489
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   490
    if (handle == NULL) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   491
        size = sizeof (AudioDeviceID);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   492
        addr.mSelector =
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   493
            ((iscapture) ? kAudioHardwarePropertyDefaultInputDevice :
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   494
            kAudioHardwarePropertyDefaultOutputDevice);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   495
        result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &addr,
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   496
                                            0, NULL, &size, &devid);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   497
        CHECK_RESULT("AudioHardwareGetProperty (default device)");
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   498
    }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   499
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   500
    addr.mSelector = kAudioDevicePropertyDeviceIsAlive;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   501
    addr.mScope = iscapture ? kAudioDevicePropertyScopeInput :
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   502
                    kAudioDevicePropertyScopeOutput;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   503
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   504
    size = sizeof (alive);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   505
    result = AudioObjectGetPropertyData(devid, &addr, 0, NULL, &size, &alive);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   506
    CHECK_RESULT
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   507
        ("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)");
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   508
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   509
    if (!alive) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   510
        SDL_SetError("CoreAudio: requested device exists, but isn't alive.");
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   511
        return 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   512
    }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   513
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   514
    addr.mSelector = kAudioDevicePropertyHogMode;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   515
    size = sizeof (pid);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   516
    result = AudioObjectGetPropertyData(devid, &addr, 0, NULL, &size, &pid);
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   517
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   518
    /* some devices don't support this property, so errors are fine here. */
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   519
    if ((result == noErr) && (pid != -1)) {
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   520
        SDL_SetError("CoreAudio: requested device is being hogged.");
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   521
        return 0;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   522
    }
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   523
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   524
    this->hidden->deviceID = devid;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   525
    return 1;
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   526
}
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   527
#endif
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   528
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   529
static int
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   530
prepare_audiounit(_THIS, void *handle, int iscapture,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   531
                  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
   532
{
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   533
    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
   534
    AURenderCallbackStruct callback;
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   535
    AudioComponentDescription desc;
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   536
    AudioComponent comp = NULL;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   537
    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
   538
    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
   539
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   540
#if MACOSX_COREAUDIO
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   541
    if (!prepare_device(this, handle, iscapture)) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   542
        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
   543
    }
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   544
#endif
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   545
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   546
    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
   547
    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
   548
    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
   549
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   550
#if MACOSX_COREAUDIO
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   551
    desc.componentSubType = iscapture ? kAudioUnitSubType_HALOutput : kAudioUnitSubType_DefaultOutput;
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   552
#else
6413
701a0c0d70d0 Removed a FIXME; RemoteIO is correct for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 6352
diff changeset
   553
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
10176
3115d38204aa Removed Mac OS 10.5 support (bug #3137). Also fixed a warning about deprecated Carbon code when using SDL_audio (bug #3127, thanks Dominik!)
Alex Szpakowski <slime73@gmail.com>
parents: 9998
diff changeset
   554
#endif
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   555
    comp = AudioComponentFindNext(NULL, &desc);
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   556
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   557
    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
   558
        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
   559
        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
   560
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   561
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   562
    /* 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
   563
    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
   564
    CHECK_RESULT("AudioComponentInstanceNew");
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   565
2055
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   566
    this->hidden->audioUnitOpened = 1;
72cc2226d6a3 Fix potential memory leaks if CoreAudio initialization fails.
Ryan C. Gordon <icculus@icculus.org>
parents: 2049
diff changeset
   567
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   568
    if (iscapture) {  /* have to do EnableIO only for capture devices. */
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   569
        UInt32 enable = 1;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   570
        result = AudioUnitSetProperty(this->hidden->audioUnit,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   571
                                      kAudioOutputUnitProperty_EnableIO,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   572
                                      kAudioUnitScope_Input, input_bus,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   573
                                      &enable, sizeof (enable));
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   574
        CHECK_RESULT
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   575
            ("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO input bus)");
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   576
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   577
        enable = 0;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   578
        result = AudioUnitSetProperty(this->hidden->audioUnit,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   579
                                      kAudioOutputUnitProperty_EnableIO,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   580
                                      kAudioUnitScope_Output, output_bus,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   581
                                      &enable, sizeof (enable));
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   582
        CHECK_RESULT
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   583
            ("AudioUnitSetProperty (kAudioOutputUnitProperty_EnableIO output bus)");
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   584
    }
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   585
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   586
#if MACOSX_COREAUDIO
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   587
    /* this is always on the output_bus, even for capture devices. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   588
    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
   589
                                  kAudioOutputUnitProperty_CurrentDevice,
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   590
                                  kAudioUnitScope_Global, output_bus,
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   591
                                  &this->hidden->deviceID,
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   592
                                  sizeof(AudioDeviceID));
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   593
    CHECK_RESULT
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   594
        ("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   595
#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
   596
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   597
    /* 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
   598
    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
   599
                                  kAudioUnitProperty_StreamFormat,
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   600
                                  iscapture ? kAudioUnitScope_Output : kAudioUnitScope_Input,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   601
                                  iscapture ? input_bus : output_bus,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   602
                                  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
   603
    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
   604
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   605
    if (iscapture) {  /* only need to do this for capture devices. */
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   606
        void *ptr;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   607
        UInt32 framesize = 0;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   608
        UInt32 propsize = sizeof (UInt32);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   609
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   610
        result = AudioUnitGetProperty(this->hidden->audioUnit,
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   611
                                      kAudioUnitProperty_MaximumFramesPerSlice,
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   612
                                      kAudioUnitScope_Global, output_bus,
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   613
                                      &framesize, &propsize);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   614
        CHECK_RESULT
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   615
            ("AudioUnitGetProperty (kAudioDevicePropertyBufferFrameSize)");
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   616
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   617
        framesize *= SDL_AUDIO_BITSIZE(this->spec.format) / 8;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   618
        ptr = SDL_calloc(1, framesize);
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   619
        if (ptr == NULL) {
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   620
            SDL_OutOfMemory();
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   621
            return 0;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   622
        }
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   623
        this->hidden->captureBufferList.mNumberBuffers = 1;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   624
        this->hidden->captureBufferList.mBuffers[0].mNumberChannels = this->spec.channels;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   625
        this->hidden->captureBufferList.mBuffers[0].mDataByteSize = framesize;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   626
        this->hidden->captureBufferList.mBuffers[0].mData = ptr;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   627
    }
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   628
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   629
    /* Set the audio callback */
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   630
    SDL_zero(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
   631
    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
   632
    callback.inputProcRefCon = this;
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   633
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   634
    result = AudioUnitSetProperty(this->hidden->audioUnit,
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   635
                                  iscapture ? kAudioOutputUnitProperty_SetInputCallback : kAudioUnitProperty_SetRenderCallback,
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   636
                                  kAudioUnitScope_Global,
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   637
                                  iscapture ? input_bus : output_bus,
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   638
                                  &callback, sizeof (callback));
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   639
    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
   640
        ("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
   641
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   642
    /* 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
   643
    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
   644
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   645
    /* Allocate a sample buffer */
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   646
    this->hidden->bufferSize = this->spec.size;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   647
    this->hidden->bufferOffset = iscapture ? 0 : this->hidden->bufferSize;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   648
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   649
    this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   650
    if (this->hidden->buffer == NULL) {
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   651
        SDL_OutOfMemory();
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   652
        return 0;
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   653
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   654
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   655
    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
   656
    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
   657
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   658
    /* 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
   659
    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
   660
    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
   661
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   662
/* !!! FIXME: what does iOS do when a bluetooth audio device vanishes? Headphones unplugged? */
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   663
/* !!! FIXME: (we only do a "default" device on iOS right now...can we do more?) */
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   664
#if MACOSX_COREAUDIO
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   665
    /* Fire a callback if the device stops being "alive" (disconnected, etc). */
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   666
    AudioObjectAddPropertyListener(this->hidden->deviceID, &alive_address, device_unplugged, this);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   667
#endif
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   668
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   669
    /* 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
   670
    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
   671
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   672
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   673
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   674
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   675
COREAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   676
{
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   677
    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
   678
    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
   679
    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
   680
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   681
    /* 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
   682
    this->hidden = (struct SDL_PrivateAudioData *)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   683
        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
   684
    if (this->hidden == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6977
diff changeset
   685
        return SDL_OutOfMemory();
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   686
    }
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   687
    SDL_zerop(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
   688
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   689
    if (iscapture) {
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   690
        open_capture_devices++;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   691
    } else {
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   692
        open_playback_devices++;
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   693
    }
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   694
    update_audio_session();
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   695
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   696
    /* Setup a AudioStreamBasicDescription with the requested format */
10214
4f29bb9e19c3 audio: Implemented capture support for Mac OS X CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 10176
diff changeset
   697
    SDL_zero(strdesc);
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   698
    strdesc.mFormatID = kAudioFormatLinearPCM;
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   699
    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
   700
    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
   701
    strdesc.mSampleRate = this->spec.freq;
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   702
    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
   703
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   704
    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
   705
        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
   706
        /* 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
   707
        switch (test_format) {
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   708
        case AUDIO_U8:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   709
        case AUDIO_S8:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   710
        case AUDIO_U16LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   711
        case AUDIO_S16LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   712
        case AUDIO_U16MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   713
        case AUDIO_S16MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   714
        case AUDIO_S32LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   715
        case AUDIO_S32MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   716
        case AUDIO_F32LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   717
        case AUDIO_F32MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   718
            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
   719
            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
   720
            if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   721
                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
   722
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   723
            if (SDL_AUDIO_ISFLOAT(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   724
                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
   725
            else if (SDL_AUDIO_ISSIGNED(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   726
                strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   727
            break;
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   728
        }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   729
    }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   730
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
   731
    if (!valid_datatype) {      /* shouldn't happen, but just in case... */
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6977
diff changeset
   732
        return SDL_SetError("Unsupported audio format");
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   733
    }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
   734
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   735
    strdesc.mBytesPerFrame =
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   736
        strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   737
    strdesc.mBytesPerPacket =
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
   738
        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
   739
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   740
    if (!prepare_audiounit(this, handle, iscapture, &strdesc)) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6977
diff changeset
   741
        return -1;      /* 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
   742
    }
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
   743
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6977
diff changeset
   744
    return 0;   /* 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
   745
}
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   746
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   747
static void
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   748
COREAUDIO_Deinitialize(void)
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   749
{
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   750
#if MACOSX_COREAUDIO
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   751
    AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &devlist_address, device_list_changed, NULL);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   752
    free_audio_device_list(&capture_devs);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   753
    free_audio_device_list(&output_devs);
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   754
#endif
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   755
}
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   756
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   757
static int
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2057
diff changeset
   758
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
   759
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   760
    /* 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
   761
    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
   762
    impl->CloseDevice = COREAUDIO_CloseDevice;
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   763
    impl->Deinitialize = COREAUDIO_Deinitialize;
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   764
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   765
#if MACOSX_COREAUDIO
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   766
    impl->DetectDevices = COREAUDIO_DetectDevices;
9393
ed79a66e57e5 Initial work on audio device hotplug support.
Ryan C. Gordon <icculus@icculus.org>
parents: 9332
diff changeset
   767
    AudioObjectAddPropertyListener(kAudioObjectSystemObject, &devlist_address, device_list_changed, NULL);
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   768
#else
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   769
    impl->OnlyHasDefaultOutputDevice = 1;
10240
0ba7106e9a6d audio: Changed OnlyHasDefaultInputDevice to OnlyHasDefaultCaptureDevice.
Ryan C. Gordon <icculus@icculus.org>
parents: 10237
diff changeset
   770
    impl->OnlyHasDefaultCaptureDevice = 1;
6628
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   771
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   772
    /* Set category to ambient sound so that other music continues playing.
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   773
       You can change this at runtime in your own code if you need different
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   774
       behavior.  If this is common, we can add an SDL hint for this.
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   775
    */
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   776
    /* !!! FIXME: move this to AVAudioSession. This is deprecated, and the new version is available as of (ancient!) iOS 3.0 */
6628
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   777
    AudioSessionInitialize(NULL, NULL, NULL, nil);
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   778
    UInt32 category = kAudioSessionCategory_AmbientSound;
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   779
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(UInt32), &category);
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   780
#endif
6628
7994e6979876 Allow playing iPod music in the background of SDL applications.
Sam Lantinga <slouken@libsdl.org>
parents: 6413
diff changeset
   781
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   782
    impl->ProvidesOwnCallbackThread = 1;
10227
f912cd5baa5d coreaudio: Implemented audio capture for iOS.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   783
    impl->HasCaptureSupport = 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
   784
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
   785
    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
   786
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   787
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   788
AudioBootStrap COREAUDIO_bootstrap = {
5596
d4b2f6543074 Merged Mac OS X and iOS audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5595
diff changeset
   789
    "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
   790
};
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   791
9332
e29fec41a2c6 Added missing guards.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8657
diff changeset
   792
#endif /* SDL_AUDIO_DRIVER_COREAUDIO */
e29fec41a2c6 Added missing guards.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8657
diff changeset
   793
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
   794
/* vi: set ts=4 sw=4 expandtab: */