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.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 
    22     This file hacked^H^H^H^H^H^Hwritten by Ryan C. Gordon
    23         (icculus@icculus.org)
    24 */
    25 #include "SDL_config.h"
    26 
    27 /* Output raw audio data to a file. */
    28 
    29 #if HAVE_STDIO_H
    30 #include <stdio.h>
    31 #endif
    32 
    33 #include "SDL_rwops.h"
    34 #include "SDL_timer.h"
    35 #include "SDL_audio.h"
    36 #include "../SDL_audiomem.h"
    37 #include "../SDL_audio_c.h"
    38 #include "../SDL_audiodev_c.h"
    39 #include "SDL_dummyaudio.h"
    40 
    41 /* The tag name used by DUMMY audio */
    42 #define DUMMYAUD_DRIVER_NAME         "dummy"
    43 
    44 /* environment variables and defaults. */
    45 #define DUMMYENVR_WRITEDELAY      "SDL_DUMMYAUDIODELAY"
    46 #define DUMMYDEFAULT_WRITEDELAY   150
    47 
    48 /* Audio driver functions */
    49 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
    50 static void DUMMYAUD_WaitAudio(_THIS);
    51 static void DUMMYAUD_PlayAudio(_THIS);
    52 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
    53 static void DUMMYAUD_CloseAudio(_THIS);
    54 
    55 /* Audio driver bootstrap functions */
    56 static int DUMMYAUD_Available(void)
    57 {
    58 	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
    59 	if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
    60 		return(1);
    61 	}
    62 	return(0);
    63 }
    64 
    65 static void DUMMYAUD_DeleteDevice(SDL_AudioDevice *device)
    66 {
    67 	SDL_free(device->hidden);
    68 	SDL_free(device);
    69 }
    70 
    71 static SDL_AudioDevice *DUMMYAUD_CreateDevice(int devindex)
    72 {
    73 	SDL_AudioDevice *this;
    74 	const char *envr;
    75 
    76 	/* Initialize all variables that we clean on shutdown */
    77 	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
    78 	if ( this ) {
    79 		SDL_memset(this, 0, (sizeof *this));
    80 		this->hidden = (struct SDL_PrivateAudioData *)
    81 				SDL_malloc((sizeof *this->hidden));
    82 	}
    83 	if ( (this == NULL) || (this->hidden == NULL) ) {
    84 		SDL_OutOfMemory();
    85 		if ( this ) {
    86 			SDL_free(this);
    87 		}
    88 		return(0);
    89 	}
    90 	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    91 
    92 	envr = SDL_getenv(DUMMYENVR_WRITEDELAY);
    93 	this->hidden->write_delay = (envr) ? SDL_atoi(envr) : DUMMYDEFAULT_WRITEDELAY;
    94 
    95 	/* Set the function pointers */
    96 	this->OpenAudio = DUMMYAUD_OpenAudio;
    97 	this->WaitAudio = DUMMYAUD_WaitAudio;
    98 	this->PlayAudio = DUMMYAUD_PlayAudio;
    99 	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
   100 	this->CloseAudio = DUMMYAUD_CloseAudio;
   101 
   102 	this->free = DUMMYAUD_DeleteDevice;
   103 
   104 	return this;
   105 }
   106 
   107 AudioBootStrap DUMMYAUD_bootstrap = {
   108 	DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
   109 	DUMMYAUD_Available, DUMMYAUD_CreateDevice
   110 };
   111 
   112 /* This function waits until it is possible to write a full sound buffer */
   113 static void DUMMYAUD_WaitAudio(_THIS)
   114 {
   115 	SDL_Delay(this->hidden->write_delay);
   116 }
   117 
   118 static void DUMMYAUD_PlayAudio(_THIS)
   119 {
   120     /* no-op...this is a null driver. */
   121 }
   122 
   123 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
   124 {
   125 	return(this->hidden->mixbuf);
   126 }
   127 
   128 static void DUMMYAUD_CloseAudio(_THIS)
   129 {
   130 	if ( this->hidden->mixbuf != NULL ) {
   131 		SDL_FreeAudioMem(this->hidden->mixbuf);
   132 		this->hidden->mixbuf = NULL;
   133 	}
   134 }
   135 
   136 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
   137 {
   138 #if HAVE_STDIO_H
   139 	fprintf(stderr, "\nWARNING: You are using the SDL dummy audio driver!"
   140                     " No sound will be output!\n\n");
   141 #endif
   142 
   143 	/* Allocate mixing buffer */
   144 	this->hidden->mixlen = spec->size;
   145 	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   146 	if ( this->hidden->mixbuf == NULL ) {
   147 		return(-1);
   148 	}
   149 	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   150 
   151 	/* We're ready to rock and roll. :-) */
   152 	return(0);
   153 }
   154