src/audio/SDL_audio.c
author Ryan C. Gordon
Sun, 08 Jul 2012 07:20:50 -0400
branchSDL-1.2
changeset 6353 dfcbd0d9209c
parent 6137 4720145f848b
child 6935 b6b2829cd7ef
permissions -rw-r--r--
Added OpenBSD "sndio" audio target.

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