Pass WAVE PCM byte array to FFT for pitch detection
I made a code to find PCM data from an audio file. How should I apply this data to the Fast Fourier Transform algorithm? There are a few more things to consider before applying the byte array to the FFT algorithm.
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]);
}
}
source to share
You need to skip the wav header and convert the PCM samples to floating point values between -1 and 1. For example, a PCM wav byte array with 16 bits per sample and small end requires the following conversion (from 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;
}
After this call you will get float[]
one that you can use to analyze the FFT.
To make this easier, the JVM includes classes AudioSystem
and AudioInputStream
.
The source code for TarsosDSP , a Java audio processing library, is full of examples. The TarosDSP Guide explains the relationship between PCM data and working patterns.
source to share