src/audio/dummy/SDL_dummyaudio.c
author Sam Lantinga
Tue, 14 Mar 2006 16:19:32 +0000
changeset 1533 38c1eb6b0083
parent 1532 30f189cdd82b
child 1537 60620d71e324
permissions -rw-r--r--
By default all the real targets have dummy audio/video and disk audio.
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@1532
    22
    This file hacked^H^H^H^H^H^Hwritten by Ryan C. Gordon
icculus@1532
    23
        (icculus@icculus.org)
icculus@1532
    24
*/
icculus@1532
    25
#include "SDL_config.h"
icculus@1532
    26
slouken@1533
    27
/* Output audio to nowhere... */
icculus@1532
    28
icculus@1532
    29
#include "SDL_rwops.h"
icculus@1532
    30
#include "SDL_timer.h"
icculus@1532
    31
#include "SDL_audio.h"
icculus@1532
    32
#include "../SDL_audiomem.h"
icculus@1532
    33
#include "../SDL_audio_c.h"
icculus@1532
    34
#include "../SDL_audiodev_c.h"
icculus@1532
    35
#include "SDL_dummyaudio.h"
icculus@1532
    36
icculus@1532
    37
/* The tag name used by DUMMY audio */
icculus@1532
    38
#define DUMMYAUD_DRIVER_NAME         "dummy"
icculus@1532
    39
icculus@1532
    40
/* environment variables and defaults. */
icculus@1532
    41
#define DUMMYENVR_WRITEDELAY      "SDL_DUMMYAUDIODELAY"
icculus@1532
    42
#define DUMMYDEFAULT_WRITEDELAY   150
icculus@1532
    43
icculus@1532
    44
/* Audio driver functions */
icculus@1532
    45
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
icculus@1532
    46
static void DUMMYAUD_WaitAudio(_THIS);
icculus@1532
    47
static void DUMMYAUD_PlayAudio(_THIS);
icculus@1532
    48
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
icculus@1532
    49
static void DUMMYAUD_CloseAudio(_THIS);
icculus@1532
    50
icculus@1532
    51
/* Audio driver bootstrap functions */
icculus@1532
    52
static int DUMMYAUD_Available(void)
icculus@1532
    53
{
icculus@1532
    54
	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
icculus@1532
    55
	if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
icculus@1532
    56
		return(1);
icculus@1532
    57
	}
icculus@1532
    58
	return(0);
icculus@1532
    59
}
icculus@1532
    60
icculus@1532
    61
static void DUMMYAUD_DeleteDevice(SDL_AudioDevice *device)
icculus@1532
    62
{
icculus@1532
    63
	SDL_free(device->hidden);
icculus@1532
    64
	SDL_free(device);
icculus@1532
    65
}
icculus@1532
    66
icculus@1532
    67
static SDL_AudioDevice *DUMMYAUD_CreateDevice(int devindex)
icculus@1532
    68
{
icculus@1532
    69
	SDL_AudioDevice *this;
icculus@1532
    70
	const char *envr;
icculus@1532
    71
icculus@1532
    72
	/* Initialize all variables that we clean on shutdown */
icculus@1532
    73
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
icculus@1532
    74
	if ( this ) {
icculus@1532
    75
		SDL_memset(this, 0, (sizeof *this));
icculus@1532
    76
		this->hidden = (struct SDL_PrivateAudioData *)
icculus@1532
    77
				SDL_malloc((sizeof *this->hidden));
icculus@1532
    78
	}
icculus@1532
    79
	if ( (this == NULL) || (this->hidden == NULL) ) {
icculus@1532
    80
		SDL_OutOfMemory();
icculus@1532
    81
		if ( this ) {
icculus@1532
    82
			SDL_free(this);
icculus@1532
    83
		}
icculus@1532
    84
		return(0);
icculus@1532
    85
	}
icculus@1532
    86
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@1532
    87
icculus@1532
    88
	envr = SDL_getenv(DUMMYENVR_WRITEDELAY);
icculus@1532
    89
	this->hidden->write_delay = (envr) ? SDL_atoi(envr) : DUMMYDEFAULT_WRITEDELAY;
icculus@1532
    90
icculus@1532
    91
	/* Set the function pointers */
icculus@1532
    92
	this->OpenAudio = DUMMYAUD_OpenAudio;
icculus@1532
    93
	this->WaitAudio = DUMMYAUD_WaitAudio;
icculus@1532
    94
	this->PlayAudio = DUMMYAUD_PlayAudio;
icculus@1532
    95
	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
icculus@1532
    96
	this->CloseAudio = DUMMYAUD_CloseAudio;
icculus@1532
    97
icculus@1532
    98
	this->free = DUMMYAUD_DeleteDevice;
icculus@1532
    99
icculus@1532
   100
	return this;
icculus@1532
   101
}
icculus@1532
   102
icculus@1532
   103
AudioBootStrap DUMMYAUD_bootstrap = {
icculus@1532
   104
	DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
icculus@1532
   105
	DUMMYAUD_Available, DUMMYAUD_CreateDevice
icculus@1532
   106
};
icculus@1532
   107
icculus@1532
   108
/* This function waits until it is possible to write a full sound buffer */
icculus@1532
   109
static void DUMMYAUD_WaitAudio(_THIS)
icculus@1532
   110
{
icculus@1532
   111
	SDL_Delay(this->hidden->write_delay);
icculus@1532
   112
}
icculus@1532
   113
icculus@1532
   114
static void DUMMYAUD_PlayAudio(_THIS)
icculus@1532
   115
{
icculus@1532
   116
    /* no-op...this is a null driver. */
icculus@1532
   117
}
icculus@1532
   118
icculus@1532
   119
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
icculus@1532
   120
{
icculus@1532
   121
	return(this->hidden->mixbuf);
icculus@1532
   122
}
icculus@1532
   123
icculus@1532
   124
static void DUMMYAUD_CloseAudio(_THIS)
icculus@1532
   125
{
icculus@1532
   126
	if ( this->hidden->mixbuf != NULL ) {
icculus@1532
   127
		SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@1532
   128
		this->hidden->mixbuf = NULL;
icculus@1532
   129
	}
icculus@1532
   130
}
icculus@1532
   131
icculus@1532
   132
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
icculus@1532
   133
{
icculus@1532
   134
	/* Allocate mixing buffer */
icculus@1532
   135
	this->hidden->mixlen = spec->size;
icculus@1532
   136
	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@1532
   137
	if ( this->hidden->mixbuf == NULL ) {
icculus@1532
   138
		return(-1);
icculus@1532
   139
	}
icculus@1532
   140
	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
icculus@1532
   141
icculus@1532
   142
	/* We're ready to rock and roll. :-) */
icculus@1532
   143
	return(0);
icculus@1532
   144
}
icculus@1532
   145