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