将WAVE PCM字节数组传递给FFT进行基音检测



我完成了从音频文件中查找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标头并将PCM样本转换为-1和1之间的浮点值。例如,对于具有PCM wav的字节数组,每个样本有16位,并且有小端序,需要以下转换(来自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类。

Java音频处理库TarsosDSP的源代码中充满了示例。TarosDSP手册解释了PCM数据和可操作样本之间的关系。

最新更新