src/audio/dummy/SDL_dummyaudio.c
author Sam Lantinga
Sat, 31 Dec 2011 09:16:08 -0500
branchSDL-1.2
changeset 6137 4720145f848b
parent 4159 a1b03ba2fcd0
permissions -rw-r--r--
Happy New Year!
icculus@1532
     1
/*
icculus@1532
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
icculus@1532
     4
icculus@1532
     5
    This library is free software; you can redistribute it and/or
icculus@1532
     6
    modify it under the terms of the GNU Lesser General Public
icculus@1532
     7
    License as published by the Free Software Foundation; either
icculus@1532
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@1532
     9
icculus@1532
    10
    This library is distributed in the hope that it will be useful,
icculus@1532
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1532
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1532
    13
    Lesser General Public License for more details.
icculus@1532
    14
icculus@1532
    15
    You should have received a copy of the GNU Lesser General Public
icculus@1532
    16
    License along with this library; if not, write to the Free Software
icculus@1532
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1532
    18
icculus@1532
    19
    Sam Lantinga
icculus@1532
    20
    slouken@libsdl.org
icculus@1532
    21
icculus@1537
    22
    This file written by Ryan C. Gordon (icculus@icculus.org)
icculus@1532
    23
*/
icculus@1532
    24
#include "SDL_config.h"
icculus@1532
    25
slouken@1533
    26
/* Output audio to nowhere... */
icculus@1532
    27
icculus@1532
    28
#include "SDL_rwops.h"
icculus@1532
    29
#include "SDL_timer.h"
icculus@1532
    30
#include "SDL_audio.h"
icculus@1532
    31
#include "../SDL_audiomem.h"
icculus@1532
    32
#include "../SDL_audio_c.h"
icculus@1532
    33
#include "../SDL_audiodev_c.h"
icculus@1532
    34
#include "SDL_dummyaudio.h"
icculus@1532
    35
icculus@1532
    36
/* The tag name used by DUMMY audio */
icculus@1532
    37
#define DUMMYAUD_DRIVER_NAME         "dummy"
icculus@1532
    38
icculus@1532
    39
/* Audio driver functions */
icculus@1532
    40
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
icculus@1532
    41
static void DUMMYAUD_WaitAudio(_THIS);
icculus@1532
    42
static void DUMMYAUD_PlayAudio(_THIS);
icculus@1532
    43
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
icculus@1532
    44
static void DUMMYAUD_CloseAudio(_THIS);
icculus@1532
    45
icculus@1532
    46
/* Audio driver bootstrap functions */
icculus@1532
    47
static int DUMMYAUD_Available(void)
icculus@1532
    48
{
icculus@1532
    49
	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
icculus@1532
    50
	if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
icculus@1532
    51
		return(1);
icculus@1532
    52
	}
icculus@1532
    53
	return(0);
icculus@1532
    54
}
icculus@1532
    55
icculus@1532
    56
static void DUMMYAUD_DeleteDevice(SDL_AudioDevice *device)
icculus@1532
    57
{
icculus@1532
    58
	SDL_free(device->hidden);
icculus@1532
    59
	SDL_free(device);
icculus@1532
    60
}
icculus@1532
    61
icculus@1532
    62
