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