gpt4 book ai didi

java - 绘制音频波形图 Java

转载 作者:行者123 更新时间:2023-12-02 03:12:40 33 4
gpt4 key购买 nike

我想绘制 .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我哪里错了?不是我必须占用的文件字节吗?

最佳答案

在数组“结果”中,有您可以找到的点。

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

关于java - 绘制音频波形图 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40810702/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com