【问题标题】:How do I play multiple notes one after another in Tone.js如何在 Tone.js 中一个接一个地演奏多个音符
【发布时间】:2021-12-03 01:37:06
【问题描述】:

我想出了这个解决方案:

  for(var i=0; i < max; i++){
   
   var note=arrays[i].pitch;
   var time=arrays[i].timing;            
   if(i===0){synth.triggerAttackRelease(note,time,0)}
   if(i===1){synth.triggerAttackRelease(note,time,arrays[0].timing)}
   if(i===2){synth.triggerAttackRelease(note,time,arrays[1].timing+arrays[0].timing)}
   if(i===3){synth.triggerAttackRelease(note,time,arrays[2].timing+arrays[1].timing+arrays[0].timing)}
   if(i===4){synth.triggerAttackRelease(note,time,arrays[3].timing+arrays[2].timing+arrays[1].timing+arrays[0].timing)}
   if(i===5){synth.triggerAttackRelease(note,time,arrays[4].timing+arrays[3].timing+arrays[2].timing+arrays[1].timing+arrays[0].timing)}
   if(i===6){synth.triggerAttackRelease(note,time,arrays[5].timing+arrays[4].timing+arrays[3].timing+arrays[2].timing+arrays[1].timing+arrays[0].timing)}
   if(i===7){synth.triggerAttackRelease(note,time,arrays[6].timing+arrays[5].timing+arrays[4].timing+arrays[3].timing+arrays[2].timing+arrays[1].timing+arrays[0].timing)}
}

但是代码太多,只能弹7个音符左右;

是否有一个简单的快捷方式,只需几行?

【问题讨论】:

    标签: javascript tone.js


    【解决方案1】:

    如果您可以在数组中声明音符及其长度(您似乎正在这样做)。然后,您可以循环播放音符并播放它们,累积时间延迟以计算每个音符从起点开始的正确延迟。

    例如

    const synth = new Tone.Synth().toDestination();
    
    const notes = [
        { pitch: "C4", timing: 0 },
        { pitch: "D4", timing: 1 },
        { pitch: "E4", timing: 1 },
        { pitch: "F4", timing: 1 },
        { pitch: "G4", timing: 1 }
    ];
    
    function play() {
        let delay = Tone.now();
        for(let i = 0; i < notes.length; i++) {
            delay += notes[i].timing;
            synth.triggerAttackRelease(notes[i].pitch, '8n', delay);  
        }
    }
    
    play() // call this when someone interacts with your program.
    

    【讨论】:

      猜你喜欢
      • 2017-03-13
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多