Web Audio API Analyser Node Not Working With Microphone Input
It was a variable scoping issue. For the second example, I was defining the microphone locally and then trying to access its stream with the analyser in another function. I just made all the Web Audio API nodes globals for peace of mind. Also it takes a few seconds for the analyser node to start reporting non -100 values. Working code for those interested:
// Globalsvar aCtx;var analyser;var microphone;if (navigator.getUserMedia) { navigator.getUserMedia({audio: true}, function(stream) { aCtx = new webkitAudioContext(); analyser = aCtx.createAnalyser(); microphone = aCtx.createMediaStreamSource(stream); microphone.connect(analyser); // analyser.connect(aCtx.destination); process(); });};function process(){ setInterval(function(){ FFTData = new Float32Array(analyser.frequencyBinCount); analyser.getFloatFrequencyData(FFTData); console.log(FFTData[0]); },10);}
If you would like to hear the live audio, you can connect the analyser to destination (speakers) as commented out above. Watch out for some lovely feedback though!