gpt4 book ai didi

java - 将 WAVE PCM 字节数组传递给 FFT 以进行音调检测

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:46:58 25 4
gpt4 key购买 nike

我完成了从音频文件中查找 PCM 数据的代码。我应该如何将这些数据应用于快速傅里叶变换算法?在将字节数组应用于 FFT 算法之前是否还有更多需要考虑的事情。

public static void main(String[] args) throws FileNotFoundException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream("adios.wav"));

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

最佳答案

您需要跳过 wav header 并将 PCM 样本转换为 -1 和 1 之间的浮点值。例如,对于每个样本 16 位和小端字节序的 PCM wav 字节数组,需要以下转换(来自 com.sun.media.sound.AudioFloatConverter):

public float[] toFloatArray(byte[] in_buff, int in_offset,
float[] out_buff, int out_offset, int out_len) {
int ix = in_offset;
int len = out_offset + out_len;
for (int ox = out_offset; ox < len; ox++) {
out_buff[ox] = ((short) ((in_buff[ix++] & 0xFF) |
(in_buff[ix++] << 8))) * (1.0f / 32767.0f);
}
return out_buff;
}

在此调用之后,您将得到一个可用于 FFT 分析的 float[]

为了使这更容易,JVM 包括 AudioSystemAudioInputStream 类。

TarsosDSP的源代码,一个 Java 音频处理库,里面有很多例子。 TarosDSP manual解释 PCM 数据与可工作样本之间的关系。

关于java - 将 WAVE PCM 字节数组传递给 FFT 以进行音调检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25636574/

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