绘制音频波形图Java

Plot audio waveform graph Java

我想绘制 .wav 音频文件的波形图。我在这个网站上找到了一个提取 .wav 字节的函数:

ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = null;
try {
    in = new BufferedInputStream(new FileInputStream(args[0]));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

int read;
byte[] buff = new byte[1024];
while ((read = in.read(buff)) > 0)
{
    out.write(buff, 0, read);
}
out.flush();

byte[] audioBytes = out.toByteArray();
for (int i=0; i<audioBytes.length; i++) {
    System.out.println(audioBytes[i]);
}

然后我使用我在控制台 (System.out...) 中找到的点在 "Microsoft Excel" 中绘制我的音频波形,结果是:

waveform on Excel 但是我的 .wav 文件的这个波形与绘制(即)开源的波形有很大不同 "Praat" :

waveform on Praat 我哪里错了?不是我必须占用的文件字节数?

您似乎假设文件中的每个字节都代表下一个时间点的波幅。这(一般来说)并非如此。除了文件以 header 开头这一事实外,每个样本都由多个通道组成,并且在每个通道内,一个样本可能占用更少(例如 4 位或更多(例如 16 位)space不仅仅是一个字节。 例如,参见以下解释:http://www.topherlee.com/software/pcm-tut-wavformat.html.

在数组 "result" 中有你会找到的点。

public double[] extract(File inputFile) {
        AudioInputStream in = null;
        try {
            in = AudioSystem.getAudioInputStream(inputFile);
        } catch (Exception e) {
            System.out.println("Cannot read audio file");
            return new double[0];
        }
        AudioFormat format = in.getFormat();
        byte[] audioBytes = readBytes(in);

        int[] result = null;
        if (format.getSampleSizeInBits() == 16) {
            int samplesLength = audioBytes.length / 2;
            result = new int[samplesLength];
            if (format.isBigEndian()) {
                for (int i = 0; i < samplesLength; ++i) {
                    byte MSB = audioBytes[i * 2];
                    byte LSB = audioBytes[i * 2 + 1];
                    result[i] = MSB << 8 | (255 & LSB);
                }
            } else {
                for (int i = 0; i < samplesLength; i += 2) {
                    byte LSB = audioBytes[i * 2];
                    byte MSB = audioBytes[i * 2 + 1];
                    result[i / 2] = MSB << 8 | (255 & LSB);
                }
            }
        } else {
            int samplesLength = audioBytes.length;
            result = new int[samplesLength];
            if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
                for (int i = 0; i < samplesLength; ++i) {
                    result[i] = audioBytes[i];
                }
            } else {
                for (int i = 0; i < samplesLength; ++i) {
                    result[i] = audioBytes[i] - 128;
                }
            }
        }

        return result;
    }