19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This file written by Ryan C. Gordon (icculus@icculus.org)
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Output audio to nowhere... */
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_rwops.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dummyaudio.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by DUMMY audio */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DUMMYAUD_DRIVER_NAME         "dummy"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DUMMYAUD_WaitAudio(_THIS);
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DUMMYAUD_PlayAudio(_THIS);
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DUMMYAUD_CloseAudio(_THIS);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DUMMYAUD_Available(void)
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(1);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DUMMYAUD_DeleteDevice(SDL_AudioDevice *device)
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *DUMMYAUD_CreateDevice(int devindex)
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = DUMMYAUD_OpenAudio;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = DUMMYAUD_WaitAudio;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = DUMMYAUD_PlayAudio;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = DUMMYAUD_CloseAudio;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = DUMMYAUD_DeleteDevice;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap DUMMYAUD_bootstrap = {
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	DUMMYAUD_Available, DUMMYAUD_CreateDevice
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DUMMYAUD_WaitAudio(_THIS)
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't block on first calls to simulate initial fragment filling. */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (this->hidden->initial_calls)
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden->initial_calls--;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Delay(this->hidden->write_delay);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DUMMYAUD_PlayAudio(_THIS)
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* no-op...this is a null driver. */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(this->hidden->mixbuf);
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DUMMYAUD_CloseAudio(_THIS)
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->hidden->mixbuf != NULL ) {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeAudioMem(this->hidden->mixbuf);
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden->mixbuf = NULL;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	float bytes_per_sec = 0.0f;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mixing buffer */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->mixlen = spec->size;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this->hidden->mixbuf == NULL ) {
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                   spec->channels * spec->freq);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * We try to make this request more audio at the correct rate for
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  a given audio spec, so timing stays fairly faithful.
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Also, we have it not block at all for the first two calls, so
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  it seems like we're filling two audio fragments right out of the
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *  gate, like other SDL drivers tend to do.
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->initial_calls = 2;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->hidden->write_delay =
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	               (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to rock and roll. :-) */
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
157