Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
517 lines (443 loc) · 13.8 KB

SDL_romaudio.c

File metadata and controls

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