comparison src/audio/macrom/SDL_romaudio.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents f12379c41042
children c27292a690b7
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
24 #if defined(__APPLE__) && defined(__MACH__) 24 #if defined(__APPLE__) && defined(__MACH__)
25 # include <Carbon/Carbon.h> 25 # include <Carbon/Carbon.h>
26 #elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335) 26 #elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
27 # include <Carbon.h> 27 # include <Carbon.h>
28 #else 28 #else
29 # include <Sound.h> /* SoundManager interface */ 29 # include <Sound.h> /* SoundManager interface */
30 # include <Gestalt.h> 30 # include <Gestalt.h>
31 # include <DriverServices.h> 31 # include <DriverServices.h>
32 #endif 32 #endif
33 33
34 #if !defined(NewSndCallBackUPP) && (UNIVERSAL_INTERFACES_VERSION < 0x0335) 34 #if !defined(NewSndCallBackUPP) && (UNIVERSAL_INTERFACES_VERSION < 0x0335)
35 #if !defined(NewSndCallBackProc) /* avoid circular redefinition... */ 35 #if !defined(NewSndCallBackProc) /* avoid circular redefinition... */
36 #define NewSndCallBackUPP NewSndCallBackProc 36 #define NewSndCallBackUPP NewSndCallBackProc
37 #endif 37 #endif
38 #if !defined(NewSndCallBackUPP) 38 #if !defined(NewSndCallBackUPP)
39 #define NewSndCallBackUPP NewSndCallBackProc 39 #define NewSndCallBackUPP NewSndCallBackProc
40 #endif 40 #endif
46 #include "SDL_romaudio.h" 46 #include "SDL_romaudio.h"
47 47
48 /* Audio driver functions */ 48 /* Audio driver functions */
49 49
50 static void Mac_CloseAudio(_THIS); 50 static void Mac_CloseAudio(_THIS);
51 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec); 51 static int Mac_OpenAudio(_THIS, SDL_AudioSpec * spec);
52 static void Mac_LockAudio(_THIS); 52 static void Mac_LockAudio(_THIS);
53 static void Mac_UnlockAudio(_THIS); 53 static void Mac_UnlockAudio(_THIS);
54 54
55 /* Audio driver bootstrap functions */ 55 /* Audio driver bootstrap functions */
56 56
57 57
58 static int Audio_Available(void) 58 static int
59 { 59 Audio_Available(void)
60 return(1); 60 {
61 } 61 return (1);
62 62 }
63 static void Audio_DeleteDevice(SDL_AudioDevice *device) 63
64 static void
65 Audio_DeleteDevice(SDL_AudioDevice * device)
64 { 66 {
65 SDL_free(device->hidden); 67 SDL_free(device->hidden);
66 SDL_free(device); 68 SDL_free(device);
67 } 69 }
68 70
69 static SDL_AudioDevice *Audio_CreateDevice(int devindex) 71 static SDL_AudioDevice *
72 Audio_CreateDevice(int devindex)
70 { 73 {
71 SDL_AudioDevice *this; 74 SDL_AudioDevice *this;
72 75
73 /* Initialize all variables that we clean on shutdown */ 76 /* Initialize all variables that we clean on shutdown */
74 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice)); 77 this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
75 if ( this ) { 78 if (this) {
76 SDL_memset(this, 0, (sizeof *this)); 79 SDL_memset(this, 0, (sizeof *this));
77 this->hidden = (struct SDL_PrivateAudioData *) 80 this->hidden = (struct SDL_PrivateAudioData *)
78 SDL_malloc((sizeof *this->hidden)); 81 SDL_malloc((sizeof *this->hidden));
79 } 82 }
80 if ( (this == NULL) || (this->hidden == NULL) ) { 83 if ((this == NULL) || (this->hidden == NULL)) {
81 SDL_OutOfMemory(); 84 SDL_OutOfMemory();
82 if ( this ) { 85 if (this) {
83 SDL_free(this); 86 SDL_free(this);
84 } 87 }
85 return(0); 88 return (0);
86 } 89 }
87 SDL_memset(this->hidden, 0, (sizeof *this->hidden)); 90 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
88 91
89 /* Set the function pointers */ 92 /* Set the function pointers */
90 this->OpenAudio = Mac_OpenAudio; 93 this->OpenAudio = Mac_OpenAudio;
91 this->CloseAudio = Mac_CloseAudio; 94 this->CloseAudio = Mac_CloseAudio;
92 this->LockAudio = Mac_LockAudio; 95 this->LockAudio = Mac_LockAudio;
93 this->UnlockAudio = Mac_UnlockAudio; 96 this->UnlockAudio = Mac_UnlockAudio;
94 this->free = Audio_DeleteDevice; 97 this->free = Audio_DeleteDevice;
95 98
96 #ifdef __MACOSX__ /* Mac OS X uses threaded audio, so normal thread code is okay */ 99 #ifdef __MACOSX__ /* Mac OS X uses threaded audio, so normal thread code is okay */
97 this->LockAudio = NULL; 100 this->LockAudio = NULL;
98 this->UnlockAudio = NULL; 101 this->UnlockAudio = NULL;
99 #endif 102 #endif
100 return this; 103 return this;
101 } 104 }
102 105
103 AudioBootStrap SNDMGR_bootstrap = { 106 AudioBootStrap SNDMGR_bootstrap = {
104 "sndmgr", "MacOS SoundManager 3.0", 107 "sndmgr", "MacOS SoundManager 3.0",
105 Audio_Available, Audio_CreateDevice 108 Audio_Available, Audio_CreateDevice
106 }; 109 };
107 110
108 #if defined(TARGET_API_MAC_CARBON) || defined(USE_RYANS_SOUNDCODE) 111 #if defined(TARGET_API_MAC_CARBON) || defined(USE_RYANS_SOUNDCODE)
109 /* This works correctly on Mac OS X */ 112 /* This works correctly on Mac OS X */
110 113
111 #pragma options align=power 114 #pragma options align=power
112 115
113 static volatile SInt32 audio_is_locked = 0; 116 static volatile SInt32 audio_is_locked = 0;
114 static volatile SInt32 need_to_mix = 0; 117 static volatile SInt32 need_to_mix = 0;
115 118
116 static UInt8 *buffer[2]; 119 static UInt8 *buffer[2];
117 static volatile UInt32 running = 0; 120 static volatile UInt32 running = 0;
118 static CmpSoundHeader header; 121 static CmpSoundHeader header;
119 static volatile Uint32 fill_me = 0; 122 static volatile Uint32 fill_me = 0;
120 123
121 static void mix_buffer(SDL_AudioDevice *audio, UInt8 *buffer) 124 static void
122 { 125 mix_buffer(SDL_AudioDevice * audio, UInt8 * buffer)
123 if ( ! audio->paused ) { 126 {
127 if (!audio->paused) {
124 #ifdef __MACOSX__ 128 #ifdef __MACOSX__
125 SDL_mutexP(audio->mixer_lock); 129 SDL_mutexP(audio->mixer_lock);
126 #endif 130 #endif
127 if ( audio->convert.needed ) { 131 if (audio->convert.needed) {
128 audio->spec.callback(audio->spec.userdata, 132 audio->spec.callback(audio->spec.userdata,
129 (Uint8 *)audio->convert.buf,audio->convert.len); 133 (Uint8 *) audio->convert.buf,
134 audio->convert.len);
130 SDL_ConvertAudio(&audio->convert); 135 SDL_ConvertAudio(&audio->convert);
131 if ( audio->convert.len_cvt != audio->spec.size ) { 136 if (audio->convert.len_cvt != audio->spec.size) {
132 /* Uh oh... probably crashes here */; 137 /* Uh oh... probably crashes here */ ;
133 } 138 }
134 SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt); 139 SDL_memcpy(buffer, audio->convert.buf, audio->convert.len_cvt);
135 } else { 140 } else {
136 audio->spec.callback(audio->spec.userdata, buffer, audio->spec.size); 141 audio->spec.callback(audio->spec.userdata, buffer,
142 audio->spec.size);
137 } 143 }
138 #ifdef __MACOSX__ 144 #ifdef __MACOSX__
139 SDL_mutexV(audio->mixer_lock); 145 SDL_mutexV(audio->mixer_lock);
140 #endif 146 #endif
141 } 147 }
142 148
143 DecrementAtomic((SInt32 *) &need_to_mix); 149 DecrementAtomic((SInt32 *) & need_to_mix);
144 } 150 }
145 151
146 static void Mac_LockAudio(_THIS) 152 static void
147 { 153 Mac_LockAudio(_THIS)
148 IncrementAtomic((SInt32 *) &audio_is_locked); 154 {
149 } 155 IncrementAtomic((SInt32 *) & audio_is_locked);
150 156 }
151 static void Mac_UnlockAudio(_THIS) 157
158 static void
159 Mac_UnlockAudio(_THIS)
152 { 160 {
153 SInt32 oldval; 161 SInt32 oldval;
154 162
155 oldval = DecrementAtomic((SInt32 *) &audio_is_locked); 163 oldval = DecrementAtomic((SInt32 *) & audio_is_locked);
156 if ( oldval != 1 ) /* != 1 means audio is still locked. */ 164 if (oldval != 1) /* != 1 means audio is still locked. */
157 return; 165 return;
158 166
159 /* Did we miss the chance to mix in an interrupt? Do it now. */ 167 /* Did we miss the chance to mix in an interrupt? Do it now. */
160 if ( BitAndAtomic (0xFFFFFFFF, (UInt32 *) &need_to_mix) ) { 168 if (BitAndAtomic(0xFFFFFFFF, (UInt32 *) & need_to_mix)) {
161 /* 169 /*
162 * Note that this could be a problem if you missed an interrupt 170 * Note that this could be a problem if you missed an interrupt
163 * while the audio was locked, and get preempted by a second 171 * while the audio was locked, and get preempted by a second
164 * interrupt here, but that means you locked for way too long anyhow. 172 * interrupt here, but that means you locked for way too long anyhow.
165 */ 173 */
166 mix_buffer (this, buffer[fill_me]); 174 mix_buffer(this, buffer[fill_me]);
167 } 175 }
168 } 176 }
169 177
170 static void callBackProc (SndChannel *chan, SndCommand *cmd_passed ) { 178 static void
171 UInt32 play_me; 179 callBackProc(SndChannel * chan, SndCommand * cmd_passed)
172 SndCommand cmd; 180 {
173 SDL_AudioDevice *audio = (SDL_AudioDevice *)chan->userInfo; 181 UInt32 play_me;
174 182 SndCommand cmd;
175 IncrementAtomic((SInt32 *) &need_to_mix); 183 SDL_AudioDevice *audio = (SDL_AudioDevice *) chan->userInfo;
176 184
177 fill_me = cmd_passed->param2; /* buffer that has just finished playing, so fill it */ 185 IncrementAtomic((SInt32 *) & need_to_mix);
178 play_me = ! fill_me; /* filled buffer to play _now_ */ 186
179 187 fill_me = cmd_passed->param2; /* buffer that has just finished playing, so fill it */
180 if ( ! audio->enabled ) { 188 play_me = !fill_me; /* filled buffer to play _now_ */
181 return; 189
182 } 190 if (!audio->enabled) {
183 191 return;
184 /* queue previously mixed buffer for playback. */ 192 }
185 header.samplePtr = (Ptr)buffer[play_me]; 193
186 cmd.cmd = bufferCmd; 194 /* queue previously mixed buffer for playback. */
187 cmd.param1 = 0; 195 header.samplePtr = (Ptr) buffer[play_me];
188 cmd.param2 = (long)&header; 196 cmd.cmd = bufferCmd;
189 SndDoCommand (chan, &cmd, 0); 197 cmd.param1 = 0;
190 198 cmd.param2 = (long) &header;
191 memset (buffer[fill_me], 0, audio->spec.size); 199 SndDoCommand(chan, &cmd, 0);
192 200
193 /* 201 memset(buffer[fill_me], 0, audio->spec.size);
194 * if audio device isn't locked, mix the next buffer to be queued in 202
195 * the memory block that just finished playing. 203 /*
196 */ 204 * if audio device isn't locked, mix the next buffer to be queued in
197 if ( ! BitAndAtomic(0xFFFFFFFF, (UInt32 *) &audio_is_locked) ) { 205 * the memory block that just finished playing.
198 mix_buffer (audio, buffer[fill_me]); 206 */
199 } 207 if (!BitAndAtomic(0xFFFFFFFF, (UInt32 *) & audio_is_locked)) {
200 208 mix_buffer(audio, buffer[fill_me]);
201 /* set this callback to run again when current buffer drains. */ 209 }
202 if ( running ) { 210
203 cmd.cmd = callBackCmd; 211 /* set this callback to run again when current buffer drains. */
204 cmd.param1 = 0; 212 if (running) {
205 cmd.param2 = play_me; 213 cmd.cmd = callBackCmd;
206 214 cmd.param1 = 0;
207 SndDoCommand (chan, &cmd, 0); 215 cmd.param2 = play_me;
208 } 216
209 } 217 SndDoCommand(chan, &cmd, 0);
210 218 }
211 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec) { 219 }
212 220
213 SndCallBackUPP callback; 221 static int
214 int sample_bits; 222 Mac_OpenAudio(_THIS, SDL_AudioSpec * spec)
215 int i; 223 {
216 long initOptions; 224
217 225 SndCallBackUPP callback;
218 /* Very few conversions are required, but... */ 226 int sample_bits;
227 int i;
228 long initOptions;
229
230 /* Very few conversions are required, but... */
219 switch (spec->format) { 231 switch (spec->format) {
220 case AUDIO_S8: 232 case AUDIO_S8:
221 spec->format = AUDIO_U8; 233 spec->format = AUDIO_U8;
222 break; 234 break;
223 case AUDIO_U16LSB: 235 case AUDIO_U16LSB:
224 spec->format = AUDIO_S16LSB; 236 spec->format = AUDIO_S16LSB;
225 break; 237 break;
226 case AUDIO_U16MSB: 238 case AUDIO_U16MSB:
227 spec->format = AUDIO_S16MSB; 239 spec->format = AUDIO_S16MSB;
228 break; 240 break;
229 } 241 }
230 SDL_CalculateAudioSpec(spec); 242 SDL_CalculateAudioSpec(spec);
231 243
232 /* initialize bufferCmd header */ 244 /* initialize bufferCmd header */
233 memset (&header, 0, sizeof(header)); 245 memset(&header, 0, sizeof(header));
234 callback = (SndCallBackUPP) NewSndCallBackUPP (callBackProc); 246 callback = (SndCallBackUPP) NewSndCallBackUPP(callBackProc);
235 sample_bits = spec->size / spec->samples / spec->channels * 8; 247 sample_bits = spec->size / spec->samples / spec->channels * 8;
236 248
237 #ifdef DEBUG_AUDIO 249 #ifdef DEBUG_AUDIO
238 fprintf(stderr, 250 fprintf(stderr,
239 "Audio format 0x%x, channels = %d, sample_bits = %d, frequency = %d\n", 251 "Audio format 0x%x, channels = %d, sample_bits = %d, frequency = %d\n",
240 spec->format, spec->channels, sample_bits, spec->freq); 252 spec->format, spec->channels, sample_bits, spec->freq);
241 #endif /* DEBUG_AUDIO */ 253 #endif /* DEBUG_AUDIO */
242 254
243 header.numChannels = spec->channels; 255 header.numChannels = spec->channels;
244 header.sampleSize = sample_bits; 256 header.sampleSize = sample_bits;
245 header.sampleRate = spec->freq << 16; 257 header.sampleRate = spec->freq << 16;
246 header.numFrames = spec->samples; 258 header.numFrames = spec->samples;
247 header.encode = cmpSH; 259 header.encode = cmpSH;
248 260
249 /* Note that we install the 16bitLittleEndian Converter if needed. */ 261 /* Note that we install the 16bitLittleEndian Converter if needed. */
250 if ( spec->format == 0x8010 ) { 262 if (spec->format == 0x8010) {
251 header.compressionID = fixedCompression; 263 header.compressionID = fixedCompression;
252 header.format = k16BitLittleEndianFormat; 264 header.format = k16BitLittleEndianFormat;
253 } 265 }
254 266
255 /* allocate 2 buffers */ 267 /* allocate 2 buffers */
256 for (i=0; i<2; i++) { 268 for (i = 0; i < 2; i++) {
257 buffer[i] = (UInt8*)malloc (sizeof(UInt8) * spec->size); 269 buffer[i] = (UInt8 *) malloc(sizeof(UInt8) * spec->size);
258 if (buffer[i] == NULL) { 270 if (buffer[i] == NULL) {
259 SDL_OutOfMemory(); 271 SDL_OutOfMemory();
260 return (-1); 272 return (-1);
261 } 273 }
262 memset (buffer[i], 0, spec->size); 274 memset(buffer[i], 0, spec->size);
263 } 275 }
264 276
265 /* Create the sound manager channel */ 277 /* Create the sound manager channel */
266 channel = (SndChannelPtr)SDL_malloc(sizeof(*channel)); 278 channel = (SndChannelPtr) SDL_malloc(sizeof(*channel));
267 if ( channel == NULL ) { 279 if (channel == NULL) {
268 SDL_OutOfMemory(); 280 SDL_OutOfMemory();
269 return(-1); 281 return (-1);
270 } 282 }
271 if ( spec->channels >= 2 ) { 283 if (spec->channels >= 2) {
272 initOptions = initStereo; 284 initOptions = initStereo;
273 } else { 285 } else {
274 initOptions = initMono; 286 initOptions = initMono;
275 } 287 }
276 channel->userInfo = (long)this; 288 channel->userInfo = (long) this;
277 channel->qLength = 128; 289 channel->qLength = 128;
278 if ( SndNewChannel(&channel, sampledSynth, initOptions, callback) != noErr ) { 290 if (SndNewChannel(&channel, sampledSynth, initOptions, callback) != noErr) {
279 SDL_SetError("Unable to create audio channel"); 291 SDL_SetError("Unable to create audio channel");
280 SDL_free(channel); 292 SDL_free(channel);
281 channel = NULL; 293 channel = NULL;
282 return(-1); 294 return (-1);
283 } 295 }
284 296
285 /* start playback */ 297 /* start playback */
286 { 298 {
287 SndCommand cmd; 299 SndCommand cmd;
288 cmd.cmd = callBackCmd; 300 cmd.cmd = callBackCmd;
289 cmd.param2 = 0; 301 cmd.param2 = 0;
290 running = 1; 302 running = 1;
291 SndDoCommand (channel, &cmd, 0); 303 SndDoCommand(channel, &cmd, 0);
292 } 304 }
293 305
294 return 1; 306 return 1;
295 } 307 }
296 308
297 static void Mac_CloseAudio(_THIS) { 309 static void
298 310 Mac_CloseAudio(_THIS)
299 int i; 311 {
300 312
301 running = 0; 313 int i;
302 314
303 if (channel) { 315 running = 0;
304 SndDisposeChannel (channel, true); 316
305 channel = NULL; 317 if (channel) {
306 } 318 SndDisposeChannel(channel, true);
307 319 channel = NULL;
308 for ( i=0; i<2; ++i ) { 320 }
309 if ( buffer[i] ) { 321
322 for (i = 0; i < 2; ++i) {
323 if (buffer[i]) {
310 SDL_free(buffer[i]); 324 SDL_free(buffer[i]);
311 buffer[i] = NULL; 325 buffer[i] = NULL;
312 } 326 }
313 } 327 }
314 } 328 }
315 329
316 #else /* !TARGET_API_MAC_CARBON && !USE_RYANS_SOUNDCODE */ 330 #else /* !TARGET_API_MAC_CARBON && !USE_RYANS_SOUNDCODE */
317 331
318 static void Mac_LockAudio(_THIS) 332 static void
333 Mac_LockAudio(_THIS)
319 { 334 {
320 /* no-op. */ 335 /* no-op. */
321 } 336 }
322 337
323 static void Mac_UnlockAudio(_THIS) 338 static void
339 Mac_UnlockAudio(_THIS)
324 { 340 {
325 /* no-op. */ 341 /* no-op. */
326 } 342 }
327 343
328 344
329 /* This function is called by Sound Manager when it has exhausted one of 345 /* This function is called by Sound Manager when it has exhausted one of
330 the buffers, so we'll zero it to silence and fill it with audio if 346 the buffers, so we'll zero it to silence and fill it with audio if
331 we're not paused. 347 we're not paused.
332 */ 348 */
333 static pascal 349 static pascal void
334 void sndDoubleBackProc (SndChannelPtr chan, SndDoubleBufferPtr newbuf) 350 sndDoubleBackProc(SndChannelPtr chan, SndDoubleBufferPtr newbuf)
335 { 351 {
336 SDL_AudioDevice *audio = (SDL_AudioDevice *)newbuf->dbUserInfo[0]; 352 SDL_AudioDevice *audio = (SDL_AudioDevice *) newbuf->dbUserInfo[0];
337 353
338 /* If audio is quitting, don't do anything */ 354 /* If audio is quitting, don't do anything */
339 if ( ! audio->enabled ) { 355 if (!audio->enabled) {
340 return; 356 return;
341 } 357 }
342 memset (newbuf->dbSoundData, 0, audio->spec.size); 358 memset(newbuf->dbSoundData, 0, audio->spec.size);
343 newbuf->dbNumFrames = audio->spec.samples; 359 newbuf->dbNumFrames = audio->spec.samples;
344 if ( ! audio->paused ) { 360 if (!audio->paused) {
345 if ( audio->convert.needed ) { 361 if (audio->convert.needed) {
346 audio->spec.callback(audio->spec.userdata, 362 audio->spec.callback(audio->spec.userdata,
347 (Uint8 *)audio->convert.buf,audio->convert.len); 363 (Uint8 *) audio->convert.buf,
364 audio->convert.len);
348 SDL_ConvertAudio(&audio->convert); 365 SDL_ConvertAudio(&audio->convert);
349 #if 0 366 #if 0
350 if ( audio->convert.len_cvt != audio->spec.size ) { 367 if (audio->convert.len_cvt != audio->spec.size) {
351 /* Uh oh... probably crashes here */; 368 /* Uh oh... probably crashes here */ ;
352 } 369 }
353 #endif 370 #endif
354 SDL_memcpy(newbuf->dbSoundData, audio->convert.buf, 371 SDL_memcpy(newbuf->dbSoundData, audio->convert.buf,
355 audio->convert.len_cvt); 372 audio->convert.len_cvt);
356 } else { 373 } else {
357 audio->spec.callback(audio->spec.userdata, 374 audio->spec.callback(audio->spec.userdata,
358 (Uint8 *)newbuf->dbSoundData, audio->spec.size); 375 (Uint8 *) newbuf->dbSoundData,
359 } 376 audio->spec.size);
360 } 377 }
361 newbuf->dbFlags |= dbBufferReady; 378 }
362 } 379 newbuf->dbFlags |= dbBufferReady;
363 380 }
364 static int DoubleBufferAudio_Available(void) 381
382 static int
383 DoubleBufferAudio_Available(void)
365 { 384 {
366 int available; 385 int available;
367 NumVersion sndversion; 386 NumVersion sndversion;
368 long response; 387 long response;
369 388
370 available = 0; 389 available = 0;
371 sndversion = SndSoundManagerVersion(); 390 sndversion = SndSoundManagerVersion();
372 if ( sndversion.majorRev >= 3 ) { 391 if (sndversion.majorRev >= 3) {
373 if ( Gestalt(gestaltSoundAttr, &response) == noErr ) { 392 if (Gestalt(gestaltSoundAttr, &response) == noErr) {
374 if ( (response & (1 << gestaltSndPlayDoubleBuffer)) ) { 393 if ((response & (1 << gestaltSndPlayDoubleBuffer))) {
375 available = 1; 394 available = 1;
376 } 395 }
377 } 396 }
378 } else { 397 } else {
379 if ( Gestalt(gestaltSoundAttr, &response) == noErr ) { 398 if (Gestalt(gestaltSoundAttr, &response) == noErr) {
380 if ( (response & (1 << gestaltHasASC)) ) { 399 if ((response & (1 << gestaltHasASC))) {
381 available = 1; 400 available = 1;
382 } 401 }
383 } 402 }
384 } 403 }
385 return(available); 404 return (available);
386 } 405 }
387 406
388 static void Mac_CloseAudio(_THIS) 407 static void
408 Mac_CloseAudio(_THIS)
389 { 409 {
390 int i; 410 int i;
391 411
392 if ( channel != NULL ) { 412 if (channel != NULL) {
393 /* Clean up the audio channel */ 413 /* Clean up the audio channel */
394 SndDisposeChannel(channel, true); 414 SndDisposeChannel(channel, true);
395 channel = NULL; 415 channel = NULL;
396 } 416 }
397 for ( i=0; i<2; ++i ) { 417 for (i = 0; i < 2; ++i) {
398 if ( audio_buf[i] ) { 418 if (audio_buf[i]) {
399 SDL_free(audio_buf[i]); 419 SDL_free(audio_buf[i]);
400 audio_buf[i] = NULL; 420 audio_buf[i] = NULL;
401 } 421 }
402 } 422 }
403 } 423 }
404 424
405 static int Mac_OpenAudio(_THIS, SDL_AudioSpec *spec) 425 static int
426 Mac_OpenAudio(_THIS, SDL_AudioSpec * spec)
406 { 427 {
407 SndDoubleBufferHeader2 audio_dbh; 428 SndDoubleBufferHeader2 audio_dbh;
408 int i; 429 int i;
409 long initOptions; 430 long initOptions;
410 int sample_bits; 431 int sample_bits;
411 SndDoubleBackUPP doubleBackProc; 432 SndDoubleBackUPP doubleBackProc;
412 433
413 /* Check to make sure double-buffered audio is available */ 434 /* Check to make sure double-buffered audio is available */
414 if ( ! DoubleBufferAudio_Available() ) { 435 if (!DoubleBufferAudio_Available()) {
415 SDL_SetError("Sound manager doesn't support double-buffering"); 436 SDL_SetError("Sound manager doesn't support double-buffering");
416 return(-1); 437 return (-1);
417 } 438 }
418 439
419 /* Very few conversions are required, but... */ 440 /* Very few conversions are required, but... */
420 switch (spec->format) { 441 switch (spec->format) {
421 case AUDIO_S8: 442 case AUDIO_S8:
422 spec->format = AUDIO_U8; 443 spec->format = AUDIO_U8;
423 break; 444 break;
424 case AUDIO_U16LSB: 445 case AUDIO_U16LSB:
425 spec->format = AUDIO_S16LSB; 446 spec->format = AUDIO_S16LSB;
426 break; 447 break;
427 case AUDIO_U16MSB: 448 case AUDIO_U16MSB:
428 spec->format = AUDIO_S16MSB; 449 spec->format = AUDIO_S16MSB;
429 break; 450 break;
430 } 451 }
431 SDL_CalculateAudioSpec(spec); 452 SDL_CalculateAudioSpec(spec);
432 453
433 /* initialize the double-back header */ 454 /* initialize the double-back header */
434 SDL_memset(&audio_dbh, 0, sizeof(audio_dbh)); 455 SDL_memset(&audio_dbh, 0, sizeof(audio_dbh));
435 doubleBackProc = NewSndDoubleBackProc (sndDoubleBackProc); 456 doubleBackProc = NewSndDoubleBackProc(sndDoubleBackProc);
436 sample_bits = spec->size / spec->samples / spec->channels * 8; 457 sample_bits = spec->size / spec->samples / spec->channels * 8;
437 458
438 audio_dbh.dbhNumChannels = spec->channels; 459 audio_dbh.dbhNumChannels = spec->channels;
439 audio_dbh.dbhSampleSize = sample_bits; 460 audio_dbh.dbhSampleSize = sample_bits;
440 audio_dbh.dbhCompressionID = 0; 461 audio_dbh.dbhCompressionID = 0;
441 audio_dbh.dbhPacketSize = 0; 462 audio_dbh.dbhPacketSize = 0;
442 audio_dbh.dbhSampleRate = spec->freq << 16; 463 audio_dbh.dbhSampleRate = spec->freq << 16;
443 audio_dbh.dbhDoubleBack = doubleBackProc; 464 audio_dbh.dbhDoubleBack = doubleBackProc;
444 audio_dbh.dbhFormat = 0; 465 audio_dbh.dbhFormat = 0;
445 466
446 /* Note that we install the 16bitLittleEndian Converter if needed. */ 467 /* Note that we install the 16bitLittleEndian Converter if needed. */
447 if ( spec->format == 0x8010 ) { 468 if (spec->format == 0x8010) {
448 audio_dbh.dbhCompressionID = fixedCompression; 469 audio_dbh.dbhCompressionID = fixedCompression;
449 audio_dbh.dbhFormat = k16BitLittleEndianFormat; 470 audio_dbh.dbhFormat = k16BitLittleEndianFormat;
450 } 471 }
451 472
452 /* allocate the 2 double-back buffers */ 473 /* allocate the 2 double-back buffers */
453 for ( i=0; i<2; ++i ) { 474 for (i = 0; i < 2; ++i) {
454 audio_buf[i] = SDL_calloc(1, sizeof(SndDoubleBuffer)+spec->size); 475 audio_buf[i] = SDL_calloc(1, sizeof(SndDoubleBuffer) + spec->size);
455 if ( audio_buf[i] == NULL ) { 476 if (audio_buf[i] == NULL) {
456 SDL_OutOfMemory(); 477 SDL_OutOfMemory();
457 return(-1); 478 return (-1);
458 } 479 }
459 audio_buf[i]->dbNumFrames = spec->samples; 480 audio_buf[i]->dbNumFrames = spec->samples;
460 audio_buf[i]->dbFlags = dbBufferReady; 481 audio_buf[i]->dbFlags = dbBufferReady;
461 audio_buf[i]->dbUserInfo[0] = (long)this; 482 audio_buf[i]->dbUserInfo[0] = (long) this;
462 audio_dbh.dbhBufferPtr[i] = audio_buf[i]; 483 audio_dbh.dbhBufferPtr[i] = audio_buf[i];
463 } 484 }
464 485
465 /* Create the sound manager channel */ 486 /* Create the sound manager channel */
466 channel = (SndChannelPtr)SDL_malloc(sizeof(*channel)); 487 channel = (SndChannelPtr) SDL_malloc(sizeof(*channel));
467 if ( channel == NULL ) { 488 if (channel == NULL) {
468 SDL_OutOfMemory(); 489 SDL_OutOfMemory();
469 return(-1); 490 return (-1);
470 } 491 }
471 if ( spec->channels >= 2 ) { 492 if (spec->channels >= 2) {
472 initOptions = initStereo; 493 initOptions = initStereo;
473 } else { 494 } else {
474 initOptions = initMono; 495 initOptions = initMono;
475 } 496 }
476 channel->userInfo = 0; 497 channel->userInfo = 0;
477 channel->qLength = 128; 498 channel->qLength = 128;
478 if ( SndNewChannel(&channel, sampledSynth, initOptions, 0L) != noErr ) { 499 if (SndNewChannel(&channel, sampledSynth, initOptions, 0L) != noErr) {
479 SDL_SetError("Unable to create audio channel"); 500 SDL_SetError("Unable to create audio channel");
480 SDL_free(channel); 501 SDL_free(channel);
481 channel = NULL; 502 channel = NULL;
482 return(-1); 503 return (-1);
483 } 504 }
484 505
485 /* Start playback */ 506 /* Start playback */
486 if ( SndPlayDoubleBuffer(channel, (SndDoubleBufferHeaderPtr)&audio_dbh) 507 if (SndPlayDoubleBuffer(channel, (SndDoubleBufferHeaderPtr) & audio_dbh)
487 != noErr ) { 508 != noErr) {
488 SDL_SetError("Unable to play double buffered audio"); 509 SDL_SetError("Unable to play double buffered audio");
489 return(-1); 510 return (-1);
490 } 511 }
491 512
492 return 1; 513 return 1;
493 } 514 }
494 515
495 #endif /* TARGET_API_MAC_CARBON || USE_RYANS_SOUNDCODE */ 516 #endif /* TARGET_API_MAC_CARBON || USE_RYANS_SOUNDCODE */
496 517 /* vi: set ts=4 sw=4 expandtab: */