src/audio/dummy/SDL_dummyaudio.c
author Ryan C. Gordon
Tue, 14 Mar 2006 19:12:49 +0000
changeset 1537 60620d71e324
parent 1533 38c1eb6b0083
child 1538 b1f8c14f0df5
permissions -rw-r--r--
Corrected dummy audio callback firing to be realistic, cleaned up tabs.
icculus@1532
     1
/*
icculus@1532
     2
    SDL - Simple DirectMedia Layer
icculus@1532
     3
    Copyright (C) 1997-2006 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
	const char *envr;
icculus@1532
    66
icculus@1532
    67
	/* Initialize all variables that we clean on shutdown */
icculus@1532
    68
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
icculus@1532
    69
	if ( this ) {
icculus@1532
    70
		SDL_memset(this, 0, (sizeof *this));
icculus@1532
    71
		this->hidden = (struct SDL_PrivateAudioData *)
icculus@1532
    72
				SDL_malloc((sizeof *this->hidden));
icculus@1532
    73
	}
icculus@1532
    74
	if ( (this == NULL) || (this->hidden == NULL) ) {
icculus@1532
    75
		SDL_OutOfMemory();
icculus@1532
    76
		if ( this ) {
icculus@1532
    77
			SDL_free(this);
icculus@1532
    78
		}
icculus@1532
    79
		return(0);
icculus@1532
    80
	}
icculus@1532
    81
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@1532
    82
icculus@1532
    83
	/* Set the function pointers */
icculus@1532
    84
	this->OpenAudio = DUMMYAUD_OpenAudio;
icculus@1532
    85
	this->WaitAudio = DUMMYAUD_WaitAudio;
icculus@1532
    86
	this->PlayAudio = DUMMYAUD_PlayAudio;
icculus@1532
    87
	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
icculus@1532
    88
	this->CloseAudio = DUMMYAUD_CloseAudio;
icculus@1532
    89
icculus@1532
    90
	this->free = DUMMYAUD_DeleteDevice;
icculus@1532
    91
icculus@1532
    92
	return this;
icculus@1532
    93
}
icculus@1532
    94
icculus@1532
    95
AudioBootStrap DUMMYAUD_bootstrap = {
icculus@1532
    96
	DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
icculus@1532
    97
	DUMMYAUD_Available, DUMMYAUD_CreateDevice
icculus@1532
    98
};
icculus@1532
    99
icculus@1532
   100
/* This function waits until it is possible to write a full sound buffer */
icculus@1532
   101
static void DUMMYAUD_WaitAudio(_THIS)
icculus@1532
   102
{
icculus@1537
   103
	/* Don't block on first calls to simulate initial fragment filling. */
icculus@1537
   104
	if (this->hidden->initial_calls)
icculus@1537
   105
		this->hidden->initial_calls--;
icculus@1537
   106
	else
icculus@1537
   107
		SDL_Delay(this->hidden->write_delay);
icculus@1532
   108
}
icculus@1532
   109
icculus@1532
   110
static void DUMMYAUD_PlayAudio(_THIS)
icculus@1532
   111
{
icculus@1537
   112
	/* no-op...this is a null driver. */
icculus@1532
   113
}
icculus@1532
   114
icculus@1532
   115
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
icculus@1532
   116
{
icculus@1532
   117
	return(this->hidden->mixbuf);
icculus@1532
   118
}
icculus@1532
   119
icculus@1532
   120
static void DUMMYAUD_CloseAudio(_THIS)
icculus@1532
   121
{
icculus@1532
   122
	if ( this->hidden->mixbuf != NULL ) {
icculus@1532
   123
		SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@1532
   124
		this->hidden->mixbuf = NULL;
icculus@1532
   125
	}
icculus@1532
   126
}
icculus@1532
   127
icculus@1532
   128
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
icculus@1532
   129
{
icculus@1537
   130
	float bytes_per_sec = 0.0f;
icculus@1537
   131
icculus@1532
   132
	/* Allocate mixing buffer */
icculus@1532
   133
	this->hidden->mixlen = spec->size;
icculus@1532
   134
	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@1532
   135
	if ( this->hidden->mixbuf == NULL ) {
icculus@1532
   136
		return(-1);
icculus@1532
   137
	}
icculus@1532
   138
	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
icculus@1532
   139
icculus@1537
   140
	bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
icculus@1537
   141
	                   spec->channels * spec->freq);
icculus@1537
   142
icculus@1537
   143
	/*
icculus@1537
   144
	 * We try to make this request more audio at the correct rate for
icculus@1537
   145
	 *  a given audio spec, so timing stays fairly faithful.
icculus@1537
   146
	 * Also, we have it not block at all for the first two calls, so
icculus@1537
   147
	 *  it seems like we're filling two audio fragments right out of the
icculus@1537
   148
	 *  gate, like other SDL drivers tend to do.
icculus@1537
   149
	 */
icculus@1537
   150
	this->hidden->initial_calls = 2;
icculus@1537
   151
	this->hidden->write_delay =
icculus@1537
   152
	               (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
icculus@1537
   153
icculus@1532
   154
	/* We're ready to rock and roll. :-) */
icculus@1532
   155
	return(0);
icculus@1532
   156
}
icculus@1532
   157