annotate src/audio/macosx/SDL_coreaudio.c @ 3787:8d74a4721ca9 SDL-ryan-multiple-audio-device

Added deinitialize hook.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 03 Oct 2006 16:26:42 +0000
parents b70f4e9291bb
children 7006b176ef4f
rev   line source
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 936
diff changeset
3 Copyright (C) 1997-2006 Sam Lantinga
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 936
diff changeset
6 modify it under the terms of the GNU Lesser General Public
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 936
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 936
diff changeset
13 Lesser General Public License for more details.
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 936
diff changeset
15 You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 936
diff changeset
16 License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 936
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 */
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
22 #include "SDL_config.h"
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
24 #include <CoreAudio/CoreAudio.h>
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25 #include <AudioUnit/AudioUnit.h>
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26
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"
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
33 typedef struct COREAUDIO_DeviceList
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
34 {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
35 AudioDeviceID id;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
36 const char *name;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
37 } COREAUDIO_DeviceList;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
38
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
39 static COREAUDIO_DeviceList *inputDevices = NULL;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
40 static int inputDeviceCount = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
41 static COREAUDIO_DeviceList *outputDevices = NULL;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
42 static int outputDeviceCount = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
43
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
44 static void
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
45 free_device_list(COREAUDIO_DeviceList **devices, int *devCount)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
46 {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
47 if (*devices) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
48 int i = *devCount;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
49 while (i--)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
50 SDL_free((void *) (*devices)[i].name);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
51 SDL_free(*devices);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
52 *devices = NULL;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
53 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
54 *devCount = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
55 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
56
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
57
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
58 static void
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
59 build_device_list(int iscapture, COREAUDIO_DeviceList **devices, int *devCount)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
60 {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
61 Boolean outWritable = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
62 OSStatus result = noErr;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
63 UInt32 size = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
64 AudioDeviceID *devs = NULL;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
65 UInt32 i = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
66 UInt32 max = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
67
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
68 free_device_list(devices, devCount);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
69
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
70 result = AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
71 &size, &outWritable);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
72
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
73 if (result != kAudioHardwareNoError)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
74 return;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
75
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
76 devs = (AudioDeviceID *) alloca(size);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
77 if (devs == NULL)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
78 return;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
79
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
80 max = size / sizeof (AudioDeviceID);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
81 *devices = (COREAUDIO_DeviceList *) SDL_malloc(max * sizeof (**devices));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
82 if (*devices == NULL)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
83 return;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
84
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
85 result = AudioHardwareGetProperty(kAudioHardwarePropertyDevices,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
86 &size, devs);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
87 if (result != kAudioHardwareNoError)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
88 return;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
89
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
90 for (i = 0; i < max; i++) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
91 char *ptr = NULL;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
92 AudioDeviceID dev = devs[i];
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
93 AudioBufferList *buflist = NULL;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
94 int usable = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
95
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
96 result = AudioDeviceGetPropertyInfo(dev, 0, iscapture,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
97 kAudioDevicePropertyStreamConfiguration,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
98 &size, &outWritable);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
99 if (result != noErr)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
100 continue;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
101
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
102 buflist = (AudioBufferList *) SDL_malloc(size);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
103 if (buflist == NULL)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
104 continue;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
105
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
106 result = AudioDeviceGetProperty(dev, 0, iscapture,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
107 kAudioDevicePropertyStreamConfiguration,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
108 &size, buflist);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
109
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
110 if (result == noErr) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
111 UInt32 j;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
112 for (j = 0; j < buflist->mNumberBuffers; j++) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
113 if (buflist->mBuffers[j].mNumberChannels > 0) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
114 usable = 1;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
115 break;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
116 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
117 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
118 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
119
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
120 SDL_free(buflist);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
121
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
122 if (!usable)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
123 continue;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
124
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
125 /* !!! FIXME: use CFStrings, instead, and convert to UTF-8. */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
126 result = AudioDeviceGetPropertyInfo(dev, 0, iscapture,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
127 kAudioDevicePropertyDeviceName,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
128 &size, &outWritable);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
129
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
130 if (result != kAudioHardwareNoError)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
131 continue;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
132
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
133 ptr = (char *) SDL_malloc(size + 1);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
134 if (ptr == NULL)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
135 continue;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
136
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
137 result = AudioDeviceGetProperty(dev, 0, iscapture,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
138 kAudioDevicePropertyDeviceName,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
139 &size, ptr);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
140
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
141 if (result != kAudioHardwareNoError)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
142 continue;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
143
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
144 while ((size > 0) && (ptr[size-1] == ' '))
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
145 size--; /* I have a USB device with whitespace at the end... */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
146
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
147 if (size == 0) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
148 SDL_free(ptr);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
149 } else {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
150 ptr[size] = '\0';
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
151 (*devices)[*devCount].id = dev;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
152 (*devices)[*devCount].name = ptr;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
153 (*devCount)++;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
154 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
155 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
156 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
157
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
158 static int
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
159 find_device_id(const char *devname, int iscapture, AudioDeviceID *id)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
160 {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
161 int i = ((iscapture) ? inputDeviceCount : outputDeviceCount);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
162 COREAUDIO_DeviceList *devs = ((iscapture) ? inputDevices : outputDevices);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
163 while (i--) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
164 if (SDL_strcmp(devname, devs->name) == 0) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
165 *id = devs->id;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
166 return 1;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
167 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
168 devs++;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
169 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
170
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
171 return 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
172 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
173
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
174
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
175 /* Audio driver functions */
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
176
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
177 static int COREAUDIO_OpenAudio(_THIS, const char *devname, int iscapture);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
178 static void COREAUDIO_WaitAudio(_THIS);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
179 static void COREAUDIO_PlayAudio(_THIS);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
180 static Uint8 *COREAUDIO_GetAudioBuf(_THIS);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
181 static void COREAUDIO_CloseAudio(_THIS);
3787
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
182 static void COREAUDIO_Deinitialize(void);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
183
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
184 /* Audio driver bootstrap functions */
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
185
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
186 static int
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
187 COREAUDIO_Available(void)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
188 {
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
189 return (1);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
190 }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
191
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
192 static int
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
193 COREAUDIO_Init(SDL_AudioDriverImpl *impl)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
194 {
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
195 /* !!! FIXME: should these _really_ be static? */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
196 build_device_list(0, &outputDevices, &outputDeviceCount);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
197 build_device_list(1, &inputDevices, &inputDeviceCount);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
198
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
199 /* Set the function pointers */
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
200 impl->OpenAudio = COREAUDIO_OpenAudio;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
201 impl->WaitAudio = COREAUDIO_WaitAudio;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
202 impl->PlayAudio = COREAUDIO_PlayAudio;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
203 impl->GetAudioBuf = COREAUDIO_GetAudioBuf;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
204 impl->CloseAudio = COREAUDIO_CloseAudio;
3787
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
205 impl->Deinitialize = COREAUDIO_Deinitialize;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
206
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
207 return 1;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
208 }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
209
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
210 AudioBootStrap COREAUDIO_bootstrap = {
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
211 "coreaudio", "Mac OS X CoreAudio",
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
212 COREAUDIO_Available, COREAUDIO_Init
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
213 };
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
214
3787
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
215
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
216 static void
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
217 COREAUDIO_Deinitialize(void)
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
218 {
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
219 free_device_list(0, &outputDevices, &outputDeviceCount);
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
220 free_device_list(1, &inputDevices, &inputDeviceCount);
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
221 }
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
222
8d74a4721ca9 Added deinitialize hook.
Ryan C. Gordon <icculus@icculus.org>
parents: 3786
diff changeset
223
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
224 /* 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
225 static OSStatus
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
226 outputCallback(void *inRefCon,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
227 AudioUnitRenderActionFlags *ioActionFlags,
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
228 const AudioTimeStamp * inTimeStamp,
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
229 UInt32 inBusNumber, UInt32 inNumberFrames,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
230 AudioBufferList *ioDataList)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
231 {
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
232 SDL_AudioDevice *this = (SDL_AudioDevice *) inRefCon;
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
233 AudioBuffer *ioData = &ioDataList->mBuffers[0];
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
234 UInt32 remaining, len;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
235 void *ptr;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
236
3786
b70f4e9291bb High level now lets user select a specific audio device with an environment
Ryan C. Gordon <icculus@icculus.org>
parents: 3785
diff changeset
237 /*
b70f4e9291bb High level now lets user select a specific audio device with an environment
Ryan C. Gordon <icculus@icculus.org>
parents: 3785
diff changeset
238 * !!! FIXME: I'm not sure if you can ever have more than one
b70f4e9291bb High level now lets user select a specific audio device with an environment
Ryan C. Gordon <icculus@icculus.org>
parents: 3785
diff changeset
239 * buffer, or what this signifies, or what to do with it...
b70f4e9291bb High level now lets user select a specific audio device with an environment
Ryan C. Gordon <icculus@icculus.org>
parents: 3785
diff changeset
240 */
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
241 if (ioDataList->mNumberBuffers != 1) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
242 return noErr;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
243 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
244
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
245 /* Only do anything if audio is enabled and not paused */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
246 if (!this->enabled || this->paused) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
247 SDL_memset(ioData->mData, this->spec.silence, ioData->mDataByteSize);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
248 return 0;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
249 }
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
250
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
251 /* 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
252 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
253 */
1338
604d73db6802 Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents: 1336
diff changeset
254 /*
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
255 assert(!this->convert.needed);
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
256 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
257 */
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
258
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
259 remaining = ioData->mDataByteSize;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
260 ptr = ioData->mData;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
261 while (remaining > 0) {
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
262 if (this->hidden->bufferOffset >= this->hidden->bufferSize) {
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
263 /* Generate the data */
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
264 SDL_memset(this->hidden->buffer, this->spec.silence,
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
265 this->hidden->bufferSize);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
266 SDL_mutexP(this->mixer_lock);
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
267 (*this->spec.callback) (this->spec.userdata, this->hidden->buffer,
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
268 this->hidden->bufferSize);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
269 SDL_mutexV(this->mixer_lock);
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
270 this->hidden->bufferOffset = 0;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
271 }
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
272
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
273 len = this->hidden->bufferSize - this->hidden->bufferOffset;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
274 if (len > remaining)
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
275 len = remaining;
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
276 SDL_memcpy(ptr,
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
277 (char *) this->hidden->buffer + this->hidden->bufferOffset,
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
278 len);
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
279 ptr = (char *) ptr + len;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
280 remaining -= len;
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
281 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
282 }
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
283
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
284 return 0;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
285 }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
286
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
287 static OSStatus
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
288 inputCallback(void *inRefCon,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
289 AudioUnitRenderActionFlags *ioActionFlags,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
290 const AudioTimeStamp * inTimeStamp,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
291 UInt32 inBusNumber, UInt32 inNumberFrames,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
292 AudioBufferList *ioData)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
293 {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
294 //err = AudioUnitRender(afr->fAudioUnit, ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, afr->fAudioBuffer);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
295 // !!! FIXME: write me!
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
296 return noErr;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
297 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
298
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
299
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
300 /* Dummy functions -- we don't use thread-based audio */
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
301 void
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
302 COREAUDIO_WaitAudio(_THIS)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
303 {
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
304 return;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
305 }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
306
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
307 void
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
308 COREAUDIO_PlayAudio(_THIS)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
309 {
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
310 return;
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
311 }
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 Uint8 *
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
314 COREAUDIO_GetAudioBuf(_THIS)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
315 {
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
316 return (NULL);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
317 }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
318
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
319 void
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
320 COREAUDIO_CloseAudio(_THIS)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
321 {
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
322 if (this->hidden != NULL) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
323 OSStatus result = noErr;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
324 AURenderCallbackStruct callback;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
325 const AudioUnitElement output_bus = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
326 const AudioUnitElement input_bus = 1;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
327 const int iscapture = this->hidden->isCapture;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
328 const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
329 const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
330 kAudioUnitScope_Input);
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
331
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
332 /* stop processing the audio unit */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
333 result = AudioOutputUnitStop(this->hidden->audioUnit);
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
334
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
335 /* Remove the input callback */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
336 memset(&callback, '\0', sizeof (AURenderCallbackStruct));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
337 result = AudioUnitSetProperty(this->hidden->audioUnit,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
338 kAudioUnitProperty_SetRenderCallback,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
339 scope, bus, &callback, sizeof (callback));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
340
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
341 CloseComponent(this->hidden->audioUnit);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
342
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
343 SDL_free(this->hidden->buffer);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
344 SDL_free(this->hidden);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
345 this->hidden = NULL;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
346 }
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
347 }
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
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
350 #define CHECK_RESULT(msg) \
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
351 if (result != noErr) { \
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
352 COREAUDIO_CloseAudio(this); \
2017
df06e6eb65c6 Fixed macosx audio initialization (passed wrong struct to CoreAudio).
Ryan C. Gordon <icculus@icculus.org>
parents: 2016
diff changeset
353 SDL_SetError("CoreAudio error (%s): %d", msg, (int) result); \
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
354 return 0; \
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
355 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
356
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
357 static int
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
358 find_device_by_name(_THIS, const char *devname, int iscapture)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
359 {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
360 AudioDeviceID devid = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
361 OSStatus result = noErr;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
362 UInt32 size = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
363 UInt32 alive = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
364 pid_t pid = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
365
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
366 if (devname == NULL) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
367 size = sizeof (AudioDeviceID);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
368 const AudioHardwarePropertyID propid =
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
369 ((iscapture) ? kAudioHardwarePropertyDefaultInputDevice :
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
370 kAudioHardwarePropertyDefaultOutputDevice);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
371
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
372 result = AudioHardwareGetProperty(propid, &size, &devid);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
373 CHECK_RESULT("AudioHardwareGetProperty (default device)");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
374 } else {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
375 if (!find_device_id(devname, iscapture, &devid)) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
376 SDL_SetError("CoreAudio: No such audio device.");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
377 return 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
378 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
379 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
380
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
381 size = sizeof (alive);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
382 result = AudioDeviceGetProperty(devid, 0, iscapture,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
383 kAudioDevicePropertyDeviceIsAlive,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
384 &size, &alive);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
385 CHECK_RESULT("AudioDeviceGetProperty (kAudioDevicePropertyDeviceIsAlive)");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
386
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
387 if (!alive) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
388 SDL_SetError("CoreAudio: requested device exists, but isn't alive.");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
389 return 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
390 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
391
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
392 size = sizeof (pid);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
393 result = AudioDeviceGetProperty(devid, 0, iscapture,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
394 kAudioDevicePropertyHogMode, &size, &pid);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
395
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
396 /* some devices don't support this property, so errors are fine here. */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
397 if ((result == noErr) && (pid != -1)) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
398 SDL_SetError("CoreAudio: requested device is being hogged.");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
399 return 0;
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
400 }
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
401
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
402 this->hidden->deviceID = devid;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
403 return 1;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
404 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
405
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
406
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
407 static int
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
408 prepare_audiounit(_THIS, const char *devname, int iscapture,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
409 const AudioStreamBasicDescription *strdesc)
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
410 {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
411 OSStatus result = noErr;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
412 AURenderCallbackStruct callback;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
413 ComponentDescription desc;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
414 Component comp = NULL;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
415 int use_system_device = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
416 UInt32 enableIO = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
417 const AudioUnitElement output_bus = 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
418 const AudioUnitElement input_bus = 1;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
419 const AudioUnitElement bus = ((iscapture) ? input_bus : output_bus);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
420 const AudioUnitScope scope = ((iscapture) ? kAudioUnitScope_Output :
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
421 kAudioUnitScope_Input);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
422
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
423 if (!find_device_by_name(this, devname, iscapture)) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
424 SDL_SetError("Couldn't find requested CoreAudio device");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
425 return 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
426 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
427
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
428 memset(&desc, '\0', sizeof(ComponentDescription));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
429 desc.componentType = kAudioUnitType_Output;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
430 desc.componentSubType = kAudioUnitSubType_HALOutput;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
431 desc.componentManufacturer = kAudioUnitManufacturer_Apple;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
432
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
433 comp = FindNextComponent(NULL, &desc);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
434 if (comp == NULL) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
435 SDL_SetError("Couldn't find requested CoreAudio component");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
436 return 0;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
437 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
438
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
439 /* Open & initialize the audio unit */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
440 result = OpenAComponent(comp, &this->hidden->audioUnit);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
441 CHECK_RESULT("OpenAComponent");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
442
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
443 // !!! FIXME: this is wrong?
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
444 enableIO = ((iscapture) ? 1 : 0);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
445 result = AudioUnitSetProperty(this->hidden->audioUnit,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
446 kAudioOutputUnitProperty_EnableIO,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
447 kAudioUnitScope_Input, input_bus,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
448 &enableIO, sizeof (enableIO));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
449 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO input)");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
450
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
451 // !!! FIXME: this is wrong?
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
452 enableIO = ((iscapture) ? 0 : 1);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
453 result = AudioUnitSetProperty(this->hidden->audioUnit,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
454 kAudioOutputUnitProperty_EnableIO,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
455 kAudioUnitScope_Output, output_bus,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
456 &enableIO, sizeof (enableIO));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
457 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_EnableIO output)");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
458
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
459 result = AudioUnitSetProperty(this->hidden->audioUnit,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
460 kAudioOutputUnitProperty_CurrentDevice,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
461 kAudioUnitScope_Global, 0,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
462 &this->hidden->deviceID,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
463 sizeof (AudioDeviceID));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
464 CHECK_RESULT("AudioUnitSetProperty (kAudioOutputUnitProperty_CurrentDevice)");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
465
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
466 /* Set the data format of the audio unit. */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
467 result = AudioUnitSetProperty(this->hidden->audioUnit,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
468 kAudioUnitProperty_StreamFormat,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
469 scope, bus, strdesc, sizeof (*strdesc));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
470 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_StreamFormat)");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
471
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
472 /* Set the audio callback */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
473 memset(&callback, '\0', sizeof (AURenderCallbackStruct));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
474 callback.inputProc = ((iscapture) ? inputCallback : outputCallback);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
475 callback.inputProcRefCon = this;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
476 result = AudioUnitSetProperty(this->hidden->audioUnit,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
477 kAudioUnitProperty_SetRenderCallback,
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
478 scope, bus, &callback, sizeof (callback));
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
479 CHECK_RESULT("AudioUnitSetProperty (kAudioUnitProperty_SetInputCallback)");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
480
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
481 /* Calculate the final parameters for this audio specification */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
482 SDL_CalculateAudioSpec(&this->spec);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
483
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
484 /* Allocate a sample buffer */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
485 this->hidden->bufferOffset = this->hidden->bufferSize = this->spec.size;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
486 this->hidden->buffer = SDL_malloc(this->hidden->bufferSize);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
487
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
488 result = AudioUnitInitialize(this->hidden->audioUnit);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
489 CHECK_RESULT("AudioUnitInitialize");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
490
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
491 /* Finally, start processing of the audio unit */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
492 result = AudioOutputUnitStart(this->hidden->audioUnit);
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
493 CHECK_RESULT("AudioOutputUnitStart");
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
494
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
495 /* We're running! */
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
496 return 1;
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
497 }
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
498
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
499
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
500 int
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
501 COREAUDIO_OpenAudio(_THIS, const char *devname, int iscapture)
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
502 {
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
503 AudioStreamBasicDescription strdesc;
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
504 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
505 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
506
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
507 /* Initialize all variables that we clean on shutdown */
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
508 this->hidden = (struct SDL_PrivateAudioData *)
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
509 SDL_malloc((sizeof *this->hidden));
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
510 if (this->hidden == NULL) {
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
511 SDL_OutOfMemory();
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
512 return (0);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
513 }
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
514 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
515
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
516 this->hidden->isCapture = iscapture;
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
517
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
518 /* Setup a AudioStreamBasicDescription with the requested format */
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
519 memset(&strdesc, '\0', sizeof(AudioStreamBasicDescription));
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
520 strdesc.mFormatID = kAudioFormatLinearPCM;
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
521 strdesc.mFormatFlags = kLinearPCMFormatFlagIsPacked;
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
522 strdesc.mChannelsPerFrame = this->spec.channels;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
523 strdesc.mSampleRate = this->spec.freq;
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
524 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
525
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
526 while ((!valid_datatype) && (test_format)) {
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
527 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
528 /* 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
529 switch (test_format) {
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
530 case AUDIO_U8:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
531 case AUDIO_S8:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
532 case AUDIO_U16LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
533 case AUDIO_S16LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
534 case AUDIO_U16MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
535 case AUDIO_S16MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
536 case AUDIO_S32LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
537 case AUDIO_S32MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
538 case AUDIO_F32LSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
539 case AUDIO_F32MSB:
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
540 valid_datatype = 1;
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
541 strdesc.mBitsPerChannel = SDL_AUDIO_BITSIZE(this->spec.format);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
542 if (SDL_AUDIO_ISBIGENDIAN(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
543 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
544
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
545 if (SDL_AUDIO_ISFLOAT(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
546 strdesc.mFormatFlags |= kLinearPCMFormatFlagIsFloat;
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
547 else if (SDL_AUDIO_ISSIGNED(this->spec.format))
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
548 strdesc.mFormatFlags |= kLinearPCMFormatFlagIsSignedInteger;
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
549 break;
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
550 }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
551 }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
552
2043
adf732f1f016 Formatting update
Sam Lantinga <slouken@libsdl.org>
parents: 2017
diff changeset
553 if (!valid_datatype) { /* shouldn't happen, but just in case... */
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
554 SDL_SetError("Unsupported audio format");
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
555 return 0;
2003
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
556 }
506851d3efa4 Mac OS X audio backend now supports int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
557
2016
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
558 strdesc.mBytesPerFrame =
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
559 strdesc.mBitsPerChannel * strdesc.mChannelsPerFrame / 8;
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
560 strdesc.mBytesPerPacket =
72fa74928062 Compile fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 2003
diff changeset
561 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
562
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
563 if (!prepare_audiounit(this, devname, iscapture, &strdesc)) {
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
564 return 0; /* prepare_audiounit() will call SDL_SetError()... */
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
565 }
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
566
3785
da2ea0694d11 Ton of work on CoreAudio driver for new 1.3 features...most of the
Ryan C. Gordon <icculus@icculus.org>
parents: 3784
diff changeset
567 return 1; /* good to go. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1487
diff changeset
568 }
935
f8d5ddc7aef1 Audio improvements from Max Horn, including a new CoreAudio driver for MacOSX
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
569
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
570 /* vi: set ts=4 sw=4 expandtab: */