I think I understand the basic concept of the web audio API, as well as how sounds work in general. And although I managed to make the sound “fade”, I can’t understand why it doesn’t “fade” in the following fragment that I wrote to present the problem:
(function () { 'use strict'; var context = new AudioContext(), wave = context.createOscillator(), gain = context.createGain(), ZERO = 0.000001; wave.connect(gain); gain.connect(context.destination); wave.type = 'sine'; wave.frequency.value = 200; gain.gain.value = ZERO; wave.start(context.currentTime); gain.gain.exponentialRampToValueAtTime(1.00, 1.0); gain.gain.exponentialRampToValueAtTime(ZERO, 3.0); })();
NOTE. . A similar problem arose in Firefox (Linux) and Chrome (Windows).
Replacing the line gain.gain.value = ZEROwith:
gain.gain.value = ZERO
gain.gain.setValueAtTime(ZERO, 0);
fix the problem.
The rationale is in the actual specification of the function exponentialRampToValueAtTime():
exponentialRampToValueAtTime()
, ( ), . linearRampToValueAtTime.
linearRampToValueAtTime
MDN:
AudioParam.value... , , - , AudioParam, , .
AudioParam.value
gain.gain.value = ZERO;
AudioParam - ( 1 = 0). . ; 75%, , .