Mercurial > sdl-ios-xcode
annotate src/audio/macrom/SDL_romaudio.c @ 1198:b616ac4f39d1
Fixed gcc 4 warnings
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 08 Dec 2005 14:48:21 +0000 |
parents | cf6133247d34 |
children | c9b51268668f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
348
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
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 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
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 #if defined(__APPLE__) && defined(__MACH__) |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
29 # 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
|
30 #elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335) |
0 | 31 # include <Carbon.h> |
32 #else | |
33 # include <Sound.h> /* SoundManager interface */ | |
34 # 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
|
35 # include <DriverServices.h> |
0 | 36 #endif |
37 | |
38 #include <stdlib.h> | |
39 #include <stdio.h> | |
40 | |
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 #if !defined(NewSndCallBackUPP) && (UNIVERSAL_INTERFACES_VERSION < 0x0335) |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
42 #if !defined(NewSndCallBackProc) /* avoid circular redefinition... */ |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
43 #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
|
44 #endif |
1135
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
1133
diff
changeset
|
45 #if !defined(NewSndCallBackUPP) |
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
1133
diff
changeset
|
46 #define NewSndCallBackUPP NewSndCallBackProc |
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
1133
diff
changeset
|
47 #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
|
48 #endif |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
49 |
0 | 50 #include "SDL_endian.h" |
51 #include "SDL_audio.h" | |
52 #include "SDL_audio_c.h" | |
53 #include "SDL_audiomem.h" | |
54 #include "SDL_sysaudio.h" | |
55 #include "SDL_romaudio.h" | |
56 | |
57 /* Audio driver functions */ | |
58 | |
59 static void Mac_CloseAudio(_THIS); | |
60 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
61 static void Mac_LockAudio(_THIS); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
62 static void Mac_UnlockAudio(_THIS); |
0 | 63 |
64 /* Audio driver bootstrap functions */ | |
65 | |
66 | |
67 static int Audio_Available(void) | |
68 { | |
69 return(1); | |
70 } | |
71 | |
72 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
73 { | |
74 free(device->hidden); | |
75 free(device); | |
76 } | |
77 | |
78 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
79 { | |
80 SDL_AudioDevice *this; | |
81 | |
82 /* Initialize all variables that we clean on shutdown */ | |
83 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
84 if ( this ) { | |
85 memset(this, 0, (sizeof *this)); | |
86 this->hidden = (struct SDL_PrivateAudioData *) | |
87 malloc((sizeof *this->hidden)); | |
88 } | |
89 if ( (this == NULL) || (this->hidden == NULL) ) { | |
90 SDL_OutOfMemory(); | |
91 if ( this ) { | |
92 free(this); | |
93 } | |
94 return(0); | |
95 } | |
96 memset(this->hidden, 0, (sizeof *this->hidden)); | |
97 | |
98 /* Set the function pointers */ | |
99 this->OpenAudio = Mac_OpenAudio; | |
100 this->CloseAudio = Mac_CloseAudio; | |
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 this->LockAudio = Mac_LockAudio; |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
102 this->UnlockAudio = Mac_UnlockAudio; |
0 | 103 this->free = Audio_DeleteDevice; |
104 | |
348
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
105 #ifdef MACOSX /* MacOS X uses threaded audio, so normal thread code is okay */ |
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
106 this->LockAudio = NULL; |
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
107 this->UnlockAudio = NULL; |
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
108 #endif |
0 | 109 return this; |
110 } | |
111 | |
112 AudioBootStrap SNDMGR_bootstrap = { | |
113 "sndmgr", "MacOS SoundManager 3.0", | |
114 Audio_Available, Audio_CreateDevice | |
115 }; | |
116 | |
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 #if defined(TARGET_API_MAC_CARBON) || defined(USE_RYANS_SOUNDCODE) |
348
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
118 /* This works correctly on MacOS X */ |
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 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
120 #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
|
121 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
122 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
|
123 static volatile SInt32 need_to_mix = 0; |
0 | 124 |
125 static UInt8 *buffer[2]; | |
126 static volatile UInt32 running = 0; | |
127 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
|
128 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
|
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 static void mix_buffer(SDL_AudioDevice *audio, UInt8 *buffer) |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
131 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
132 if ( ! audio->paused ) { |
348
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
133 #ifdef MACOSX |
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
134 SDL_mutexP(audio->mixer_lock); |
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
135 #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
|
136 if ( audio->convert.needed ) { |
348
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
137 audio->spec.callback(audio->spec.userdata, |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
138 (Uint8 *)audio->convert.buf,audio->convert.len); |
348
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
139 SDL_ConvertAudio(&audio->convert); |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
140 if ( audio->convert.len_cvt != audio->spec.size ) { |
348
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
141 /* 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
|
142 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
143 memcpy(buffer, audio->convert.buf, audio->convert.len_cvt); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
144 } else { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
145 audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
146 } |
348
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
147 #ifdef MACOSX |
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
148 SDL_mutexV(audio->mixer_lock); |
25809353f877
Re-added MacOS X audio locking
Sam Lantinga <slouken@libsdl.org>
parents:
324
diff
changeset
|
149 #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
|
150 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
151 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
152 DecrementAtomic((SInt32 *) &need_to_mix); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
153 } |
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 static void Mac_LockAudio(_THIS) |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
156 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
157 IncrementAtomic((SInt32 *) &audio_is_locked); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
158 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
159 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
160 static void Mac_UnlockAudio(_THIS) |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
161 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
162 SInt32 oldval; |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
163 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
164 oldval = DecrementAtomic((SInt32 *) &audio_is_locked); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
165 if ( oldval != 1 ) /* != 1 means audio is still locked. */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
166 return; |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
167 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
168 /* Did we miss the chance to mix in an interrupt? Do it now. */ |
324
f25f666d609a
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
323
diff
changeset
|
169 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
|
170 /* |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
171 * 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
|
172 * 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
|
173 * 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
|
174 */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
175 mix_buffer (this, buffer[fill_me]); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
176 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
177 } |
0 | 178 |
179 static void callBackProc (SndChannel *chan, SndCommand *cmd_passed ) { | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
180 UInt32 play_me; |
0 | 181 SndCommand cmd; |
182 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
|
183 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
184 IncrementAtomic((SInt32 *) &need_to_mix); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
185 |
47
45b1c4303f87
Added initial support for Quartz video (thanks Darrell!)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
186 fill_me = cmd_passed->param2; /* buffer that has just finished playing, so fill it */ |
0 | 187 play_me = ! fill_me; /* filled buffer to play _now_ */ |
188 | |
189 if ( ! audio->enabled ) { | |
190 return; | |
191 } | |
192 | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
193 /* queue previously mixed buffer for playback. */ |
0 | 194 header.samplePtr = (Ptr)buffer[play_me]; |
195 cmd.cmd = bufferCmd; | |
196 cmd.param1 = 0; | |
197 cmd.param2 = (long)&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
|
198 SndDoCommand (chan, &cmd, 0); |
47
45b1c4303f87
Added initial support for Quartz video (thanks Darrell!)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
199 |
0 | 200 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
|
201 |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
202 /* |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
203 * if audio device isn't locked, mix the next buffer to be queued in |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
204 * the memory block that just finished playing. |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
205 */ |
324
f25f666d609a
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
323
diff
changeset
|
206 if ( ! BitAndAtomic(0xFFFFFFFF, (UInt32 *) &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
|
207 mix_buffer (audio, buffer[fill_me]); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
208 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
209 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
210 /* set this callback to run again when current buffer drains. */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
211 if ( running ) { |
0 | 212 cmd.cmd = callBackCmd; |
213 cmd.param1 = 0; | |
214 cmd.param2 = play_me; | |
215 | |
216 SndDoCommand (chan, &cmd, 0); | |
217 } | |
218 } | |
219 | |
220 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec) { | |
221 | |
222 SndCallBackUPP callback; | |
223 int sample_bits; | |
224 int i; | |
225 long initOptions; | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
226 |
0 | 227 /* Very few conversions are required, but... */ |
228 switch (spec->format) { | |
229 case AUDIO_S8: | |
230 spec->format = AUDIO_U8; | |
231 break; | |
232 case AUDIO_U16LSB: | |
233 spec->format = AUDIO_S16LSB; | |
234 break; | |
235 case AUDIO_U16MSB: | |
236 spec->format = AUDIO_S16MSB; | |
237 break; | |
238 } | |
239 SDL_CalculateAudioSpec(spec); | |
240 | |
241 /* initialize bufferCmd header */ | |
242 memset (&header, 0, sizeof(header)); | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
243 callback = (SndCallBackUPP) NewSndCallBackUPP (callBackProc); |
0 | 244 sample_bits = spec->size / spec->samples / spec->channels * 8; |
245 | |
246 #ifdef DEBUG_AUDIO | |
247 fprintf(stderr, | |
248 "Audio format 0x%x, channels = %d, sample_bits = %d, frequency = %d\n", | |
249 spec->format, spec->channels, sample_bits, spec->freq); | |
250 #endif /* DEBUG_AUDIO */ | |
251 | |
252 header.numChannels = spec->channels; | |
253 header.sampleSize = sample_bits; | |
254 header.sampleRate = spec->freq << 16; | |
255 header.numFrames = spec->samples; | |
256 header.encode = cmpSH; | |
257 | |
258 /* Note that we install the 16bitLittleEndian Converter if needed. */ | |
259 if ( spec->format == 0x8010 ) { | |
260 header.compressionID = fixedCompression; | |
261 header.format = k16BitLittleEndianFormat; | |
262 } | |
263 | |
264 /* allocate 2 buffers */ | |
265 for (i=0; i<2; i++) { | |
266 buffer[i] = (UInt8*)malloc (sizeof(UInt8) * spec->size); | |
267 if (buffer[i] == NULL) { | |
268 SDL_OutOfMemory(); | |
269 return (-1); | |
270 } | |
271 memset (buffer[i], 0, spec->size); | |
272 } | |
273 | |
274 /* Create the sound manager channel */ | |
275 channel = (SndChannelPtr)malloc(sizeof(*channel)); | |
276 if ( channel == NULL ) { | |
277 SDL_OutOfMemory(); | |
278 return(-1); | |
279 } | |
280 if ( spec->channels >= 2 ) { | |
281 initOptions = initStereo; | |
282 } else { | |
283 initOptions = initMono; | |
284 } | |
285 channel->userInfo = (long)this; | |
286 channel->qLength = 128; | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
287 if ( SndNewChannel(&channel, sampledSynth, initOptions, callback) != noErr ) { |
0 | 288 SDL_SetError("Unable to create audio channel"); |
289 free(channel); | |
290 channel = NULL; | |
291 return(-1); | |
292 } | |
293 | |
294 /* start playback */ | |
295 { | |
296 SndCommand cmd; | |
297 cmd.cmd = callBackCmd; | |
298 cmd.param2 = 0; | |
299 running = 1; | |
300 SndDoCommand (channel, &cmd, 0); | |
301 } | |
302 | |
303 return 1; | |
304 } | |
305 | |
306 static void Mac_CloseAudio(_THIS) { | |
307 | |
308 int i; | |
309 | |
310 running = 0; | |
311 | |
312 if (channel) { | |
313 SndDisposeChannel (channel, true); | |
314 channel = NULL; | |
315 } | |
316 | |
317 for ( i=0; i<2; ++i ) { | |
318 if ( buffer[i] ) { | |
319 free(buffer[i]); | |
320 buffer[i] = NULL; | |
321 } | |
322 } | |
323 } | |
324 | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
325 #else /* !TARGET_API_MAC_CARBON && !USE_RYANS_SOUNDCODE */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
326 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
327 static void Mac_LockAudio(_THIS) |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
328 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
329 /* no-op. */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
330 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
331 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
332 static void Mac_UnlockAudio(_THIS) |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
333 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
334 /* no-op. */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
335 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
336 |
0 | 337 |
338 /* This function is called by Sound Manager when it has exhausted one of | |
339 the buffers, so we'll zero it to silence and fill it with audio if | |
340 we're not paused. | |
341 */ | |
342 static pascal | |
343 void sndDoubleBackProc (SndChannelPtr chan, SndDoubleBufferPtr newbuf) | |
344 { | |
345 SDL_AudioDevice *audio = (SDL_AudioDevice *)newbuf->dbUserInfo[0]; | |
346 | |
347 /* If audio is quitting, don't do anything */ | |
348 if ( ! audio->enabled ) { | |
349 return; | |
350 } | |
351 memset (newbuf->dbSoundData, 0, audio->spec.size); | |
352 newbuf->dbNumFrames = audio->spec.samples; | |
353 if ( ! audio->paused ) { | |
354 if ( audio->convert.needed ) { | |
355 audio->spec.callback(audio->spec.userdata, | |
356 (Uint8 *)audio->convert.buf,audio->convert.len); | |
357 SDL_ConvertAudio(&audio->convert); | |
358 #if 0 | |
359 if ( audio->convert.len_cvt != audio->spec.size ) { | |
360 /* Uh oh... probably crashes here */; | |
361 } | |
362 #endif | |
363 memcpy(newbuf->dbSoundData, audio->convert.buf, | |
364 audio->convert.len_cvt); | |
365 } else { | |
366 audio->spec.callback(audio->spec.userdata, | |
367 (Uint8 *)newbuf->dbSoundData, audio->spec.size); | |
368 } | |
369 } | |
370 newbuf->dbFlags |= dbBufferReady; | |
371 } | |
372 | |
373 static int DoubleBufferAudio_Available(void) | |
374 { | |
375 int available; | |
376 NumVersion sndversion; | |
377 long response; | |
378 | |
379 available = 0; | |
380 sndversion = SndSoundManagerVersion(); | |
381 if ( sndversion.majorRev >= 3 ) { | |
382 if ( Gestalt(gestaltSoundAttr, &response) == noErr ) { | |
383 if ( (response & (1 << gestaltSndPlayDoubleBuffer)) ) { | |
384 available = 1; | |
385 } | |
386 } | |
387 } else { | |
388 if ( Gestalt(gestaltSoundAttr, &response) == noErr ) { | |
389 if ( (response & (1 << gestaltHasASC)) ) { | |
390 available = 1; | |
391 } | |
392 } | |
393 } | |
394 return(available); | |
395 } | |
396 | |
397 static void Mac_CloseAudio(_THIS) | |
398 { | |
399 int i; | |
400 | |
401 if ( channel != NULL ) { | |
402 /* Clean up the audio channel */ | |
403 SndDisposeChannel(channel, true); | |
404 channel = NULL; | |
405 } | |
406 for ( i=0; i<2; ++i ) { | |
407 if ( audio_buf[i] ) { | |
408 free(audio_buf[i]); | |
409 audio_buf[i] = NULL; | |
410 } | |
411 } | |
412 } | |
413 | |
414 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
415 { | |
416 SndDoubleBufferHeader2 audio_dbh; | |
417 int i; | |
418 long initOptions; | |
419 int sample_bits; | |
420 SndDoubleBackUPP doubleBackProc; | |
421 | |
422 /* Check to make sure double-buffered audio is available */ | |
423 if ( ! DoubleBufferAudio_Available() ) { | |
424 SDL_SetError("Sound manager doesn't support double-buffering"); | |
425 return(-1); | |
426 } | |
427 | |
428 /* Very few conversions are required, but... */ | |
429 switch (spec->format) { | |
430 case AUDIO_S8: | |
431 spec->format = AUDIO_U8; | |
432 break; | |
433 case AUDIO_U16LSB: | |
434 spec->format = AUDIO_S16LSB; | |
435 break; | |
436 case AUDIO_U16MSB: | |
437 spec->format = AUDIO_S16MSB; | |
438 break; | |
439 } | |
440 SDL_CalculateAudioSpec(spec); | |
441 | |
442 /* initialize the double-back header */ | |
443 memset(&audio_dbh, 0, sizeof(audio_dbh)); | |
444 doubleBackProc = NewSndDoubleBackProc (sndDoubleBackProc); | |
445 sample_bits = spec->size / spec->samples / spec->channels * 8; | |
446 | |
447 audio_dbh.dbhNumChannels = spec->channels; | |
448 audio_dbh.dbhSampleSize = sample_bits; | |
449 audio_dbh.dbhCompressionID = 0; | |
450 audio_dbh.dbhPacketSize = 0; | |
451 audio_dbh.dbhSampleRate = spec->freq << 16; | |
452 audio_dbh.dbhDoubleBack = doubleBackProc; | |
453 audio_dbh.dbhFormat = 0; | |
454 | |
455 /* Note that we install the 16bitLittleEndian Converter if needed. */ | |
456 if ( spec->format == 0x8010 ) { | |
457 audio_dbh.dbhCompressionID = fixedCompression; | |
458 audio_dbh.dbhFormat = k16BitLittleEndianFormat; | |
459 } | |
460 | |
461 /* allocate the 2 double-back buffers */ | |
462 for ( i=0; i<2; ++i ) { | |
463 audio_buf[i] = calloc(1, sizeof(SndDoubleBuffer)+spec->size); | |
464 if ( audio_buf[i] == NULL ) { | |
465 SDL_OutOfMemory(); | |
466 return(-1); | |
467 } | |
468 audio_buf[i]->dbNumFrames = spec->samples; | |
469 audio_buf[i]->dbFlags = dbBufferReady; | |
470 audio_buf[i]->dbUserInfo[0] = (long)this; | |
471 audio_dbh.dbhBufferPtr[i] = audio_buf[i]; | |
472 } | |
473 | |
474 /* Create the sound manager channel */ | |
475 channel = (SndChannelPtr)malloc(sizeof(*channel)); | |
476 if ( channel == NULL ) { | |
477 SDL_OutOfMemory(); | |
478 return(-1); | |
479 } | |
480 if ( spec->channels >= 2 ) { | |
481 initOptions = initStereo; | |
482 } else { | |
483 initOptions = initMono; | |
484 } | |
485 channel->userInfo = 0; | |
486 channel->qLength = 128; | |
487 if ( SndNewChannel(&channel, sampledSynth, initOptions, 0L) != noErr ) { | |
488 SDL_SetError("Unable to create audio channel"); | |
489 free(channel); | |
490 channel = NULL; | |
491 return(-1); | |
492 } | |
493 | |
494 /* Start playback */ | |
495 if ( SndPlayDoubleBuffer(channel, (SndDoubleBufferHeaderPtr)&audio_dbh) | |
496 != noErr ) { | |
497 SDL_SetError("Unable to play double buffered audio"); | |
498 return(-1); | |
499 } | |
500 | |
501 return 1; | |
502 } | |
503 | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
504 #endif /* TARGET_API_MAC_CARBON || USE_RYANS_SOUNDCODE */ |
0 | 505 |