1

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

4

1 回答 1

9

第二个 impl 正在编写完整的 WAVE '文件格式'。第二个缓冲区比第一个缓冲区大44 字节吗?

[编辑:好奇到可以实际尝试一下——以上是正确的]

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;
    }
}

我用这个示例 WAV 文件尝试了这个。

结果:

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

注意:我对你的代码片段进行了一些自由清理。

  1. System.exit(0)是一个明确的禁忌。
  2. if(counter > 0)不是真的有必要,因为 counter 必须大于0如果 read 方法的返回值 is not -1
于 2011-08-03T23:11:07.377 回答