Tone.js 错误:“开始时间必须严格大于上一个开始时间”

cha*_*yin 8 javascript web-audio-api tone.js

我很难理解为什么我会抛出这个错误:

Debug.ts:8 未捕获错误:启动时间必须严格大于先前的启动时间

奇怪的是,它只在我刷新页面的 4/5 次时抛出此错误。大约有 1/5 的机会它会正常工作。这是我的代码:

let synth = new Tone.MetalSynth({
  portamento: 0,
  volume: -15,
  envelope: {
    attack: 0.001,
    decay: 1.4,
    release: 1,
  },
  harmonicity: 18.1,
  modulationIndex: 12,
  resonance: 1000,
  octaves: 1.5,
});

synth.chain(Tone.Destination);

let notes = ['A2', 'B2', 'C3', 'D3', 'E3', 'F3', 'G#3'];
let html = '';
for (let i = 0; i < notes.length; i++) {
  html += `<div class="whitenote" onmouseover="noteDown(this)" data-note="${notes[i]}"></div>`;
}

document.querySelector('.container-4').innerHTML = html;

function noteDown(el) {
  let note = el.dataset.note;
  synth.triggerAttackRelease(note, '16n');
}
Run Code Online (Sandbox Code Playgroud)

s1g*_*r1d 4

我也有同样的问题。你必须添加时间。

这对我有用:

const synth = new Tone.Synth().toDestination();
        
melody.forEach(tune => {
      const now = Tone.now()
      synth.triggerAttackRelease(tune.note, tune.duration, now + tune.timing)
})
Run Code Online (Sandbox Code Playgroud)

我的数据(旋律)看起来像这样:

[{ note: "E5", duration: "8n", timing: 0 },
{ note: "D#5", duration: "8n", timing: 0.25 },
{ note: "E5", duration: "8n", timing: 0.5 },
{ note: "D#5", duration: "8n", timing: 0.75 },
{ note: "E5", duration: "8n", timing: 1 },
{ note: "B4", duration: "8n", timing: 1.25 }]
Run Code Online (Sandbox Code Playgroud)