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

[Feature] Callback when finishing a sequence / pattern

Open davidfloegel opened this issue 5 years ago • 1 comments

Hi there,

this is a feature request to get a callback when the pattern or sequence that's currently playing has finished.

I'm in the situation where I have a play button to play a sequence and when it plays I'm changing the label to "playing". Once the sequence stops I need to reset that label. There doesn't seem to be any way to have a callback that's executed when the sequence stops so I'm having to calculate the duration of the sequence and then stop manually which is quite flakey.

Any chance to get this?

Thanks David

davidfloegel avatar Dec 06 '20 12:12 davidfloegel

It seems like this should be possible to do outside of Tone.js without setting a timeout or computing the duration of the sequence. Sequence/Pattern are already giving you a callback. So if you'd like to know when those sequences end, i would add it in that callback. Then you'll have both a callback and the precise time when the sequence ends.

Here's a way to do it by creating a counter and waiting until the count hits the length of the sequence.

let index = 0
const noteSeq = ['C4', 'G5', 'A2', 'B3']
const sequence = new Tone.Sequence((note, time) => {
  index++
  if (index === noteSeq.length){
    console.log('sequence ended')
  }
}, noteSeq)

Another way to do this would be add some kind of special indicator at the end of your sequence so you could wait for that event and then you would know when the sequence is over.

const sequence = new Tone.Sequence((note, time) => {
  if (note === 'END'){
    console.log('sequence ended')
  }
}, ['C4', 'G5', 'A2', 'B3', 'END'])

tambien avatar Dec 09 '20 14:12 tambien