gpt4 book ai didi

javascript - 网络音频API : how to silence all oscillators and pause melody

转载 作者:行者123 更新时间:2023-11-28 15:43:47 25 4
gpt4 key购买 nike

我为我弹奏的每个音符制作一个新的振荡器。

function playSound(freq, duration) { 
var attack = 5,
decay = duration,
gain = context.createGain(),
osc = context.createOscillator();

gain.connect(context.destination);
gain.gain.setValueAtTime(0, context.currentTime);
gain.gain.linearRampToValueAtTime(0.1, context.currentTime + attack / 1000);
gain.gain.linearRampToValueAtTime(0, context.currentTime + decay / 1000);

osc.frequency.value = freq;
osc.type = "sine";
osc.connect(gain);
osc.start(0);

setTimeout(function() {
osc.stop(0);
osc.disconnect(gain);
gain.disconnect(context.destination);
}, decay)
}

旋律在 for 循环中播放,其中调用了 playSound。当我单击暂停按钮时,我想让旋律静音并暂停 for 循环,这样如果我再次单击播放按钮,旋律就会恢复。如何访问所有当前振荡器以断开它们?

最佳答案

在这段代码中你不能。

1) 根据设计,Web Audio API 中没有对节点图进行自省(introspection) - 它可以优化垃圾收集,并针对大量节点进行优化。两种可能的解决方案 - 要么维护播放振荡器的列表,要么将它们全部连接到单个增益节点(即将它们的包络增益节点连接到“混音器”增益节点),然后断开(并释放对该增益的引用)节点。

2)不确定“暂停 for 循环”是什么意思 - 我认为您在播放音符方法周围有一个 for 循环?

关于javascript - 网络音频API : how to silence all oscillators and pause melody,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23016365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com