3
votes

I did the code up to finding the PCM Data from a audio file. How should i apply these data to the Fast Fourier Transform Algorithm? Are there more things to consider before applying the byte array to 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]);
        }
}
1
first: using that code you have just stored the content of adios.wav into an array, but that doesn't mean you have successfully converted the file into an array of number. to do that, you need to read the file header first, and then decode the file content accordingly with the header data type (int, float, 8- 16-bits) and eventual compression. Secondly, which FFT class you want to use? try to use it first, and then tell us how it went wrong (if it did).lCapp

1 Answers

3
votes

You need to skip the wav header and transform the PCM samples to float values between -1 and 1. For example for a byte array with PCM wav with 16 bits per sample and little endian the following conversion is needed (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 end up with a float[] that can be used for an FFT analysis.

To make this more easy the JVM includes the AudioSystem and AudioInputStream classes.

The source code of TarsosDSP, a Java audio processing library, is full of examples. The TarosDSP manual explains the relation between PCM data and workable samples.