src/audio/SDL_audio.c
author Sam Lantinga
Mon, 10 Jun 2002 20:42:53 +0000
changeset 398 d219b0e02f5f
parent 322 fd93a09655e3
child 509 dad72daf44b3
permissions -rw-r--r--
Added Atari audio support (thanks Patrice!)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Allow access to a raw mixing buffer */
slouken@0
    29
#include <stdlib.h>
slouken@0
    30
#include <stdio.h>
slouken@0
    31
#include <string.h>
slouken@0
    32
slouken@0
    33
#include "SDL.h"
slouken@0
    34
#include "SDL_audio.h"
slouken@0
    35
#include "SDL_timer.h"
slouken@0
    36
#include "SDL_error.h"
slouken@0
    37
#include "SDL_audio_c.h"
slouken@0
    38
#include "SDL_audiomem.h"
slouken@0
    39
#include "SDL_sysaudio.h"
slouken@0
    40
slouken@0
    41
/* Available audio drivers */
slouken@0
    42
static AudioBootStrap *bootstrap[] = {
slouken@121
    43
#ifdef OPENBSD_AUDIO_SUPPORT
slouken@121
    44
	&OPENBSD_AUDIO_bootstrap,
slouken@94
    45
#endif
slouken@0
    46
#ifdef OSS_SUPPORT
slouken@0
    47
	&DSP_bootstrap,
slouken@0
    48
	&DMA_bootstrap,
slouken@0
    49
#endif
slouken@0
    50
#ifdef ALSA_SUPPORT
slouken@0
    51
	&ALSA_bootstrap,
slouken@0
    52
#endif
slouken@148
    53
#ifdef SUNAUDIO_SUPPORT
slouken@148
    54
	&SUNAUDIO_bootstrap,
slouken@148
    55
#endif
slouken@148
    56
#ifdef DMEDIA_SUPPORT
slouken@148
    57
	&DMEDIA_bootstrap,
slouken@35
    58
#endif
slouken@0
    59
#ifdef ARTSC_SUPPORT
slouken@0
    60
	&ARTSC_bootstrap,
slouken@0
    61
#endif
slouken@0
    62
#ifdef ESD_SUPPORT
slouken@0
    63
	&ESD_bootstrap,
slouken@0
    64
#endif
slouken@0
    65
#ifdef NAS_SUPPORT
slouken@0
    66
	&NAS_bootstrap,
slouken@0
    67
#endif
slouken@0
    68
#ifdef ENABLE_DIRECTX
slouken@0
    69
	&DSOUND_bootstrap,
slouken@0
    70
#endif
slouken@0
    71
#ifdef ENABLE_WINDIB
slouken@0
    72
	&WAVEOUT_bootstrap,
slouken@0
    73
#endif
slouken@0
    74
#ifdef __BEOS__
slouken@0
    75
	&BAUDIO_bootstrap,
slouken@0
    76
#endif
slouken@0
    77
#if defined(macintosh) || TARGET_API_MAC_CARBON
slouken@0
    78
	&SNDMGR_bootstrap,
slouken@0
    79
#endif
slouken@0
    80
#ifdef _AIX
slouken@0
    81
	&Paud_bootstrap,
slouken@0
    82
#endif
slouken@21
    83
#ifdef ENABLE_AHI
slouken@21
    84
	&AHI_bootstrap,
slouken@21
    85
#endif
slouken@398
    86
#ifdef MINTAUDIO_SUPPORT
slouken@398
    87
	&MINTAUDIO_bootstrap,
slouken@398
    88
#endif
slouken@68
    89
#ifdef DISKAUD_SUPPORT
slouken@68
    90
	&DISKAUD_bootstrap,
slouken@68
    91
#endif
slouken@0
    92
	NULL
slouken@0
    93
};
slouken@0
    94
SDL_AudioDevice *current_audio = NULL;
slouken@0
    95
slouken@0
    96
/* Various local functions */
slouken@0
    97
int SDL_AudioInit(const char *driver_name);
slouken@0
    98
void SDL_AudioQuit(void);
slouken@0
    99
slouken@21
   100
#ifdef ENABLE_AHI
slouken@21
   101
static int audio_configured = 0;
slouken@21
   102
#endif
slouken@0
   103
slouken@0
   104
/* The general mixing thread function */
slouken@0
   105
