annotate src/audio/macrom/SDL_romaudio.c @ 3788:7006b176ef4f SDL-ryan-multiple-audio-device

More work on the 1.3 CoreAudio code.
author Ryan C. Gordon <icculus@icculus.org>
date Tue, 03 Oct 2006 18:32:34 +0000
parents 37c9c4590689
children 866c310e2cb5
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 1135
diff changeset
3 Copyright (C) 1997-2006 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 1135
diff changeset
6 modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 1135
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 1135
diff changeset
13 Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1135
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: 1135
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: 1135
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
19 Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 47
diff changeset
20 slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23
3782
8225ef1f4dee Removed the pre-Carbon Mac OS 9 audio code, so Mac OS 8 is officially
Ryan C. Gordon <icculus@icculus.org>
parents: 2004
diff changeset
24 /* This should work on PowerPC and Intel Mac OS X, and Carbonized Mac OS 9. */
8225ef1f4dee Removed the pre-Carbon Mac OS 9 audio code, so Mac OS 8 is officially
Ryan C. Gordon <icculus@icculus.org>
parents: 2004
diff changeset
25
1133
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
26 #if defined(__APPLE__) && defined(__MACH__)
3782
8225ef1f4dee Removed the pre-Carbon Mac OS 9 audio code, so Mac OS 8 is officially
Ryan C. Gordon <icculus@icculus.org>
parents: 2004
diff changeset
27 # define SDL_MACOS_NAME "Mac OS X"
1133
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
28 # include <Carbon/Carbon.h>
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
29 #elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
3782
8225ef1f4dee Removed the pre-Carbon Mac OS 9 audio code, so Mac OS 8 is officially
Ryan C. Gordon <icculus@icculus.org>
parents: 2004
diff changeset
30 # define SDL_MACOS_NAME "Mac OS 9"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31 # include <Carbon.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32 #else
3782
8225ef1f4dee Removed the pre-Carbon Mac OS 9 audio code, so Mac OS 8 is officially
Ryan C. Gordon <icculus@icculus.org>
parents: 2004
diff changeset
33 # define SDL_MACOS_NAME "Mac OS 9"
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
34 # include <Sound.h> /* SoundManager interface */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
35 # include <Gestalt.h>
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
36 # include <DriverServices.h>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
37 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
38
1133
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
39 #if !defined(NewSndCallBackUPP) && (UNIVERSAL_INTERFACES_VERSION < 0x0335)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
40 #if !defined(NewSndCallBackProc) /* avoid circular redefinition... */
1133
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
41 #define NewSndCallBackUPP NewSndCallBackProc
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
42 #endif
1135
cf6133247d34 Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 1133
diff changeset
43 #if !defined(NewSndCallBackUPP)
cf6133247d34 Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 1133
diff changeset
44 #define NewSndCallBackUPP NewSndCallBackProc
cf6133247d34 Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents: 1133
diff changeset
45 #endif
1133
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
46 #endif
609c060fd2a2 The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents: 769
diff changeset
47
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
48 #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
49 #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
50 #include "../SDL_sysaudio.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
51 #include "SDL_romaudio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
52
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
53 /* Audio driver functions */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
54
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
55 static void SNDMGR_CloseAudio(_THIS);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
56 static int SNDMGR_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: 3782
diff changeset
57 static void SNDMGR_LockAudio(_THIS);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
58 static void SNDMGR_UnlockAudio(_THIS);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
59
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
60 /* Audio driver bootstrap functions */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
61
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
62
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
63 static int
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
64 SNDMGR_Available(void)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
65 {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
66 return (1);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
67 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
68
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
69
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
70 static int
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
71 SNDMGR_Init(SDL_AudioDriverImpl *impl)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
72 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
73 /* Set the function pointers */
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
74 impl->OpenAudio = SNDMGR_OpenAudio;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
75 impl->CloseAudio = SNDMGR_CloseAudio;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
76 impl->LockAudio = SNDMGR_LockAudio;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
77 impl->UnlockAudio = SNDMGR_UnlockAudio;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
78
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
79 #ifdef __MACOSX__ /* Mac OS X uses threaded audio, so normal thread code is okay */
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
80 impl->LockAudio = NULL;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
81 impl->UnlockAudio = NULL;
348
25809353f877 Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents: 324
diff changeset
82 #endif
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
83
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
84 return 1;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
85 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
86
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
87 AudioBootStrap SNDMGR_bootstrap = {
3782
8225ef1f4dee Removed the pre-Carbon Mac OS 9 audio code, so Mac OS 8 is officially
Ryan C. Gordon <icculus@icculus.org>
parents: 2004
diff changeset
88 "sndmgr", SDL_MACOS_NAME " SoundManager",
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
89 SNDMGR_Available, SNDMGR_Init
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
90 };
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
91
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
92 #pragma options align=power
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
93
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
94 static volatile int audio_is_opened = 0;
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
95 static volatile SInt32 audio_is_locked = 0;
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
96 static volatile SInt32 need_to_mix = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
97
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
98 static UInt8 *buffer[2];
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
99 static volatile UInt32 running = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
100 static CmpSoundHeader header;
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
101 static volatile Uint32 fill_me = 0;
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
102
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
103 static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
104 mix_buffer(SDL_AudioDevice * audio, UInt8 * buffer)
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
105 {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
106 if (!audio->paused) {
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
107 #ifdef __MACOSX__
348
25809353f877 Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents: 324
diff changeset
108 SDL_mutexP(audio->mixer_lock);
25809353f877 Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents: 324
diff changeset
109 #endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
110 if (audio->convert.needed) {
348
25809353f877 Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents: 324
diff changeset
111 audio->spec.callback(audio->spec.userdata,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
112 (Uint8 *) audio->convert.buf,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
113 audio->convert.len);
348
25809353f877 Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents: 324
diff changeset
114 SDL_ConvertAudio(&audio->convert);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
115 if (audio->convert.len_cvt != audio->spec.size) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
116 /* Uh oh... probably crashes here */ ;
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
117 }
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
118 SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt);
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
119 } else {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
120 audio->spec.callback(audio->spec.userdata, buffer,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
121 audio->spec.size);
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
122 }
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
123 #ifdef __MACOSX__
348
25809353f877 Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents: 324
diff changeset
124 SDL_mutexV(audio->mixer_lock);
25809353f877 Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents: 324
diff changeset
125 #endif
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
126 }
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
127
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
128 DecrementAtomic((SInt32 *) & need_to_mix);
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
129 }
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
130
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
131 static void
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
132 SNDMGR_LockAudio(_THIS)
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
133 {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
134 IncrementAtomic((SInt32 *) & audio_is_locked);
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
135 }
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
136
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
137 static void
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
138 SNDMGR_UnlockAudio(_THIS)
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
139 {
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
140 SInt32 oldval;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
141
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
142 oldval = DecrementAtomic((SInt32 *) & audio_is_locked);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
143 if (oldval != 1) /* != 1 means audio is still locked. */
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
144 return;
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
145
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
146 /* Did we miss the chance to mix in an interrupt? Do it now. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
147 if (BitAndAtomic(0xFFFFFFFF, (UInt32 *) & need_to_mix)) {
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
148 /*
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
149 * Note that this could be a problem if you missed an interrupt
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
150 * while the audio was locked, and get preempted by a second
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
151 * interrupt here, but that means you locked for way too long anyhow.
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
152 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
153 mix_buffer(this, buffer[fill_me]);
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
154 }
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
155 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
156
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
157 static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
158 callBackProc(SndChannel * chan, SndCommand * cmd_passed)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
159 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
160 UInt32 play_me;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
161 SndCommand cmd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
162 SDL_AudioDevice *audio = (SDL_AudioDevice *) chan->userInfo;
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
163
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
164 IncrementAtomic((SInt32 *) & need_to_mix);
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
165
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
166 fill_me = cmd_passed->param2; /* buffer that has just finished playing, so fill it */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
167 play_me = !fill_me; /* filled buffer to play _now_ */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
168
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
169 if (!audio->enabled) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
170 return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
171 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
172
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
173 /* queue previously mixed buffer for playback. */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
174 header.samplePtr = (Ptr) buffer[play_me];
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
175 cmd.cmd = bufferCmd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
176 cmd.param1 = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
177 cmd.param2 = (long) &header;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
178 SndDoCommand(chan, &cmd, 0);
47
45b1c4303f87 Added initial support for Quartz video (thanks Darrell!)
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
179
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
180 memset(buffer[fill_me], 0, audio->spec.size);
47
45b1c4303f87 Added initial support for Quartz video (thanks Darrell!)
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
181
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
182 /*
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
183 * if audio device isn't locked, mix the next buffer to be queued in
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
184 * the memory block that just finished playing.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
185 */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
186 if (!BitAndAtomic(0xFFFFFFFF, (UInt32 *) & audio_is_locked)) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
187 mix_buffer(audio, buffer[fill_me]);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
188 }
323
b7e8038e40ae The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
189
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
190 /* set this callback to run again when current buffer drains. */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
191 if (running) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
192 cmd.cmd = callBackCmd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
193 cmd.param1 = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
194 cmd.param2 = play_me;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
195
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
196 SndDoCommand(chan, &cmd, 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
197 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
198 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
199
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
200 static int
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
201 SNDMGR_OpenAudio(_THIS, const char *devname, int iscapture)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
202 {
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
203 SDL_AudioSpec *spec = &this->spec;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
204 SndChannelPtr channel = NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
205 SndCallBackUPP callback;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
206 int sample_bits;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
207 int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
208 long initOptions;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
209
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
210 if (audio_is_opened) {
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
211 SDL_SetError("SoundManager driver doesn't support multiple opens");
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
212 return 0;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
213 }
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
214
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
215 if (iscapture) {
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
216 SDL_SetError("SoundManager driver doesn't support recording");
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
217 return 0;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
218 }
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
219
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
220 /* !!! FIXME: ignore devname? */
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
221
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
222 /* 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: 3782
diff changeset
223 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: 3782
diff changeset
224 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: 3782
diff changeset
225 if (this->hidden == NULL) {
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
226 SDL_OutOfMemory();
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
227 return 0;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
228 }
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
229 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: 3782
diff changeset
230
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
231 /* !!! FIXME: check devname and iscapture... */
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
232
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
233 /* !!! FIXME: iterate through format matrix... */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
234 /* Very few conversions are required, but... */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
235 switch (spec->format) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
236 case AUDIO_S8:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
237 spec->format = AUDIO_U8;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
238 break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
239 case AUDIO_U16LSB:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
240 spec->format = AUDIO_S16LSB;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
241 break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
242 case AUDIO_U16MSB:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
243 spec->format = AUDIO_S16MSB;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
244 break;
2004
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
245 case AUDIO_F32LSB:
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
246 spec->format = AUDIO_F32MSB;
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
247 break;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
248 }
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
249 SDL_CalculateAudioSpec(&this->spec);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
250
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
251 /* initialize bufferCmd header */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
252 memset(&header, 0, sizeof(header));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
253 callback = (SndCallBackUPP) NewSndCallBackUPP(callBackProc);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
254 sample_bits = spec->size / spec->samples / spec->channels * 8;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
255
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
256 #ifdef DEBUG_AUDIO
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
257 fprintf(stderr,
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
258 "Audio format 0x%x, channels = %d, sample_bits = %d, frequency = %d\n",
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
259 spec->format, spec->channels, sample_bits, spec->freq);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
260 #endif /* DEBUG_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: 1621
diff changeset
261
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
262 header.numChannels = spec->channels;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
263 header.sampleSize = sample_bits;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
264 header.sampleRate = spec->freq << 16;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
265 header.numFrames = spec->samples;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
266 header.encode = cmpSH;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
267
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
268 /* Note that we install the 16bitLittleEndian Converter if needed. */
2004
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
269 if (spec->format == AUDIO_S16LSB) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
270 header.compressionID = fixedCompression;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
271 header.format = k16BitLittleEndianFormat;
2004
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
272 } else if (spec->format == AUDIO_S32MSB) {
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
273 header.compressionID = fixedCompression;
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
274 header.format = k32BitFormat;
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
275 } else if (spec->format == AUDIO_S32LSB) {
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
276 header.compressionID = fixedCompression;
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
277 header.format = k32BitLittleEndianFormat;
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
278 } else if (spec->format == AUDIO_F32MSB) {
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
279 header.compressionID = fixedCompression;
c27292a690b7 Mac OS 9 support for int32/float32.
Ryan C. Gordon <icculus@icculus.org>
parents: 1895
diff changeset
280 header.format = kFloat32Format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
281 }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
282
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
283 /* allocate 2 buffers */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
284 for (i = 0; i < 2; i++) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
285 buffer[i] = (UInt8 *) malloc(sizeof(UInt8) * spec->size);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
286 if (buffer[i] == NULL) {
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
287 SNDMGR_CloseAudio(this);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
288 SDL_OutOfMemory();
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
289 return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
290 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
291 memset(buffer[i], 0, spec->size);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
292 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
293
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
294 /* Create the sound manager channel */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
295 channel = (SndChannelPtr) SDL_malloc(sizeof(*channel));
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
296 if (channel == NULL) {
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
297 SNDMGR_CloseAudio(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
298 SDL_OutOfMemory();
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
299 return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
300 }
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
301 this->hidden->channel = channel;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
302 if (spec->channels >= 2) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
303 initOptions = initStereo;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
304 } else {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
305 initOptions = initMono;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
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: 1621
diff changeset
307 channel->userInfo = (long) this;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
308 channel->qLength = 128;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
309 if (SndNewChannel(&channel, sampledSynth, initOptions, callback) != noErr) {
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
310 SNDMGR_CloseAudio(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
311 SDL_SetError("Unable to create audio channel");
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
312 return 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
313 }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
314
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
315 /* start playback */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
316 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
317 SndCommand cmd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
318 cmd.cmd = callBackCmd;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
319 cmd.param2 = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
320 running = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
321 SndDoCommand(channel, &cmd, 0);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
322 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
323
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
324 audio_is_opened = 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: 1621
diff changeset
325 return 1;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
326 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
327
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
328 static void
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
329 SNDMGR_CloseAudio(_THIS)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
330 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
331
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
332 int i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
333
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
334 running = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
335
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
336 if (this->hidden->channel) {
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
337 SndDisposeChannel(this->hidden->channel, true);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
338 this->hidden->channel = NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
339 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
340
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
341 for (i = 0; i < 2; ++i) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
342 if (buffer[i]) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
343 SDL_free(buffer[i]);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
344 buffer[i] = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
345 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
346 }
3784
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
347 SDL_free(this->hidden);
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
348 this->hidden = NULL;
37c9c4590689 First batch of heavy lifting on supporting multiple audio devices at once.
Ryan C. Gordon <icculus@icculus.org>
parents: 3782
diff changeset
349 audio_is_opened = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
350 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
351
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
352 /* vi: set ts=4 sw=4 expandtab: */