Bungus
Bungus

Reputation: 602

How do you check the value of an AudioParam after setting it with setValueAtTime()

When I set the value of a gain AudioParam using setValueAtTime() or linearRampToValueAtTime() the value property does not change.

// create a gain node
let context = new AudioContext();
let node = context.createGain();

// check the gain value
console.log("gain value before: " + node.gain.value);

// set the gain value at current time
node.gain.setValueAtTime(0.5, context.currentTime);

// 1 second later the value has not changed
setTimeout(function() {
  console.log("gain value after: " + node.gain.value);
}, 1000)

https://jsfiddle.net/uk6f4Lha/

How are you supposed to check the value of an AudioParam in this case?

Any help would be greatly appreciated as I've been tripping over this for hours!

Upvotes: 0

Views: 231

Answers (1)

chrisguttandin
chrisguttandin

Reputation: 9076

I think this is a bug in Chrome. Your example works in Firefox. The value property should return the internal value. https://webaudio.github.io/web-audio-api/#dom-audioparam-value

However that internal value does not yet take any signal into account that might be connected to the AudioParam. It only reflects the value that the AudioParam has when applying the automations.

If you don't have any signals connected to your AudioParams you could also compute the internal value yourself by keeping track of the automations that you apply. I built a library for that use called automation-events. It gets used by standardized-audio-context for exactly that purpose.

Upvotes: 2

Related Questions