How to correctly undo the currently changing AudioParam in the web audio API?

I am trying to implement volume envelopes that can restart at any time, even if it is already in the middle of the param-motion element, but I cannot figure out how to do this without clicks in the received audio (which seems somewhat irregular as to when they are occur).

Is it possible? I see that AudioParam.cancelScheduledValues() "cancels all planned future changes in AudioParam", but I'm not sure what will happen with the current change.

This is the code that I use to start / restart the volume envelope.

 var now = context.currentTime; var currentVol = gain.gain.value; gain.gain.cancelScheduledValues(now); gain.gain.setValueAtTime(currentVol, now); gain.gain.exponentialRampToValueAtTime(1, now + volAttack); gain.gain.exponentialRampToValueAtTime(0.000001, now + volAttack + volDecay); 
+6
source share
3 answers

I find custom curves are more reliable and more manageable

 function expCurve(start, end) { var count = 10; var t = 0; var curve = new Float32Array(count + 1); start = Math.max(start, 0.0000001); end = Math.max(end, 0.0000001); for (var i = 0; i <= count; ++i) { curve[i] = start * Math.pow(end / start, t); t += 1/count; } return curve; } gain.gain.cancelScheduledValues(0); var currentVol = gain.gain.value; var now = context.currentTime; gain.gain.setValueCurveAtTime(expCurve(currentVol, 1), now, volAttack); gain.gain.setValueCurveAtTime(expCurve(1, 0), now + volAttack, volDecay); 
+2
source

After you asked on the weak Web Audio channel, they explained to me that this is a known issue with the current specification.

Here is the link to the question on GitHub: https://github.com/WebAudio/web-audio-api/issues/344

So the short answer is that stopping the ramp halfway is not currently supported. Therefore, as a workaround, they should be monitored in each segment.

+2
source

I believe you can look for cancelAndHoldAtTime() . According to MDN:

The cancelAndHoldAtTime () property of the AudioParam interface cancels all planned future changes in AudioParam, but retains its value at the specified time until further changes are made using other methods.

The added ability to hold the value until further changes are made seems to be what you are looking for. I am using the web audio API to create an amplitude envelope right now and it seems to work.

Note : as of March 2018, this feature is only implemented in Chrome. See Compatibility Chart in MDN for updates.

+1
source

All Articles