Date: 10 Jun 2003 15:30:59 -0400
authorSam Lantinga <slouken@libsdl.org>
Tue, 24 Jan 2006 07:20:18 +0000
changeset 126080f8c94b5199
parent 1259 36f24cdfcda7
child 1261 031e093ba2a5
Date: 10 Jun 2003 15:30:59 -0400
From: Mike Shal
Subject: [SDL] Bug in SDL_wave.c?

Hey everyone, I'm not sure if this is a bug in SDL, or if I just have
incorrect WAV files. The problem I'm having is loading multiple
concatenated WAVs from SDL_LoadWAV_RW. Some WAV files put comments at
the end of the file (which may be bad form), and SDL doesn't skip past
them when reading from the RWops. So the next WAV I try to load will
start at the comment section of the previous WAV, which obviously
doesn't work. If anyone else is having this problem, one quick fix you
can do is run sox on the bad WAVs, which strips out all of the comment
sections.
Eg:

$ sox sound.wav tmp.wav
$ mv -f tmp.wav sound.wav

The other fix is to patch SDL_wave.c, which is included with this email.
(Assuming I made the patch correctly :). All it does is calculate how
much remaining space there is in the WAV file after the data chunk, and
does SDL_RWseek to skip it. I don't think it should interfere with
anything else, but if someone could check it that would be nice :). If
the bug is really with SDL and not with my WAVs, can someone work this
into the next version of SDL? Thanks,

-Mike Shal
src/audio/SDL_wave.c
     1.1 --- a/src/audio/SDL_wave.c	Tue Jan 24 06:36:43 2006 +0000
     1.2 +++ b/src/audio/SDL_wave.c	Tue Jan 24 07:20:18 2006 +0000
     1.3 @@ -418,8 +418,9 @@
     1.4  
     1.5  	/* WAV magic header */
     1.6  	Uint32 RIFFchunk;
     1.7 -	Uint32 wavelen;
     1.8 +	Uint32 wavelen = 0;
     1.9  	Uint32 WAVEmagic;
    1.10 +	Uint32 headerDiff = 0;
    1.11  
    1.12  	/* FMT chunk */
    1.13  	WaveFMT *format = NULL;
    1.14 @@ -446,6 +447,7 @@
    1.15  		was_error = 1;
    1.16  		goto done;
    1.17  	}
    1.18 +	headerDiff += sizeof(Uint32); // for WAVE
    1.19  
    1.20  	/* Read the audio data format chunk */
    1.21  	chunk.data = NULL;
    1.22 @@ -458,6 +460,8 @@
    1.23  			was_error = 1;
    1.24  			goto done;
    1.25  		}
    1.26 +		// 2 Uint32's for chunk header+len, plus the lenread
    1.27 +		headerDiff += lenread + 2 * sizeof(Uint32);
    1.28  	} while ( (chunk.magic == FACT) || (chunk.magic == LIST) );
    1.29  
    1.30  	/* Decode the audio data format */
    1.31 @@ -535,7 +539,9 @@
    1.32  		}
    1.33  		*audio_len = lenread;
    1.34  		*audio_buf = chunk.data;
    1.35 +		if(chunk.magic != DATA) headerDiff += lenread + 2 * sizeof(Uint32);
    1.36  	} while ( chunk.magic != DATA );
    1.37 +	headerDiff += 2 * sizeof(Uint32); // for the data chunk and len
    1.38  
    1.39  	if ( MS_ADPCM_encoded ) {
    1.40  		if ( MS_ADPCM_decode(audio_buf, audio_len) < 0 ) {
    1.41 @@ -561,6 +567,10 @@
    1.42  	if ( freesrc && src ) {
    1.43  		SDL_RWclose(src);
    1.44  	}
    1.45 +	else {
    1.46 +		// seek to the end of the file (given by the RIFF chunk)
    1.47 +		SDL_RWseek(src, wavelen - chunk.length - headerDiff, SEEK_CUR);
    1.48 +	}
    1.49  	if ( was_error ) {
    1.50  		spec = NULL;
    1.51  	}