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