Graphing the pitch (frequency) of a sound

Posted by Coronatus on Stack Overflow See other posts from Stack Overflow or by Coronatus
Published on 2011-01-16T22:46:09Z Indexed on 2011/01/16 22:53 UTC
Read the original article Hit count: 272

Filed under:
|
|
|
|

I want to plot the pitch of a sound into a graph.

Currently I can plot the amplitude. The graph below is created by the data returned by getUnscaledAmplitude():

alt text

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(new FileInputStream(file)));
byte[] bytes = new byte[(int) (audioInputStream.getFrameLength()) * (audioInputStream.getFormat().getFrameSize())];
audioInputStream.read(bytes);

// Get amplitude values for each audio channel in an array.
graphData = type.getUnscaledAmplitude(bytes, this);


public int[][] getUnscaledAmplitude(byte[] eightBitByteArray, AudioInfo audioInfo)
{
    int[][] toReturn = new int[audioInfo.getNumberOfChannels()][eightBitByteArray.length / (2 * audioInfo.
        getNumberOfChannels())];
    int index = 0;

    for (int audioByte = 0; audioByte < eightBitByteArray.length;)
    {
        for (int channel = 0; channel < audioInfo.getNumberOfChannels(); channel++)
        {
            // Do the byte to sample conversion.
            int low = (int) eightBitByteArray[audioByte];
            audioByte++;
            int high = (int) eightBitByteArray[audioByte];
            audioByte++;
            int sample = (high << 8) + (low & 0x00ff);

            if (sample < audioInfo.sampleMin)
            {
                audioInfo.sampleMin = sample;
            }
            else if (sample > audioInfo.sampleMax)
            {
                audioInfo.sampleMax = sample;
            }

            toReturn[channel][index] = sample;
        }
        index++;
    }

    return toReturn;
}

But I need to show the audio's pitch, not amplitude. Fast Fourier transform appears to get the pitch, but it needs to know more variables than the raw bytes I have, and is very complex and mathematical.

Is there a way I can do this?

© Stack Overflow or respective owner

Related posts about java

Related posts about audio