Skip to content

Latest commit

 

History

History
713 lines (647 loc) · 16.5 KB

SDL_audio.c

File metadata and controls

713 lines (647 loc) · 16.5 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Dec 31, 2011
Dec 31, 2011
3
Copyright (C) 1997-2012 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[] = {
Oct 13, 2009
Oct 13, 2009
39
40
41
#if SDL_AUDIO_DRIVER_PULSE
&PULSE_bootstrap,
#endif
Oct 10, 2009
Oct 10, 2009
42
43
44
#if SDL_AUDIO_DRIVER_ALSA
&ALSA_bootstrap,
#endif
Jul 8, 2012
Jul 8, 2012
45
46
47
#if SDL_AUDIO_DRIVER_SNDIO
&SNDIO_bootstrap,
#endif
Jul 18, 2010
Jul 18, 2010
48
49
50
#if SDL_AUDIO_DRIVER_BSD
&BSD_AUDIO_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
51
#if SDL_AUDIO_DRIVER_OSS
Apr 26, 2001
Apr 26, 2001
52
53
54
&DSP_bootstrap,
&DMA_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
55
#if SDL_AUDIO_DRIVER_QNXNTO
Aug 4, 2003
Aug 4, 2003
56
57
&QNXNTOAUDIO_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
58
#if SDL_AUDIO_DRIVER_SUNAUDIO
Aug 9, 2001
Aug 9, 2001
59
60
&SUNAUDIO_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
61
#if SDL_AUDIO_DRIVER_DMEDIA
Aug 9, 2001
Aug 9, 2001
62
&DMEDIA_bootstrap,
May 23, 2001
May 23, 2001
63
#endif
Feb 16, 2006
Feb 16, 2006
64
65
#if SDL_AUDIO_DRIVER_ARTS
&ARTS_bootstrap,
Apr 26, 2001
Apr 26, 2001
66
#endif
Feb 16, 2006
Feb 16, 2006
67
#if SDL_AUDIO_DRIVER_ESD
Apr 26, 2001
Apr 26, 2001
68
69
&ESD_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
70
#if SDL_AUDIO_DRIVER_NAS
Apr 26, 2001
Apr 26, 2001
71
72
&NAS_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
73
#if SDL_AUDIO_DRIVER_DSOUND
Apr 26, 2001
Apr 26, 2001
74
75
&DSOUND_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
76
#if SDL_AUDIO_DRIVER_WAVEOUT
Apr 26, 2001
Apr 26, 2001
77
78
&WAVEOUT_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
79
80
81
82
#if SDL_AUDIO_DRIVER_PAUD
&Paud_bootstrap,
#endif
#if SDL_AUDIO_DRIVER_BAUDIO
Apr 26, 2001
Apr 26, 2001
83
84
&BAUDIO_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
85
#if SDL_AUDIO_DRIVER_COREAUDIO
Aug 21, 2004
Aug 21, 2004
86
87
&COREAUDIO_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
88
#if SDL_AUDIO_DRIVER_SNDMGR
Apr 26, 2001
Apr 26, 2001
89
90
&SNDMGR_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
91
#if SDL_AUDIO_DRIVER_MINT
Jul 7, 2003
Jul 7, 2003
92
93
94
95
96
&MINTAUDIO_GSXB_bootstrap,
&MINTAUDIO_MCSN_bootstrap,
&MINTAUDIO_STFA_bootstrap,
&MINTAUDIO_XBIOS_bootstrap,
&MINTAUDIO_DMA8_bootstrap,
Jun 10, 2002
Jun 10, 2002
97
#endif
Feb 16, 2006
Feb 16, 2006
98
#if SDL_AUDIO_DRIVER_DISK
Jun 16, 2001
Jun 16, 2001
99
&DISKAUD_bootstrap,
Oct 5, 2002
Oct 5, 2002
100
#endif
Mar 14, 2006
Mar 14, 2006
101
102
103
#if SDL_AUDIO_DRIVER_DUMMY
&DUMMYAUD_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
104
#if SDL_AUDIO_DRIVER_DC
Oct 5, 2002
Oct 5, 2002
105
&DCAUD_bootstrap,
May 29, 2003
May 29, 2003
106
#endif
Jun 25, 2007
Jun 25, 2007
107
108
109
#if SDL_AUDIO_DRIVER_NDS
&NDSAUD_bootstrap,
#endif
Feb 16, 2006
Feb 16, 2006
110
111
112
113
114
#if SDL_AUDIO_DRIVER_MMEAUDIO
&MMEAUDIO_bootstrap,
#endif
#if SDL_AUDIO_DRIVER_DART
&DART_bootstrap,
Jun 24, 2007
Jun 24, 2007
115
116
117
#endif
#if SDL_AUDIO_DRIVER_EPOCAUDIO
&EPOCAudio_bootstrap,
Jun 16, 2001
Jun 16, 2001
118
#endif
Apr 26, 2001
Apr 26, 2001
119
120
121
122
123
124
125
126
127
NULL
};
SDL_AudioDevice *current_audio = NULL;
/* Various local functions */
int SDL_AudioInit(const char *driver_name);
void SDL_AudioQuit(void);
/* The general mixing thread function */
May 7, 2006
May 7, 2006
128
int SDLCALL SDL_RunAudio(void *audiop)
Apr 26, 2001
Apr 26, 2001
129
130
131
132
133
{
SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop;
Uint8 *stream;
int stream_len;
void *udata;
May 7, 2006
May 7, 2006
134
void (SDLCALL *fill)(void *userdata,Uint8 *stream, int len);
Apr 26, 2001
Apr 26, 2001
135
136
137
138
139
140
141
142
143
144
145
int silence;
/* Perform any thread setup */
if ( audio->ThreadInit ) {
audio->ThreadInit(audio);
}
audio->threadid = SDL_ThreadID();
/* Set up the mixing function */
fill = audio->spec.callback;
udata = audio->spec.userdata;
May 10, 2001
May 10, 2001
146
Apr 26, 2001
Apr 26, 2001
147
148
149
150
151
152
153
154
155
156
157
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
158
Nov 23, 2005
Nov 23, 2005
159
#ifdef __OS2__
Mar 9, 2006
Mar 9, 2006
160
161
/* Increase the priority of this thread to make sure that
the audio will be continuous all the time! */
Nov 23, 2005
Nov 23, 2005
162
#ifdef USE_DOSSETPRIORITY
Feb 26, 2006
Feb 26, 2006
163
164
if (SDL_getenv("SDL_USE_TIMECRITICAL_AUDIO"))
{
Nov 23, 2005
Nov 23, 2005
165
#ifdef DEBUG_BUILD
Feb 26, 2006
Feb 26, 2006
166
printf("[SDL_RunAudio] : Setting priority to TimeCritical+0! (TID%d)\n", SDL_ThreadID());
Nov 23, 2005
Nov 23, 2005
167
#endif
Feb 26, 2006
Feb 26, 2006
168
169
170
171
172
173
174
175
176
DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
}
else
{
#ifdef DEBUG_BUILD
printf("[SDL_RunAudio] : Setting priority to ForegroundServer+0! (TID%d)\n", SDL_ThreadID());
#endif
DosSetPriority(PRTYS_THREAD, PRTYC_FOREGROUNDSERVER, 0, 0);
}
Nov 23, 2005
Nov 23, 2005
177
178
#endif
#endif
May 10, 2001
May 10, 2001
179
Apr 26, 2001
Apr 26, 2001
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* 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 {
stream = audio->GetAudioBuf(audio);
if ( stream == NULL ) {
stream = audio->fake_stream;
}
}
Jul 5, 2007
Jul 5, 2007
196
Jul 9, 2007
Jul 9, 2007
197
SDL_memset(stream, silence, stream_len);
Apr 26, 2001
Apr 26, 2001
198
199
200
201
202
203
204
205
206
207
208
209
210
211
if ( ! audio->paused ) {
SDL_mutexP(audio->mixer_lock);
(*fill)(udata, stream, stream_len);
SDL_mutexV(audio->mixer_lock);
}
/* Convert the audio if necessary */
if ( audio->convert.needed ) {
SDL_ConvertAudio(&audio->convert);
stream = audio->GetAudioBuf(audio);
if ( stream == NULL ) {
stream = audio->fake_stream;
}
Feb 7, 2006
Feb 7, 2006
212
SDL_memcpy(stream, audio->convert.buf,
Apr 26, 2001
Apr 26, 2001
213
214
215
216
217
218
audio->convert.len_cvt);
}
/* Ready current buffer for play and change current buffer */
if ( stream != audio->fake_stream ) {
audio->PlayAudio(audio);
Mar 21, 2006
Mar 21, 2006
219
220
221
222
223
224
225
}
/* Wait for an audio buffer to become available */
if ( stream == audio->fake_stream ) {
SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
} else {
audio->WaitAudio(audio);
Apr 26, 2001
Apr 26, 2001
226
227
}
}
Mar 21, 2006
Mar 21, 2006
228
Apr 26, 2001
Apr 26, 2001
229
230
231
232
/* Wait for the audio to drain.. */
if ( audio->WaitDone ) {
audio->WaitDone(audio);
}
May 10, 2001
May 10, 2001
233
Nov 23, 2005
Nov 23, 2005
234
235
236
237
#ifdef __OS2__
#ifdef DEBUG_BUILD
printf("[SDL_RunAudio] : Task exiting. (TID%d)\n", SDL_ThreadID());
#endif
May 10, 2001
May 10, 2001
238
#endif
Apr 26, 2001
Apr 26, 2001
239
240
241
return(0);
}
Mar 30, 2002
Mar 30, 2002
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
static void SDL_LockAudio_Default(SDL_AudioDevice *audio)
{
if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
return;
}
SDL_mutexP(audio->mixer_lock);
}
static void SDL_UnlockAudio_Default(SDL_AudioDevice *audio)
{
if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
return;
}
SDL_mutexV(audio->mixer_lock);
}
May 9, 2006
May 9, 2006
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
static Uint16 SDL_ParseAudioFormat(const char *string)
{
Uint16 format = 0;
switch (*string) {
case 'U':
++string;
format |= 0x0000;
break;
case 'S':
++string;
format |= 0x8000;
break;
default:
return 0;
}
switch (SDL_atoi(string)) {
case 8:
string += 1;
format |= 8;
break;
case 16:
string += 2;
format |= 16;
if ( SDL_strcmp(string, "LSB") == 0
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
|| SDL_strcmp(string, "SYS") == 0
#endif
) {
format |= 0x0000;
}
if ( SDL_strcmp(string, "MSB") == 0
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
|| SDL_strcmp(string, "SYS") == 0
#endif
) {
format |= 0x1000;
}
break;
default:
return 0;
}
return format;
}
Apr 26, 2001
Apr 26, 2001
303
304
305
306
307
308
309
310
311
312
int SDL_AudioInit(const char *driver_name)
{
SDL_AudioDevice *audio;
int i = 0, idx;
/* Check to make sure we don't overwrite 'current_audio' */
if ( current_audio != NULL ) {
SDL_AudioQuit();
}
Mar 24, 2018
Mar 24, 2018
313
#if SDL_AUDIO_DRIVER_PULSE
Feb 27, 2013
Feb 27, 2013
314
315
316
317
/* SDL 2.0 uses the name "pulseaudio", so we'll support both */
if ( driver_name && SDL_strcasecmp(driver_name, "pulseaudio") == 0 ) {
driver_name = "pulse";
}
Mar 24, 2018
Mar 24, 2018
318
#endif /* */
Feb 27, 2013
Feb 27, 2013
319
Apr 26, 2001
Apr 26, 2001
320
321
322
/* Select the proper audio driver */
audio = NULL;
idx = 0;
Feb 16, 2006
Feb 16, 2006
323
#if SDL_AUDIO_DRIVER_ESD
Feb 7, 2006
Feb 7, 2006
324
if ( (driver_name == NULL) && (SDL_getenv("ESPEAKER") != NULL) ) {
Apr 26, 2001
Apr 26, 2001
325
326
327
328
329
/* 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 ) {
Oct 1, 2006
Oct 1, 2006
330
if ( SDL_strcasecmp(bootstrap[i]->name, "esd") == 0 ) {
Feb 7, 2006
Feb 7, 2006
331
#ifdef HAVE_PUTENV
Apr 26, 2001
Apr 26, 2001
332
333
334
const char *esd_no_spawn;
/* Don't start ESD if it's not running */
Feb 7, 2006
Feb 7, 2006
335
esd_no_spawn = getenv("ESD_NO_SPAWN");
Apr 26, 2001
Apr 26, 2001
336
337
338
if ( esd_no_spawn == NULL ) {
putenv("ESD_NO_SPAWN=1");
}
Feb 7, 2006
Feb 7, 2006
339
#endif
Apr 26, 2001
Apr 26, 2001
340
341
342
343
if ( bootstrap[i]->available() ) {
audio = bootstrap[i]->create(0);
break;
}
Feb 7, 2006
Feb 7, 2006
344
#ifdef HAVE_UNSETENV
Apr 26, 2001
Apr 26, 2001
345
346
347
348
349
350
351
if ( esd_no_spawn == NULL ) {
unsetenv("ESD_NO_SPAWN");
}
#endif
}
}
}
Feb 16, 2006
Feb 16, 2006
352
#endif /* SDL_AUDIO_DRIVER_ESD */
Apr 26, 2001
Apr 26, 2001
353
354
355
if ( audio == NULL ) {
if ( driver_name != NULL ) {
#if 0 /* This will be replaced with a better driver selection API */
Feb 7, 2006
Feb 7, 2006
356
357
if ( SDL_strrchr(driver_name, ':') != NULL ) {
idx = atoi(SDL_strrchr(driver_name, ':')+1);
Apr 26, 2001
Apr 26, 2001
358
359
360
}
#endif
for ( i=0; bootstrap[i]; ++i ) {
Oct 1, 2006
Oct 1, 2006
361
if (SDL_strcasecmp(bootstrap[i]->name, driver_name) == 0) {
Apr 26, 2001
Apr 26, 2001
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
if ( bootstrap[i]->available() ) {
audio=bootstrap[i]->create(idx);
break;
}
}
}
} else {
for ( i=0; bootstrap[i]; ++i ) {
if ( bootstrap[i]->available() ) {
audio = bootstrap[i]->create(idx);
if ( audio != NULL ) {
break;
}
}
}
}
if ( audio == NULL ) {
SDL_SetError("No available audio device");
#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;
Mar 30, 2002
Mar 30, 2002
390
391
392
393
if ( !current_audio->LockAudio && !current_audio->UnlockAudio ) {
current_audio->LockAudio = SDL_LockAudio_Default;
current_audio->UnlockAudio = SDL_UnlockAudio_Default;
}
Apr 26, 2001
Apr 26, 2001
394
395
396
397
398
399
400
}
return(0);
}
char *SDL_AudioDriverName(char *namebuf, int maxlen)
{
if ( current_audio != NULL ) {
Feb 19, 2006
Feb 19, 2006
401
SDL_strlcpy(namebuf, current_audio->name, maxlen);
Apr 26, 2001
Apr 26, 2001
402
403
404
405
406
407
408
409
return(namebuf);
}
return(NULL);
}
int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
{
SDL_AudioDevice *audio;
May 9, 2006
May 9, 2006
410
const char *env;
Apr 26, 2001
Apr 26, 2001
411
412
413
414
415
416
417
418
419
420
/* Start up the audio driver, if necessary */
if ( ! current_audio ) {
if ( (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) ||
(current_audio == NULL) ) {
return(-1);
}
}
audio = current_audio;
Jan 9, 2002
Jan 9, 2002
421
422
423
424
425
if (audio->opened) {
SDL_SetError("Audio device is already opened");
return(-1);
}
Apr 26, 2001
Apr 26, 2001
426
/* Verify some parameters */
May 9, 2006
May 9, 2006
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
if ( desired->freq == 0 ) {
env = SDL_getenv("SDL_AUDIO_FREQUENCY");
if ( env ) {
desired->freq = SDL_atoi(env);
}
}
if ( desired->freq == 0 ) {
/* Pick some default audio frequency */
desired->freq = 22050;
}
if ( desired->format == 0 ) {
env = SDL_getenv("SDL_AUDIO_FORMAT");
if ( env ) {
desired->format = SDL_ParseAudioFormat(env);
}
}
if ( desired->format == 0 ) {
/* Pick some default audio format */
desired->format = AUDIO_S16;
}
if ( desired->channels == 0 ) {
env = SDL_getenv("SDL_AUDIO_CHANNELS");
if ( env ) {
Jul 11, 2007
Jul 11, 2007
450
desired->channels = (Uint8)SDL_atoi(env);
May 9, 2006
May 9, 2006
451
452
453
454
455
}
}
if ( desired->channels == 0 ) {
/* Pick a default number of channels */
desired->channels = 2;
Apr 26, 2001
Apr 26, 2001
456
457
458
459
}
switch ( desired->channels ) {
case 1: /* Mono */
case 2: /* Stereo */
Aug 21, 2004
Aug 21, 2004
460
461
case 4: /* surround */
case 6: /* surround with center and lfe */
Apr 26, 2001
Apr 26, 2001
462
463
464
465
466
break;
default:
SDL_SetError("1 (mono) and 2 (stereo) channels supported");
return(-1);
}
May 9, 2006
May 9, 2006
467
468
469
if ( desired->samples == 0 ) {
env = SDL_getenv("SDL_AUDIO_SAMPLES");
if ( env ) {
Jul 11, 2007
Jul 11, 2007
470
desired->samples = (Uint16)SDL_atoi(env);
May 9, 2006
May 9, 2006
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
}
}
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 ) {
SDL_SetError("SDL_OpenAudio() passed a NULL callback");
return(-1);
}
Apr 26, 2001
Apr 26, 2001
486
Jun 25, 2007
Jun 25, 2007
487
#if SDL_THREADS_DISABLED
Jun 10, 2002
Jun 10, 2002
488
/* Uses interrupt driven audio, without thread */
Apr 26, 2001
Apr 26, 2001
489
490
491
492
493
494
495
496
#else
/* Create a semaphore for locking the sound buffers */
audio->mixer_lock = SDL_CreateMutex();
if ( audio->mixer_lock == NULL ) {
SDL_SetError("Couldn't create mixer lock");
SDL_CloseAudio();
return(-1);
}
Jun 25, 2007
Jun 25, 2007
497
#endif /* SDL_THREADS_DISABLED */
Apr 26, 2001
Apr 26, 2001
498
499
500
501
502
/* Calculate the silence and size of the audio specification */
SDL_CalculateAudioSpec(desired);
/* Open the audio subsystem */
Feb 7, 2006
Feb 7, 2006
503
SDL_memcpy(&audio->spec, desired, sizeof(audio->spec));
Apr 26, 2001
Apr 26, 2001
504
505
506
audio->convert.needed = 0;
audio->enabled = 1;
audio->paused = 1;
May 10, 2001
May 10, 2001
507
Apr 26, 2001
Apr 26, 2001
508
audio->opened = audio->OpenAudio(audio, &audio->spec)+1;
May 10, 2001
May 10, 2001
509
Apr 26, 2001
Apr 26, 2001
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
if ( ! audio->opened ) {
SDL_CloseAudio();
return(-1);
}
/* If the audio driver changes the buffer size, accept it */
if ( audio->spec.samples != desired->samples ) {
desired->samples = audio->spec.samples;
SDL_CalculateAudioSpec(desired);
}
/* Allocate a fake audio memory buffer */
audio->fake_stream = SDL_AllocAudioMem(audio->spec.size);
if ( audio->fake_stream == NULL ) {
SDL_CloseAudio();
SDL_OutOfMemory();
return(-1);
}
/* See if we need to do any conversion */
Feb 11, 2004
Feb 11, 2004
530
if ( obtained != NULL ) {
Feb 7, 2006
Feb 7, 2006
531
SDL_memcpy(obtained, &audio->spec, sizeof(audio->spec));
Feb 11, 2004
Feb 11, 2004
532
533
534
535
536
537
538
539
540
541
542
} else if ( desired->freq != audio->spec.freq ||
desired->format != audio->spec.format ||
desired->channels != audio->spec.channels ) {
/* Build an audio conversion block */
if ( SDL_BuildAudioCVT(&audio->convert,
desired->format, desired->channels,
desired->freq,
audio->spec.format, audio->spec.channels,
audio->spec.freq) < 0 ) {
SDL_CloseAudio();
return(-1);
Apr 26, 2001
Apr 26, 2001
543
}
Feb 11, 2004
Feb 11, 2004
544
if ( audio->convert.needed ) {
Sep 21, 2009
Sep 21, 2009
545
audio->convert.len = (int) ( ((double) audio->spec.size) /
Oct 27, 2006
Oct 27, 2006
546
audio->convert.len_ratio );
Feb 11, 2004
Feb 11, 2004
547
548
549
audio->convert.buf =(Uint8 *)SDL_AllocAudioMem(
audio->convert.len*audio->convert.len_mult);
if ( audio->convert.buf == NULL ) {
Apr 26, 2001
Apr 26, 2001
550
SDL_CloseAudio();
Feb 11, 2004
Feb 11, 2004
551
SDL_OutOfMemory();
Apr 26, 2001
Apr 26, 2001
552
553
554
555
556
557
558
559
560
return(-1);
}
}
}
/* Start the audio thread if necessary */
switch (audio->opened) {
case 1:
/* Start the audio thread */
Jun 24, 2007
Jun 24, 2007
561
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC) && !defined(__SYMBIAN32__)
Feb 6, 2006
Feb 6, 2006
562
563
564
#undef SDL_CreateThread
audio->thread = SDL_CreateThread(SDL_RunAudio, audio, NULL, NULL);
#else
Apr 26, 2001
Apr 26, 2001
565
audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
Feb 6, 2006
Feb 6, 2006
566
#endif
Apr 26, 2001
Apr 26, 2001
567
568
569
570
571
572
573
574
575
576
577
if ( audio->thread == NULL ) {
SDL_CloseAudio();
SDL_SetError("Couldn't create audio thread");
return(-1);
}
break;
default:
/* The audio is now playing */
break;
}
May 10, 2001
May 10, 2001
578
Apr 26, 2001
Apr 26, 2001
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
return(0);
}
SDL_audiostatus SDL_GetAudioStatus(void)
{
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);
}
void SDL_PauseAudio (int pause_on)
{
SDL_AudioDevice *audio = current_audio;
if ( audio ) {
audio->paused = pause_on;
}
}
void SDL_LockAudio (void)
{
SDL_AudioDevice *audio = current_audio;
/* Obtain a lock on the mixing buffers */
Mar 30, 2002
Mar 30, 2002
612
613
if ( audio && audio->LockAudio ) {
audio->LockAudio(audio);
Apr 26, 2001
Apr 26, 2001
614
615
616
617
618
619
620
621
}
}
void SDL_UnlockAudio (void)
{
SDL_AudioDevice *audio = current_audio;
/* Release lock on the mixing buffers */
Mar 30, 2002
Mar 30, 2002
622
623
if ( audio && audio->UnlockAudio ) {
audio->UnlockAudio(audio);
Apr 26, 2001
Apr 26, 2001
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
}
}
void SDL_CloseAudio (void)
{
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}
void SDL_AudioQuit(void)
{
SDL_AudioDevice *audio = current_audio;
if ( audio ) {
audio->enabled = 0;
if ( audio->thread != NULL ) {
SDL_WaitThread(audio->thread, NULL);
}
if ( audio->mixer_lock != NULL ) {
SDL_DestroyMutex(audio->mixer_lock);
}
if ( audio->fake_stream != NULL ) {
SDL_FreeAudioMem(audio->fake_stream);
}
if ( audio->convert.needed ) {
SDL_FreeAudioMem(audio->convert.buf);
May 10, 2001
May 10, 2001
649
Apr 26, 2001
Apr 26, 2001
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
}
if ( audio->opened ) {
audio->CloseAudio(audio);
audio->opened = 0;
}
/* Free the driver data */
audio->free(audio);
current_audio = NULL;
}
}
#define NUM_FORMATS 6
static int format_idx;
static int format_idx_sub;
static Uint16 format_list[NUM_FORMATS][NUM_FORMATS] = {
{ 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 },
};
Uint16 SDL_FirstAudioFormat(Uint16 format)
{
for ( format_idx=0; format_idx < NUM_FORMATS; ++format_idx ) {
if ( format_list[format_idx][0] == format ) {
break;
}
}
format_idx_sub = 0;
return(SDL_NextAudioFormat());
}
Uint16 SDL_NextAudioFormat(void)
{
if ( (format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS) ) {
return(0);
}
return(format_list[format_idx][format_idx_sub++]);
}
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
{
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;
}
Jan 24, 2010
Jan 24, 2010
706
707
708
709
710
711
712
void SDL_Audio_SetCaption(const char *caption)
{
if ((current_audio) && (current_audio->SetCaption)) {
current_audio->SetCaption(current_audio, caption);
}
}