grimmdude / MidiPlayerJS

♬ MIDI parser & player engine for browser or Node. As a parser converts MIDI events into JSON. Works well with single or multitrack MIDI files.
https://grimmdude.com/MidiPlayerJS/
MIT License
357 stars 52 forks source link

endOfFile event should fire after stop() #86

Open serg472 opened 2 years ago

serg472 commented 2 years ago

I am trying to play midi in a loop. For this I am calling play() inside endOfFile event listener which sounds like a sensible thing to do, but this fails with an error "already playing". I tried calling stop() and then play(), or skipToTick(0) but it has no effect.

The problem is that the library is firing the endOfFile before the internal stop(). I think it should be the other way around, otherwise it makes it impossible to do anything with the Player playback within the event handler, as while I am processing the event in my event handler the library is firing the stop() command next no matter what.

So I think endOfFile trigger should be implemented like this:

if (!dryRun && this.endOfFile()) {
  //console.log('end of file')
  this.stop(); 
  this.triggerPlayerEvent('endOfFile'); //afer stop()
} else {

Then calling play() inside the endOfFile event handler causes the midi to loop infinitely, as expected.

Thanks.