int SDL_RunAudio(void *audiop)
slouken@0
   106
{
slouken@0
   107
	SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop;
slouken@0
   108
	Uint8 *stream;
slouken@0
   109
	int    stream_len;
slouken@0
   110
	void  *udata;
slouken@0
   111
	void (*fill)(void *userdata,Uint8 *stream, int len);
slouken@0
   112
	int    silence;
slouken@21
   113
#ifdef ENABLE_AHI
slouken@21
   114
	int started = 0;
slouken@21
   115
slouken@21
   116
/* AmigaOS NEEDS that the audio driver is opened in the thread that uses it! */
slouken@21
   117
slouken@21
   118
	D(bug("Task audio started audio struct:<%lx>...\n",audiop));
slouken@21
   119
slouken@21
   120
	D(bug("Before Openaudio..."));
slouken@21
   121
	if(audio->OpenAudio(audio, &audio->spec)==-1)
slouken@21
   122
	{
slouken@21
   123
		D(bug("Open audio failed...\n"));
slouken@21
   124
		return(-1);
slouken@21
   125
	}
slouken@21
   126
	D(bug("OpenAudio...OK\n"));
slouken@21
   127
#endif
slouken@0
   128
slouken@0
   129
	/* Perform any thread setup */
slouken@0
   130
	if ( audio->ThreadInit ) {
slouken@0
   131
		audio->ThreadInit(audio);
slouken@0
   132
	}
slouken@0
   133
	audio->threadid = SDL_ThreadID();
slouken@0
   134
slouken@0
   135
	/* Set up the mixing function */
slouken@0
   136
	fill  = audio->spec.callback;
slouken@0
   137
	udata = audio->spec.userdata;
slouken@21
   138
slouken@21
   139
#ifdef ENABLE_AHI
slouken@21
   140
	audio_configured = 1;
slouken@21
   141
slouken@21
   142
	D(bug("Audio configured... Checking for conversion\n"));
slouken@21
   143
	SDL_mutexP(audio->mixer_lock);
slouken@21
   144
	D(bug("Semaphore obtained...\n"));
slouken@21
   145
#endif
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
	}
slouken@0
   158
	stream = audio->fake_stream;
slouken@0
   159
slouken@21
   160
#ifdef ENABLE_AHI
slouken@21
   161
	SDL_mutexV(audio->mixer_lock);
slouken@21
   162
	D(bug("Entering audio loop...\n"));
slouken@21
   163
#endif
slouken@21
   164
slouken@21
   165
slouken@0
   166
	/* Loop, filling the audio buffers */
slouken@0
   167
	while ( audio->enabled ) {
slouken@0
   168
slouken@0
   169
		/* Wait for new current buffer to finish playing */
slouken@0
   170
		if ( stream == audio->fake_stream ) {
slouken@0
   171
			SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
slouken@0
   172
		} else {
slouken@21
   173
#ifdef ENABLE_AHI
slouken@21
   174
			if ( started > 1 )
slouken@21
   175
#endif
slouken@0
   176
			audio->WaitAudio(audio);
slouken@0
   177
		}
slouken@0
   178
slouken@0
   179
		/* Fill the current buffer with sound */
slouken@0
   180
		if ( audio->convert.needed ) {
slouken@0
   181
			if ( audio->convert.buf ) {
slouken@0
   182
				stream = audio->convert.buf;
slouken@0
   183
			} else {
slouken@0
   184
				continue;
slouken@0
   185
			}
slouken@0
   186
		} else {
slouken@0
   187
			stream = audio->GetAudioBuf(audio);
slouken@0
   188
			if ( stream == NULL ) {
slouken@0
   189
				stream = audio->fake_stream;
slouken@0
   190
			}
slouken@0
   191
		}
slouken@0
   192
		memset(stream, silence, stream_len);
slouken@0
   193
slouken@0
   194
		if ( ! audio->paused ) {
slouken@0
   195
			SDL_mutexP(audio->mixer_lock);
slouken@0
   196
			(*fill)(udata, stream, stream_len);
slouken@0
   197
			SDL_mutexV(audio->mixer_lock);
slouken@0
   198
		}
slouken@0
   199
slouken@0
   200
		/* Convert the audio if necessary */
slouken@0
   201
		if ( audio->convert.needed ) {
slouken@0
   202
			SDL_ConvertAudio(&audio->convert);
slouken@0
   203
			stream = audio->GetAudioBuf(audio);
slouken@0
   204
			if ( stream == NULL ) {
slouken@0
   205
				stream = audio->fake_stream;
slouken@0
   206
			}
slouken@0
   207
			memcpy(stream, audio->convert.buf,
slouken@0
   208
			               audio->convert.len_cvt);
slouken@0
   209
		}
slouken@0
   210
slouken@0
   211
		/* Ready current buffer for play and change current buffer */
slouken@0
   212
		if ( stream != audio->fake_stream ) {
slouken@0
   213
			audio->PlayAudio(audio);
slouken@21
   214
#ifdef ENABLE_AHI
slouken@21
   215
/* AmigaOS don't have to wait the first time audio is played! */
slouken@21
   216
			started++;
slouken@21
   217
#endif
slouken@0
   218
		}
slouken@0
   219
	}
