Java: Extracting bytes from audio file

Samer Makary picture Samer Makary · Aug 4, 2011 · Viewed 11.8k times · Source

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

Answer

alphazero picture alphazero · Aug 4, 2011

The 2nd impl is writing the full WAVE 'file format'. Is 2nd buffer 44 bytes larger than the first?

[edit: curious enough to actually try it - the above is correct]

package so_6933920;

import java.io.ByteArrayOutputStream;
import java.io.File;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class AudioFiles {
    public static void main(String[] args) {
        String file = "clarinet.wav";
        AudioFiles afiles = new AudioFiles();
        byte[] data1 = afiles.readAudioFileData(file);
        byte[] data2 = afiles.readWAVAudioFileData(file);
        System.out.format("data len: %d\n", data1.length);
        System.out.format("data len: %d\n", data2.length);
        System.out.format("diff len: %d\n", data2.length - data1.length);
    }
    public byte[] readAudioFileData(final String filePath) {
        byte[] data = null;
        try {
            final ByteArrayOutputStream baout = new ByteArrayOutputStream();
            final File file = new File(filePath);
            final AudioInputStream audioInputStream = AudioSystem
            .getAudioInputStream(file);

            byte[] buffer = new byte[4096];
            int c;
            while ((c = audioInputStream.read(buffer, 0, buffer.length)) != -1) {
                baout.write(buffer, 0, c);
            }
            audioInputStream.close();
            baout.close();
            data = baout.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
    public byte[] readWAVAudioFileData(final String filePath){
        byte[] data = null;
        try {
            final ByteArrayOutputStream baout = new ByteArrayOutputStream();
            final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(filePath));

            AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, baout);
            audioInputStream.close();
            baout.close();
            data = baout.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
}

I tried this with this sample WAV file.

Results:

data len: 489708
data len: 489752
diff len: 44

Note: I took some liberties with your snippet to clean it up.

  1. That System.exit(0) is a definite no-no.
  2. if(counter > 0) isn't really necessary since counter must be greater than 0 if return value of the read method is not -1.