How to find out if the WebAudio oscillator is working?

Can I find out when the WebAudio oscillator is turned off, and then call its method stop?

My reason is to ask about it, because if you do not call stopthe oscillator, it hangs indefinitely in memory. But the oscillators have no length or duration, so there is no way to find out if the sound it makes is finished so that you can call stopwhen it was done. So I wonder if there is a way to check if the generator makes any audible sound or silence?

+4
source share
2 answers

You can place the analyzer between the generator and its output:

var size = 2048;
var analyser = audioCtx.createAnalyser();
var data = new Float32Array(size);
analyser.fftSize = size;
theOscillator.connect(analyser);
analyser.connect(theOutput);

var silenceChecker = setInterval(function() {
    analyser.getFloatTimeDomainData(data);
    for (var i = 0; i < size; ++i) {
        if (data[i] !== 0) return;
    }
    // It is silent.
    clearInterval(silenceChecker);
    theOscillator.stop();
    theOscillator.disconnect();
    analyser.disconnect();
}, Math.floor(size / audioCtx.sampleRate * 1000));

, , , , , . , , .

+4

, , - , .

- ​​ / .

+1

Source: https://habr.com/ru/post/1622021/


All Articles