slouken@0
   220
	/* Wait for the audio to drain.. */
slouken@0
   221
	if ( audio->WaitDone ) {
slouken@0
   222
		audio->WaitDone(audio);
slouken@0
   223
	}
slouken@21
   224
slouken@21
   225
#ifdef ENABLE_AHI
slouken@21
   226
	D(bug("WaitAudio...Done\n"));
slouken@21
   227
slouken@21
   228
	audio->CloseAudio(audio);
slouken@21
   229
slouken@21
   230
	D(bug("CloseAudio..Done, subtask exiting...\n"));
slouken@21
   231
	audio_configured = 0;
slouken@21
   232
#endif
slouken@0
   233
	return(0);
slouken@0
   234
}
slouken@0
   235
slouken@322
   236
static void SDL_LockAudio_Default(SDL_AudioDevice *audio)
slouken@322
   237
{
slouken@322
   238
	if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
slouken@322
   239
		return;
slouken@322
   240
	}
slouken@322
   241
	SDL_mutexP(audio->mixer_lock);
slouken@322
   242
}
slouken@322
   243
slouken@322
   244
static void SDL_UnlockAudio_Default(SDL_AudioDevice *audio)
slouken@322
   245
{
slouken@322
   246
	if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
slouken@322
   247
		return;
slouken@322
   248
	}
slouken@322
   249
	SDL_mutexV(audio->mixer_lock);
slouken@322
   250
}
slouken@322
   251
slouken@0
   252
