我正试图以速度设置的方式播放Web Audio中的音符,就像Google Chromium Shiny Drum Machine示例一样。音符随后向下播放,每行包含两个八度音符(演示,go here并在选择音符之前单击Sticky,然后单击开始)
我有一个开始按钮:
$("#start").click(function () {
noteTime = 0.0;
startTime = audioCtx.currentTime + 0.005;
schedule();
});
日程安排功能:
function 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);
}
}
}
问题是,一旦你开始,它会同时播放每个音符而不考虑时间或节奏。当声音每秒继续播放时,这一切都会导致浏览器超载,我不知道会出现什么问题。这里有什么不对劲?
提前致谢!
答案 0 :(得分:2)
noteTime = secondsPerBeat
应该是noteTime += secondsPerBeat
;