src/audio/SDL_audio.c
author Sam Lantinga
Tue, 13 Dec 2005 16:40:12 +0000
changeset 1204 f9794ee91dfc
parent 1190 173c063d4f55
child 1312 c9b51268668f
permissions -rw-r--r--
Date: Tue, 13 Dec 2005 13:33:50 +0000
From: "alan buckley"
Subject: Patch to fix audio locking on RISC OS

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