int SDL_AudioInit(const char *driver_name)
slouken@0
   253
{
slouken@0
   254
	SDL_AudioDevice *audio;
slouken@0
   255
	int i = 0, idx;
slouken@0
   256
slouken@0
   257
	/* Check to make sure we don't overwrite 'current_audio' */
slouken@0
   258
	if ( current_audio != NULL ) {
slouken@0
   259
		SDL_AudioQuit();
slouken@0
   260
	}
slouken@0
   261
slouken@0
   262
	/* Select the proper audio driver */
slouken@0
   263
	audio = NULL;
slouken@0
   264
	idx = 0;
slouken@0
   265
#ifdef unix
slouken@0
   266
	if ( (driver_name == NULL) && (getenv("ESPEAKER") != NULL) ) {
slouken@0
   267
		/* Ahem, we know that if ESPEAKER is set, user probably wants
slouken@0
   268
		   to use ESD, but don't start it if it's not already running.
slouken@0
   269
		   This probably isn't the place to do this, but... Shh! :)
slouken@0
   270
		 */
slouken@0
   271
		for ( i=0; bootstrap[i]; ++i ) {
slouken@0
   272
			if ( strcmp(bootstrap[i]->name, "esd") == 0 ) {
slouken@0
   273
				const char *esd_no_spawn;
slouken@0
   274
slouken@0
   275
				/* Don't start ESD if it's not running */
slouken@0
   276
				esd_no_spawn = getenv("ESD_NO_SPAWN");
slouken@0
   277
				if ( esd_no_spawn == NULL ) {
slouken@0
   278
					putenv("ESD_NO_SPAWN=1");
slouken@0
   279
				}
slouken@0
   280
				if ( bootstrap[i]->available() ) {
slouken@0
   281
					audio = bootstrap[i]->create(0);
slouken@0
   282
					break;
slouken@0
   283
				}
slouken@0
   284
#ifdef linux	/* No unsetenv() on most platforms */
slouken@0
   285
				if ( esd_no_spawn == NULL ) {
slouken@0
   286
					unsetenv("ESD_NO_SPAWN");
slouken@0
   287
				}
slouken@0
   288
#endif
slouken@0
   289
			}
slouken@0
   290
		}
slouken@0
   291
	}
slouken@0
   292
#endif /* unix */
slouken@0
   293
	if ( audio == NULL ) {
slouken@0
   294
		if ( driver_name != NULL ) {
slouken@0
   295
#if 0	/* This will be replaced with a better driver selection API */
slouken@0
   296
			if ( strrchr(driver_name, ':') != NULL ) {
slouken@0
   297
				idx = atoi(strrchr(driver_name, ':')+1);
slouken@0
   298
			}
slouken@0
   299
#endif
slouken@0
   300
			for ( i=0; bootstrap[i]; ++i ) {
slouken@0
   301
				if (strncmp(bootstrap[i]->name, driver_name,
slouken@0
   302
				            strlen(bootstrap[i]->name)) == 0) {
slouken@0
   303
					if ( bootstrap[i]->available() ) {
slouken@0
   304
						audio=bootstrap[i]->create(idx);
slouken@0
   305
						break;
slouken@0
   306
					}
slouken@0
   307
				}
slouken@0
   308
			}
slouken@0
   309
		} else {
slouken@0
   310
			for ( i=0; bootstrap[i]; ++i ) {
slouken@0
   311
				if ( bootstrap[i]->available() ) {
slouken@0
   312
					audio = bootstrap[i]->create(idx);
slouken@0
   313
					if ( audio != NULL ) {
slouken@0
   314
						break;
slouken@0
   315
					}
slouken@0
   316
				}
slouken@0
   317
			}
slouken@0
   318
		}
slouken@0
   319
		if ( audio == NULL ) {
slouken@0
   320
			SDL_SetError("No available audio device");
slouken@0
   321
#if 0 /* Don't fail SDL_Init() if audio isn't available.
slouken@0
   322
         SDL_OpenAudio() will handle it at that point.  *sigh*
slouken@0
   323
       */
slouken@0
   324
			return(-1);
slouken@0
   325
#endif
slouken@0
   326
		}
slouken@0
   327
	}
slouken@0
   328
	current_audio = audio;
slouken@0
   329
	if ( current_audio ) {
slouken@0
   330
		current_audio->name = bootstrap[i]->name;
slouken@322
   331
		if ( !current_audio->LockAudio && !current_audio->UnlockAudio ) {
slouken@322
   332
			current_audio->LockAudio = SDL_LockAudio_Default;
slouken@322
   333
			current_audio->UnlockAudio = SDL_UnlockAudio_Default;
slouken@322
   334
		}
slouken@0
   335
	}
slouken@0
   336
	return(0);
slouken@0
   337
}
slouken@0
   338
slouken@0
   339
char *SDL_AudioDriverName(char *namebuf, int maxlen)
slouken@0
   340
{
slouken@0
   341
	if ( current_audio != NULL ) {
slouken@0
   342
		strncpy(namebuf, current_audio->name, maxlen-1);
slouken@0
   343
		namebuf[maxlen-1] = '\0';
slouken@0
   344
		return(namebuf);
slouken@0
   345
	}
slouken@0
   346
	return(NULL);
slouken@0
   347
}
slouken@0
   348
slouken@0
   349
