src/audio/dummy/SDL_dummyaudio.c
author Ryan C. Gordon
Tue, 14 Mar 2006 08:53:33 +0000
changeset 1532 30f189cdd82b
child 1533 38c1eb6b0083
permissions -rw-r--r--
Implemented dummy audio driver.

Fixes Bugzilla #161.
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
icculus@1532
    27
/* Output raw audio data to a file. */
icculus@1532
    28
icculus@1532
    29
#if HAVE_STDIO_H
icculus@1532
    30
#include <stdio.h>
icculus@1532
    31
#endif
icculus@1532
    32
icculus@1532
    33
#include "SDL_rwops.h"
icculus@1532
    34
#include "SDL_timer.h"
icculus@1532
    35
#include "SDL_audio.h"
icculus@1532
    36
#include "../SDL_audiomem.h"
icculus@1532
    37
#include "../SDL_audio_c.h"
icculus@1532
    38
#include "../SDL_audiodev_c.h"
icculus@1532
    39
#include "SDL_dummyaudio.h"
icculus@1532
    40
icculus@1532
    41
/* The tag name used by DUMMY audio */
icculus@1532
    42
#define DUMMYAUD_DRIVER_NAME         "dummy"
icculus@1532
    43
icculus@1532
    44
/* environment variables and defaults. */
icculus@1532
    45
#define DUMMYENVR_WRITEDELAY      "SDL_DUMMYAUDIODELAY"
icculus@1532
    46
#define DUMMYDEFAULT_WRITEDELAY   150
icculus@1532
    47
icculus@1532
    48
/* Audio driver functions */
icculus@1532
    49
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
icculus@1532
    50
static void DUMMYAUD_WaitAudio(_THIS);
icculus@1532
    51
static void DUMMYAUD_PlayAudio(_THIS);
icculus@1532
    52
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
icculus@1532
    53
static void DUMMYAUD_CloseAudio(_THIS);
icculus@1532
    54
icculus@1532
    55
/* Audio driver bootstrap functions */
icculus@1532
    56
static int DUMMYAUD_Available(void)
icculus@1532
    57
{
icculus@1532
    58
	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
icculus@1532
    59
	if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
icculus@1532
    60
		return(1);
icculus@1532
    61
	}
icculus@1532
    62
	return(0);
icculus@1532
    63
}
icculus@1532
    64
icculus@1532
    65
static void DUMMYAUD_DeleteDevice(SDL_AudioDevice *device)
icculus@1532
    66
{
icculus@1532
    67
	SDL_free(device->hidden);
icculus@1532
    68
	SDL_free(device);
icculus@1532
    69
}
icculus@1532
    70
icculus@1532
    71
static SDL_AudioDevice *DUMMYAUD_CreateDevice(int devindex)
icculus@1532
    72
{
icculus@1532
    73
	SDL_AudioDevice *this;
icculus@1532
    74
	const char *envr;
icculus@1532
    75
icculus@1532
    76
	/* Initialize all variables that we clean on shutdown */
icculus@1532
    77
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
icculus@1532
    78
	if ( this ) {
icculus@1532
    79
		SDL_memset(this, 0, (sizeof *this));
icculus@1532
    80
		this->hidden = (struct SDL_PrivateAudioData *)
icculus@1532
    81
				SDL_malloc((sizeof *this->hidden));
icculus@1532
    82
	}
icculus@1532
    83
	if ( (this == NULL) || (this->hidden == NULL) ) {
icculus@1532
    84
		SDL_OutOfMemory();
icculus@1532
    85
		if ( this ) {
icculus@1532
    86
			SDL_free(this);
icculus@1532
    87
		}
icculus@1532
    88
		return(0);
icculus@1532
    89
	}
icculus@1532
    90
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
icculus@1532
    91
icculus@1532
    92
	envr = SDL_getenv(DUMMYENVR_WRITEDELAY);
icculus@1532
    93
	this->hidden->write_delay = (envr) ? SDL_atoi(envr) : DUMMYDEFAULT_WRITEDELAY;
icculus@1532
    94
icculus@1532
    95
	/* Set the function pointers */
icculus@1532
    96
	this->OpenAudio = DUMMYAUD_OpenAudio;
icculus@1532
    97
	this->WaitAudio = DUMMYAUD_WaitAudio;
icculus@1532
    98
	this->PlayAudio = DUMMYAUD_PlayAudio;
icculus@1532
    99
	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
icculus@1532
   100
	this->CloseAudio = DUMMYAUD_CloseAudio;
icculus@1532
   101
icculus@1532
   102
	this->free = DUMMYAUD_DeleteDevice;
icculus@1532
   103
icculus@1532
   104
	return this;
icculus@1532
   105
}
icculus@1532
   106
icculus@1532
   107
AudioBootStrap DUMMYAUD_bootstrap = {
icculus@1532
   108
	DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
icculus@1532
   109
	DUMMYAUD_Available, DUMMYAUD_CreateDevice
icculus@1532
   110
};
icculus@1532
   111
icculus@1532
   112
/* This function waits until it is possible to write a full sound buffer */
icculus@1532
   113
static void DUMMYAUD_WaitAudio(_THIS)
icculus@1532
   114
{
icculus@1532
   115
	SDL_Delay(this->hidden->write_delay);
icculus@1532
   116
}
icculus@1532
   117
icculus@1532
   118
static void DUMMYAUD_PlayAudio(_THIS)
icculus@1532
   119
{
icculus@1532
   120
    /* no-op...this is a null driver. */
icculus@1532
   121
}
icculus@1532
   122
icculus@1532
   123
static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
icculus@1532
   124
{
icculus@1532
   125
	return(this->hidden->mixbuf);
icculus@1532
   126
}
icculus@1532
   127
icculus@1532
   128
static void DUMMYAUD_CloseAudio(_THIS)
icculus@1532
   129
{
icculus@1532
   130
	if ( this->hidden->mixbuf != NULL ) {
icculus@1532
   131
		SDL_FreeAudioMem(this->hidden->mixbuf);
icculus@1532
   132
		this->hidden->mixbuf = NULL;
icculus@1532
   133
	}
icculus@1532
   134
}
icculus@1532
   135
icculus@1532
   136
static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
icculus@1532
   137
{
icculus@1532
   138
#if HAVE_STDIO_H
icculus@1532
   139
	fprintf(stderr, "\nWARNING: You are using the SDL dummy audio driver!"
icculus@1532
   140
                    " No sound will be output!\n\n");
icculus@1532
   141
#endif
icculus@1532
   142
icculus@1532
   143
	/* Allocate mixing buffer */
icculus@1532
   144
	this->hidden->mixlen = spec->size;
icculus@1532
   145
	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
icculus@1532
   146
	if ( this->hidden->mixbuf == NULL ) {
icculus@1532
   147
		return(-1);
icculus@1532
   148
	}
icculus@1532
   149
	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
icculus@1532
   150
icculus@1532
   151
	/* We're ready to rock and roll. :-) */
icculus@1532
   152
	return(0);
icculus@1532
   153
}
icculus@1532
   154