我正在尝试以由节奏设置的方式从 Web Audio 播放音符,就像Google Chromium Shiny Drum Machine示例一样。音符随后向下播放,每行包含两个八度音符(要演示,请转到此处并在选择音符之前单击粘滞,然后单击开始)
我有一个开始按钮:
$("#start").click(function () {
noteTime = 0.0;
startTime = audioCtx.currentTime + 0.005;
schedule();
});
调度功能:
函数 schedule() { var currentTime = audioCtx.currentTime;
currentTime -= startTime;
while (noteTime < currentTime + 0.200) {
var contextPlayTime = noteTime + startTime;
for (var i = 0; i < theArray.length; i++) {
if ($("." + rhythmIndex + ".bar" + i).hasClass("selected")) {
$("." + rhythmIndex + ".bar" + i).effect("highlight", {}, 10);
playSound($("." + rhythmIndex + ".bar" + i).attr("freq"), 400, contextPlayTime);
}
}
advanceNote();
}
timeoutId = setTimeout("schedule()", 0);
}
以及选择下一个演奏音符的高级音符功能:
function advanceNote() {
// Advance time by a 16th note...
var secondsPerBeat = 60.0 / $("#tempo").slider("option", "value");
rhythmIndex++;
if (rhythmIndex == loopLength) {
rhythmIndex = 0;
}
noteTime = secondsPerBeat;
}
最后是我的声音播放器:
function playSound(x, y, quick, noteTime) {
if (soundBuffer) {
var sound = audioCtx.createBufferSource();
var gain = audioCtx.createGainNode();
sound.buffer = soundBuffer;
sound.playbackRate.value = x / canvas.width * 2;
sound.connect(gain);
gain.connect(audioCtx.destination);
var volume = 0.5;
gain.gain.value = volume;
if (quick) {
sound.noteGrainOn(0., .2, .4);
} else {
sound.noteOn(noteTime);
}
}
}
问题是,一旦您按下开始,它就会同时播放每个音符,而不考虑时间或速度。当声音每秒都在继续播放时,这一切最终都会使浏览器超载,我不知道可能出了什么问题。这里有什么问题?
提前致谢!