What time window does the data in AnalyserNode.getByteFrequencyData correspond to?

Viewed 123

I would like to capture fourier transform of a small time window (ie ~1 second).

let audioCtx = new AudioContext();
let analyser = audioCtx.createAnalyser()
let buffer = new Uint8Array(analyser.frequencyBinCount);

//given an audio stream 'stream'
let source = audioCtx.createMediaStreamSource(stream);

setInterval(()=>{
    analyser.getByteFrequencyData(buffer);
    //Do some analysis with buffer...
},1000)

Using WebAPI's AnalyserNode I can get fourier data in bins of width 44100/analyser.fftSize = 22050/analyser.frequencyBinCount, allowing for specifying the resolution by setting analyser.fftSize. What is unclear to me from the documentation is how the time window is set.

From the API:

Rendering an audio graph is done in blocks of 128 samples-frames. A block of 128 samples-frames is called a render quantum, and the render quantum size is 128.

Does this imply a time window of 128/44100Hz=2.9ms?

1 Answers

Rendering an audio graph is done in blocks of 128 samples-frames. A block of 128 samples-frames is called a render quantum, and the render quantum size is 128.

Does this imply a time window of 128/44100Hz=2.9ms?

Not quite. The render quantum size is the number of sample frames that gets processed at a time by the render loop, but it does not prevent nodes from accumulating additional samples in buffers. In the particular case of the AnalyserNode, the last fftSize samples are kept for the FFT computation. So the time window is effectively analyser.fftSize/sampleRate, where sampleRate is your configured sample rate (which may be 44100, but could vary depending on the output device). To capture ~1 second of audio, assuming a sampling rate of 44100Hz you would thus need fftSize = 32768 (which would result in a time window of ~0.74 second).

Related