int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
slouken@0
   350
{
slouken@0
   351
	SDL_AudioDevice *audio;
slouken@0
   352
slouken@0
   353
	/* Start up the audio driver, if necessary */
slouken@0
   354
	if ( ! current_audio ) {
slouken@0
   355
		if ( (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) ||
slouken@0
   356
		     (current_audio == NULL) ) {
slouken@0
   357
			return(-1);
slouken@0
   358
		}
slouken@0
   359
	}
slouken@0
   360
	audio = current_audio;
slouken@0
   361
slouken@262
   362
	if (audio->opened) {
slouken@262
   363
		SDL_SetError("Audio device is already opened");
slouken@262
   364
		return(-1);
slouken@262
   365
	}
slouken@262
   366
slouken@0
   367
	/* Verify some parameters */
slouken@0
   368
	if ( desired->callback == NULL ) {
slouken@0
   369
		SDL_SetError("SDL_OpenAudio() passed a NULL callback");
slouken@0
   370
		return(-1);
slouken@0
   371
	}
slouken@0
   372
	switch ( desired->channels ) {
slouken@0
   373
	    case 1:	/* Mono */
slouken@0
   374
	    case 2:	/* Stereo */
slouken@0
   375
		break;
slouken@0
   376
	    default:
slouken@0
   377
		SDL_SetError("1 (mono) and 2 (stereo) channels supported");
slouken@0
   378
		return(-1);
slouken@0
   379
	}
slouken@0
   380
slouken@0
   381
#ifdef macintosh
slouken@0
   382
	/* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */
slouken@0
   383
#else
slouken@398
   384
#if defined(__MINT__) && !defined(ENABLE_THREADS)
slouken@398
   385
	/* Uses interrupt driven audio, without thread */
slouken@398
   386
#else
slouken@0
   387
	/* Create a semaphore for locking the sound buffers */
slouken@0
   388
	audio->mixer_lock = SDL_CreateMutex();
slouken@0
   389
	if ( audio->mixer_lock == NULL ) {
slouken@0
   390
		SDL_SetError("Couldn't create mixer lock");
slouken@0
   391
		SDL_CloseAudio();
slouken@0
   392
		return(-1);
slouken@0
   393
	}
slouken@398
   394
#endif /* __MINT__ */
slouken@398
   395
#endif /* macintosh */
slouken@0
   396
slouken@0
   397
	/* Calculate the silence and size of the audio specification */
slouken@0
   398
	SDL_CalculateAudioSpec(desired);
slouken@0
   399
slouken@0
   400
	/* Open the audio subsystem */
slouken@0
   401
	memcpy(&audio->spec, desired, sizeof(audio->spec));
slouken@0
   402
	audio->convert.needed = 0;
slouken@0
   403
	audio->enabled = 1;
slouken@0
   404
	audio->paused  = 1;
slouken@21
   405
slouken@21
   406
#ifndef ENABLE_AHI
slouken@21
   407
slouken@21
   408
/* AmigaOS opens audio inside the main loop */
slouken@0
   409
	audio->opened = audio->OpenAudio(audio, &audio->spec)+1;
slouken@21
   410
slouken@0
   411
	if ( ! audio->opened ) {
slouken@0
   412
		SDL_CloseAudio();
slouken@0
   413
		return(-1);
slouken@0
   414
	}
slouken@21
   415
#else
slouken@21
   416
	D(bug("Locking semaphore..."));
slouken@21
   417
	SDL_mutexP(audio->mixer_lock);
slouken@21
   418
slouken@21
   419
	audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
slouken@21
   420
	D(bug("Created thread...\n"));
slouken@21
   421
slouken@21
   422
	if ( audio->thread == NULL ) {
slouken@21
   423
		SDL_mutexV(audio->mixer_lock);
slouken@21
   424
		SDL_CloseAudio();
slouken@21
   425
		SDL_SetError("Couldn't create audio thread");
slouken@21
   426
		return(-1);
slouken@21
   427
	}
slouken@21
   428
slouken@21
   429
	while(!audio_configured)
slouken@21
   430
		SDL_Delay(100);
slouken@21
   431
#endif
slouken@0
   432
slouken@0
   433
	/* If the audio driver changes the buffer size, accept it */
slouken@0
   434
	if ( audio->spec.samples != desired->samples ) {
slouken@0
   435
		desired->samples = audio->spec.samples;
slouken@0
   436
		SDL_CalculateAudioSpec(desired);
slouken@0
   437
	}
slouken@0
   438
slouken@0
   439
	/* Allocate a fake audio memory buffer */
slouken@0
   440
	audio->fake_stream = SDL_AllocAudioMem(audio->spec.size);
slouken@0
   441
	if ( audio->fake_stream == NULL ) {
slouken@0
   442
		SDL_CloseAudio();
slouken@0
   443
		SDL_OutOfMemory();
slouken@0
   444
		return(-1);
slouken@0
   445
	}
slouken@0
   446
slouken@0
   447
	/* See if we need to do any conversion */
slouken@0
   448
	if ( memcmp(desired, &audio->spec, sizeof(audio->spec)) == 0 ) {
slouken@0
   449
		/* Just copy over the desired audio specification */
slouken@0
   450
		if ( obtained != NULL ) {
slouken@0
   451
			memcpy(obtained, &audio->spec, sizeof(audio->spec));
slouken@0
   452
		}
slouken@0
   453
	} else {
slouken@0
   454
		/* Copy over the audio specification if possible */
slouken@0
   455
		if ( obtained != NULL ) {
slouken@0
   456
			memcpy(obtained, &audio->spec, sizeof(audio->spec));
slouken@0
   457
		} else {
slouken@0
   458
			/* Build an audio conversion block */
slouken@0
   459
			if ( SDL_BuildAudioCVT(&audio->convert,
slouken@0
   460
				desired->format, desired->channels,
slouken@0
   461
						desired->freq,
slouken@0
   462
				audio->spec.format, audio->spec.channels,
slouken@0
   463
						audio->spec.freq) < 0 ) {
slouken@0
   464
				SDL_CloseAudio();
slouken@0
   465
				return(-1);
slouken@0
   466
			}
slouken@0
   467
			if ( audio->convert.needed ) {
slouken@0
   468
				audio->convert.len = desired->size;
slouken@0
   469
				audio->convert.buf =(Uint8 *)SDL_AllocAudioMem(
slouken@0
   470
				   audio->convert.len*audio->convert.len_mult);
slouken@0
   471
				if ( audio->convert.buf == NULL ) {
slouken@0
   472
					SDL_CloseAudio();
slouken@0
   473
					SDL_OutOfMemory();
slouken@0
   474
					return(-1);
slouken@0
   475
				}
slouken@0
   476
			}
slouken@0
   477
		}
slouken@0
   478
	}
slouken@0
   479
slouken@21
   480
#ifndef ENABLE_AHI
slouken@0
   481
	/* Start the audio thread if necessary */
slouken@0
   482
	switch (audio->opened) {
slouken@0
   483
		case  1:
slouken@0
   484
			/* Start the audio thread */
slouken@0
   485
			audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
slouken@0
   486
			if ( audio->thread == NULL ) {
slouken@0
   487
				SDL_CloseAudio();
slouken@0
   488
				SDL_SetError("Couldn't create audio thread");
slouken@0
   489
				return(-1);
slouken@0
   490
			}
slouken@0
   491
			break;
slouken@0
   492
slouken@0
   493
		default:
slouken@0
   494
			/* The audio is now playing */
slouken@0
   495
			break;
slouken@0
   496
	}
slouken@21
   497
#else
slouken@21
   498
	SDL_mutexV(audio->mixer_lock);
slouken@21
   499
	D(bug("SDL_OpenAudio USCITA...\n"));
slouken@21
   500
slouken@21
   501
#endif
slouken@21
   502
slouken@0
   503
	return(0);
slouken@0
   504
}
slouken@0
   505
