audioParam.exponentialRampToValueAtTime如何工作?

Nik*_*kov 7 javascript web-audio-api

我无法通过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 );
Run Code Online (Sandbox Code Playgroud)

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

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

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

Rus*_*nas 9

看来这个函数不喜欢0值.FF抛出"SyntaxError:指定了无效或非法字符串".下面的代码将正确斜坡.见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 );
Run Code Online (Sandbox Code Playgroud)

更新: "根据Web Audio规范,如果此值小于或等于0,或者如果上一个事件的值小于或等于0",则必须抛出NotSupportedError异常.正如@cwilso所说(见评论).

  • 我认为这是问题:https://github.com/WebKit/webkit/blob/master/Source/WebCore/Modules/webaudio/AudioParamTimeline.cpp#L232和this:https://github.com/WebKit/webkit /blob/master/Source/WebCore/Modules/webaudio/AudioParamTimeline.cpp#L237这是在Webkit中,但在Blink中也是如此. (2认同)
  • 这不是一个bug; 它在规范中明确说明."如果此值小于或等于0,或者前一个事件的值小于或等于0,则必须抛出NotSupportedError异常." (来自http://webaudio.github.io/web-audio-api/#widl-AudioParam-exponentialRampToValueAtTime-void-float-value-double-endTime) (2认同)