/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* WAVE file reader.
*
* @author Kara Kytle
* @author Jan Borgersen
* @author Florian Bomers
*/
/**
* Constructs a new WaveFileReader object.
*/
public WaveFileReader() {
}
/**
* Obtains the audio file format of the input stream provided. The stream must
* point to valid audio file data. In general, audio file providers may
* need to read some data from the stream before determining whether they
* support it. These parsers must
* be able to mark the stream, read enough data to determine whether they
* support the stream, and, if not, reset the stream's read pointer to its original
* position. If the input stream does not support this, this method may fail
* with an IOException.
* @param stream the input stream from which file format information should be
* extracted
* @return an <code>AudioFileFormat</code> object describing the audio file format
* @throws UnsupportedAudioFileException if the stream does not point to valid audio
* file data recognized by the system
* @throws IOException if an I/O exception occurs
* @see InputStream#markSupported
* @see InputStream#mark
*/
public AudioFileFormat getAudioFileFormat(InputStream stream) throws UnsupportedAudioFileException, IOException {
// fix for 4489272: AudioSystem.getAudioFileFormat() fails for InputStream, but works for URL
// the following is not strictly necessary - but was implemented like that in 1.3.0 - 1.4.1
// so I leave it as it was. May remove this for 1.5.0
return aff;
}
/**
* Obtains the audio file format of the URL provided. The URL must
* point to valid audio file data.
* @param url the URL from which file format information should be
* extracted
* @return an <code>AudioFileFormat</code> object describing the audio file format
* @throws UnsupportedAudioFileException if the URL does not point to valid audio
* file data recognized by the system
* @throws IOException if an I/O exception occurs
*/
public AudioFileFormat getAudioFileFormat(URL url) throws UnsupportedAudioFileException, IOException {
try {
} finally {
}
return fileFormat;
}
/**
* Obtains the audio file format of the File provided. The File must
* point to valid audio file data.
* @param file the File from which file format information should be
* extracted
* @return an <code>AudioFileFormat</code> object describing the audio file format
* @throws UnsupportedAudioFileException if the File does not point to valid audio
* file data recognized by the system
* @throws IOException if an I/O exception occurs
*/
public AudioFileFormat getAudioFileFormat(File file) throws UnsupportedAudioFileException, IOException {
// part of fix for 4325421
try {
} finally {
}
return fileFormat;
}
/**
* Obtains an audio stream from the input stream provided. The stream must
* point to valid audio file data. In general, audio file providers may
* need to read some data from the stream before determining whether they
* support it. These parsers must
* be able to mark the stream, read enough data to determine whether they
* support the stream, and, if not, reset the stream's read pointer to its original
* position. If the input stream does not support this, this method may fail
* with an IOException.
* @param stream the input stream from which the <code>AudioInputStream</code> should be
* constructed
* @return an <code>AudioInputStream</code> object based on the audio file data contained
* in the input stream.
* @throws UnsupportedAudioFileException if the stream does not point to valid audio
* file data recognized by the system
* @throws IOException if an I/O exception occurs
* @see InputStream#markSupported
* @see InputStream#mark
*/
public AudioInputStream getAudioInputStream(InputStream stream) throws UnsupportedAudioFileException, IOException {
// getFMT leaves the input stream at the beginning of the audio data
AudioFileFormat fileFormat = getFMT(stream, true); // throws UnsupportedAudioFileException, IOException
// we've got everything, and the stream is at the
// beginning of the audio data, so return an AudioInputStream.
}
/**
* Obtains an audio stream from the URL provided. The URL must
* point to valid audio file data.
* @param url the URL for which the <code>AudioInputStream</code> should be
* constructed
* @return an <code>AudioInputStream</code> object based on the audio file data pointed
* to by the URL
* @throws UnsupportedAudioFileException if the URL does not point to valid audio
* file data recognized by the system
* @throws IOException if an I/O exception occurs
*/
public AudioInputStream getAudioInputStream(URL url) throws UnsupportedAudioFileException, IOException {
try {
} finally {
if (fileFormat == null) {
}
}
}
/**
* Obtains an audio stream from the File provided. The File must
* point to valid audio file data.
* @param file the File for which the <code>AudioInputStream</code> should be
* constructed
* @return an <code>AudioInputStream</code> object based on the audio file data pointed
* to by the File
* @throws UnsupportedAudioFileException if the File does not point to valid audio
* file data recognized by the system
* @throws IOException if an I/O exception occurs
*/
public AudioInputStream getAudioInputStream(File file) throws UnsupportedAudioFileException, IOException {
// part of fix for 4325421
try {
} finally {
if (fileFormat == null) {
}
}
}
//--------------------------------------------------------------------
private AudioFileFormat getFMT(InputStream stream, boolean doReset) throws UnsupportedAudioFileException, IOException {
// assumes sream is rewound
int bytesRead;
int nread = 0;
int fmt;
int length = 0;
int wav_type = 0;
short channels;
long sampleRate;
long avgBytesPerSec;
short blockAlign;
int sampleSizeInBits;
if (doReset) {
}
int totallength;
if (fileLength <= 0) {
} else {
}
// not WAVE, throw UnsupportedAudioFileException
if (doReset) {
}
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 {
nread += 4;
// we've found the 'fmt' chunk
break;
} else {
// else not 'fmt', skip this chunk
nread += 4;
}
} 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.
nread += 4;
// This is the nread position at the end of the format chunk
// Read the wave format data out of the format chunk.
// encoding.
else {
// we don't support any other WAVE formats....
throw new UnsupportedAudioFileException("Not a supported WAV file");
}
// channels
// sample rate.
// this is the avgBytesPerSec
// this is blockAlign value
// this is the PCM-specific value bitsPerSample
// if sampleSizeInBits==8, we need to use 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.
// $$jb: 07.28.99: endLength>nread, not length>nread.
// This fixes #4257986
// 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{
nread+=4;
// we've found the 'data' chunk
break;
} else {
// else not 'data', skip this chunk
}
} 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
// now build the new AudioFileFormat and return
(float)sampleRate,
(float)sampleRate, false);
}
}