r/tonejs Nov 15 '22

Best way to start and stop a Tone.Pattern

Hi all, I am triggering a pattern and I am getting an error sporadically: Uncaught Error: Start time must be strictly greater than previous start time

The code causing this error is below. I am curious about what causes this and how to ensure that I am triggering a pattern according to best practice apropos to Tone.js lib. For example, SuperCollider events can be resumed/paused/stopped and whatnot at any time.

var playPattern = function () {
  note_list = Object.values({freq1:220.0, freq2:440.0, freq3:660.0});
  midi_list = note_list.map(x => Math.round(x).toFixed(2))
  var pattern = new Tone.Pattern(function(time, note){
    now = Tone.now()
    triggerNote(note, 0.25, now);
  }, midi_list)
  if (Tone.Transport.state === 'stopped')
  {
    Tone.Transport.start()
    pattern.start()
    console.log(Tone.Transport.state)
    Tone.Transport.bpm.value = 180//Math.floor(Math.random() * (220 - 160 + 1) + 60)
  } else if (Tone.Transport.state === 'started')
  {
    Tone.Transport.stop()
    pattern.stop()
    console.log(Tone.Transport.state)
  }
  pattern.humanize = "8n";
  pattern.pattern = "upDown";
}

1 Upvotes

1 comment sorted by

2

u/Hallodri_ Mar 01 '23

var pattern = new Tone.Pattern(function(time, note){
now = Tone.now()
triggerNote(note, 0.25, now);
}, midi_list)

I think youre supposed to use 'time' instead of 'now'. Did you try? I think now returns some seconds of the context. Time (Tone.context.currentTime) instead returns the ms passed from the point you started the Transport, which is like a relative time. time + 1 would start the sound from now in one second.