Skip to content

Latest commit

 

History

History
496 lines (421 loc) · 13.7 KB

SDL_romaudio.c

File metadata and controls

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