if ((magic != WaveFileFormat.RIFF_MAGIC) || (waveMagic != WaveFileFormat.WAVE_MAGIC)) {
// not WAVE, throw UnsupportedAudioFileException
if (doReset) {
dis.reset();
}
throw new UnsupportedAudioFileException("not a WAVE file");
}
// find and read the "fmt" chunk
// we break out of this loop either by hitting EOF or finding "fmt "
while(true) {
try {
fmt = dis.readInt();
nread += 4;
if( fmt==WaveFileFormat.FMT_MAGIC ) {
// we've found the 'fmt' chunk
break;
} else {
// else not 'fmt', skip this chunk
length = rllong(dis);
nread += 4;
if (length % 2 > 0) length++;
nread += dis.skipBytes(length);
}
} catch (EOFException eof) {
// we've reached the end of the file without finding the 'fmt' chunk
throw new UnsupportedAudioFileException("Not a valid WAV file");
}
}
// Read the format chunk size.
length = rllong(dis);
nread += 4;
// This is the nread position at the end of the format chunk
int endLength = nread + length;
// Read the wave format data out of the format chunk.
// encoding.
wav_type = rlshort(dis); nread += 2;
if (wav_type == WaveFileFormat.WAVE_FORMAT_PCM)
encoding = AudioFormat.Encoding.PCM_SIGNED; // if 8-bit, we need PCM_UNSIGNED, below...
else if ( wav_type == WaveFileFormat.WAVE_FORMAT_ALAW )
encoding = AudioFormat.Encoding.ALAW;
else if ( wav_type == WaveFileFormat.WAVE_FORMAT_MULAW )
encoding = AudioFormat.Encoding.ULAW;
else {
// we don't support any other WAVE formats....
throw new UnsupportedAudioFileException("Not a supported WAV file");
}
// channels
channels = rlshort(dis); nread += 2;
// sample rate.
sampleRate = rllong(dis); nread += 4;
// this is the avgBytesPerSec
avgBytesPerSec = rllong(dis); nread += 4;
// this is blockAlign value
blockAlign = rlshort(dis); nread += 2;
// this is the PCM-specific value bitsPerSample
sampleSizeInBits = (int)rlshort(dis); nread += 2;
// if sampleSizeInBits==8, we need to use PCM_UNSIGNED
if ((sampleSizeInBits==8) && encoding.equals(AudioFormat.Encoding.PCM_SIGNED))
encoding = AudioFormat.Encoding.PCM_UNSIGNED;
// skip any difference between the length of the format chunk
// and what we read
// if the length of the chunk is odd, there's an extra pad byte
// at the end. i've never seen this in the fmt chunk, but we
// should check to make sure.
if (length % 2 != 0) length += 1;
// $$jb: 07.28.99: endLength>nread, not length>nread.
// This fixes #4257986
if (endLength > nread)
nread += dis.skipBytes(endLength - nread);
// we have a format now, so find the "data" chunk
// we break out of this loop either by hitting EOF or finding "data"
// $$kk: if "data" chunk precedes "fmt" chunk we are hosed -- can this legally happen?
nread = 0;
while(true) {
try{
int datahdr = dis.readInt();
nread+=4;
if (datahdr == WaveFileFormat.DATA_MAGIC) {
// we've found the 'data' chunk
break;
} else {
// else not 'data', skip this chunk
int thisLength = rllong(dis); nread += 4;
if (thisLength % 2 > 0) thisLength++;
nread += dis.skipBytes(thisLength);
}
} catch (EOFException eof) {
// we've reached the end of the file without finding the 'data' chunk
throw new UnsupportedAudioFileException("Not a valid WAV file");
}
}
// this is the length of the data chunk
int dataLength = rllong(dis); nread += 4;