Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
How do I play multiple notes one after another in Tone.js

I came up with this solution:

  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)}
}

But it is too much code, and it can only play 7 notes or so;

Is there a simple shortcut for this in just a couple lines?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you can declare the notes and their lengths in an array (you seem to be doing that). Then you can just loop over the notes and play them, accumulating the time delay to calculate the correct delay for each note from the start point.

e.g.

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.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda