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