Don't do extra memory allocations if we're not going to end up converting the audio.
authorSam Lantinga <slouken@libsdl.org>
Fri, 13 Jan 2012 02:39:41 -0500
changeset 5600fc8dffb86c5
parent 559 e759a462d7da
child 561 87bdb4c81c0b
Don't do extra memory allocations if we're not going to end up converting the audio.
Added a missing file close.
mixer.c
     1.1 --- a/mixer.c	Mon Jan 09 08:02:24 2012 -0500
     1.2 +++ b/mixer.c	Fri Jan 13 02:39:41 2012 -0500
     1.3 @@ -614,6 +614,9 @@
     1.4  	}
     1.5  	if ( !loaded ) {
     1.6  		free(chunk);
     1.7 +		if ( freesrc ) {
     1.8 +			SDL_RWclose(src);
     1.9 +		}
    1.10  		return(NULL);
    1.11  	}
    1.12  
    1.13 @@ -623,31 +626,36 @@
    1.14  #endif
    1.15  
    1.16  	/* Build the audio converter and create conversion buffers */
    1.17 -	if ( SDL_BuildAudioCVT(&wavecvt,
    1.18 -			wavespec.format, wavespec.channels, wavespec.freq,
    1.19 -			mixer.format, mixer.channels, mixer.freq) < 0 ) {
    1.20 +	if ( wavespec.format != mixer.format ||
    1.21 +		 wavespec.channels != mixer.channels ||
    1.22 +		 wavespec.freq != mixer.freq ) {
    1.23 +		if ( SDL_BuildAudioCVT(&wavecvt,
    1.24 +				wavespec.format, wavespec.channels, wavespec.freq,
    1.25 +				mixer.format, mixer.channels, mixer.freq) < 0 ) {
    1.26 +			SDL_FreeWAV(chunk->abuf);
    1.27 +			free(chunk);
    1.28 +			return(NULL);
    1.29 +		}
    1.30 +		samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
    1.31 +		wavecvt.len = chunk->alen & ~(samplesize-1);
    1.32 +		wavecvt.buf = (Uint8 *)calloc(1, wavecvt.len*wavecvt.len_mult);
    1.33 +		if ( wavecvt.buf == NULL ) {
    1.34 +			SDL_SetError("Out of memory");
    1.35 +			SDL_FreeWAV(chunk->abuf);
    1.36 +			free(chunk);
    1.37 +			return(NULL);
    1.38 +		}
    1.39 +		memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
    1.40  		SDL_FreeWAV(chunk->abuf);
    1.41 -		free(chunk);
    1.42 -		return(NULL);
    1.43 +
    1.44 +		/* Run the audio converter */
    1.45 +		if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
    1.46 +			free(wavecvt.buf);
    1.47 +			free(chunk);
    1.48 +			return(NULL);
    1.49 +		}
    1.50  	}
    1.51 -	samplesize = ((wavespec.format & 0xFF)/8)*wavespec.channels;
    1.52 -	wavecvt.len = chunk->alen & ~(samplesize-1);
    1.53 -	wavecvt.buf = (Uint8 *)calloc(1, wavecvt.len*wavecvt.len_mult);
    1.54 -	if ( wavecvt.buf == NULL ) {
    1.55 -		SDL_SetError("Out of memory");
    1.56 -		SDL_FreeWAV(chunk->abuf);
    1.57 -		free(chunk);
    1.58 -		return(NULL);
    1.59 -	}
    1.60 -	memcpy(wavecvt.buf, chunk->abuf, chunk->alen);
    1.61 -	SDL_FreeWAV(chunk->abuf);
    1.62  
    1.63 -	/* Run the audio converter */
    1.64 -	if ( SDL_ConvertAudio(&wavecvt) < 0 ) {
    1.65 -		free(wavecvt.buf);
    1.66 -		free(chunk);
    1.67 -		return(NULL);
    1.68 -	}
    1.69  	chunk->allocated = 1;
    1.70  	chunk->abuf = wavecvt.buf;
    1.71  	chunk->alen = wavecvt.len_cvt;