Fixed bug #137
authorSam Lantinga <slouken@libsdl.org>
Tue, 09 May 2006 08:52:54 +0000
changeset 17945605a9820134
parent 1793 4d66375c2012
child 1795 398ac0f88e4d
Fixed bug #137

If SDL_OpenAudio() is passed zero for the desired format
fields, the following environment variables will be used
to fill them in:
SDL_AUDIO_FREQUENCY
SDL_AUDIO_FORMAT
SDL_AUDIO_CHANNELS
SDL_AUDIO_SAMPLES
If an environment variable is not specified, it will be set
to a reasonable default value.
WhatsNew
src/audio/SDL_audio.c
     1.1 --- a/WhatsNew	Tue May 09 07:52:04 2006 +0000
     1.2 +++ b/WhatsNew	Tue May 09 08:52:54 2006 +0000
     1.3 @@ -4,6 +4,16 @@
     1.4  Version 1.0:
     1.5  
     1.6  1.2.10:
     1.7 +	If SDL_OpenAudio() is passed zero for the desired format
     1.8 +	fields, the following environment variables will be used
     1.9 +	to fill them in:
    1.10 +		SDL_AUDIO_FREQUENCY
    1.11 +		SDL_AUDIO_FORMAT
    1.12 +		SDL_AUDIO_CHANNELS
    1.13 +		SDL_AUDIO_SAMPLES
    1.14 +	If an environment variable is not specified, it will be set
    1.15 +	to a reasonable default value.
    1.16 +
    1.17  	Added support for the SDL_VIDEO_FULLSCREEN_HEAD environment
    1.18  	variable, currently supported on X11 Xinerama configurations.
    1.19  
     2.1 --- a/src/audio/SDL_audio.c	Tue May 09 07:52:04 2006 +0000
     2.2 +++ b/src/audio/SDL_audio.c	Tue May 09 08:52:54 2006 +0000
     2.3 @@ -285,6 +285,51 @@
     2.4  	SDL_mutexV(audio->mixer_lock);
     2.5  }
     2.6  
     2.7 +static Uint16 SDL_ParseAudioFormat(const char *string)
     2.8 +{
     2.9 +	Uint16 format = 0;
    2.10 +
    2.11 +	switch (*string) {
    2.12 +	    case 'U':
    2.13 +		++string;
    2.14 +		format |= 0x0000;
    2.15 +		break;
    2.16 +	    case 'S':
    2.17 +		++string;
    2.18 +		format |= 0x8000;
    2.19 +		break;
    2.20 +	    default:
    2.21 +		return 0;
    2.22 +	}
    2.23 +	switch (SDL_atoi(string)) {
    2.24 +	    case 8:
    2.25 +		string += 1;
    2.26 +		format |= 8;
    2.27 +		break;
    2.28 +	    case 16:
    2.29 +		string += 2;
    2.30 +		format |= 16;
    2.31 +		if ( SDL_strcmp(string, "LSB") == 0
    2.32 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    2.33 +		     || SDL_strcmp(string, "SYS") == 0
    2.34 +#endif
    2.35 +		    ) {
    2.36 +			format |= 0x0000;
    2.37 +		}
    2.38 +		if ( SDL_strcmp(string, "MSB") == 0
    2.39 +#if SDL_BYTEORDER == SDL_BIG_ENDIAN
    2.40 +		     || SDL_strcmp(string, "SYS") == 0
    2.41 +#endif
    2.42 +		    ) {
    2.43 +			format |= 0x1000;
    2.44 +		}
    2.45 +		break;
    2.46 +	    default:
    2.47 +		return 0;
    2.48 +	}
    2.49 +	return format;
    2.50 +}
    2.51 +
    2.52  int SDL_AudioInit(const char *driver_name)
    2.53  {
    2.54  	SDL_AudioDevice *audio;
    2.55 @@ -386,6 +431,7 @@
    2.56  int SDL_OpenAudio(SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
    2.57  {
    2.58  	SDL_AudioDevice *audio;
    2.59 +	const char *env;
    2.60  
    2.61  	/* Start up the audio driver, if necessary */
    2.62  	if ( ! current_audio ) {
    2.63 @@ -402,9 +448,35 @@
    2.64  	}
    2.65  
    2.66  	/* Verify some parameters */
    2.67 -	if ( desired->callback == NULL ) {
    2.68 -		SDL_SetError("SDL_OpenAudio() passed a NULL callback");
    2.69 -		return(-1);
    2.70 +	if ( desired->freq == 0 ) {
    2.71 +		env = SDL_getenv("SDL_AUDIO_FREQUENCY");
    2.72 +		if ( env ) {
    2.73 +			desired->freq = SDL_atoi(env);
    2.74 +		}
    2.75 +	}
    2.76 +	if ( desired->freq == 0 ) {
    2.77 +		/* Pick some default audio frequency */
    2.78 +		desired->freq = 22050;
    2.79 +	}
    2.80 +	if ( desired->format == 0 ) {
    2.81 +		env = SDL_getenv("SDL_AUDIO_FORMAT");
    2.82 +		if ( env ) {
    2.83 +			desired->format = SDL_ParseAudioFormat(env);
    2.84 +		}
    2.85 +	}
    2.86 +	if ( desired->format == 0 ) {
    2.87 +		/* Pick some default audio format */
    2.88 +		desired->format = AUDIO_S16;
    2.89 +	}
    2.90 +	if ( desired->channels == 0 ) {
    2.91 +		env = SDL_getenv("SDL_AUDIO_CHANNELS");
    2.92 +		if ( env ) {
    2.93 +			desired->channels = SDL_atoi(env);
    2.94 +		}
    2.95 +	}
    2.96 +	if ( desired->channels == 0 ) {
    2.97 +		/* Pick a default number of channels */
    2.98 +		desired->channels = 2;
    2.99  	}
   2.100  	switch ( desired->channels ) {
   2.101  	    case 1:	/* Mono */
   2.102 @@ -416,6 +488,25 @@
   2.103  		SDL_SetError("1 (mono) and 2 (stereo) channels supported");
   2.104  		return(-1);
   2.105  	}
   2.106 +	if ( desired->samples == 0 ) {
   2.107 +		env = SDL_getenv("SDL_AUDIO_SAMPLES");
   2.108 +		if ( env ) {
   2.109 +			desired->samples = SDL_atoi(env);
   2.110 +		}
   2.111 +	}
   2.112 +	if ( desired->samples == 0 ) {
   2.113 +		/* Pick a default of ~46 ms at desired frequency */
   2.114 +		int samples = (desired->freq / 1000) * 46;
   2.115 +		int power2 = 1;
   2.116 +		while ( power2 < samples ) {
   2.117 +			power2 *= 2;
   2.118 +		}
   2.119 +		desired->samples = power2;
   2.120 +	}
   2.121 +	if ( desired->callback == NULL ) {
   2.122 +		SDL_SetError("SDL_OpenAudio() passed a NULL callback");
   2.123 +		return(-1);
   2.124 +	}
   2.125  
   2.126  #if defined(__MACOS__) || (defined(__RISCOS__) && SDL_THREADS_DISABLED)
   2.127  	/* FIXME: Need to implement PPC interrupt asm for SDL_LockAudio() */