0
votes

I'm simply trying to read the FFT values of a 1000Hz sine wave in this code. However the console output displays '-128' a 1000 times. Why doesn't the analyzer node work in this?

window.onload = init;
var sourceNode;

function init(){

var context = new AudioContext();
var osc = context.createOscillator();
var analyser = context.createAnalyser();
var gain = context.createGain();
sourceNode = context.createBufferSource();
var amplitudeArray = new Float32Array(analyser.frequencyBinCount);

osc.frequency.value=1000;
osc.start();


gain.gain.value=0.07;

osc.connect(analyser);
analyser.connect(gain);
gain.connect(context.destination);

analyser.getFloatFrequencyData(amplitudeArray);

for(var i=0;i<amplitudeArray.length;i++){
    console.log(amplitudeArray[i]);
}

}
2

2 Answers

0
votes

You are basically asking the analyser for the FFT data right when the oscillator starts. At this point, the internal buffers are full of zeroes, so the output is -128 dB. That is, everything is zero.

Try waiting for a second before calling analyser.getFloatFrequencyData. You'll see that the output is not a constant -128.

0
votes

In this particular case, because you're IMMEDIATELY requesting the data; although you've called osc.start(), the audio system hasn't processed any data yet, so when you call getFloatFrequencyData() it's full of empty data. (FloatFrequencyData is in decibels, so -128 is the noise floor - aka "zero").

If you had enough of a time gap between start() and the getFloatFrequencyData() call for some audio to be processed, I expect you'd see some data.