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