我正在尝试使用 Javascript 开发节拍器。我按照本文中的说明创建了一个调度程序,它经常被调用并调度“滴答”,它存在于一个名为ticksInQueue
. 我希望“滴答声”是节拍器的实际声音,而不是振荡器产生的频率。
振荡器
function scheduler() {
while (nextTickTime < audioContext.currentTime + lookahead) { // while there are notes that will need to play before the next interval
ticksInQueue.push({tick: currentTick, time: nextTickTime}); // push the note on the queue, even if we're not playing.
//create an oscillator
var osc = audioContext.createOscillator();
osc.connect(audioContext.destination);
osc.frequency.value = 440.0;
osc.start(nextTickTime);
osc.stop(nextTickTime + 0.1);
nextTickTime += 60.0 / tempo; // Add beat length to last beat time
currentTick = currentTick + 1; // Advance the beat number, wrap to zero
if (currentTick === 5) {
currentTick = 0;
}
}
}
按照这个关于如何创建节点的视频教程AudioBufferSource
,我编写了这段代码,在加载窗口时执行。
audioContext = new AudioContext();
request = new XMLHttpRequest();
request.open("GET", "tick.mp3", true);
request.responseType = "arraybuffer";
function onDecoded(buffer) {
bufferSource = audioContext.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.connect(audioContext.destination);
}
request.onload = function () {
// audio data is in request.response
audioContext.decodeAudioData(request.response, onDecoded);
};
request.send();
但是,如果我用缓冲区替换振荡器,如下所示,控制台会告诉我“ Uncaught InvalidStateError: Failed to execute 'start' on 'AudioBufferSourceNode': cannot call start more than once.
”
音频缓冲源
function scheduler() {
while (nextTickTime < audioContext.currentTime + lookahead) {
ticksInQueue.push({tick: currentTick, time: nextTickTime});
//Changed code
bufferSource.start(nextTickTime);
bufferSource.stop(nextTickTime + 0.1);
nextTickTime += 60.0 / tempo; // Add beat length to last beat time
currentTick = currentTick + 1; // Advance the beat number, wrap to zero
if (currentTick === 5) {
currentTick = 0;
}
}
}