【发布时间】:2019-07-15 07:31:41
【问题描述】:
只是在玩Tone.js 并不太了解Tone.Transport.scheduleRepeat 的细微之处
当多次使用某个功能时,声音开始失真并且时间发生变化。
我正在关注这个tutorial,并尝试在Codepen 中复制。
尽管索引计数器被重置为零,但我的“playScore”函数每次运行时都会发生变化。
<!-- HTML -- >
<button onclick="playScore()">
TEST SCORE
</button>
//js
function playScore() {
console.clear();
console.log("TEST SCORE");
const synthScore = new Tone.Synth
synthScore.oscillator.type = 'sine';
synthScore.toMaster();
//synth.triggerAttackRelease ('E4', '8n' );
const notes = [
'C4', 'E4', 'G4',
'C5', 'E5', 'G5'
];
let index = 0;
Tone.Transport.scheduleRepeat(time => {
repeat(time);
}, "8n");
function repeat(time) {
console.log("index: " + index + " notes.length: " + notes.length);
let note = notes[index % notes.length];
synthScore.triggerAttackRelease(note, '8n', time);
console.log("note:" + note + " time: " + time);
index++;
}
Tone.Transport.start();
setTimeout(() => {
Tone.Transport.stop();
}, 5000)
// Tone.Transport.bpm.value = 120
我希望以相同的方式以相同的顺序演奏相同的音符。
相反,我看到它在每次迭代中都会发生变化。
我发现这是因为显然,我有 2 个索引变量和逻辑,函数内有局部实例,函数外有全局实例。
【问题讨论】: