【发布时间】:2015-10-05 12:44:09
【问题描述】:
每当我尝试一个简单的振荡器时(查看这个例子,不是我的,但它显示了同样的问题http://webaudioapi.com/samples/oscillator/),当它开始和结束时,我都会听到“咔哒”声。
如何绕过这个问题? 谢谢
【问题讨论】:
标签: javascript html web-audio-api
每当我尝试一个简单的振荡器时(查看这个例子,不是我的,但它显示了同样的问题http://webaudioapi.com/samples/oscillator/),当它开始和结束时,我都会听到“咔哒”声。
如何绕过这个问题? 谢谢
【问题讨论】:
标签: javascript html web-audio-api
要停止咔嗒声,您需要平稳地提升振荡器,而不是立即启动它。类似于以下内容:
var osc = context.createOscillator();
var gain = context.createGain();
osc.connect(gain);
gain.gain.setValueAtTime(0, context.currentTime);
gain.gain.linearRampToValueAtTime(1, context.currentTime + <some small time>);
osc.start();
...
// To stop the oscillator, ramp the gain down.
gain.gain.linearRampToValueAtTime(0, endTime - <small time>);
osc.stop(endTime);
【讨论】:
如果您不想增加增益,可以在固定周期数后使振荡停止:
const ac = new AudioContext
const osc = ac.createOscillator()
const freq = 220
const start = 2 + ac.currentTime
const length = Math.random() * 2
const period = length * freq
const roundedPeriod = Math.round(period)
const correctedLength = roundedPeriod / freq
const end = start + correctedLength
osc.connect(ac.destination)
osc.frequency.value = freq
osc.start(start)
osc.stop(end)
这里有更强大的示例:
https://codepen.io/yukulele/pen/QWMZGOZ
【讨论】: