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

Latest commit

 

History

History
803 lines (726 loc) · 20.5 KB

SDL_audio.c

File metadata and controls

803 lines (726 loc) · 20.5 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
24
25
26
27
28
29
30
/* Allow access to a raw mixing buffer */
#include "SDL.h"
#include "SDL_audio_c.h"
#include "SDL_audiomem.h"
#include "SDL_sysaudio.h"
Nov 23, 2005
Nov 23, 2005
31
#ifdef __OS2__
Mar 9, 2006
Mar 9, 2006
32
/* We'll need the DosSetPriority() API! */
Nov 23, 2005
Nov 23, 2005
33
34
35
36
#define INCL_DOSPROCESS
#include <os2.h>
#endif
Apr 26, 2001
Apr 26, 2001
37
38
/* Available audio drivers */
static AudioBootStrap *bootstrap[] = {
Mar 21, 2006
Mar 21, 2006
39
#if SDL_AUDIO_DRIVER_BSD
May 28, 2006
May 28, 2006
40
&BSD_AUDIO_bootstrap,
Jul 8, 2001
Jul 8, 2001
41
#endif
Feb 16, 2006
Feb 16, 2006
42
#if SDL_AUDIO_DRIVER_OSS
May 28, 2006
May 28, 2006
43
44
&DSP_bootstrap,
&DMA_bootstrap,
Apr 26, 2001
Apr 26, 2001
45
#endif
Feb 16, 2006
Feb 16, 2006
46
#if SDL_AUDIO_DRIVER_ALSA
May 28, 2006
May 28, 2006
47
&ALSA_bootstrap,
Apr 26, 2001
Apr 26, 2001
48
#endif
Feb 16, 2006
Feb 16, 2006
49
#if SDL_AUDIO_DRIVER_QNXNTO
May 28, 2006
May 28, 2006
50
&QNXNTOAUDIO_bootstrap,
Aug 4, 2003
Aug 4, 2003
51
#endif
Feb 16, 2006
Feb 16, 2006
52
#if SDL_AUDIO_DRIVER_SUNAUDIO
May 28, 2006
May 28, 2006
53
&SUNAUDIO_bootstrap,
Aug 9, 2001
Aug 9, 2001
54
#endif
Feb 16, 2006
Feb 16, 2006
55
#if SDL_AUDIO_DRIVER_DMEDIA
May 28, 2006
May 28, 2006
56
&DMEDIA_bootstrap,
May 23, 2001
May 23, 2001
57
#endif
Feb 16, 2006
Feb 16, 2006
58
#if SDL_AUDIO_DRIVER_ARTS
May 28, 2006
May 28, 2006
59
&ARTS_bootstrap,
Apr 26, 2001
Apr 26, 2001
60
#endif
Feb 16, 2006
Feb 16, 2006
61
#if SDL_AUDIO_DRIVER_ESD
May 28, 2006
May 28, 2006
62
&ESD_bootstrap,
Apr 26, 2001
Apr 26, 2001
63
#endif
Feb 16, 2006
Feb 16, 2006
64
#if SDL_AUDIO_DRIVER_NAS
May 28, 2006
May 28, 2006
65
&NAS_bootstrap,
Apr 26, 2001
Apr 26, 2001
66
#endif
Feb 16, 2006
Feb 16, 2006
67
#if SDL_AUDIO_DRIVER_DSOUND
May 28, 2006
May 28, 2006
68
&DSOUND_bootstrap,
Apr 26, 2001
Apr 26, 2001
69
#endif
Feb 16, 2006
Feb 16, 2006
70
#if SDL_AUDIO_DRIVER_WAVEOUT
May 28, 2006
May 28, 2006
71
&WAVEOUT_bootstrap,
Apr 26, 2001
Apr 26, 2001
72
#endif
Feb 16, 2006
Feb 16, 2006
73
#if SDL_AUDIO_DRIVER_PAUD
May 28, 2006
May 28, 2006
74
&Paud_bootstrap,
Feb 16, 2006
Feb 16, 2006
75
76
#endif
#if SDL_AUDIO_DRIVER_BAUDIO
May 28, 2006
May 28, 2006
77
&BAUDIO_bootstrap,
Apr 26, 2001
Apr 26, 2001
78
#endif
Feb 16, 2006
Feb 16, 2006
79
#if SDL_AUDIO_DRIVER_COREAUDIO
May 28, 2006
May 28, 2006
80
&COREAUDIO_bootstrap,
Aug 21, 2004
Aug 21, 2004
81
#endif
Feb 16, 2006
Feb 16, 2006
82
#if SDL_AUDIO_DRIVER_SNDMGR
May 28, 2006
May 28, 2006
83
&SNDMGR_bootstrap,
Apr 26, 2001
Apr 26, 2001
84
#endif
Feb 16, 2006
Feb 16, 2006
85
#if SDL_AUDIO_DRIVER_AHI
May 28, 2006
May 28, 2006
86
&AHI_bootstrap,
May 10, 2001
May 10, 2001
87
#endif
Feb 16, 2006
Feb 16, 2006
88
#if SDL_AUDIO_DRIVER_MINT
May 28, 2006
May 28, 2006
89
90
91
92
93
&MINTAUDIO_GSXB_bootstrap,
&MINTAUDIO_MCSN_bootstrap,
&MINTAUDIO_STFA_bootstrap,
&MINTAUDIO_XBIOS_bootstrap,
&MINTAUDIO_DMA8_bootstrap,
Jun 10, 2002
Jun 10, 2002
94
#endif
Feb 16, 2006
Feb 16, 2006
95
#if SDL_AUDIO_DRIVER_DISK
May 28, 2006
May 28, 2006
96
&DISKAUD_bootstrap,
Oct 5, 2002
Oct 5, 2002
97
#endif
Mar 14, 2006
Mar 14, 2006
98
#if SDL_AUDIO_DRIVER_DUMMY
May 28, 2006
May 28, 2006
99
&DUMMYAUD_bootstrap,
Mar 14, 2006
Mar 14, 2006
100
#endif
Feb 16, 2006
Feb 16, 2006
101
#if SDL_AUDIO_DRIVER_DC
May 28, 2006
May 28, 2006
102
&DCAUD_bootstrap,
May 29, 2003
May 29, 2003
103
#endif
Feb 16, 2006
Feb 16, 2006
104
#if SDL_AUDIO_DRIVER_MMEAUDIO
May 28, 2006
May 28, 2006
105
&MMEAUDIO_bootstrap,
Feb 16, 2006
Feb 16, 2006
106
107
#endif
#if SDL_AUDIO_DRIVER_DART
May 28, 2006
May 28, 2006
108
&DART_bootstrap,
Jun 16, 2001
Jun 16, 2001
109
#endif
May 28, 2006
May 28, 2006
110
NULL
Apr 26, 2001
Apr 26, 2001
111
112
113
114
};
SDL_AudioDevice *current_audio = NULL;
/* Various local functions */
May 29, 2006
May 29, 2006
115
116
int SDL_AudioInit(const char *driver_name);
void SDL_AudioQuit(void);
Apr 26, 2001
Apr 26, 2001
117
Feb 21, 2006
Feb 21, 2006
118
#if SDL_AUDIO_DRIVER_AHI
May 10, 2001
May 10, 2001
119
120
static int audio_configured = 0;
#endif
Apr 26, 2001
Apr 26, 2001
121
122
/* The general mixing thread function */
May 28, 2006
May 28, 2006
123
int SDLCALL
May 29, 2006
May 29, 2006
124
SDL_RunAudio(void *audiop)
Apr 26, 2001
Apr 26, 2001
125
{
May 28, 2006
May 28, 2006
126
127
128
129
130
131
SDL_AudioDevice *audio = (SDL_AudioDevice *) audiop;
Uint8 *stream;
int stream_len;
void *udata;
void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
int silence;
Feb 21, 2006
Feb 21, 2006
132
#if SDL_AUDIO_DRIVER_AHI
May 28, 2006
May 28, 2006
133
int started = 0;
May 10, 2001
May 10, 2001
134
135
136
/* AmigaOS NEEDS that the audio driver is opened in the thread that uses it! */
May 29, 2006
May 29, 2006
137
D(bug("Task audio started audio struct:<%lx>...\n", audiop));
May 10, 2001
May 10, 2001
138
May 29, 2006
May 29, 2006
139
140
141
D(bug("Before Openaudio..."));
if (audio->OpenAudio(audio, &audio->spec) == -1) {
D(bug("Open audio failed...\n"));
May 28, 2006
May 28, 2006
142
143
return (-1);
}
May 29, 2006
May 29, 2006
144
D(bug("OpenAudio...OK\n"));
May 10, 2001
May 10, 2001
145
#endif
Apr 26, 2001
Apr 26, 2001
146
May 28, 2006
May 28, 2006
147
148
/* Perform any thread setup */
if (audio->ThreadInit) {
May 29, 2006
May 29, 2006
149
audio->ThreadInit(audio);
May 28, 2006
May 28, 2006
150
}
May 29, 2006
May 29, 2006
151
audio->threadid = SDL_ThreadID();
Apr 26, 2001
Apr 26, 2001
152
May 28, 2006
May 28, 2006
153
154
155
/* Set up the mixing function */
fill = audio->spec.callback;
udata = audio->spec.userdata;
May 10, 2001
May 10, 2001
156
Feb 21, 2006
Feb 21, 2006
157
#if SDL_AUDIO_DRIVER_AHI
May 28, 2006
May 28, 2006
158
audio_configured = 1;
May 10, 2001
May 10, 2001
159
May 29, 2006
May 29, 2006
160
161
162
D(bug("Audio configured... Checking for conversion\n"));
SDL_mutexP(audio->mixer_lock);
D(bug("Semaphore obtained...\n"));
May 10, 2001
May 10, 2001
163
164
#endif
May 28, 2006
May 28, 2006
165
166
167
168
169
170
171
172
173
174
175
if (audio->convert.needed) {
if (audio->convert.src_format == AUDIO_U8) {
silence = 0x80;
} else {
silence = 0;
}
stream_len = audio->convert.len;
} else {
silence = audio->spec.silence;
stream_len = audio->spec.size;
}
Mar 21, 2006
Mar 21, 2006
176
Feb 21, 2006
Feb 21, 2006
177
#if SDL_AUDIO_DRIVER_AHI
May 29, 2006
May 29, 2006
178
179
SDL_mutexV(audio->mixer_lock);
D(bug("Entering audio loop...\n"));
May 10, 2001
May 10, 2001
180
181
#endif
Nov 23, 2005
Nov 23, 2005
182
#ifdef __OS2__
May 28, 2006
May 28, 2006
183
184
/* Increase the priority of this thread to make sure that
the audio will be continuous all the time! */
Nov 23, 2005
Nov 23, 2005
185
#ifdef USE_DOSSETPRIORITY
May 29, 2006
May 29, 2006
186
if (SDL_getenv("SDL_USE_TIMECRITICAL_AUDIO")) {
Nov 23, 2005
Nov 23, 2005
187
#ifdef DEBUG_BUILD
May 28, 2006
May 28, 2006
188
189
printf
("[SDL_RunAudio] : Setting priority to TimeCritical+0! (TID%d)\n",
May 29, 2006
May 29, 2006
190
SDL_ThreadID());
Nov 23, 2005
Nov 23, 2005
191
#endif
May 29, 2006
May 29, 2006
192
DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
May 28, 2006
May 28, 2006
193
} else {
Feb 26, 2006
Feb 26, 2006
194
#ifdef DEBUG_BUILD
May 28, 2006
May 28, 2006
195
196
printf
("[SDL_RunAudio] : Setting priority to ForegroundServer+0! (TID%d)\n",
May 29, 2006
May 29, 2006
197
SDL_ThreadID());
May 28, 2006
May 28, 2006
198
#endif
May 29, 2006
May 29, 2006
199
DosSetPriority(PRTYS_THREAD, PRTYC_FOREGROUNDSERVER, 0, 0);
May 28, 2006
May 28, 2006
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
}
#endif
#endif
/* Loop, filling the audio buffers */
while (audio->enabled) {
/* Fill the current buffer with sound */
if (audio->convert.needed) {
if (audio->convert.buf) {
stream = audio->convert.buf;
} else {
continue;
}
} else {
May 29, 2006
May 29, 2006
215
stream = audio->GetAudioBuf(audio);
May 28, 2006
May 28, 2006
216
217
218
219
if (stream == NULL) {
stream = audio->fake_stream;
}
}
May 29, 2006
May 29, 2006
220
SDL_memset(stream, silence, stream_len);
May 28, 2006
May 28, 2006
221
222
if (!audio->paused) {
May 29, 2006
May 29, 2006
223
SDL_mutexP(audio->mixer_lock);
May 28, 2006
May 28, 2006
224
(*fill) (udata, stream, stream_len);
May 29, 2006
May 29, 2006
225
SDL_mutexV(audio->mixer_lock);
May 28, 2006
May 28, 2006
226
227
228
229
}
/* Convert the audio if necessary */
if (audio->convert.needed) {
May 29, 2006
May 29, 2006
230
231
SDL_ConvertAudio(&audio->convert);
stream = audio->GetAudioBuf(audio);
May 28, 2006
May 28, 2006
232
233
234
if (stream == NULL) {
stream = audio->fake_stream;
}
May 29, 2006
May 29, 2006
235
SDL_memcpy(stream, audio->convert.buf, audio->convert.len_cvt);
Feb 26, 2006
Feb 26, 2006
236
}
May 10, 2001
May 10, 2001
237
May 28, 2006
May 28, 2006
238
239
/* Ready current buffer for play and change current buffer */
if (stream != audio->fake_stream) {
May 29, 2006
May 29, 2006
240
audio->PlayAudio(audio);
May 28, 2006
May 28, 2006
241
242
243
244
}
/* Wait for an audio buffer to become available */
if (stream == audio->fake_stream) {
May 29, 2006
May 29, 2006
245
SDL_Delay((audio->spec.samples * 1000) / audio->spec.freq);
May 28, 2006
May 28, 2006
246
} else {
May 29, 2006
May 29, 2006
247
audio->WaitAudio(audio);
May 28, 2006
May 28, 2006
248
249
}
}
May 10, 2001
May 10, 2001
250
May 28, 2006
May 28, 2006
251
252
/* Wait for the audio to drain.. */
if (audio->WaitDone) {
May 29, 2006
May 29, 2006
253
audio->WaitDone(audio);
May 28, 2006
May 28, 2006
254
}
Feb 21, 2006
Feb 21, 2006
255
#if SDL_AUDIO_DRIVER_AHI
May 29, 2006
May 29, 2006
256
D(bug("WaitAudio...Done\n"));
May 10, 2001
May 10, 2001
257
May 29, 2006
May 29, 2006
258
audio->CloseAudio(audio);
May 10, 2001
May 10, 2001
259
May 29, 2006
May 29, 2006
260
D(bug("CloseAudio..Done, subtask exiting...\n"));
May 28, 2006
May 28, 2006
261
audio_configured = 0;
Nov 23, 2005
Nov 23, 2005
262
263
264
#endif
#ifdef __OS2__
#ifdef DEBUG_BUILD
May 29, 2006
May 29, 2006
265
printf("[SDL_RunAudio] : Task exiting. (TID%d)\n", SDL_ThreadID());
Nov 23, 2005
Nov 23, 2005
266
#endif
May 10, 2001
May 10, 2001
267
#endif
May 28, 2006
May 28, 2006
268
return (0);
Apr 26, 2001
Apr 26, 2001
269
270
}
May 28, 2006
May 28, 2006
271
static void
May 29, 2006
May 29, 2006
272
SDL_LockAudio_Default(SDL_AudioDevice * audio)
Mar 30, 2002
Mar 30, 2002
273
{
May 29, 2006
May 29, 2006
274
if (audio->thread && (SDL_ThreadID() == audio->threadid)) {
May 28, 2006
May 28, 2006
275
276
return;
}
May 29, 2006
May 29, 2006
277
SDL_mutexP(audio->mixer_lock);
Mar 30, 2002
Mar 30, 2002
278
279
}
May 28, 2006
May 28, 2006
280
static void
May 29, 2006
May 29, 2006
281
SDL_UnlockAudio_Default(SDL_AudioDevice * audio)
Mar 30, 2002
Mar 30, 2002
282
{
May 29, 2006
May 29, 2006
283
if (audio->thread && (SDL_ThreadID() == audio->threadid)) {
May 28, 2006
May 28, 2006
284
285
return;
}
May 29, 2006
May 29, 2006
286
SDL_mutexV(audio->mixer_lock);
Mar 30, 2002
Mar 30, 2002
287
288
}
May 28, 2006
May 28, 2006
289
static Uint16
May 29, 2006
May 29, 2006
290
SDL_ParseAudioFormat(const char *string)
May 17, 2006
May 17, 2006
291
{
May 28, 2006
May 28, 2006
292
293
294
295
296
297
298
299
300
301
302
303
304
305
Uint16 format = 0;
switch (*string) {
case 'U':
++string;
format |= 0x0000;
break;
case 'S':
++string;
format |= 0x8000;
break;
default:
return 0;
}
May 29, 2006
May 29, 2006
306
switch (SDL_atoi(string)) {
May 28, 2006
May 28, 2006
307
308
309
310
311
312
313
case 8:
string += 1;
format |= 8;
break;
case 16:
string += 2;
format |= 16;
May 29, 2006
May 29, 2006
314
if (SDL_strcmp(string, "LSB") == 0
May 17, 2006
May 17, 2006
315
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 29, 2006
May 29, 2006
316
|| SDL_strcmp(string, "SYS") == 0
May 17, 2006
May 17, 2006
317
#endif
May 28, 2006
May 28, 2006
318
319
320
) {
format |= 0x0000;
}
May 29, 2006
May 29, 2006
321
if (SDL_strcmp(string, "MSB") == 0
May 17, 2006
May 17, 2006
322
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
May 29, 2006
May 29, 2006
323
|| SDL_strcmp(string, "SYS") == 0
May 28, 2006
May 28, 2006
324
325
326
327
328
329
330
331
332
#endif
) {
format |= 0x1000;
}
break;
default:
return 0;
}
return format;
May 17, 2006
May 17, 2006
333
334
}
May 28, 2006
May 28, 2006
335
int
May 29, 2006
May 29, 2006
336
SDL_GetNumAudioDrivers(void)
May 20, 2006
May 20, 2006
337
{
May 29, 2006
May 29, 2006
338
return (SDL_arraysize(bootstrap) - 1);
May 20, 2006
May 20, 2006
339
340
}
May 28, 2006
May 28, 2006
341
const char *
May 29, 2006
May 29, 2006
342
SDL_GetAudioDriver(int index)
May 20, 2006
May 20, 2006
343
{
May 29, 2006
May 29, 2006
344
if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
May 28, 2006
May 28, 2006
345
346
347
return (bootstrap[index]->name);
}
return (NULL);
May 20, 2006
May 20, 2006
348
349
}
May 28, 2006
May 28, 2006
350
int
May 29, 2006
May 29, 2006
351
SDL_AudioInit(const char *driver_name)
Apr 26, 2001
Apr 26, 2001
352
{
May 28, 2006
May 28, 2006
353
354
SDL_AudioDevice *audio;
int i = 0, idx;
Apr 26, 2001
Apr 26, 2001
355
May 28, 2006
May 28, 2006
356
357
/* Check to make sure we don't overwrite 'current_audio' */
if (current_audio != NULL) {
May 29, 2006
May 29, 2006
358
SDL_AudioQuit();
May 28, 2006
May 28, 2006
359
}
Apr 26, 2001
Apr 26, 2001
360
May 28, 2006
May 28, 2006
361
362
363
/* Select the proper audio driver */
audio = NULL;
idx = 0;
Feb 16, 2006
Feb 16, 2006
364
#if SDL_AUDIO_DRIVER_ESD
May 29, 2006
May 29, 2006
365
if ((driver_name == NULL) && (SDL_getenv("ESPEAKER") != NULL)) {
May 28, 2006
May 28, 2006
366
367
368
369
370
/* Ahem, we know that if ESPEAKER is set, user probably wants
to use ESD, but don't start it if it's not already running.
This probably isn't the place to do this, but... Shh! :)
*/
for (i = 0; bootstrap[i]; ++i) {
May 29, 2006
May 29, 2006
371
if (SDL_strcmp(bootstrap[i]->name, "esd") == 0) {
Feb 7, 2006
Feb 7, 2006
372
#ifdef HAVE_PUTENV
May 28, 2006
May 28, 2006
373
374
375
const char *esd_no_spawn;
/* Don't start ESD if it's not running */
May 29, 2006
May 29, 2006
376
esd_no_spawn = getenv("ESD_NO_SPAWN");
May 28, 2006
May 28, 2006
377
if (esd_no_spawn == NULL) {
May 29, 2006
May 29, 2006
378
putenv("ESD_NO_SPAWN=1");
May 28, 2006
May 28, 2006
379
380
}
#endif
May 29, 2006
May 29, 2006
381
382
if (bootstrap[i]->available()) {
audio = bootstrap[i]->create(0);
May 28, 2006
May 28, 2006
383
384
break;
}
Feb 7, 2006
Feb 7, 2006
385
#ifdef HAVE_UNSETENV
May 28, 2006
May 28, 2006
386
if (esd_no_spawn == NULL) {
May 29, 2006
May 29, 2006
387
unsetenv("ESD_NO_SPAWN");
May 28, 2006
May 28, 2006
388
}
Apr 26, 2001
Apr 26, 2001
389
#endif
May 28, 2006
May 28, 2006
390
391
392
}
}
}
Feb 16, 2006
Feb 16, 2006
393
#endif /* SDL_AUDIO_DRIVER_ESD */
May 28, 2006
May 28, 2006
394
395
396
if (audio == NULL) {
if (driver_name != NULL) {
#if 0 /* This will be replaced with a better driver selection API */
May 29, 2006
May 29, 2006
397
398
if (SDL_strrchr(driver_name, ':') != NULL) {
idx = atoi(SDL_strrchr(driver_name, ':') + 1);
May 28, 2006
May 28, 2006
399
400
401
}
#endif
for (i = 0; bootstrap[i]; ++i) {
May 29, 2006
May 29, 2006
402
403
404
405
if (SDL_strncmp(bootstrap[i]->name, driver_name,
SDL_strlen(bootstrap[i]->name)) == 0) {
if (bootstrap[i]->available()) {
audio = bootstrap[i]->create(idx);
May 28, 2006
May 28, 2006
406
407
408
409
410
411
}
break;
}
}
} else {
for (i = 0; bootstrap[i]; ++i) {
May 29, 2006
May 29, 2006
412
413
if (bootstrap[i]->available()) {
audio = bootstrap[i]->create(idx);
May 28, 2006
May 28, 2006
414
415
416
417
418
419
420
421
if (audio != NULL) {
break;
}
}
}
}
if (audio == NULL) {
if (driver_name) {
May 29, 2006
May 29, 2006
422
SDL_SetError("%s not available", driver_name);
May 28, 2006
May 28, 2006
423
} else {
May 29, 2006
May 29, 2006
424
SDL_SetError("No available audio device");
May 28, 2006
May 28, 2006
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
}
#if 0 /* Don't fail SDL_Init() if audio isn't available.
SDL_OpenAudio() will handle it at that point. *sigh*
*/
return (-1);
#endif
}
}
current_audio = audio;
if (current_audio) {
current_audio->name = bootstrap[i]->name;
if (!current_audio->LockAudio && !current_audio->UnlockAudio) {
current_audio->LockAudio = SDL_LockAudio_Default;
current_audio->UnlockAudio = SDL_UnlockAudio_Default;
}
}
return (0);
Apr 26, 2001
Apr 26, 2001
442
443
}
May 28, 2006
May 28, 2006
444
445
446
447
/*
* Get the current audio driver name
*/
const char *
May 29, 2006
May 29, 2006
448
SDL_GetCurrentAudioDriver()
Apr 26, 2001
Apr 26, 2001
449
{
May 28, 2006
May 28, 2006
450
451
452
453
if (current_audio) {
return current_audio->name;
}
return (NULL);
Apr 26, 2001
Apr 26, 2001
454
455
}
May 28, 2006
May 28, 2006
456
int
May 29, 2006
May 29, 2006
457
SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
Apr 26, 2001
Apr 26, 2001
458
{
May 28, 2006
May 28, 2006
459
460
461
462
463
SDL_AudioDevice *audio;
const char *env;
/* Start up the audio driver, if necessary */
if (!current_audio) {
May 29, 2006
May 29, 2006
464
if ((SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) ||
May 28, 2006
May 28, 2006
465
466
467
468
469
470
471
(current_audio == NULL)) {
return (-1);
}
}
audio = current_audio;
if (audio->opened) {
May 29, 2006
May 29, 2006
472
SDL_SetError("Audio device is already opened");
May 28, 2006
May 28, 2006
473
474
475
476
477
return (-1);
}
/* Verify some parameters */
if (desired->freq == 0) {
May 29, 2006
May 29, 2006
478
env = SDL_getenv("SDL_AUDIO_FREQUENCY");
May 28, 2006
May 28, 2006
479
if (env) {
May 29, 2006
May 29, 2006
480
desired->freq = SDL_atoi(env);
May 28, 2006
May 28, 2006
481
482
483
484
485
486
487
}
}
if (desired->freq == 0) {
/* Pick some default audio frequency */
desired->freq = 22050;
}
if (desired->format == 0) {
May 29, 2006
May 29, 2006
488
env = SDL_getenv("SDL_AUDIO_FORMAT");
May 28, 2006
May 28, 2006
489
if (env) {
May 29, 2006
May 29, 2006
490
desired->format = SDL_ParseAudioFormat(env);
May 28, 2006
May 28, 2006
491
492
493
494
495
496
497
}
}
if (desired->format == 0) {
/* Pick some default audio format */
desired->format = AUDIO_S16;
}
if (desired->channels == 0) {
May 29, 2006
May 29, 2006
498
env = SDL_getenv("SDL_AUDIO_CHANNELS");
May 28, 2006
May 28, 2006
499
if (env) {
May 29, 2006
May 29, 2006
500
desired->channels = SDL_atoi(env);
May 28, 2006
May 28, 2006
501
502
503
504
505
506
507
508
509
510
511
512
513
}
}
if (desired->channels == 0) {
/* Pick a default number of channels */
desired->channels = 2;
}
switch (desired->channels) {
case 1: /* Mono */
case 2: /* Stereo */
case 4: /* surround */
case 6: /* surround with center and lfe */
break;
default:
May 29, 2006
May 29, 2006
514
SDL_SetError("1 (mono) and 2 (stereo) channels supported");
May 28, 2006
May 28, 2006
515
516
517
return (-1);
}
if (desired->samples == 0) {
May 29, 2006
May 29, 2006
518
env = SDL_getenv("SDL_AUDIO_SAMPLES");
May 28, 2006
May 28, 2006
519
if (env) {
May 29, 2006
May 29, 2006
520
desired->samples = SDL_atoi(env);
May 28, 2006
May 28, 2006
521
522
523
524
525
526
527
528
529
530
531
532
}
}
if (desired->samples == 0) {
/* Pick a default of ~46 ms at desired frequency */
int samples = (desired->freq / 1000) * 46;
int power2 = 1;
while (power2 < samples) {
power2 *= 2;
}
desired->samples = power2;
}
if (desired->callback == NULL) {
May 29, 2006
May 29, 2006
533
SDL_SetError("SDL_OpenAudio() passed a NULL callback");
May 28, 2006
May 28, 2006
534
535
return (-1);
}
Feb 21, 2006
Feb 21, 2006
536
#if defined(__MACOS__) || (defined(__RISCOS__) && SDL_THREADS_DISABLED)
May 28, 2006
May 28, 2006
537
/* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */
Jun 10, 2002
Jun 10, 2002
538
#else
Feb 16, 2006
Feb 16, 2006
539
#if defined(__MINT__) && SDL_THREADS_DISABLED
May 28, 2006
May 28, 2006
540
/* Uses interrupt driven audio, without thread */
Apr 26, 2001
Apr 26, 2001
541
#else
May 28, 2006
May 28, 2006
542
/* Create a semaphore for locking the sound buffers */
May 29, 2006
May 29, 2006
543
audio->mixer_lock = SDL_CreateMutex();
May 28, 2006
May 28, 2006
544
if (audio->mixer_lock == NULL) {
May 29, 2006
May 29, 2006
545
546
SDL_SetError("Couldn't create mixer lock");
SDL_CloseAudio();
May 28, 2006
May 28, 2006
547
548
return (-1);
}
Jun 10, 2002
Jun 10, 2002
549
#endif /* __MINT__ */
Feb 21, 2006
Feb 21, 2006
550
#endif /* __MACOS__ */
Apr 26, 2001
Apr 26, 2001
551
May 28, 2006
May 28, 2006
552
/* Calculate the silence and size of the audio specification */
May 29, 2006
May 29, 2006
553
SDL_CalculateAudioSpec(desired);
Apr 26, 2001
Apr 26, 2001
554
May 28, 2006
May 28, 2006
555
/* Open the audio subsystem */
May 29, 2006
May 29, 2006
556
SDL_memcpy(&audio->spec, desired, sizeof(audio->spec));
May 28, 2006
May 28, 2006
557
558
559
audio->convert.needed = 0;
audio->enabled = 1;
audio->paused = 1;
May 10, 2001
May 10, 2001
560
Feb 21, 2006
Feb 21, 2006
561
#if !SDL_AUDIO_DRIVER_AHI
May 10, 2001
May 10, 2001
562
563
/* AmigaOS opens audio inside the main loop */
May 29, 2006
May 29, 2006
564
audio->opened = audio->OpenAudio(audio, &audio->spec) + 1;
May 10, 2001
May 10, 2001
565
May 28, 2006
May 28, 2006
566
if (!audio->opened) {
May 29, 2006
May 29, 2006
567
SDL_CloseAudio();
May 28, 2006
May 28, 2006
568
569
return (-1);
}
May 10, 2001
May 10, 2001
570
#else
May 29, 2006
May 29, 2006
571
572
D(bug("Locking semaphore..."));
SDL_mutexP(audio->mixer_lock);
May 28, 2006
May 28, 2006
573
574
May 29, 2006
May 29, 2006
575
576
audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
D(bug("Created thread...\n"));
May 28, 2006
May 28, 2006
577
578
if (audio->thread == NULL) {
May 29, 2006
May 29, 2006
579
580
581
SDL_mutexV(audio->mixer_lock);
SDL_CloseAudio();
SDL_SetError("Couldn't create audio thread");
May 28, 2006
May 28, 2006
582
583
584
585
return (-1);
}
while (!audio_configured)
May 29, 2006
May 29, 2006
586
SDL_Delay(100);
May 28, 2006
May 28, 2006
587
588
589
590
591
#endif
/* If the audio driver changes the buffer size, accept it */
if (audio->spec.samples != desired->samples) {
desired->samples = audio->spec.samples;
May 29, 2006
May 29, 2006
592
SDL_CalculateAudioSpec(desired);
May 28, 2006
May 28, 2006
593
594
595
}
/* Allocate a fake audio memory buffer */
May 29, 2006
May 29, 2006
596
audio->fake_stream = SDL_AllocAudioMem(audio->spec.size);
May 28, 2006
May 28, 2006
597
if (audio->fake_stream == NULL) {
May 29, 2006
May 29, 2006
598
599
SDL_CloseAudio();
SDL_OutOfMemory();
May 28, 2006
May 28, 2006
600
601
602
603
604
return (-1);
}
/* See if we need to do any conversion */
if (obtained != NULL) {
May 29, 2006
May 29, 2006
605
SDL_memcpy(obtained, &audio->spec, sizeof(audio->spec));
May 28, 2006
May 28, 2006
606
607
608
609
} else if (desired->freq != audio->spec.freq ||
desired->format != audio->spec.format ||
desired->channels != audio->spec.channels) {
/* Build an audio conversion block */
May 29, 2006
May 29, 2006
610
611
612
613
614
615
if (SDL_BuildAudioCVT(&audio->convert,
desired->format, desired->channels,
desired->freq,
audio->spec.format, audio->spec.channels,
audio->spec.freq) < 0) {
SDL_CloseAudio();
May 28, 2006
May 28, 2006
616
617
618
619
620
return (-1);
}
if (audio->convert.needed) {
audio->convert.len = desired->size;
audio->convert.buf =
May 29, 2006
May 29, 2006
621
622
(Uint8 *) SDL_AllocAudioMem(audio->convert.len *
audio->convert.len_mult);
May 28, 2006
May 28, 2006
623
if (audio->convert.buf == NULL) {
May 29, 2006
May 29, 2006
624
625
SDL_CloseAudio();
SDL_OutOfMemory();
May 28, 2006
May 28, 2006
626
627
628
629
return (-1);
}
}
}
Feb 21, 2006
Feb 21, 2006
630
#if !SDL_AUDIO_DRIVER_AHI
May 28, 2006
May 28, 2006
631
632
633
634
/* Start the audio thread if necessary */
switch (audio->opened) {
case 1:
/* Start the audio thread */
Feb 21, 2006
Feb 21, 2006
635
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
Feb 6, 2006
Feb 6, 2006
636
#undef SDL_CreateThread
May 29, 2006
May 29, 2006
637
audio->thread = SDL_CreateThread(SDL_RunAudio, audio, NULL, NULL);
Feb 6, 2006
Feb 6, 2006
638
#else
May 29, 2006
May 29, 2006
639
audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
May 28, 2006
May 28, 2006
640
641
#endif
if (audio->thread == NULL) {
May 29, 2006
May 29, 2006
642
643
SDL_CloseAudio();
SDL_SetError("Couldn't create audio thread");
May 28, 2006
May 28, 2006
644
645
646
647
648
649
650
651
return (-1);
}
break;
default:
/* The audio is now playing */
break;
}
May 10, 2001
May 10, 2001
652
#else
May 29, 2006
May 29, 2006
653
654
SDL_mutexV(audio->mixer_lock);
D(bug("SDL_OpenAudio USCITA...\n"));
May 10, 2001
May 10, 2001
655
656
657
#endif
May 28, 2006
May 28, 2006
658
return (0);
Apr 26, 2001
Apr 26, 2001
659
660
}
May 28, 2006
May 28, 2006
661
SDL_audiostatus
May 29, 2006
May 29, 2006
662
SDL_GetAudioStatus(void)
Apr 26, 2001
Apr 26, 2001
663
{
May 28, 2006
May 28, 2006
664
665
666
667
668
669
670
671
672
673
674
675
SDL_AudioDevice *audio = current_audio;
SDL_audiostatus status;
status = SDL_AUDIO_STOPPED;
if (audio && audio->enabled) {
if (audio->paused) {
status = SDL_AUDIO_PAUSED;
} else {
status = SDL_AUDIO_PLAYING;
}
}
return (status);
Apr 26, 2001
Apr 26, 2001
676
677
}
May 28, 2006
May 28, 2006
678
void
May 29, 2006
May 29, 2006
679
SDL_PauseAudio(int pause_on)
Apr 26, 2001
Apr 26, 2001
680
{
May 28, 2006
May 28, 2006
681
SDL_AudioDevice *audio = current_audio;
Apr 26, 2001
Apr 26, 2001
682
May 28, 2006
May 28, 2006
683
684
685
if (audio) {
audio->paused = pause_on;
}
Apr 26, 2001
Apr 26, 2001
686
687
}
May 28, 2006
May 28, 2006
688
void
May 29, 2006
May 29, 2006
689
SDL_LockAudio(void)
Apr 26, 2001
Apr 26, 2001
690
{
May 28, 2006
May 28, 2006
691
SDL_AudioDevice *audio = current_audio;
Apr 26, 2001
Apr 26, 2001
692
May 28, 2006
May 28, 2006
693
694
/* Obtain a lock on the mixing buffers */
if (audio && audio->LockAudio) {
May 29, 2006
May 29, 2006
695
audio->LockAudio(audio);
May 28, 2006
May 28, 2006
696
}
Apr 26, 2001
Apr 26, 2001
697
698
}
May 28, 2006
May 28, 2006
699
void
May 29, 2006
May 29, 2006
700
SDL_UnlockAudio(void)
Apr 26, 2001
Apr 26, 2001
701
{
May 28, 2006
May 28, 2006
702
SDL_AudioDevice *audio = current_audio;
Apr 26, 2001
Apr 26, 2001
703
May 28, 2006
May 28, 2006
704
705
/* Release lock on the mixing buffers */
if (audio && audio->UnlockAudio) {
May 29, 2006
May 29, 2006
706
audio->UnlockAudio(audio);
May 28, 2006
May 28, 2006
707
}
Apr 26, 2001
Apr 26, 2001
708
709
}
May 28, 2006
May 28, 2006
710
void
May 29, 2006
May 29, 2006
711
SDL_CloseAudio(void)
Apr 26, 2001
Apr 26, 2001
712
{
May 29, 2006
May 29, 2006
713
SDL_QuitSubSystem(SDL_INIT_AUDIO);
Apr 26, 2001
Apr 26, 2001
714
715
}
May 28, 2006
May 28, 2006
716
void
May 29, 2006
May 29, 2006
717
SDL_AudioQuit(void)
Apr 26, 2001
Apr 26, 2001
718
{
May 28, 2006
May 28, 2006
719
720
721
722
723
SDL_AudioDevice *audio = current_audio;
if (audio) {
audio->enabled = 0;
if (audio->thread != NULL) {
May 29, 2006
May 29, 2006
724
SDL_WaitThread(audio->thread, NULL);
May 28, 2006
May 28, 2006
725
726
}
if (audio->mixer_lock != NULL) {
May 29, 2006
May 29, 2006
727
SDL_DestroyMutex(audio->mixer_lock);
May 28, 2006
May 28, 2006
728
729
}
if (audio->fake_stream != NULL) {
May 29, 2006
May 29, 2006
730
SDL_FreeAudioMem(audio->fake_stream);
May 28, 2006
May 28, 2006
731
732
}
if (audio->convert.needed) {
May 29, 2006
May 29, 2006
733
SDL_FreeAudioMem(audio->convert.buf);
May 28, 2006
May 28, 2006
734
735
}
Feb 21, 2006
Feb 21, 2006
736
#if !SDL_AUDIO_DRIVER_AHI
May 28, 2006
May 28, 2006
737
if (audio->opened) {
May 29, 2006
May 29, 2006
738
audio->CloseAudio(audio);
May 28, 2006
May 28, 2006
739
740
741
742
audio->opened = 0;
}
#endif
/* Free the driver data */
May 29, 2006
May 29, 2006
743
audio->free(audio);
May 28, 2006
May 28, 2006
744
745
current_audio = NULL;
}
Apr 26, 2001
Apr 26, 2001
746
747
748
749
750
751
}
#define NUM_FORMATS 6
static int format_idx;
static int format_idx_sub;
static Uint16 format_list[NUM_FORMATS][NUM_FORMATS] = {
May 28, 2006
May 28, 2006
752
753
754
755
756
757
758
759
760
761
762
763
{AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
AUDIO_U16MSB},
{AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
AUDIO_U16MSB},
{AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8,
AUDIO_S8},
{AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8,
AUDIO_S8},
{AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U8,
AUDIO_S8},
{AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U8,
AUDIO_S8},
Apr 26, 2001
Apr 26, 2001
764
765
};
May 28, 2006
May 28, 2006
766
Uint16
May 29, 2006
May 29, 2006
767
SDL_FirstAudioFormat(Uint16 format)
Apr 26, 2001
Apr 26, 2001
768
{
May 28, 2006
May 28, 2006
769
770
771
772
773
774
for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
if (format_list[format_idx][0] == format) {
break;
}
}
format_idx_sub = 0;
May 29, 2006
May 29, 2006
775
return (SDL_NextAudioFormat());
Apr 26, 2001
Apr 26, 2001
776
777
}
May 28, 2006
May 28, 2006
778
Uint16
May 29, 2006
May 29, 2006
779
SDL_NextAudioFormat(void)
Apr 26, 2001
Apr 26, 2001
780
{
May 28, 2006
May 28, 2006
781
782
783
784
if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
return (0);
}
return (format_list[format_idx][format_idx_sub++]);
Apr 26, 2001
Apr 26, 2001
785
786
}
May 28, 2006
May 28, 2006
787
void
May 29, 2006
May 29, 2006
788
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
Apr 26, 2001
Apr 26, 2001
789
{
May 28, 2006
May 28, 2006
790
791
792
793
794
795
796
797
798
799
800
switch (spec->format) {
case AUDIO_U8:
spec->silence = 0x80;
break;
default:
spec->silence = 0x00;
break;
}
spec->size = (spec->format & 0xFF) / 8;
spec->size *= spec->channels;
spec->size *= spec->samples;
Apr 26, 2001
Apr 26, 2001
801
}
May 28, 2006
May 28, 2006
802
803
/* vi: set ts=4 sw=4 expandtab: */