Tone.js icon indicating copy to clipboard operation
Tone.js copied to clipboard

Tone.Sequence sometimes skips the first note

Open gabrielflorit opened this issue 8 years ago • 10 comments

First things first: thanks for this amazing library! 💯 💯 💯 💯

I've noticed Tone.Sequence sometimes skips the first callback invocation. In this sample code, often the first C5 is skipped.

Tone.Transport.start()

const synth = new Tone.Synth().toMaster()

const seq = new Tone.Sequence((time, note) => {
  synth.triggerAttackRelease(note, '8n', time)
}, ['C5', 'C4', 'C3', 'C2'], '4n')

Here's the fiddle: https://jsfiddle.net/gabrielflorit/635a0c4g/16/ . Click start/stop. Note that the bug doesn't happen consistently, so keep playing stop/start. It usually happens within the first 4 tries.

gabrielflorit avatar Jun 29 '18 16:06 gabrielflorit

Thanks @gabrielflorit. The quick fix would be to schedule 'start' slightly in the future: https://jsfiddle.net/5d86p1ne/

I think the longer answer has to do with the fact that since the Transport is already running, when you hit start, it will add it to the timeline on the current tick. And there's a chance that the current tick has already been evaluated (all of the events on that tick have already been scheduled) in which case you wouldn't hear the event on that tick. If this is the issue, i think the solution would be to schedule on the 'next' tick when scheduling something immediately. i'll look into this a little more when i have time.

tambien avatar Jul 21 '18 12:07 tambien

Thanks for the reply @tambien . I'll see about tweaking my timeline setup.

By the way, I've been using Tone.js for my JS gamemaker's music tracker - here's an example: https://script-8.github.io/?id=cd8d6811adb3afb472aaf7505729cf01. You can edit four audio channels by going to EDIT > MUSIC.

gabrielflorit avatar Jul 21 '18 14:07 gabrielflorit

@gabrielflorit this is awesome! Please submit as a demo when you're ready to publish.

tambien avatar Jul 21 '18 14:07 tambien

I've a very similar issue, also with Tone.Sequence. If I change the bpm while transport is running, some sounds are sometimes skipped.

issue

danielfranze avatar Jul 31 '18 23:07 danielfranze

@danielfranze looks like your issue might be related to #385, which should be fixed now

i think the fix for that issue also seems to have fixed the skipping notes. I've made this little fiddle to test your case with the latest build and i haven't heard the issue once.

@gabrielflorit take a listen and close the issue if it you think it's fixed

tambien avatar Sep 10 '18 18:09 tambien

I'm having the same problem with v15.1.22 (and 15.5.26 when tried with tone@next). Also looked at #403, but for me it seams to be on both Chromium based (tested on Brave) and Firefox.

In my case, I am working with a started Transport and always use quantization to add events. So I don't think it's related to the events being registered to late for the current tick. I always use .start('@4n') , but still bugs with '@1m'.

Any idea where to look?

jmvie5 avatar Jul 02 '26 19:07 jmvie5

As a follow-up, when I look directly at the scheduled transport events, the first event is always present, even when it is not triggered correctly.

const sequence = new Tone.Sequence(
    (time, note) => {
      console.log(time, note)
    },
    ['C4', 'C4', C4', 'D4'],
   "4n",
  ).start("@4n")

const firstEventFireTime =
    Tone.TransportTime(
      Object.values(Tone.getTransport()._scheduledEvents)[0].event.time,
      'i',
    ).toSeconds() + Tone.getTransport().context.lookAhead
  console.log(
    'firstEventFireTime',
    firstEventFireTime,
    'current time',
    Tone.TransportTime().toSeconds(),
  )

I get these logs: -> firstEventFireTime 4.198958333333333 current time 2.3680000000000003 -> 4.699041666666673

So, even though the event is registered at time 2.368 and the first event is at 4.2, the first log from inside the Tone.Sequence is the second, at 4.7.

The error is not consistant, I would say everything works normaly most of the time. Example logs when everything is ok: -> firstEventFireTime 2.1989583333333336 current time 1.8133333333333332 -> 2.201000000000004

jmvie5 avatar Jul 03 '26 17:07 jmvie5

Thanks for following up @jmvie5 , do you have a fuller way to repro beyond this snippet?

tambien avatar Jul 05 '26 13:07 tambien

Here is the repro

It happens like one out of ten times, you need the refresh the page each time because once the Sequence loads it always play the same way, either good or with the first note skip. Quite frustrating to test sorry.

It seams to happen more when I change the bpm value before starting the transport. But I'm really not sure about this.

jmvie5 avatar Jul 06 '26 14:07 jmvie5

Thanks for the link, i was able to reproduce the issue, but i noticed you are starting the transport with an offset relative to Tone.now()

transport.start('+0.1', Tone.now());

Which means that you are actually starting the Transport probably past the first beat since the second argument is the "offset" time you want to start the transport and that value is most likely > 0 when onMounted is called. Docs

I changed it to transport.start('+0.1'); and could no longer reproduce the issue.

tambien avatar Jul 08 '26 19:07 tambien