src/audio/dummy/SDL_dummyaudio.c
changeset 1537 60620d71e324
parent 1533 38c1eb6b0083
child 1538 b1f8c14f0df5
equal deleted inserted replaced
1536:5151662ab728 1537:60620d71e324
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    18 
    19     Sam Lantinga
    19     Sam Lantinga
    20     slouken@libsdl.org
    20     slouken@libsdl.org
    21 
    21 
    22     This file hacked^H^H^H^H^H^Hwritten by Ryan C. Gordon
    22     This file written by Ryan C. Gordon (icculus@icculus.org)
    23         (icculus@icculus.org)
       
    24 */
    23 */
    25 #include "SDL_config.h"
    24 #include "SDL_config.h"
    26 
    25 
    27 /* Output audio to nowhere... */
    26 /* Output audio to nowhere... */
    28 
    27 
    34 #include "../SDL_audiodev_c.h"
    33 #include "../SDL_audiodev_c.h"
    35 #include "SDL_dummyaudio.h"
    34 #include "SDL_dummyaudio.h"
    36 
    35 
    37 /* The tag name used by DUMMY audio */
    36 /* The tag name used by DUMMY audio */
    38 #define DUMMYAUD_DRIVER_NAME         "dummy"
    37 #define DUMMYAUD_DRIVER_NAME         "dummy"
    39 
       
    40 /* environment variables and defaults. */
       
    41 #define DUMMYENVR_WRITEDELAY      "SDL_DUMMYAUDIODELAY"
       
    42 #define DUMMYDEFAULT_WRITEDELAY   150
       
    43 
    38 
    44 /* Audio driver functions */
    39 /* Audio driver functions */
    45 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
    40 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
    46 static void DUMMYAUD_WaitAudio(_THIS);
    41 static void DUMMYAUD_WaitAudio(_THIS);
    47 static void DUMMYAUD_PlayAudio(_THIS);
    42 static void DUMMYAUD_PlayAudio(_THIS);
    83 		}
    78 		}
    84 		return(0);
    79 		return(0);
    85 	}
    80 	}
    86 	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    81 	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    87 
    82 
    88 	envr = SDL_getenv(DUMMYENVR_WRITEDELAY);
       
    89 	this->hidden->write_delay = (envr) ? SDL_atoi(envr) : DUMMYDEFAULT_WRITEDELAY;
       
    90 
       
    91 	/* Set the function pointers */
    83 	/* Set the function pointers */
    92 	this->OpenAudio = DUMMYAUD_OpenAudio;
    84 	this->OpenAudio = DUMMYAUD_OpenAudio;
    93 	this->WaitAudio = DUMMYAUD_WaitAudio;
    85 	this->WaitAudio = DUMMYAUD_WaitAudio;
    94 	this->PlayAudio = DUMMYAUD_PlayAudio;
    86 	this->PlayAudio = DUMMYAUD_PlayAudio;
    95 	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
    87 	this->GetAudioBuf = DUMMYAUD_GetAudioBuf;
   106 };
    98 };
   107 
    99 
   108 /* This function waits until it is possible to write a full sound buffer */
   100 /* This function waits until it is possible to write a full sound buffer */
   109 static void DUMMYAUD_WaitAudio(_THIS)
   101 static void DUMMYAUD_WaitAudio(_THIS)
   110 {
   102 {
   111 	SDL_Delay(this->hidden->write_delay);
   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);
   112 }
   108 }
   113 
   109 
   114 static void DUMMYAUD_PlayAudio(_THIS)
   110 static void DUMMYAUD_PlayAudio(_THIS)
   115 {
   111 {
   116     /* no-op...this is a null driver. */
   112 	/* no-op...this is a null driver. */
   117 }
   113 }
   118 
   114 
   119 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
   115 static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
   120 {
   116 {
   121 	return(this->hidden->mixbuf);
   117 	return(this->hidden->mixbuf);
   129 	}
   125 	}
   130 }
   126 }
   131 
   127 
   132 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
   128 static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
   133 {
   129 {
       
   130 	float bytes_per_sec = 0.0f;
       
   131 
   134 	/* Allocate mixing buffer */
   132 	/* Allocate mixing buffer */
   135 	this->hidden->mixlen = spec->size;
   133 	this->hidden->mixlen = spec->size;
   136 	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   134 	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   137 	if ( this->hidden->mixbuf == NULL ) {
   135 	if ( this->hidden->mixbuf == NULL ) {
   138 		return(-1);
   136 		return(-1);
   139 	}
   137 	}
   140 	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   138 	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   141 
   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 
   142 	/* We're ready to rock and roll. :-) */
   154 	/* We're ready to rock and roll. :-) */
   143 	return(0);
   155 	return(0);
   144 }
   156 }
   145 
   157