Mercurial > sdl-ios-xcode
annotate src/audio/macrom/SDL_romaudio.c @ 323:b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
The MacOS audio locking has been implemented, courtesy of Ryan Gordon
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 30 Mar 2002 20:03:27 +0000 |
parents | f6ffac90895c |
children | f25f666d609a |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 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 | |
28 #if TARGET_API_MAC_CARBON | |
29 # include <Carbon.h> | |
30 #else | |
31 # include <Sound.h> /* SoundManager interface */ | |
32 # 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
|
33 # include <DriverServices.h> |
0 | 34 #endif |
35 | |
36 #include <stdlib.h> | |
37 #include <stdio.h> | |
38 | |
39 #include "SDL_endian.h" | |
40 #include "SDL_audio.h" | |
41 #include "SDL_audio_c.h" | |
42 #include "SDL_audiomem.h" | |
43 #include "SDL_sysaudio.h" | |
44 #include "SDL_romaudio.h" | |
45 | |
46 /* Audio driver functions */ | |
47 | |
48 static void Mac_CloseAudio(_THIS); | |
49 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
|
50 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
|
51 static void Mac_UnlockAudio(_THIS); |
0 | 52 |
53 /* Audio driver bootstrap functions */ | |
54 | |
55 | |
56 static int Audio_Available(void) | |
57 { | |
58 return(1); | |
59 } | |
60 | |
61 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
62 { | |
63 free(device->hidden); | |
64 free(device); | |
65 } | |
66 | |
67 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
68 { | |
69 SDL_AudioDevice *this; | |
70 | |
71 /* Initialize all variables that we clean on shutdown */ | |
72 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
73 if ( this ) { | |
74 memset(this, 0, (sizeof *this)); | |
75 this->hidden = (struct SDL_PrivateAudioData *) | |
76 malloc((sizeof *this->hidden)); | |
77 } | |
78 if ( (this == NULL) || (this->hidden == NULL) ) { | |
79 SDL_OutOfMemory(); | |
80 if ( this ) { | |
81 free(this); | |
82 } | |
83 return(0); | |
84 } | |
85 memset(this->hidden, 0, (sizeof *this->hidden)); | |
86 | |
87 /* Set the function pointers */ | |
88 this->OpenAudio = Mac_OpenAudio; | |
89 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
|
90 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
|
91 this->UnlockAudio = Mac_UnlockAudio; |
0 | 92 this->free = Audio_DeleteDevice; |
93 | |
94 return this; | |
95 } | |
96 | |
97 AudioBootStrap SNDMGR_bootstrap = { | |
98 "sndmgr", "MacOS SoundManager 3.0", | |
99 Audio_Available, Audio_CreateDevice | |
100 }; | |
101 | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
102 #if defined(TARGET_API_MAC_CARBON) || defined(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
|
103 /* FIXME: Does this work correctly on MacOS X as well? */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
104 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
105 #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
|
106 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
107 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
|
108 static volatile SInt32 need_to_mix = 0; |
0 | 109 |
110 static UInt8 *buffer[2]; | |
111 static volatile UInt32 running = 0; | |
112 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
|
113 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
|
114 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
115 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
|
116 { |
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 ( ! audio->paused ) { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
118 if ( audio->convert.needed ) { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
119 audio->spec.callback(audio->spec.userdata, |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
120 (Uint8 *)audio->convert.buf,audio->convert.len); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
121 SDL_ConvertAudio(&audio->convert); |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
122 #if 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 if ( audio->convert.len_cvt != 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
|
124 /* Uh oh... probably crashes here; */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
125 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
126 #endif |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
127 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
|
128 } else { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
129 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
|
130 } |
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 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
133 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
|
134 } |
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 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
|
137 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
138 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
|
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 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
141 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
|
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 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
|
144 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
145 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
|
146 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
|
147 return; |
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 /* Did we miss the chance to mix in an interrupt? Do it now. */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
150 if ( BitAndAtomic (0xFFFFFFFF, &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
|
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 * 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
|
153 * 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
|
154 * 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
|
155 */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
156 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
|
157 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
158 } |
0 | 159 |
160 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
|
161 UInt32 play_me; |
0 | 162 SndCommand cmd; |
163 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
|
164 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
165 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
|
166 |
47
45b1c4303f87
Added initial support for Quartz video (thanks Darrell!)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
167 fill_me = cmd_passed->param2; /* buffer that has just finished playing, so fill it */ |
0 | 168 play_me = ! fill_me; /* filled buffer to play _now_ */ |
169 | |
170 if ( ! audio->enabled ) { | |
171 return; | |
172 } | |
173 | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
174 /* queue previously mixed buffer for playback. */ |
0 | 175 header.samplePtr = (Ptr)buffer[play_me]; |
176 cmd.cmd = bufferCmd; | |
177 cmd.param1 = 0; | |
178 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
|
179 SndDoCommand (chan, &cmd, 0); |
47
45b1c4303f87
Added initial support for Quartz video (thanks Darrell!)
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
180 |
0 | 181 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
|
182 |
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 * 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
|
185 * 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
|
186 */ |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
187 if ( ! BitAndAtomic(0xFFFFFFFF, &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
|
188 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
|
189 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
190 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
191 /* 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
|
192 if ( running ) { |
0 | 193 cmd.cmd = callBackCmd; |
194 cmd.param1 = 0; | |
195 cmd.param2 = play_me; | |
196 | |
197 SndDoCommand (chan, &cmd, 0); | |
198 } | |
199 } | |
200 | |
201 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec) { | |
202 | |
203 SndCallBackUPP callback; | |
204 int sample_bits; | |
205 int i; | |
206 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
|
207 |
0 | 208 /* Very few conversions are required, but... */ |
209 switch (spec->format) { | |
210 case AUDIO_S8: | |
211 spec->format = AUDIO_U8; | |
212 break; | |
213 case AUDIO_U16LSB: | |
214 spec->format = AUDIO_S16LSB; | |
215 break; | |
216 case AUDIO_U16MSB: | |
217 spec->format = AUDIO_S16MSB; | |
218 break; | |
219 } | |
220 SDL_CalculateAudioSpec(spec); | |
221 | |
222 /* initialize bufferCmd header */ | |
223 memset (&header, 0, sizeof(header)); | |
224 callback = NewSndCallBackUPP (callBackProc); | |
225 sample_bits = spec->size / spec->samples / spec->channels * 8; | |
226 | |
227 #ifdef DEBUG_AUDIO | |
228 fprintf(stderr, | |
229 "Audio format 0x%x, channels = %d, sample_bits = %d, frequency = %d\n", | |
230 spec->format, spec->channels, sample_bits, spec->freq); | |
231 #endif /* DEBUG_AUDIO */ | |
232 | |
233 header.numChannels = spec->channels; | |
234 header.sampleSize = sample_bits; | |
235 header.sampleRate = spec->freq << 16; | |
236 header.numFrames = spec->samples; | |
237 header.encode = cmpSH; | |
238 | |
239 /* Note that we install the 16bitLittleEndian Converter if needed. */ | |
240 if ( spec->format == 0x8010 ) { | |
241 header.compressionID = fixedCompression; | |
242 header.format = k16BitLittleEndianFormat; | |
243 } | |
244 | |
245 /* allocate 2 buffers */ | |
246 for (i=0; i<2; i++) { | |
247 buffer[i] = (UInt8*)malloc (sizeof(UInt8) * spec->size); | |
248 if (buffer[i] == NULL) { | |
249 SDL_OutOfMemory(); | |
250 return (-1); | |
251 } | |
252 memset (buffer[i], 0, spec->size); | |
253 } | |
254 | |
255 /* Create the sound manager channel */ | |
256 channel = (SndChannelPtr)malloc(sizeof(*channel)); | |
257 if ( channel == NULL ) { | |
258 SDL_OutOfMemory(); | |
259 return(-1); | |
260 } | |
261 if ( spec->channels >= 2 ) { | |
262 initOptions = initStereo; | |
263 } else { | |
264 initOptions = initMono; | |
265 } | |
266 channel->userInfo = (long)this; | |
267 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
|
268 if ( SndNewChannel(&channel, sampledSynth, initOptions, callback) != noErr ) { |
0 | 269 SDL_SetError("Unable to create audio channel"); |
270 free(channel); | |
271 channel = NULL; | |
272 return(-1); | |
273 } | |
274 | |
275 /* start playback */ | |
276 { | |
277 SndCommand cmd; | |
278 cmd.cmd = callBackCmd; | |
279 cmd.param2 = 0; | |
280 running = 1; | |
281 SndDoCommand (channel, &cmd, 0); | |
282 } | |
283 | |
284 return 1; | |
285 } | |
286 | |
287 static void Mac_CloseAudio(_THIS) { | |
288 | |
289 int i; | |
290 | |
291 running = 0; | |
292 | |
293 if (channel) { | |
294 SndDisposeChannel (channel, true); | |
295 channel = NULL; | |
296 } | |
297 | |
298 for ( i=0; i<2; ++i ) { | |
299 if ( buffer[i] ) { | |
300 free(buffer[i]); | |
301 buffer[i] = NULL; | |
302 } | |
303 } | |
304 } | |
305 | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
306 #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
|
307 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
308 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
|
309 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
310 /* 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
|
311 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
312 |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
313 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
|
314 { |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
315 /* 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
|
316 } |
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
317 |
0 | 318 |
319 /* This function is called by Sound Manager when it has exhausted one of | |
320 the buffers, so we'll zero it to silence and fill it with audio if | |
321 we're not paused. | |
322 */ | |
323 static pascal | |
324 void sndDoubleBackProc (SndChannelPtr chan, SndDoubleBufferPtr newbuf) | |
325 { | |
326 SDL_AudioDevice *audio = (SDL_AudioDevice *)newbuf->dbUserInfo[0]; | |
327 | |
328 /* If audio is quitting, don't do anything */ | |
329 if ( ! audio->enabled ) { | |
330 return; | |
331 } | |
332 memset (newbuf->dbSoundData, 0, audio->spec.size); | |
333 newbuf->dbNumFrames = audio->spec.samples; | |
334 if ( ! audio->paused ) { | |
335 if ( audio->convert.needed ) { | |
336 audio->spec.callback(audio->spec.userdata, | |
337 (Uint8 *)audio->convert.buf,audio->convert.len); | |
338 SDL_ConvertAudio(&audio->convert); | |
339 #if 0 | |
340 if ( audio->convert.len_cvt != audio->spec.size ) { | |
341 /* Uh oh... probably crashes here */; | |
342 } | |
343 #endif | |
344 memcpy(newbuf->dbSoundData, audio->convert.buf, | |
345 audio->convert.len_cvt); | |
346 } else { | |
347 audio->spec.callback(audio->spec.userdata, | |
348 (Uint8 *)newbuf->dbSoundData, audio->spec.size); | |
349 } | |
350 } | |
351 newbuf->dbFlags |= dbBufferReady; | |
352 } | |
353 | |
354 static int DoubleBufferAudio_Available(void) | |
355 { | |
356 int available; | |
357 NumVersion sndversion; | |
358 long response; | |
359 | |
360 available = 0; | |
361 sndversion = SndSoundManagerVersion(); | |
362 if ( sndversion.majorRev >= 3 ) { | |
363 if ( Gestalt(gestaltSoundAttr, &response) == noErr ) { | |
364 if ( (response & (1 << gestaltSndPlayDoubleBuffer)) ) { | |
365 available = 1; | |
366 } | |
367 } | |
368 } else { | |
369 if ( Gestalt(gestaltSoundAttr, &response) == noErr ) { | |
370 if ( (response & (1 << gestaltHasASC)) ) { | |
371 available = 1; | |
372 } | |
373 } | |
374 } | |
375 return(available); | |
376 } | |
377 | |
378 static void Mac_CloseAudio(_THIS) | |
379 { | |
380 int i; | |
381 | |
382 if ( channel != NULL ) { | |
383 /* Clean up the audio channel */ | |
384 SndDisposeChannel(channel, true); | |
385 channel = NULL; | |
386 } | |
387 for ( i=0; i<2; ++i ) { | |
388 if ( audio_buf[i] ) { | |
389 free(audio_buf[i]); | |
390 audio_buf[i] = NULL; | |
391 } | |
392 } | |
393 } | |
394 | |
395 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
396 { | |
397 SndDoubleBufferHeader2 audio_dbh; | |
398 int i; | |
399 long initOptions; | |
400 int sample_bits; | |
401 SndDoubleBackUPP doubleBackProc; | |
402 | |
403 /* Check to make sure double-buffered audio is available */ | |
404 if ( ! DoubleBufferAudio_Available() ) { | |
405 SDL_SetError("Sound manager doesn't support double-buffering"); | |
406 return(-1); | |
407 } | |
408 | |
409 /* Very few conversions are required, but... */ | |
410 switch (spec->format) { | |
411 case AUDIO_S8: | |
412 spec->format = AUDIO_U8; | |
413 break; | |
414 case AUDIO_U16LSB: | |
415 spec->format = AUDIO_S16LSB; | |
416 break; | |
417 case AUDIO_U16MSB: | |
418 spec->format = AUDIO_S16MSB; | |
419 break; | |
420 } | |
421 SDL_CalculateAudioSpec(spec); | |
422 | |
423 /* initialize the double-back header */ | |
424 memset(&audio_dbh, 0, sizeof(audio_dbh)); | |
425 doubleBackProc = NewSndDoubleBackProc (sndDoubleBackProc); | |
426 sample_bits = spec->size / spec->samples / spec->channels * 8; | |
427 | |
428 audio_dbh.dbhNumChannels = spec->channels; | |
429 audio_dbh.dbhSampleSize = sample_bits; | |
430 audio_dbh.dbhCompressionID = 0; | |
431 audio_dbh.dbhPacketSize = 0; | |
432 audio_dbh.dbhSampleRate = spec->freq << 16; | |
433 audio_dbh.dbhDoubleBack = doubleBackProc; | |
434 audio_dbh.dbhFormat = 0; | |
435 | |
436 /* Note that we install the 16bitLittleEndian Converter if needed. */ | |
437 if ( spec->format == 0x8010 ) { | |
438 audio_dbh.dbhCompressionID = fixedCompression; | |
439 audio_dbh.dbhFormat = k16BitLittleEndianFormat; | |
440 } | |
441 | |
442 /* allocate the 2 double-back buffers */ | |
443 for ( i=0; i<2; ++i ) { | |
444 audio_buf[i] = calloc(1, sizeof(SndDoubleBuffer)+spec->size); | |
445 if ( audio_buf[i] == NULL ) { | |
446 SDL_OutOfMemory(); | |
447 return(-1); | |
448 } | |
449 audio_buf[i]->dbNumFrames = spec->samples; | |
450 audio_buf[i]->dbFlags = dbBufferReady; | |
451 audio_buf[i]->dbUserInfo[0] = (long)this; | |
452 audio_dbh.dbhBufferPtr[i] = audio_buf[i]; | |
453 } | |
454 | |
455 /* Create the sound manager channel */ | |
456 channel = (SndChannelPtr)malloc(sizeof(*channel)); | |
457 if ( channel == NULL ) { | |
458 SDL_OutOfMemory(); | |
459 return(-1); | |
460 } | |
461 if ( spec->channels >= 2 ) { | |
462 initOptions = initStereo; | |
463 } else { | |
464 initOptions = initMono; | |
465 } | |
466 channel->userInfo = 0; | |
467 channel->qLength = 128; | |
468 if ( SndNewChannel(&channel, sampledSynth, initOptions, 0L) != noErr ) { | |
469 SDL_SetError("Unable to create audio channel"); | |
470 free(channel); | |
471 channel = NULL; | |
472 return(-1); | |
473 } | |
474 | |
475 /* Start playback */ | |
476 if ( SndPlayDoubleBuffer(channel, (SndDoubleBufferHeaderPtr)&audio_dbh) | |
477 != noErr ) { | |
478 SDL_SetError("Unable to play double buffered audio"); | |
479 return(-1); | |
480 } | |
481 | |
482 return 1; | |
483 } | |
484 | |
323
b7e8038e40ae
The audio lock and unlock functions are now a part of the driver.
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
485 #endif /* TARGET_API_MAC_CARBON || USE_RYANS_SOUNDCODE */ |
0 | 486 |