声音淡出,但不淡入——为什么?
Sound fades out, but does not fade in -- why?
我想我了解网络音频的主要概念 API,以及声音的一般工作原理。即使我设法发出声音 "fade out",我也想不通,为什么它不是 "fading in"我写的以下片段代表问题:
(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);
将解决问题。
基本原理在exponentialRampToValueAtTime()
函数的actual specification中:
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
你需要
gain.gain.setValueAtTime(ZERO, 0);
因为刚刚设置
gain.gain.value = ZERO;
没有在 AudioParam 调度程序中设置调度点 - 所以它是从最后知道的调度点开始调度(这是时间 = 0 时的默认值 1)。混合设置 .value 和调度往往效果不佳;很长一段时间以来,我已经写了一篇 75% 的文章,只是一直没有发布。
我想我了解网络音频的主要概念 API,以及声音的一般工作原理。即使我设法发出声音 "fade out",我也想不通,为什么它不是 "fading in"我写的以下片段代表问题:
(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);
将解决问题。
基本原理在exponentialRampToValueAtTime()
函数的actual specification中:
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
你需要
gain.gain.setValueAtTime(ZERO, 0);
因为刚刚设置
gain.gain.value = ZERO;
没有在 AudioParam 调度程序中设置调度点 - 所以它是从最后知道的调度点开始调度(这是时间 = 0 时的默认值 1)。混合设置 .value 和调度往往效果不佳;很长一段时间以来,我已经写了一篇 75% 的文章,只是一直没有发布。