static SDL_AudioDevice *DUMMYAUD_CreateDevice(int devindex)
icculus@1532
    63
{
icculus@1532
    64
	SDL_AudioDevice *this;
icculus@1532
    65
icculus@1532
    66
	/* Initialize all variables that we clean on shutdown */
icculus@1532
    67
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
icculus@1532
    68
	if ( this ) {
icculus@1532
    69
		SDL_memset(this, 0, (sizeof *this));
icculus@1532
    70
		this->hidden = (struct SDL_PrivateAudioData *)
icculus@1532
    71
				SDL_malloc((sizeof *this->hidden));
icculus@1532
    72
	}
icculus@1532
    73
	if ( (this == NULL) || (this->hidden == NULL) ) {
icculus@1532
    74
		SDL_OutOfMemory();
icculus@1532
    75
		if ( this ) {
icculus@1532
    76
			SDL_free(this);
icculus@1532
    77
		}
icculus@1532
    78
		return(0);
icculus@1532
    79
	}
icculus@1532
    80
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@1532
    81
icculus@1532
    82
	/* Set the function pointers */
icculus@1532
    83
	this->OpenAudio = DUMMYAUD_OpenAudio;
icculus@1532
    84
	this->WaitAudio = DUMMYAUD_WaitAudio;
icculus@1532
    85
	this->PlayAudio = DUMMYAUD_PlayAudio;
icculus@1532
    86
	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
icculus@1532
    87
	this->CloseAudio = DUMMYAUD_CloseAudio;
icculus@1532
    88
icculus@1532
    89
	this->free = DUMMYAUD_DeleteDevice;
icculus@1532
    90
icculus@1532
    91
	return this;
icculus@1532
    92
}
icculus@1532
    93
icculus@1532
    94
AudioBootStrap DUMMYAUD_bootstrap = {
icculus@1532
    95
	DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
icculus@1532
    96
	DUMMYAUD_Available, DUMMYAUD_CreateDevice
icculus@1532
    97
};
icculus@1532
    98
icculus@1532
    99
/* This function waits until it is possible to write a full sound buffer */
icculus@1532
   100
static void DUMMYAUD_WaitAudio(_THIS)
icculus@1532
   101
{
icculus@1537
   102
	/* Don't block on first calls to simulate initial fragment filling. */
icculus@1537
   103
	if (this->hidden->initial_calls)
icculus@1537
   104
		this->hidden->initial_calls--;
icculus@1537
   105
	else
icculus@1537
   106
		SDL_Delay(this->hidden->write_delay);
icculus@1532
   107
}
icculus@1532
   108
icculus@1532
   109
static void DUMMYAUD_PlayAudio(_THIS)
icculus@1532
   110
{
icculus@1537
   111
	/* no-op...this is a null driver. */
icculus@1532
   112
}
icculus@1532
   113
icculus@1532
   114
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
icculus@1532
   115
{
icculus@1532
   116
	return(this->hidden->mixbuf);
icculus@1532
   117
}
icculus@1532
   118
icculus@1532
   119
static void DUMMYAUD_CloseAudio(_THIS)
icculus@1532
   120
{
icculus@1532
   121
	if ( this->hidden->mixbuf != NULL ) {
icculus@1532
   122
		SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@1532
   123
		this->hidden->mixbuf = NULL;
icculus@1532
   124
	}
icculus@1532
   125
}
icculus@1532
   126
icculus@1532
   127
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
icculus@1532
   128
{
icculus@1537
   129
	float bytes_per_sec = 0.0f;
icculus@1537
   130
icculus@1532
   131
	/* Allocate mixing buffer */
icculus@1532
   132
	this->hidden->mixlen = spec->size;
icculus@1532
   133
	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@1532
   134
	if ( this->hidden->mixbuf == NULL ) {
icculus@1532
   135
		return(-1);
icculus@1532
   136
	}
icculus@1532
   137
	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
icculus@1532
   138
icculus@1537
   139
	bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
icculus@1537
   140
	                   spec->channels * spec->freq);
icculus@1537
   141
icculus@1537
   142
	/*
icculus@1537
   143
	 * We try to make this request more audio at the correct rate for
icculus@1537
   144
	 *  a given audio spec, so timing stays fairly faithful.
icculus@1537
   145
	 * Also, we have it not block at all for the first two calls, so
icculus@1537
   146
	 *  it seems like we're filling two audio fragments right out of the
icculus@1537
   147
	 *  gate, like other SDL drivers tend to do.
icculus@1537
   148
	 */
icculus@1537
   149
	this->hidden->initial_calls = 2;
icculus@1537
   150
	this->hidden->write_delay =
icculus@1537
   151
	               (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
icculus@1537
   152
icculus@1532
   153
	/* We're ready to rock and roll. :-) */
icculus@1532
   154
	return(0);
icculus@1532
   155
}
icculus@1532
   156