我想我了解 Web Audio API 的主要概念,以及声音的一般工作原理。即使我设法使声音“淡出”,我也无法弄清楚为什么在我写的以下片段中它没有“淡入”来表示问题:
(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);
})();
注意:同样的问题也出现在 Firefox (Linux) 和 Chrome (Windows) 上
最佳答案
将 gain.gain.value = ZERO
行替换为:
gain.gain.setValueAtTime(ZERO, 0);
将解决问题。
基本原理在 actual specification 中exponentialRampToValueAtTime()
函数:
Schedules an exponential continuous change in parameter value from the previous scheduled parameter value to the given value
因此,如果没有先前的预定参数值(只有固定值),则该函数无法进行插值。这同样适用于 linearRampToValueAtTime
函数。
这也可能对 MDN documentation 有用:
AudioParam.value
... Though it can be set, any modifications happening while there are automation events scheduled — that is events scheduled using the methods of the AudioParam — are ignored, without raising any exception
关于javascript - 声音淡出,但不淡入——为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127720/