/
SDL_audio.c
674 lines (600 loc) · 15.8 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
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
13
Lesser General Public License for more details.
14
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
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
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"
31
32
33
34
35
36
#ifdef __OS2__
// We'll need the DosSetPriority() API!
#define INCL_DOSPROCESS
#include <os2.h>
#endif
37
38
/* Available audio drivers */
static AudioBootStrap *bootstrap[] = {
39
#if SDL_AUDIO_DRIVER_OPENBSD
40
&OPENBSD_AUDIO_bootstrap,
41
#endif
42
#if SDL_AUDIO_DRIVER_OSS
43
44
45
&DSP_bootstrap,
&DMA_bootstrap,
#endif
46
#if SDL_AUDIO_DRIVER_ALSA
47
48
&ALSA_bootstrap,
#endif
49
#if SDL_AUDIO_DRIVER_QNXNTO
50
51
&QNXNTOAUDIO_bootstrap,
#endif
52
#if SDL_AUDIO_DRIVER_SUNAUDIO
53
54
&SUNAUDIO_bootstrap,
#endif
55
#if SDL_AUDIO_DRIVER_DMEDIA
56
&DMEDIA_bootstrap,
57
#endif
58
59
#if SDL_AUDIO_DRIVER_ARTS
&ARTS_bootstrap,
60
#endif
61
#if SDL_AUDIO_DRIVER_ESD
62
63
&ESD_bootstrap,
#endif
64
#if SDL_AUDIO_DRIVER_NAS
65
66
&NAS_bootstrap,
#endif
67
#if SDL_AUDIO_DRIVER_DSOUND
68
69
&DSOUND_bootstrap,
#endif
70
#if SDL_AUDIO_DRIVER_WAVEOUT
71
72
&WAVEOUT_bootstrap,
#endif
73
74
75
76
#if SDL_AUDIO_DRIVER_PAUD
&Paud_bootstrap,
#endif
#if SDL_AUDIO_DRIVER_BAUDIO
77
78
&BAUDIO_bootstrap,
#endif
79
#if SDL_AUDIO_DRIVER_COREAUDIO
80
81
&COREAUDIO_bootstrap,
#endif
82
#if SDL_AUDIO_DRIVER_SNDMGR
83
84
&SNDMGR_bootstrap,
#endif
85
#if SDL_AUDIO_DRIVER_AHI
86
87
&AHI_bootstrap,
#endif
88
#if SDL_AUDIO_DRIVER_MINT
89
90
91
92
93
&MINTAUDIO_GSXB_bootstrap,
&MINTAUDIO_MCSN_bootstrap,
&MINTAUDIO_STFA_bootstrap,
&MINTAUDIO_XBIOS_bootstrap,
&MINTAUDIO_DMA8_bootstrap,
94
#endif
95
#if SDL_AUDIO_DRIVER_DISK
96
&DISKAUD_bootstrap,
97
#endif
98
#if SDL_AUDIO_DRIVER_DC
99
&DCAUD_bootstrap,
100
#endif
101
102
103
104
105
#if SDL_AUDIO_DRIVER_MMEAUDIO
&MMEAUDIO_bootstrap,
#endif
#if SDL_AUDIO_DRIVER_DART
&DART_bootstrap,
106
#endif
107
108
109
110
111
112
113
114
NULL
};
SDL_AudioDevice *current_audio = NULL;
/* Various local functions */
int SDL_AudioInit(const char *driver_name);
void SDL_AudioQuit(void);
115
#if SDL_AUDIO_DRIVER_AHI
116
117
static int audio_configured = 0;
#endif
118
119
120
121
122
123
124
125
126
127
/* The general mixing thread function */
int SDL_RunAudio(void *audiop)
{
SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop;
Uint8 *stream;
int stream_len;
void *udata;
void (*fill)(void *userdata,Uint8 *stream, int len);
int silence;
128
#if SDL_AUDIO_DRIVER_AHI
129
130
131
132
133
134
135
136
137
138
139
140
141
142
int started = 0;
/* AmigaOS NEEDS that the audio driver is opened in the thread that uses it! */
D(bug("Task audio started audio struct:<%lx>...\n",audiop));
D(bug("Before Openaudio..."));
if(audio->OpenAudio(audio, &audio->spec)==-1)
{
D(bug("Open audio failed...\n"));
return(-1);
}
D(bug("OpenAudio...OK\n"));
#endif
143
144
145
146
147
148
149
150
151
152
/* 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;
153
154
#if SDL_AUDIO_DRIVER_AHI
155
156
157
158
159
160
161
audio_configured = 1;
D(bug("Audio configured... Checking for conversion\n"));
SDL_mutexP(audio->mixer_lock);
D(bug("Semaphore obtained...\n"));
#endif
162
163
164
165
166
167
168
169
170
171
172
173
174
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;
}
stream = audio->fake_stream;
175
#if SDL_AUDIO_DRIVER_AHI
176
177
178
179
SDL_mutexV(audio->mixer_lock);
D(bug("Entering audio loop...\n"));
#endif
180
181
182
183
#ifdef __OS2__
// Increase the priority of this thread to make sure that
// the audio will be continuous all the time!
#ifdef USE_DOSSETPRIORITY
184
185
if (SDL_getenv("SDL_USE_TIMECRITICAL_AUDIO"))
{
186
#ifdef DEBUG_BUILD
187
printf("[SDL_RunAudio] : Setting priority to TimeCritical+0! (TID%d)\n", SDL_ThreadID());
188
#endif
189
190
191
192
193
194
195
196
197
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);
}
198
199
#endif
#endif
200
201
202
203
204
205
206
207
/* Loop, filling the audio buffers */
while ( audio->enabled ) {
/* Wait for new current buffer to finish playing */
if ( stream == audio->fake_stream ) {
SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
} else {
208
#if SDL_AUDIO_DRIVER_AHI
209
210
if ( started > 1 )
#endif
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
audio->WaitAudio(audio);
}
/* 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;
}
}
227
SDL_memset(stream, silence, stream_len);
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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;
}
242
SDL_memcpy(stream, audio->convert.buf,
243
244
245
246
247
248
audio->convert.len_cvt);
}
/* Ready current buffer for play and change current buffer */
if ( stream != audio->fake_stream ) {
audio->PlayAudio(audio);
249
#if SDL_AUDIO_DRIVER_AHI
250
251
252
/* AmigaOS don't have to wait the first time audio is played! */
started++;
#endif
253
254
255
256
257
258
}
}
/* Wait for the audio to drain.. */
if ( audio->WaitDone ) {
audio->WaitDone(audio);
}
259
260
#if SDL_AUDIO_DRIVER_AHI
261
262
263
264
265
266
D(bug("WaitAudio...Done\n"));
audio->CloseAudio(audio);
D(bug("CloseAudio..Done, subtask exiting...\n"));
audio_configured = 0;
267
268
269
270
271
#endif
#ifdef __OS2__
#ifdef DEBUG_BUILD
printf("[SDL_RunAudio] : Task exiting. (TID%d)\n", SDL_ThreadID());
#endif
272
#endif
273
274
275
return(0);
}
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
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);
}
292
293
294
295
296
297
298
299
300
301
302
303
304
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();
}
/* Select the proper audio driver */
audio = NULL;
idx = 0;
305
#if SDL_AUDIO_DRIVER_ESD
306
if ( (driver_name == NULL) && (SDL_getenv("ESPEAKER") != NULL) ) {
307
308
309
310
311
/* 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 ) {
312
if ( SDL_strcmp(bootstrap[i]->name, "esd") == 0 ) {
313
#ifdef HAVE_PUTENV
314
315
316
const char *esd_no_spawn;
/* Don't start ESD if it's not running */
317
esd_no_spawn = getenv("ESD_NO_SPAWN");
318
319
320
if ( esd_no_spawn == NULL ) {
putenv("ESD_NO_SPAWN=1");
}
321
#endif
322
323
324
325
if ( bootstrap[i]->available() ) {
audio = bootstrap[i]->create(0);
break;
}
326
#ifdef HAVE_UNSETENV
327
328
329
330
331
332
333
if ( esd_no_spawn == NULL ) {
unsetenv("ESD_NO_SPAWN");
}
#endif
}
}
}
334
#endif /* SDL_AUDIO_DRIVER_ESD */
335
336
337
if ( audio == NULL ) {
if ( driver_name != NULL ) {
#if 0 /* This will be replaced with a better driver selection API */
338
339
if ( SDL_strrchr(driver_name, ':') != NULL ) {
idx = atoi(SDL_strrchr(driver_name, ':')+1);
340
341
342
}
#endif
for ( i=0; bootstrap[i]; ++i ) {
343
344
if (SDL_strncmp(bootstrap[i]->name, driver_name,
SDL_strlen(bootstrap[i]->name)) == 0) {
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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;
373
374
375
376
if ( !current_audio->LockAudio && !current_audio->UnlockAudio ) {
current_audio->LockAudio = SDL_LockAudio_Default;
current_audio->UnlockAudio = SDL_UnlockAudio_Default;
}
377
378
379
380
381
382
383
}
return(0);
}
char *SDL_AudioDriverName(char *namebuf, int maxlen)
{
if ( current_audio != NULL ) {
384
SDL_strlcpy(namebuf, current_audio->name, maxlen);
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
return(namebuf);
}
return(NULL);
}
int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
{
SDL_AudioDevice *audio;
/* 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;
403
404
405
406
407
if (audio->opened) {
SDL_SetError("Audio device is already opened");
return(-1);
}
408
409
410
411
412
413
414
415
/* Verify some parameters */
if ( desired->callback == NULL ) {
SDL_SetError("SDL_OpenAudio() passed a NULL callback");
return(-1);
}
switch ( desired->channels ) {
case 1: /* Mono */
case 2: /* Stereo */
416
417
case 4: /* surround */
case 6: /* surround with center and lfe */
418
419
420
421
422
423
break;
default:
SDL_SetError("1 (mono) and 2 (stereo) channels supported");
return(-1);
}
424
#if defined(__MACOS__) || (defined(__RISCOS__) && SDL_THREADS_DISABLED)
425
/* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */
426
#else
427
#if defined(__MINT__) && SDL_THREADS_DISABLED
428
/* Uses interrupt driven audio, without thread */
429
430
431
432
433
434
435
436
#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);
}
437
#endif /* __MINT__ */
438
#endif /* __MACOS__ */
439
440
441
442
443
/* Calculate the silence and size of the audio specification */
SDL_CalculateAudioSpec(desired);
/* Open the audio subsystem */
444
SDL_memcpy(&audio->spec, desired, sizeof(audio->spec));
445
446
447
audio->convert.needed = 0;
audio->enabled = 1;
audio->paused = 1;
448
449
#if !SDL_AUDIO_DRIVER_AHI
450
451
/* AmigaOS opens audio inside the main loop */
452
audio->opened = audio->OpenAudio(audio, &audio->spec)+1;
453
454
455
456
457
if ( ! audio->opened ) {
SDL_CloseAudio();
return(-1);
}
458
459
460
461
#else
D(bug("Locking semaphore..."));
SDL_mutexP(audio->mixer_lock);
462
463
464
465
466
467
468
469
470
471
472
473
474
475
audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
D(bug("Created thread...\n"));
if ( audio->thread == NULL ) {
SDL_mutexV(audio->mixer_lock);
SDL_CloseAudio();
SDL_SetError("Couldn't create audio thread");
return(-1);
}
while(!audio_configured)
SDL_Delay(100);
#endif
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* 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 */
492
if ( obtained != NULL ) {
493
SDL_memcpy(obtained, &audio->spec, sizeof(audio->spec));
494
495
496
497
498
499
500
501
502
503
504
} 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);
505
}
506
507
508
509
510
if ( audio->convert.needed ) {
audio->convert.len = desired->size;
audio->convert.buf =(Uint8 *)SDL_AllocAudioMem(
audio->convert.len*audio->convert.len_mult);
if ( audio->convert.buf == NULL ) {
511
SDL_CloseAudio();
512
SDL_OutOfMemory();
513
514
515
516
517
return(-1);
}
}
}
518
#if !SDL_AUDIO_DRIVER_AHI
519
520
521
522
/* Start the audio thread if necessary */
switch (audio->opened) {
case 1:
/* Start the audio thread */
523
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
524
525
526
#undef SDL_CreateThread
audio->thread = SDL_CreateThread(SDL_RunAudio, audio, NULL, NULL);
#else
527
audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
528
#endif
529
530
531
532
533
534
535
536
537
538
539
if ( audio->thread == NULL ) {
SDL_CloseAudio();
SDL_SetError("Couldn't create audio thread");
return(-1);
}
break;
default:
/* The audio is now playing */
break;
}
540
541
542
543
544
545
#else
SDL_mutexV(audio->mixer_lock);
D(bug("SDL_OpenAudio USCITA...\n"));
#endif
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
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 */
579
580
if ( audio && audio->LockAudio ) {
audio->LockAudio(audio);
581
582
583
584
585
586
587
588
}
}
void SDL_UnlockAudio (void)
{
SDL_AudioDevice *audio = current_audio;
/* Release lock on the mixing buffers */
589
590
if ( audio && audio->UnlockAudio ) {
audio->UnlockAudio(audio);
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
}
}
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);
616
617
}
618
#if !SDL_AUDIO_DRIVER_AHI
619
620
621
622
if ( audio->opened ) {
audio->CloseAudio(audio);
audio->opened = 0;
}
623
#endif
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
649
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
/* 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;
}