【发布时间】:2017-10-25 13:00:31
【问题描述】:
下面的代码 (also live here) 似乎表明 setTargetAtTime 的性能不一致......它“应该”在 2 秒时膨胀到最大值,然后在 7 秒时淡入静默,然后在 12 秒时终止振荡器(安全安静足以避免"the ugly click"。)
相反,它会在 2 秒时膨胀到最大值,然后在 12 秒时开始一个尚未完成的淡入淡出,此时我们确实听到了难看的咔嗒声。
谁能解释为什么会这样?请注意,短值(注释为//0.3)足够快地丢弃它以避免点击。我已经在各种情况下尝试过这个,似乎(当无论如何淡入为 0 时)第三个参数随着值的上升而按比例延伸超出适当的停止时间。
<button id = "button" >
Start then stop oscillators
</button>
<script>
var audioContext = new AudioContext();
var fadeIn = 2;
var fadeOut = 5; //0.3
var gainNode = audioContext.createGain();
var osc = audioContext.createOscillator();
osc.type = "sine";
osc.frequency.value = 300;
osc.connect(gainNode);
gainNode.gain.value = 0;
gainNode.connect(audioContext.destination);
function startAndStop() {
osc.start(audioContext.currentTime);
gainNode.gain.setTargetAtTime(1, audioContext.currentTime, fadeIn);
gainNode.gain.setTargetAtTime(0, audioContext.currentTime + fadeIn, fadeOut);
osc.stop(audioContext.currentTime + fadeIn + fadeOut + 5);
};
var button = document.getElementById("button");
button.addEventListener("click", startAndStop);
</script>
【问题讨论】: