我将一些 JS 与 ToneJS 库拼凑在一起,以打开/关闭一些声音。
function togglePlay() {
const status = Tone.Transport.state; // Is either 'started', 'stopped' or 'paused'
const element = document.getElementById("play-stop-toggle");
if (status == "started") {
element.innerText = "PLAY";
Tone.Transport.stop()
} else {
element.innerText = "STOP";
Tone.Transport.start()
}
document.querySelector('#status').textContent = status;
}
var filter = new Tone.Filter({
type: 'lowpass',
Q: 12
}).toMaster()
var fmOsc = new Tone.AMOscillator("Ab3", "sine", "square").toMaster()
var fmOsc2 = new Tone.AMOscillator("Cb3", "sine", "square").toMaster()
var fmOsc3 = new Tone.AMOscillator("Eb3", "sine", "square").toMaster()
var synth = new Tone.MembraneSynth().toMaster()
//create a loop
var loop = new Tone.Loop(function(time){
// synth.triggerAttackRelease("A1", "8n", time)
// fmOsc.start()
fmOsc2.start()
fmOsc3.start();
}, "4n")
//play the loop between 0-2m on the transport
loop.start(0)
// loop.start(0).stop('2m')
在里面loop
我有一个鼓点,我目前已经注释掉了。当它被注释时,该togglePlay()
函数将按预期启动和停止它。
但是,fmOsc2
andfmOsc3
函数将在我切换开始时启动,但在我切换停止时不会终止。
作为参考,下面是 HTML 端的样子:<div class="trackPlay" id="play-stop-toggle" onclick="togglePlay()">PLAY</div>
如何获得fmOsc2
和fmOsc3
功能以切换按钮状态?