为什么不能使用振荡器的递归 onended 属性 来创建音乐音序器?
Why can't a recursive onended property of an oscillator be used to create a music sequencer?
我很好奇为什么振荡器 onended 方法不能用于创建音乐音序器以及为什么使用“two clocks”方法更好。
这是我的想法的粗略(有点工作)代码草图。
var x = oscillator.onended = function() {
oscillator = audioContext.createOscillator();
oscillator.frequency.value = 0;
oscillator.connect(audioContext.destination);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.000001);
oscillator.onended = function() {
if (Math.abs(item - audioContext.currentTime) >= 1) {
if (tick === 8) {
tick = 1;
} else {
item = audioContext.currentTime;
tick += 1;
sounds.kick.play();
}
}
x()
}
}
这可能无法如您所愿地工作,因为您不能确切地依赖 onended 何时被触发。在一个振荡器停止和下一个振荡器开始之间可能会有一个随机间隙。
我很好奇为什么振荡器 onended 方法不能用于创建音乐音序器以及为什么使用“two clocks”方法更好。
这是我的想法的粗略(有点工作)代码草图。
var x = oscillator.onended = function() {
oscillator = audioContext.createOscillator();
oscillator.frequency.value = 0;
oscillator.connect(audioContext.destination);
oscillator.start(audioContext.currentTime);
oscillator.stop(audioContext.currentTime + 0.000001);
oscillator.onended = function() {
if (Math.abs(item - audioContext.currentTime) >= 1) {
if (tick === 8) {
tick = 1;
} else {
item = audioContext.currentTime;
tick += 1;
sounds.kick.play();
}
}
x()
}
}
这可能无法如您所愿地工作,因为您不能确切地依赖 onended 何时被触发。在一个振荡器停止和下一个振荡器开始之间可能会有一个随机间隙。