What is the difference between the following two implementations in extracting the bytes of data from an audio file ?
The file is a .wav
file and i want to extract only the data, without headers or any other thing.
Implementation 1:
public byte[] extractAudioFromFile(String filePath) {
try {
// Get an input stream on the byte array
// containing the data
File file = new File(filePath);
final AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(file);
byte[] buffer = new byte[4096];
int counter;
while ((counter = audioInputStream.read(buffer, 0, buffer.length)) != -1) {
if (counter > 0) {
byteOut.write(buffer, 0, counter);
}
}
audioInputStream.close();
byteOut.close();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}// end catch
return ((ByteArrayOutputStream) byteOut).toByteArray();
}
Implementation 2:
public byte[] readAudioFileData(String filePath) throws IOException,
UnsupportedAudioFileException {
final AudioInputStream audioInputStream = AudioSystem
.getAudioInputStream(new File(filePath));
AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, byteOut);
audioInputStream.close();
byteOut.close();
return ((ByteArrayOutputStream) byteOut).toByteArray();
}
Every implementation returns a different size of bytes.
The first one return byte[]
with length less than second implementation.
I trying to extract the bytes of data to visualize the Spectrogram of the file.
Any explanation appreciated.
Thanks,
Samer