WavInputStream (FileHandle file) {
super(file.read());
try {
if (read() != 'R' || read() != 'I' || read() != 'F' || read() != 'F')
throw new GdxRuntimeException("RIFF header not found: " + file);
skipFully(4);
if (read() != 'W' || read() != 'A' || read() != 'V' || read() != 'E')
throw new GdxRuntimeException("Invalid wave file header: " + file);
int fmtChunkLength = seekToChunk('f', 'm', 't', ' ');
int type = read() & 0xff | (read() & 0xff) << 8;
if (type != 1) throw new GdxRuntimeException("WAV files must be PCM: " + type);
channels = read() & 0xff | (read() & 0xff) << 8;
if (channels != 1 && channels != 2)
throw new GdxRuntimeException("WAV files must have 1 or 2 channels: " + channels);
sampleRate = read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24;
skipFully(6);
int bitsPerSample = read() & 0xff | (read() & 0xff) << 8;
if (bitsPerSample != 16) throw new GdxRuntimeException("WAV files must have 16 bits per sample: " + bitsPerSample);
skipFully(fmtChunkLength - 16);
dataRemaining = seekToChunk('d', 'a', 't', 'a');
} catch (Throwable ex) {
try {
close();
} catch (IOException ignored) {
}
throw new GdxRuntimeException("Error reading WAV file: " + file, ex);
}
}