audioParam.exponentialRampToValueAtTime 是如何工作的?

How does the audioParam.exponentialRampToValueAtTime work?

我无法通过 GainNode 的 exponentialRampToValueAtTime 获得音量的滑动变化。

这是一个例子:

var context = new AudioContext(),
    osc = context.createOscillator(),
    gain = context.createGain();

osc.frequency.value = 440; // A note
osc.start( 0 );
osc.connect( gain );

gain.gain.value = 0;
gain.connect( context.destination );

gain.gain.cancelScheduledValues( 0 );
gain.gain.setValueAtTime( 0, context.currentTime );
gain.gain.exponentialRampToValueAtTime( 1, context.currentTime + 2 );

根据我的理解,这应该逐渐增加音量,直到达到 1 (100%),整个过程应该需要 2 秒。这个假设是否正确?

如果是,为什么保持0 2秒,然后突然切换到最大音量?

提前感谢您的时间和精力。

看来这个函数不喜欢0值。 FF 抛出 "SyntaxError: An invalid or illegal string was specified"。下面的代码将正确斜坡。参见 Plnkr

var context = new AudioContext(),
    osc = context.createOscillator(),
    gain = context.createGain();

osc.frequency.value = 440.0; // A note
osc.start( 0 );
osc.connect( gain );

gain.connect( context.destination );

gain.gain.setValueAtTime(0.0001, context.currentTime); // <-- line of interest

gain.gain.exponentialRampToValueAtTime(1, context.currentTime + 10 );

更新: "A NotSupportedError exception must be thrown if this value is less than or equal to 0, or if the value at the time of the previous event is less than or equal to 0" 根据 Web Audio specification。正如@cwilso 所指出的(见评论)。

The docs at Mozilla说这个值必须是正数。