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.
     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 written by Ryan C. Gordon (icculus@icculus.org)
    23 */
    24 #include "SDL_config.h"
    25 
    26 /* Output audio to nowhere... */
    27 
    28 #include "SDL_rwops.h"
    29 #include "SDL_timer.h"
    30 #include "SDL_audio.h"
    31 #include "../SDL_audiomem.h"
    32 #include "../SDL_audio_c.h"
    33 #include "../SDL_audiodev_c.h"
    34 #include "SDL_dummyaudio.h"
    35 
    36 /* The tag name used by DUMMY audio */
    37 #define DUMMYAUD_DRIVER_NAME         "dummy"
    38 
    39 /* Audio driver functions */
    40 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
    41 static void DUMMYAUD_WaitAudio(_THIS);
    42 static void DUMMYAUD_PlayAudio(_THIS);
    43 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS);
    44 static void DUMMYAUD_CloseAudio(_THIS);
    45 
    46 /* Audio driver bootstrap functions */
    47 static int DUMMYAUD_Available(void)
    48 {
    49 	const char *envr = SDL_getenv("SDL_AUDIODRIVER");
    50 	if (envr && (SDL_strcmp(envr, DUMMYAUD_DRIVER_NAME) == 0)) {
    51 		return(1);
    52 	}
    53 	return(0);
    54 }
    55 
    56 static void DUMMYAUD_DeleteDevice(SDL_AudioDevice *device)
    57 {
    58 	SDL_free(device->hidden);
    59 	SDL_free(device);
    60 }
    61 
    62 static SDL_AudioDevice *DUMMYAUD_CreateDevice(int devindex)
    63 {
    64 	SDL_AudioDevice *this;
    65 	const char *envr;
    66 
    67 	/* Initialize all variables that we clean on shutdown */
    68 	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
    69 	if ( this ) {
    70 		SDL_memset(this, 0, (sizeof *this));
    71 		this->hidden = (struct SDL_PrivateAudioData *)
    72 				SDL_malloc((sizeof *this->hidden));
    73 	}
    74 	if ( (this == NULL) || (this->hidden == NULL) ) {
    75 		SDL_OutOfMemory();
    76 		if ( this ) {
    77 			SDL_free(this);
    78 		}
    79 		return(0);
    80 	}
    81 	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    82 
    83 	/* Set the function pointers */
    84 	this->OpenAudio = DUMMYAUD_OpenAudio;
    85 	this->WaitAudio = DUMMYAUD_WaitAudio;
    86 	this->PlayAudio = DUMMYAUD_PlayAudio;
    87 	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
    88 	this->CloseAudio = DUMMYAUD_CloseAudio;
    89 
    90 	this->free = DUMMYAUD_DeleteDevice;
    91 
    92 	return this;
    93 }
    94 
    95 AudioBootStrap DUMMYAUD_bootstrap = {
    96 	DUMMYAUD_DRIVER_NAME, "SDL dummy audio driver",
    97 	DUMMYAUD_Available, DUMMYAUD_CreateDevice
    98 };
    99 
   100 /* This function waits until it is possible to write a full sound buffer */
   101 static void DUMMYAUD_WaitAudio(_THIS)
   102 {
   103 	/* Don't block on first calls to simulate initial fragment filling. */
   104 	if (this->hidden->initial_calls)
   105 		this->hidden->initial_calls--;
   106 	else
   107 		SDL_Delay(this->hidden->write_delay);
   108 }
   109 
   110 static void DUMMYAUD_PlayAudio(_THIS)
   111 {
   112 	/* no-op...this is a null driver. */
   113 }
   114 
   115 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
   116 {
   117 	return(this->hidden->mixbuf);
   118 }
   119 
   120 static void DUMMYAUD_CloseAudio(_THIS)
   121 {
   122 	if ( this->hidden->mixbuf != NULL ) {
   123 		SDL_FreeAudioMem(this->hidden->mixbuf);
   124 		this->hidden->mixbuf = NULL;
   125 	}
   126 }
   127 
   128 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
   129 {
   130 	float bytes_per_sec = 0.0f;
   131 
   132 	/* Allocate mixing buffer */
   133 	this->hidden->mixlen = spec->size;
   134 	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   135 	if ( this->hidden->mixbuf == NULL ) {
   136 		return(-1);
   137 	}
   138 	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   139 
   140 	bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
   141 	                   spec->channels * spec->freq);
   142 
   143 	/*
   144 	 * We try to make this request more audio at the correct rate for
   145 	 *  a given audio spec, so timing stays fairly faithful.
   146 	 * Also, we have it not block at all for the first two calls, so
   147 	 *  it seems like we're filling two audio fragments right out of the
   148 	 *  gate, like other SDL drivers tend to do.
   149 	 */
   150 	this->hidden->initial_calls = 2;
   151 	this->hidden->write_delay =
   152 	               (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
   153 
   154 	/* We're ready to rock and roll. :-) */
   155 	return(0);
   156 }
   157