/* * WaveAudioFileReader.java * * This file is part of Tritonus: http://www.tritonus.org/ */ /* * Copyright (c) 1999,2000 by Florian Bomers * Copyright (c) 1999 by Matthias Pfisterer * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * */ /* |<--- this code is formatted to fit into 80 columns --->| */ package org.tritonus.sampled.file; import java.io.DataInputStream; import java.io.File; import java.io.InputStream; import java.io.IOException; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.spi.AudioFileReader; import org.tritonus.share.TDebug; import org.tritonus.share.sampled.file.TAudioFileFormat; import org.tritonus.share.sampled.file.TAudioFileReader; /** * Class for reading wave files. * * @author Florian Bomers * @author Matthias Pfisterer */ public class WaveAudioFileReader extends TAudioFileReader { private static final int READ_LIMIT = 1000; public WaveAudioFileReader() { super(READ_LIMIT); } protected void advanceChunk(DataInputStream dis, long prevLength, long prevRead) throws IOException { if (prevLength>0) { dis.skip(((prevLength+1) & 0xFFFFFFFE)-prevRead); } } protected long findChunk(DataInputStream dis, int key) throws UnsupportedAudioFileException, IOException { // $$fb 1999-12-18: we should take care that we don't exceed // the mark of this stream. When we exceeded the mark and // we notice that we don't support this wave file, // other potential wave file readers have no chance. int thisKey; long chunkLength=0; do { advanceChunk(dis, chunkLength, 0); try { thisKey = dis.readInt(); } catch (IOException e) { if (TDebug.TraceAllExceptions) { TDebug.out(e); } // $$fb: when we come here, we skipped past the end of the wave file // without finding the chunk. // IMHO, this is not an IOException, as there are incarnations // of WAVE files which store data in different chunks. // maybe we can find a nice description of the "required chunk" ? throw new UnsupportedAudioFileException( "unsupported WAVE file: required chunk not found."); } chunkLength = readLittleEndianInt(dis) & 0xFFFFFFFF; // unsigned } while (thisKey != key); return chunkLength; } protected AudioFormat readFormatChunk(DataInputStream dis, long chunkLength) throws UnsupportedAudioFileException, IOException { String debugAdd=""; int read=WaveTool.MIN_FMT_CHUNK_LENGTH; if (chunkLength