Skip to content Skip to sidebar Skip to footer

Determining Frequencies In Js Audiocontext.analysernode

BACKGROUND My goal is to create a JavaScript-based web app to analyse and display frequency information in audio sources, both in-page sources (

Solution 1:

So first, understand that the output of an FFT will give you an array of relative strength in frequency RANGES, not precise frequencies.

These ranges are spread out in the spectrum [0,Nyquist frequency]. The Nyquist frequency is one-half of the sample rate. So if your AudioContext.sampleRate is 48000 (Hertz), your frequency bins will range across [0,24000] (also in Hz).

If you are using the default value of 2048 for fftSize in your AnalyserNode, then frequencyBinCount will be 1024 (it's always half the FFT size). This means each frequency bin will represent (24000/1024 = 23.4) approximately 23.4Hz of range - so the bins will look something like this (off-the-cuff, rounding errors may occur here):

fData[0] is the strength of frequencies from0to23.4Hz.
fData[1] is the strength of frequencies from23.4Hzto46.8Hz.
fData[2] is the strength of frequencies from46.8Hzto70.2Hz.
fData[3] is the strength of frequencies from70.2Hzto93.6Hz.
...
fData[511] is the strength of frequencies from11976.6Hzto12000Hz.
fData[512] is the strength of frequencies from12000Hzto12023.4Hz.
...
fData[1023] is the strength of frequencies from23976.6Hzto24000Hz.

Make sense so far?

The next comment that usually comes up is "Wait a second - this is less precise, musically speaking, in the bass registers (where 23.4 Hz can cover a whole OCTAVE) than the treble registers (where there are hundreds of Hz between notes)." To that I say: Yes, yes it is. That's just how FFTs work. In the upper registers, it's easier to see tuning differences.

The NEXT next comment is usually "wow, I need a MASSIVE fftSize to be precise in the bass registers." Usually, the answer is "no, you probably shouldn't do it that way" - at some point, auto-correlation is more efficient than FFTs, and it's a lot more precise.

Hope this helps point you in the right direction, add a comment if there's a followup.

Post a Comment for "Determining Frequencies In Js Audiocontext.analysernode"