用Java绘制音频波形图

用Java绘制音频波形图,java,audio,graph,waveform,Java,Audio,Graph,Waveform,我想从.wav音频文件中绘制波形图。我在这个网站上发现了一个提取.wav字节的函数: ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(args[0])); } catch (FileNotFoundException e) { // TODO A

我想从.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]);
}
ByteArrayOutputStream out=newbytearrayoutputstream();
BufferedInputStream in=null;
试一试{
in=新的BufferedInputStream(新的FileInputStream(args[0]);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
int-read;
字节[]buff=新字节[1024];
而((read=in.read(buff))>0)
{
out.write(buff,0,read);
}
out.flush();
byte[]audioBytes=out.toByteArray();
对于(int i=0;i
但我的.wav文件的波形与绘制(即)开源“Praat”的波形有很大不同:


我哪里错了?不是我必须获取的文件字节数吗?

似乎您假设文件中的每个字节都代表下一个时间点的波的振幅。这是(一般而言)情况并非如此。除了文件以一个标头开头这一事实外,每个样本由多个通道组成,并且在每个通道内,样本占用的空间可能少于一个字节(例如,4位或更多(例如,16位)。 例如,请参见以下说明:.

在数组“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;
    }
public double[]提取(文件输入文件){
AudioInputStream in=null;
试一试{
in=AudioSystem.getAudioInputStream(inputFile);
}捕获(例外e){
System.out.println(“无法读取音频文件”);
返回新的双精度[0];
}
AudioFormat=in.getFormat();
字节[]音频字节=读取字节(英寸);
int[]结果=null;
if(format.getSampleSizeInBits()==16){
int samplesLength=audioBytes.length/2;
结果=新整数[样本长度];
if(格式为.isBigEndian()){
对于(int i=0;iresult[i]=MSB哦,对了!那么,对于你来说,我应该采取什么样的观点才能使该图表如普拉特的图像所示?