如何逐步改变webaudio中的低通频率?

时间:2018-03-16 11:17:36

标签: javascript web-audio

我正在尝试逐渐改变低通滤波器的频率,但不是逐渐发生,而是立即发生。

此代码应以一个频率开始,以1秒为指数逐渐减小到200,然后在2秒内停止。相反,它会保持不变直到1秒,然后立即跳到较低频率。

var context = new (window.AudioContext || window.webkitAudioContext)();
var oscillator = context.createOscillator();
var now = context.currentTime;


//lowpass node
var lowPass = context.createBiquadFilter();
lowPass.connect(context.destination);
lowPass.frequency.value = 500;
lowPass.Q.value = 0.5;
lowPass.frequency.exponentialRampToValueAtTime(200, now + 1);

oscillator.connect(lowPass);

oscillator.start(now);
oscillator.stop(now + 2);

编辑:我刚刚意识到它确实在chrome中有效。但我主要使用firefox,我可以不使用webaudio吗?

1 个答案:

答案 0 :(得分:0)

The AudioParam interface has 2 modes:
A. Immediate
B. Automated
In mode A you simply set the value property of the parameter.
In mode B, if you want to 'ramp' from 500 to 200 you have
to use an automation event first to set the value to 500, eg:
frequency.setValueAtTime(500, 0)
A startTime parameter of zero applies the value immediately
according to the specs.
What you are trying is to intermingle both modes, but the latter
does not take the value of the first into account.