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