slouken@0
   506
SDL_audiostatus SDL_GetAudioStatus(void)
slouken@0
   507
{
slouken@0
   508
	SDL_AudioDevice *audio = current_audio;
slouken@0
   509
	SDL_audiostatus status;
slouken@0
   510
slouken@0
   511
	status = SDL_AUDIO_STOPPED;
slouken@0
   512
	if ( audio && audio->enabled ) {
slouken@0
   513
		if ( audio->paused ) {
slouken@0
   514
			status = SDL_AUDIO_PAUSED;
slouken@0
   515
		} else {
slouken@0
   516
			status = SDL_AUDIO_PLAYING;
slouken@0
   517
		}
slouken@0
   518
	}
slouken@0
   519
	return(status);
slouken@0
   520
}
slouken@0
   521
slouken@0
   522
void SDL_PauseAudio (int pause_on)
slouken@0
   523
{
slouken@0
   524
	SDL_AudioDevice *audio = current_audio;
slouken@0
   525
slouken@0
   526
	if ( audio ) {
slouken@0
   527
		audio->paused = pause_on;
slouken@0
   528
	}
slouken@0
   529
}
slouken@0
   530
slouken@0
   531
void SDL_LockAudio (void)
slouken@0
   532
{
slouken@0
   533
	SDL_AudioDevice *audio = current_audio;
slouken@0
   534
slouken@0
   535
	/* Obtain a lock on the mixing buffers */
slouken@322
   536
	if ( audio && audio->LockAudio ) {
slouken@322
   537
		audio->LockAudio(audio);
slouken@0
   538
	}
slouken@0
   539
}
slouken@0
   540
