简体   繁体   中英

Calling Tone.JS function doesn't work second time

When I press the button the first time, it plays back the list of notes as expected, but not the second time. My console logging suggests that the second time around the function is called in the same way, but perhaps I am missing something? Why will this not play the sequence of notes the second time?

I tried eg Tone.Transport.start(0) among other potential solutions but this didn't solve the problem.

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src= "www/js/Tone.js"></script>
    <script src= "main.js"></script>
</head>
<body>

    <button onclick="playSeq([60, 61, 62], false, this.id, 'tone');">Play</button>

</body>
</html>

Javascript

function playSeq (note_list, hidePlay, id, sound) {


  midi_list = note_list.map(x => Tone.Frequency(x, "midi").toNote());
  last_note = midi_list.length;
  count = 0;
  var pattern = new Tone.Sequence(function(time, note){

  triggerNote(sound, note, 0.25);
  count = count + 1;

  if (count === last_note) {
  console.log("finished!");
  }

  }, midi_list);

  pattern.start(0).loop = false;

  console.log(Tone.Transport);
  // begin at the beginning
  Tone.Transport.start();


}

I had to call.stop() on the Transport and the Tone sequence objects after the finished playing back to solve the problem:

if (count === last_note) {
      pattern.stop();
      Tone.Transport.stop();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM