Corrected dummy audio callback firing to be realistic, cleaned up tabs.
authorRyan C. Gordon <icculus@icculus.org>
Tue, 14 Mar 2006 19:12:49 +0000
changeset 153760620d71e324
parent 1536 5151662ab728
child 1538 b1f8c14f0df5
Corrected dummy audio callback firing to be realistic, cleaned up tabs.
src/audio/dummy/SDL_dummyaudio.c
src/audio/dummy/SDL_dummyaudio.h
     1.1 --- a/src/audio/dummy/SDL_dummyaudio.c	Tue Mar 14 17:33:33 2006 +0000
     1.2 +++ b/src/audio/dummy/SDL_dummyaudio.c	Tue Mar 14 19:12:49 2006 +0000
     1.3 @@ -19,8 +19,7 @@
     1.4      Sam Lantinga
     1.5      slouken@libsdl.org
     1.6  
     1.7 -    This file hacked^H^H^H^H^H^Hwritten by Ryan C. Gordon
     1.8 -        (icculus@icculus.org)
     1.9 +    This file written by Ryan C. Gordon (icculus@icculus.org)
    1.10  */
    1.11  #include "SDL_config.h"
    1.12  
    1.13 @@ -37,10 +36,6 @@
    1.14  /* The tag name used by DUMMY audio */
    1.15  #define DUMMYAUD_DRIVER_NAME         "dummy"
    1.16  
    1.17 -/* environment variables and defaults. */
    1.18 -#define DUMMYENVR_WRITEDELAY      "SDL_DUMMYAUDIODELAY"
    1.19 -#define DUMMYDEFAULT_WRITEDELAY   150
    1.20 -
    1.21  /* Audio driver functions */
    1.22  static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec);
    1.23  static void DUMMYAUD_WaitAudio(_THIS);
    1.24 @@ -85,9 +80,6 @@
    1.25  	}
    1.26  	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    1.27  
    1.28 -	envr = SDL_getenv(DUMMYENVR_WRITEDELAY);
    1.29 -	this->hidden->write_delay = (envr) ? SDL_atoi(envr) : DUMMYDEFAULT_WRITEDELAY;
    1.30 -
    1.31  	/* Set the function pointers */
    1.32  	this->OpenAudio = DUMMYAUD_OpenAudio;
    1.33  	this->WaitAudio = DUMMYAUD_WaitAudio;
    1.34 @@ -108,12 +100,16 @@
    1.35  /* This function waits until it is possible to write a full sound buffer */
    1.36  static void DUMMYAUD_WaitAudio(_THIS)
    1.37  {
    1.38 -	SDL_Delay(this->hidden->write_delay);
    1.39 +	/* Don't block on first calls to simulate initial fragment filling. */
    1.40 +	if (this->hidden->initial_calls)
    1.41 +		this->hidden->initial_calls--;
    1.42 +	else
    1.43 +		SDL_Delay(this->hidden->write_delay);
    1.44  }
    1.45  
    1.46  static void DUMMYAUD_PlayAudio(_THIS)
    1.47  {
    1.48 -    /* no-op...this is a null driver. */
    1.49 +	/* no-op...this is a null driver. */
    1.50  }
    1.51  
    1.52  static Uint8 *DUMMYAUD_GetAudioBuf(_THIS)
    1.53 @@ -131,6 +127,8 @@
    1.54  
    1.55  static int DUMMYAUD_OpenAudio(_THIS, SDL_AudioSpec *spec)
    1.56  {
    1.57 +	float bytes_per_sec = 0.0f;
    1.58 +
    1.59  	/* Allocate mixing buffer */
    1.60  	this->hidden->mixlen = spec->size;
    1.61  	this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
    1.62 @@ -139,6 +137,20 @@
    1.63  	}
    1.64  	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
    1.65  
    1.66 +	bytes_per_sec = (float) (((spec->format & 0xFF) / 8) *
    1.67 +	                   spec->channels * spec->freq);
    1.68 +
    1.69 +	/*
    1.70 +	 * We try to make this request more audio at the correct rate for
    1.71 +	 *  a given audio spec, so timing stays fairly faithful.
    1.72 +	 * Also, we have it not block at all for the first two calls, so
    1.73 +	 *  it seems like we're filling two audio fragments right out of the
    1.74 +	 *  gate, like other SDL drivers tend to do.
    1.75 +	 */
    1.76 +	this->hidden->initial_calls = 2;
    1.77 +	this->hidden->write_delay =
    1.78 +	               (Uint32) ((((float) spec->size) / bytes_per_sec) * 1000.0f);
    1.79 +
    1.80  	/* We're ready to rock and roll. :-) */
    1.81  	return(0);
    1.82  }
     2.1 --- a/src/audio/dummy/SDL_dummyaudio.h	Tue Mar 14 17:33:33 2006 +0000
     2.2 +++ b/src/audio/dummy/SDL_dummyaudio.h	Tue Mar 14 19:12:49 2006 +0000
     2.3 @@ -34,6 +34,7 @@
     2.4  	Uint8 *mixbuf;
     2.5  	Uint32 mixlen;
     2.6  	Uint32 write_delay;
     2.7 +	Uint32 initial_calls;
     2.8  };
     2.9  
    2.10  #endif /* _SDL_dummyaudio_h */