slouken@0
   541
void SDL_UnlockAudio (void)
slouken@0
   542
{
slouken@0
   543
	SDL_AudioDevice *audio = current_audio;
slouken@0
   544
slouken@0
   545
	/* Release lock on the mixing buffers */
slouken@322
   546
	if ( audio && audio->UnlockAudio ) {
slouken@322
   547
		audio->UnlockAudio(audio);
slouken@0
   548
	}
slouken@0
   549
}
slouken@0
   550
slouken@0
   551
void SDL_CloseAudio (void)
slouken@0
   552
{
slouken@0
   553
	SDL_QuitSubSystem(SDL_INIT_AUDIO);
slouken@0
   554
}
slouken@0
   555
slouken@0
   556
void SDL_AudioQuit(void)
slouken@0
   557
{
slouken@0
   558
	SDL_AudioDevice *audio = current_audio;
slouken@0
   559
slouken@0
   560
	if ( audio ) {
slouken@0
   561
		audio->enabled = 0;
slouken@0
   562
		if ( audio->thread != NULL ) {
slouken@0
   563
			SDL_WaitThread(audio->thread, NULL);
slouken@0
   564
		}
slouken@0
   565
		if ( audio->mixer_lock != NULL ) {
slouken@0
   566
			SDL_DestroyMutex(audio->mixer_lock);
slouken@0
   567
		}
slouken@0
   568
		if ( audio->fake_stream != NULL ) {
slouken@0
   569
			SDL_FreeAudioMem(audio->fake_stream);
slouken@0
   570
		}
slouken@0
   571
		if ( audio->convert.needed ) {
slouken@0
   572
			SDL_FreeAudioMem(audio->convert.buf);
slouken@21
   573
slouken@0
   574
		}
slouken@21
   575
#ifndef ENABLE_AHI
slouken@0
   576
		if ( audio->opened ) {
slouken@0
   577
			audio->CloseAudio(audio);
slouken@0
   578
			audio->opened = 0;
slouken@0
   579
		}
slouken@21
   580
#endif
slouken@0
   581
		/* Free the driver data */
slouken@0
   582
		audio->free(audio);
slouken@0
   583
		current_audio = NULL;
slouken@0
   584
	}
slouken@0
   585
}
slouken@0
   586
slouken@0
   587
#define NUM_FORMATS	6
slouken@0
   588
static int format_idx;
slouken@0
   589
static int format_idx_sub;
slouken@0
   590
static Uint16 format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@0
   591
 { AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB },
slouken@0
   592
 { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB },
slouken@0
   593
 { AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8 },
slouken@0
   594
 { AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8 },
slouken@0
   595
 { AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U8, AUDIO_S8 },
slouken@0
   596
 { AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U8, AUDIO_S8 },
slouken@0
   597
};
slouken@0
   598
slouken@0
   599
Uint16 SDL_FirstAudioFormat(Uint16 format)
slouken@0
   600
{
slouken@0
   601
	for ( format_idx=0; format_idx < NUM_FORMATS; ++format_idx ) {
slouken@0
   602
		if ( format_list[format_idx][0] == format ) {
slouken@0
   603
			break;
slouken@0
   604
		}
slouken@0
   605
	}
slouken@0
   606
	format_idx_sub = 0;
slouken@0
   607
	return(SDL_NextAudioFormat());
slouken@0
   608
}
slouken@0
   609
slouken@0
   610
Uint16 SDL_NextAudioFormat(void)
slouken@0
   611
{
slouken@0
   612
	if ( (format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS) ) {
slouken@0
   613
		return(0);
slouken@0
   614
	}
slouken@0
   615
	return(format_list[format_idx][format_idx_sub++]);
slouken@0
   616
}
slouken@0
   617
slouken@0
   618
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
slouken@0
   619
{
slouken@0
   620
	switch (spec->format) {
slouken@0
   621
		case AUDIO_U8:
slouken@0
   622
			spec->silence = 0x80;
slouken@0
   623
			break;
slouken@0
   624
		default:
slouken@0
   625
			spec->silence = 0x00;
slouken@0
   626
			break;
slouken@0
   627
	}
slouken@0
   628
	spec->size = (spec->format&0xFF)/8;
slouken@0
   629
	spec->size *= spec->channels;
slouken@0
   630
	spec->size *= spec->samples;
slouken@0
   631
}