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