Search code examples
javaaudiofftfrequencygoertzel-algorithm

determine frequency given a byte[] of recorded audio data using java


I've been doing a bit of research on determining frequency given raw audio. There seems to be a lot of information on it, but I haven't been able to find a simple implementation using java. What I would like to do is partition an array of bytes with 44.1K elements into samples of size 44 (discarding the remainder) and determine if there is a tone over 18KHz in each sample (I'm trying to find when a tone that was played during the recording was picked up by the mic). If need be, I could use larger samples but 44 elements per sample would be ideal. I understand that I would probably need to use FFT, but the math is a little heavy for me. I had attempted to analyze the array using the goertzel algorithm, but I wasn't able to get conclusive results. My question then is how would I divide up an array of bytes with 44.1K elements into 1002 samples, determine the frequency of each sample (or detect if the frequency of a sample is over 18KHz), and what would be an example implementation of this. Please give code or pseudocode. I've read a lot of the documentation that is out there I just need to see an example.


Solution

  • About FFT in java, have a look at Reliable and fast FFT in Java

    Everything else is just a loop:

    for (int i=0; i<sampleArray.length; i+=44) {
        if (findOver18KHz(sampleArray, i, i++44)) {
            // found tone over 18KHz
        }
    }
    
    /**
     * Check for a tone over 18KHz
     */
    boolean findOver18KHz(int[] samples, start, end) {
      // call the FFT stuff
      // make sure you don't get an ArrayOutOfBoundsException for samples.
    }