src/audio/SDL_audio.c
changeset 0 74212992fb08
child 21 75a95f82bc1f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/SDL_audio.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,516 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Allow access to a raw mixing buffer */
    1.32 +#include <stdlib.h>
    1.33 +#include <stdio.h>
    1.34 +#include <string.h>
    1.35 +
    1.36 +#include "SDL.h"
    1.37 +#include "SDL_audio.h"
    1.38 +#include "SDL_timer.h"
    1.39 +#include "SDL_error.h"
    1.40 +#include "SDL_audio_c.h"
    1.41 +#include "SDL_audiomem.h"
    1.42 +#include "SDL_sysaudio.h"
    1.43 +
    1.44 +/* Available audio drivers */
    1.45 +static AudioBootStrap *bootstrap[] = {
    1.46 +#if defined(unix) && \
    1.47 +   !defined(linux) && !defined(__FreeBSD__) && !defined(__CYGWIN32__) \
    1.48 +   && !defined(__bsdi__)
    1.49 +	&AUDIO_bootstrap,
    1.50 +#endif
    1.51 +#ifdef OSS_SUPPORT
    1.52 +	&DSP_bootstrap,
    1.53 +	&DMA_bootstrap,
    1.54 +#endif
    1.55 +#ifdef ALSA_SUPPORT
    1.56 +	&ALSA_bootstrap,
    1.57 +#endif
    1.58 +#ifdef ARTSC_SUPPORT
    1.59 +	&ARTSC_bootstrap,
    1.60 +#endif
    1.61 +#ifdef ESD_SUPPORT
    1.62 +	&ESD_bootstrap,
    1.63 +#endif
    1.64 +#ifdef NAS_SUPPORT
    1.65 +	&NAS_bootstrap,
    1.66 +#endif
    1.67 +#ifdef ENABLE_DIRECTX
    1.68 +	&DSOUND_bootstrap,
    1.69 +#endif
    1.70 +#ifdef ENABLE_WINDIB
    1.71 +	&WAVEOUT_bootstrap,
    1.72 +#endif
    1.73 +#ifdef __BEOS__
    1.74 +	&BAUDIO_bootstrap,
    1.75 +#endif
    1.76 +#if defined(macintosh) || TARGET_API_MAC_CARBON
    1.77 +	&SNDMGR_bootstrap,
    1.78 +#endif
    1.79 +#ifdef _AIX
    1.80 +	&Paud_bootstrap,
    1.81 +#endif
    1.82 +	NULL
    1.83 +};
    1.84 +SDL_AudioDevice *current_audio = NULL;
    1.85 +
    1.86 +/* Various local functions */
    1.87 +int SDL_AudioInit(const char *driver_name);
    1.88 +void SDL_AudioQuit(void);
    1.89 +
    1.90 +
    1.91 +/* The general mixing thread function */
    1.92 +int SDL_RunAudio(void *audiop)
    1.93 +{
    1.94 +	SDL_AudioDevice *audio = (SDL_AudioDevice *)audiop;
    1.95 +	Uint8 *stream;
    1.96 +	int    stream_len;
    1.97 +	void  *udata;
    1.98 +	void (*fill)(void *userdata,Uint8 *stream, int len);
    1.99 +	int    silence;
   1.100 +
   1.101 +	/* Perform any thread setup */
   1.102 +	if ( audio->ThreadInit ) {
   1.103 +		audio->ThreadInit(audio);
   1.104 +	}
   1.105 +	audio->threadid = SDL_ThreadID();
   1.106 +
   1.107 +	/* Set up the mixing function */
   1.108 +	fill  = audio->spec.callback;
   1.109 +	udata = audio->spec.userdata;
   1.110 +	if ( audio->convert.needed ) {
   1.111 +		if ( audio->convert.src_format == AUDIO_U8 ) {
   1.112 +			silence = 0x80;
   1.113 +		} else {
   1.114 +			silence = 0;
   1.115 +		}
   1.116 +		stream_len = audio->convert.len;
   1.117 +	} else {
   1.118 +		silence = audio->spec.silence;
   1.119 +		stream_len = audio->spec.size;
   1.120 +	}
   1.121 +	stream = audio->fake_stream;
   1.122 +
   1.123 +	/* Loop, filling the audio buffers */
   1.124 +	while ( audio->enabled ) {
   1.125 +
   1.126 +		/* Wait for new current buffer to finish playing */
   1.127 +		if ( stream == audio->fake_stream ) {
   1.128 +			SDL_Delay((audio->spec.samples*1000)/audio->spec.freq);
   1.129 +		} else {
   1.130 +			audio->WaitAudio(audio);
   1.131 +		}
   1.132 +
   1.133 +		/* Fill the current buffer with sound */
   1.134 +		if ( audio->convert.needed ) {
   1.135 +			/* The buffer may not be allocated yet */
   1.136 +			if ( audio->convert.buf ) {
   1.137 +				stream = audio->convert.buf;
   1.138 +			} else {
   1.139 +				continue;
   1.140 +			}
   1.141 +		} else {
   1.142 +			stream = audio->GetAudioBuf(audio);
   1.143 +			if ( stream == NULL ) {
   1.144 +				stream = audio->fake_stream;
   1.145 +			}
   1.146 +		}
   1.147 +		memset(stream, silence, stream_len);
   1.148 +
   1.149 +		if ( ! audio->paused ) {
   1.150 +			SDL_mutexP(audio->mixer_lock);
   1.151 +			(*fill)(udata, stream, stream_len);
   1.152 +			SDL_mutexV(audio->mixer_lock);
   1.153 +		}
   1.154 +
   1.155 +		/* Convert the audio if necessary */
   1.156 +		if ( audio->convert.needed ) {
   1.157 +			SDL_ConvertAudio(&audio->convert);
   1.158 +			stream = audio->GetAudioBuf(audio);
   1.159 +			if ( stream == NULL ) {
   1.160 +				stream = audio->fake_stream;
   1.161 +			}
   1.162 +			memcpy(stream, audio->convert.buf,
   1.163 +			               audio->convert.len_cvt);
   1.164 +		}
   1.165 +
   1.166 +		/* Ready current buffer for play and change current buffer */
   1.167 +		if ( stream != audio->fake_stream ) {
   1.168 +			audio->PlayAudio(audio);
   1.169 +		}
   1.170 +	}
   1.171 +	/* Wait for the audio to drain.. */
   1.172 +	if ( audio->WaitDone ) {
   1.173 +		audio->WaitDone(audio);
   1.174 +	}
   1.175 +	return(0);
   1.176 +}
   1.177 +
   1.178 +int SDL_AudioInit(const char *driver_name)
   1.179 +{
   1.180 +	SDL_AudioDevice *audio;
   1.181 +	int i = 0, idx;
   1.182 +
   1.183 +	/* Check to make sure we don't overwrite 'current_audio' */
   1.184 +	if ( current_audio != NULL ) {
   1.185 +		SDL_AudioQuit();
   1.186 +	}
   1.187 +
   1.188 +	/* Select the proper audio driver */
   1.189 +	audio = NULL;
   1.190 +	idx = 0;
   1.191 +#ifdef unix
   1.192 +	if ( (driver_name == NULL) && (getenv("ESPEAKER") != NULL) ) {
   1.193 +		/* Ahem, we know that if ESPEAKER is set, user probably wants
   1.194 +		   to use ESD, but don't start it if it's not already running.
   1.195 +		   This probably isn't the place to do this, but... Shh! :)
   1.196 +		 */
   1.197 +		for ( i=0; bootstrap[i]; ++i ) {
   1.198 +			if ( strcmp(bootstrap[i]->name, "esd") == 0 ) {
   1.199 +				const char *esd_no_spawn;
   1.200 +
   1.201 +				/* Don't start ESD if it's not running */
   1.202 +				esd_no_spawn = getenv("ESD_NO_SPAWN");
   1.203 +				if ( esd_no_spawn == NULL ) {
   1.204 +					putenv("ESD_NO_SPAWN=1");
   1.205 +				}
   1.206 +				if ( bootstrap[i]->available() ) {
   1.207 +					audio = bootstrap[i]->create(0);
   1.208 +					break;
   1.209 +				}
   1.210 +#ifdef linux	/* No unsetenv() on most platforms */
   1.211 +				if ( esd_no_spawn == NULL ) {
   1.212 +					unsetenv("ESD_NO_SPAWN");
   1.213 +				}
   1.214 +#endif
   1.215 +			}
   1.216 +		}
   1.217 +	}
   1.218 +#endif /* unix */
   1.219 +	if ( audio == NULL ) {
   1.220 +		if ( driver_name != NULL ) {
   1.221 +#if 0	/* This will be replaced with a better driver selection API */
   1.222 +			if ( strrchr(driver_name, ':') != NULL ) {
   1.223 +				idx = atoi(strrchr(driver_name, ':')+1);
   1.224 +			}
   1.225 +#endif
   1.226 +			for ( i=0; bootstrap[i]; ++i ) {
   1.227 +				if (strncmp(bootstrap[i]->name, driver_name,
   1.228 +				            strlen(bootstrap[i]->name)) == 0) {
   1.229 +					if ( bootstrap[i]->available() ) {
   1.230 +						audio=bootstrap[i]->create(idx);
   1.231 +						break;
   1.232 +					}
   1.233 +				}
   1.234 +			}
   1.235 +		} else {
   1.236 +			for ( i=0; bootstrap[i]; ++i ) {
   1.237 +				if ( bootstrap[i]->available() ) {
   1.238 +					audio = bootstrap[i]->create(idx);
   1.239 +					if ( audio != NULL ) {
   1.240 +						break;
   1.241 +					}
   1.242 +				}
   1.243 +			}
   1.244 +		}
   1.245 +		if ( audio == NULL ) {
   1.246 +			SDL_SetError("No available audio device");
   1.247 +#if 0 /* Don't fail SDL_Init() if audio isn't available.
   1.248 +         SDL_OpenAudio() will handle it at that point.  *sigh*
   1.249 +       */
   1.250 +			return(-1);
   1.251 +#endif
   1.252 +		}
   1.253 +	}
   1.254 +	current_audio = audio;
   1.255 +	if ( current_audio ) {
   1.256 +		current_audio->name = bootstrap[i]->name;
   1.257 +	}
   1.258 +	return(0);
   1.259 +}
   1.260 +
   1.261 +char *SDL_AudioDriverName(char *namebuf, int maxlen)
   1.262 +{
   1.263 +	if ( current_audio != NULL ) {
   1.264 +		strncpy(namebuf, current_audio->name, maxlen-1);
   1.265 +		namebuf[maxlen-1] = '\0';
   1.266 +		return(namebuf);
   1.267 +	}
   1.268 +	return(NULL);
   1.269 +}
   1.270 +
   1.271 +int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
   1.272 +{
   1.273 +	SDL_AudioDevice *audio;
   1.274 +
   1.275 +	/* Start up the audio driver, if necessary */
   1.276 +	if ( ! current_audio ) {
   1.277 +		if ( (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) ||
   1.278 +		     (current_audio == NULL) ) {
   1.279 +			return(-1);
   1.280 +		}
   1.281 +	}
   1.282 +	audio = current_audio;
   1.283 +
   1.284 +	/* Verify some parameters */
   1.285 +	if ( desired->callback == NULL ) {
   1.286 +		SDL_SetError("SDL_OpenAudio() passed a NULL callback");
   1.287 +		return(-1);
   1.288 +	}
   1.289 +	switch ( desired->channels ) {
   1.290 +	    case 1:	/* Mono */
   1.291 +	    case 2:	/* Stereo */
   1.292 +		break;
   1.293 +	    default:
   1.294 +		SDL_SetError("1 (mono) and 2 (stereo) channels supported");
   1.295 +		return(-1);
   1.296 +	}
   1.297 +
   1.298 +#ifdef macintosh
   1.299 +	/* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */
   1.300 +#else
   1.301 +	/* Create a semaphore for locking the sound buffers */
   1.302 +	audio->mixer_lock = SDL_CreateMutex();
   1.303 +	if ( audio->mixer_lock == NULL ) {
   1.304 +		SDL_SetError("Couldn't create mixer lock");
   1.305 +		SDL_CloseAudio();
   1.306 +		return(-1);
   1.307 +	}
   1.308 +#endif
   1.309 +
   1.310 +	/* Calculate the silence and size of the audio specification */
   1.311 +	SDL_CalculateAudioSpec(desired);
   1.312 +
   1.313 +	/* Open the audio subsystem */
   1.314 +	memcpy(&audio->spec, desired, sizeof(audio->spec));
   1.315 +	audio->convert.needed = 0;
   1.316 +	audio->enabled = 1;
   1.317 +	audio->paused  = 1;
   1.318 +	audio->opened = audio->OpenAudio(audio, &audio->spec)+1;
   1.319 +	if ( ! audio->opened ) {
   1.320 +		SDL_CloseAudio();
   1.321 +		return(-1);
   1.322 +	}
   1.323 +
   1.324 +	/* If the audio driver changes the buffer size, accept it */
   1.325 +	if ( audio->spec.samples != desired->samples ) {
   1.326 +		desired->samples = audio->spec.samples;
   1.327 +		SDL_CalculateAudioSpec(desired);
   1.328 +	}
   1.329 +
   1.330 +	/* Allocate a fake audio memory buffer */
   1.331 +	audio->fake_stream = SDL_AllocAudioMem(audio->spec.size);
   1.332 +	if ( audio->fake_stream == NULL ) {
   1.333 +		SDL_CloseAudio();
   1.334 +		SDL_OutOfMemory();
   1.335 +		return(-1);
   1.336 +	}
   1.337 +
   1.338 +	/* See if we need to do any conversion */
   1.339 +	if ( memcmp(desired, &audio->spec, sizeof(audio->spec)) == 0 ) {
   1.340 +		/* Just copy over the desired audio specification */
   1.341 +		if ( obtained != NULL ) {
   1.342 +			memcpy(obtained, &audio->spec, sizeof(audio->spec));
   1.343 +		}
   1.344 +	} else {
   1.345 +		/* Copy over the audio specification if possible */
   1.346 +		if ( obtained != NULL ) {
   1.347 +			memcpy(obtained, &audio->spec, sizeof(audio->spec));
   1.348 +		} else {
   1.349 +			/* Build an audio conversion block */
   1.350 +			if ( SDL_BuildAudioCVT(&audio->convert,
   1.351 +				desired->format, desired->channels,
   1.352 +						desired->freq,
   1.353 +				audio->spec.format, audio->spec.channels,
   1.354 +						audio->spec.freq) < 0 ) {
   1.355 +				SDL_CloseAudio();
   1.356 +				return(-1);
   1.357 +			}
   1.358 +			if ( audio->convert.needed ) {
   1.359 +				audio->convert.len = desired->size;
   1.360 +				audio->convert.buf =(Uint8 *)SDL_AllocAudioMem(
   1.361 +				   audio->convert.len*audio->convert.len_mult);
   1.362 +				if ( audio->convert.buf == NULL ) {
   1.363 +					SDL_CloseAudio();
   1.364 +					SDL_OutOfMemory();
   1.365 +					return(-1);
   1.366 +				}
   1.367 +			}
   1.368 +		}
   1.369 +	}
   1.370 +
   1.371 +	/* Start the audio thread if necessary */
   1.372 +	switch (audio->opened) {
   1.373 +		case  1:
   1.374 +			/* Start the audio thread */
   1.375 +			audio->thread = SDL_CreateThread(SDL_RunAudio, audio);
   1.376 +			if ( audio->thread == NULL ) {
   1.377 +				SDL_CloseAudio();
   1.378 +				SDL_SetError("Couldn't create audio thread");
   1.379 +				return(-1);
   1.380 +			}
   1.381 +			break;
   1.382 +
   1.383 +		default:
   1.384 +			/* The audio is now playing */
   1.385 +			break;
   1.386 +	}
   1.387 +	return(0);
   1.388 +}
   1.389 +
   1.390 +SDL_audiostatus SDL_GetAudioStatus(void)
   1.391 +{
   1.392 +	SDL_AudioDevice *audio = current_audio;
   1.393 +	SDL_audiostatus status;
   1.394 +
   1.395 +	status = SDL_AUDIO_STOPPED;
   1.396 +	if ( audio && audio->enabled ) {
   1.397 +		if ( audio->paused ) {
   1.398 +			status = SDL_AUDIO_PAUSED;
   1.399 +		} else {
   1.400 +			status = SDL_AUDIO_PLAYING;
   1.401 +		}
   1.402 +	}
   1.403 +	return(status);
   1.404 +}
   1.405 +
   1.406 +void SDL_PauseAudio (int pause_on)
   1.407 +{
   1.408 +	SDL_AudioDevice *audio = current_audio;
   1.409 +
   1.410 +	if ( audio ) {
   1.411 +		audio->paused = pause_on;
   1.412 +	}
   1.413 +}
   1.414 +
   1.415 +void SDL_LockAudio (void)
   1.416 +{
   1.417 +	SDL_AudioDevice *audio = current_audio;
   1.418 +
   1.419 +	/* Obtain a lock on the mixing buffers */
   1.420 +	if ( audio ) {
   1.421 +		if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
   1.422 +			return;
   1.423 +		}
   1.424 +		SDL_mutexP(audio->mixer_lock);
   1.425 +	}
   1.426 +}
   1.427 +
   1.428 +void SDL_UnlockAudio (void)
   1.429 +{
   1.430 +	SDL_AudioDevice *audio = current_audio;
   1.431 +
   1.432 +	/* Release lock on the mixing buffers */
   1.433 +	if ( audio ) {
   1.434 +		if ( audio->thread && (SDL_ThreadID() == audio->threadid) ) {
   1.435 +			return;
   1.436 +		}
   1.437 +		SDL_mutexV(audio->mixer_lock);
   1.438 +	}
   1.439 +}
   1.440 +
   1.441 +void SDL_CloseAudio (void)
   1.442 +{
   1.443 +	SDL_QuitSubSystem(SDL_INIT_AUDIO);
   1.444 +}
   1.445 +
   1.446 +void SDL_AudioQuit(void)
   1.447 +{
   1.448 +	SDL_AudioDevice *audio = current_audio;
   1.449 +
   1.450 +	if ( audio ) {
   1.451 +		audio->enabled = 0;
   1.452 +		if ( audio->thread != NULL ) {
   1.453 +			SDL_WaitThread(audio->thread, NULL);
   1.454 +		}
   1.455 +		if ( audio->mixer_lock != NULL ) {
   1.456 +			SDL_DestroyMutex(audio->mixer_lock);
   1.457 +		}
   1.458 +		if ( audio->fake_stream != NULL ) {
   1.459 +			SDL_FreeAudioMem(audio->fake_stream);
   1.460 +		}
   1.461 +		if ( audio->convert.needed ) {
   1.462 +			SDL_FreeAudioMem(audio->convert.buf);
   1.463 +		}
   1.464 +		if ( audio->opened ) {
   1.465 +			audio->CloseAudio(audio);
   1.466 +			audio->opened = 0;
   1.467 +		}
   1.468 +
   1.469 +		/* Free the driver data */
   1.470 +		audio->free(audio);
   1.471 +		current_audio = NULL;
   1.472 +	}
   1.473 +}
   1.474 +
   1.475 +#define NUM_FORMATS	6
   1.476 +static int format_idx;
   1.477 +static int format_idx_sub;
   1.478 +static Uint16 format_list[NUM_FORMATS][NUM_FORMATS] = {
   1.479 + { AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB },
   1.480 + { AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB },
   1.481 + { AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8 },
   1.482 + { AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8 },
   1.483 + { AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U8, AUDIO_S8 },
   1.484 + { AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U8, AUDIO_S8 },
   1.485 +};
   1.486 +
   1.487 +Uint16 SDL_FirstAudioFormat(Uint16 format)
   1.488 +{
   1.489 +	for ( format_idx=0; format_idx < NUM_FORMATS; ++format_idx ) {
   1.490 +		if ( format_list[format_idx][0] == format ) {
   1.491 +			break;
   1.492 +		}
   1.493 +	}
   1.494 +	format_idx_sub = 0;
   1.495 +	return(SDL_NextAudioFormat());
   1.496 +}
   1.497 +
   1.498 +Uint16 SDL_NextAudioFormat(void)
   1.499 +{
   1.500 +	if ( (format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS) ) {
   1.501 +		return(0);
   1.502 +	}
   1.503 +	return(format_list[format_idx][format_idx_sub++]);
   1.504 +}
   1.505 +
   1.506 +void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
   1.507 +{
   1.508 +	switch (spec->format) {
   1.509 +		case AUDIO_U8:
   1.510 +			spec->silence = 0x80;
   1.511 +			break;
   1.512 +		default:
   1.513 +			spec->silence = 0x00;
   1.514 +			break;
   1.515 +	}
   1.516 +	spec->size = (spec->format&0xFF)/8;
   1.517 +	spec->size *= spec->channels;
   1.518 +	spec->size *= spec->samples;
   1.519 +}