Question

I'm playing a bit with the Web Audio API and there is some behaviour I can't understand.

var audio = document.querySelector('audio');
var context = new AudioContext();
var source = context.createMediaElementSource(audio);
var analyser = context.createAnalyser();
source.connect(analyser);
source.connect(context.destination);

setInterval(function() {
    var freqDomain = new Float32Array(analyser.frequencyBinCount);
    analyser.getFloatFrequencyData(freqDomain);
    console.log(freqDomain);
},1000);

When I pause the Audio element, the console keeps showing me data from the analyser (and the data is changing). Why does it keep sending data when the sound is paused ?

Was it helpful?

Solution

I think this is probably because of the smoothingTimeConstant of your AnalyserNode, which defaults to 0.8.

My guess is that because of this averaging over time, when you pause the <audio> element, the values will gradually decay toward -Infinity.

Anyway, that's just a guess, but I'd say I'm about 95% sure. You could verify it pretty easily be setting analyser.smoothingTimeConstant = 0 and seeing if the behavior persists.

Oh, and here's a link to the relevant portion of the spec.: https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#dfn-smoothingTimeConstant

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top