shopofolive
shopofolive

Reputation: 265

WebAudio setting gainNode.gain.value

I am trying to deal with a deprecated Chrome feature of the WebAudio API that has to do with setting gainNode.gain.value. My current code is this:

var source = ctx.createBufferSource();
var my_gain = -1; //or 1, depending on whether I want sound or not
source.gainNode.gain.value = Math.min(1.0, Math.max(-1.0, gain));

This, however, gets me an error message: "[Deprecation] GainNode.gain.value setter smoothing is deprecated and will be removed in M64, around January 2018. Please use setTargetAtTime() instead if smoothing is needed. See https://www.chromestatus.com/features/5287995770929152 for more details"

To get the error message disappear, I can do something like this:

source.gainNode.gain.setTargetAtTime(0, ctx.currentTime, 0.015);

But how do I incorporate my_gain variable into this?

Upvotes: 0

Views: 3619

Answers (1)

cwilso
cwilso

Reputation: 13908

So, you CAN just ignore this message. Smoothing shouldn't matter much to you in this situation. If you DID want smoothing, you should use:

source.gainNode.gain.setTargetAtTime(my_value, ctx.currentTime, 0.015);

The reason setting my_value to -1 isn't working is that it SHOULDN'T - all you're doing is inverting the sound (i.e., sound values are between -1 and 1 to begin with, this would flip them but not make them zero). What you SHOULD do, in order to turn the sound off, is make my_value=0.

Upvotes: 3

Related Questions