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

midiEvent fires after event should already be playing when multiple notes are played on the same tick #103

Open bethanrv opened 8 months ago

bethanrv commented 8 months ago

Greetings and thanks for sharing this library!

I've noticed an issue with .on midiEvent that is causing some delay in playback (10-15ms). Looks like every time multiple notes want to play on the same tick, there is a noticeable delay before the event is fired.

I've done a simple experiment to showcase this delay... here is the js:

  Player.on('midiEvent', function(event) {
    if(event.name == "Note on"){
      if(lastTickPlayed == event.tick) {
        return
      }

      // experiment, use tick to determine what time the note should have come in vs what time it really does
      console.log('==========================')
      console.log('scheduled time: ' + ticksToSeconds(event.tick))
      console.log('actual time: ' + audioConext.currentTime)
    }
  });

Upon reviewing the output, it looks like midiEvents usually fire about 40-50ms before their scheduled time. Unfortunately, I'm also observing that when multiple notes are scheduled for the same tick, the first midiEvent for that tick fires about 10-20ms after the scheduled time.

Notes on input midi:

Output:
========================== *Note: Curiously this issue is note apparent at time 0
scheduled time: 0
actual time: 0 
========================== *Note: A single note is scheduled here, and we see that the midi event is fired 50ms early
scheduled time: 0.25
actual time: 0.2
========================== *Note: Another single note is scheduled for this tick, fires early as expected
scheduled time: 1
actual time: 0.9306666666666666
========================== *Note: Again, a single note is fired early... will stop marking these
scheduled time: 1.5
actual time: 1.44
==========================
scheduled time: 1.96875
actual time: 1.912
==========================
scheduled time: 2.01171875
actual time: 1.96
==========================
scheduled time: 2.5
actual time: 2.44
==========================
scheduled time: 2.75
actual time: 2.6906666666666665
==========================
scheduled time: 3
actual time: 2.952
==========================
scheduled time: 3.25
actual time: 3.2
==========================
scheduled time: 3.5
actual time: 3.4506666666666668
==========================
scheduled time: 3.75
actual time: 3.7013333333333334
==========================
scheduled time: 3.796875
actual time: 3.7413333333333334
========================== *Note: Here is the first instance of the issue I've described. The actual time is 10ms ahead now
scheduled time: 4
actual time: 4.010666666666666
========================== *Note: Back to expected behavior when only a single note should fire on this tick
scheduled time: 4.25
actual time: 4.2
==========================
scheduled time: 5
actual time: 4.930666666666666
==========================
scheduled time: 5.5
actual time: 5.450666666666667
==========================
scheduled time: 6
actual time: 5.952
==========================
scheduled time: 6.125
actual time: 6.08
==========================
scheduled time: 6.25
actual time: 6.2
==========================
scheduled time: 6.375
actual time: 6.330666666666667
==========================
scheduled time: 6.5
actual time: 6.450666666666667
==========================
scheduled time: 6.75
actual time: 6.712
==========================
scheduled time: 7
actual time: 6.952
==========================
scheduled time: 7.25
actual time: 7.2
==========================
scheduled time: 7.47265625
actual time: 7.410666666666667
==========================
scheduled time: 7.515625
actual time: 7.472
==========================
scheduled time: 7.75
actual time: 7.701333333333333
==========================
scheduled time: 8
actual time: 8.021333333333333
========================== *Note: Again, this is where multiple notes should fire on the same tick together - 20ms delay
scheduled time: 8.25
actual time: 8.210666666666667
========================== *Note: And back to expected behavior again
scheduled time: 9
actual time: 8.930666666666667
==========================
scheduled time: 9.5
actual time: 9.44
==========================
scheduled time: 9.984375
actual time: 9.912
==========================
scheduled time: 10.02734375
actual time: 9.981333333333334
==========================
scheduled time: 10.5
actual time: 10.450666666666667
==========================
scheduled time: 10.75
actual time: 10.701333333333332
==========================
scheduled time: 11
actual time: 10.952
==========================
scheduled time: 11.25
actual time: 11.2
==========================
scheduled time: 11.5
actual time: 11.450666666666667
==========================
scheduled time: 11.71484375
actual time: 11.650666666666666
==========================
scheduled time: 11.7578125
actual time: 11.712
========================== *Note: Again, multiple notes should play here
scheduled time: 12
actual time: 12.010666666666667
==========================
scheduled time: 12.25
actual time: 12.192
==========================
scheduled time: 13
actual time: 12.930666666666667
==========================
scheduled time: 13.5
actual time: 13.432
==========================
scheduled time: 14
actual time: 13.952
==========================
scheduled time: 14.125
actual time: 14.072
==========================
scheduled time: 14.25
actual time: 14.2
==========================
scheduled time: 14.375
actual time: 14.32
==========================
scheduled time: 14.5
actual time: 14.450666666666667
==========================
scheduled time: 14.75
actual time: 14.701333333333332
==========================
scheduled time: 15
actual time: 14.952
==========================
scheduled time: 15.25
actual time: 15.2
==========================
scheduled time: 15.49609375
actual time: 15.421333333333333
==========================
scheduled time: 15.53515625
actual time: 15.48
==========================
scheduled time: 15.75
actual time: 15.701333333333332
========================== *Note: Again, multiple notes should play here
scheduled time: 16
actual time: 16.021333333333335
==========================
scheduled time: 16.25
actual time: 16.2
==========================
scheduled time: 17
actual time: 16.941333333333333
==========================
scheduled time: 17.5
actual time: 17.461333333333332
==========================
scheduled time: 17.9765625
actual time: 17.912
==========================
scheduled time: 18.015625
actual time: 17.970666666666666
==========================
scheduled time: 18.5
actual time: 18.450666666666667
==========================
scheduled time: 18.75
actual time: 18.701333333333334
==========================
scheduled time: 19
actual time: 18.952
==========================
scheduled time: 19.25
actual time: 19.2
==========================
scheduled time: 19.5
actual time: 19.450666666666667
==========================
scheduled time: 19.72265625
actual time: 19.650666666666666
==========================
scheduled time: 19.76953125
actual time: 19.712
========================== *Note: Again, multiple notes should play here
scheduled time: 20
actual time: 20.010666666666665
==========================
scheduled time: 20.25
actual time: 20.192
==========================
scheduled time: 21
actual time: 20.930666666666667
==========================
scheduled time: 21.5
actual time: 21.450666666666667
==========================
scheduled time: 22
actual time: 21.952
==========================
scheduled time: 22.125
actual time: 22.072
==========================
scheduled time: 22.25
actual time: 22.2
==========================
scheduled time: 22.375
actual time: 22.32
==========================
scheduled time: 22.5
actual time: 22.450666666666667
==========================
scheduled time: 22.75
actual time: 22.701333333333334
==========================
scheduled time: 23
actual time: 22.952
==========================
scheduled time: 23.25
actual time: 23.2
==========================
scheduled time: 23.47265625
actual time: 23.410666666666668
==========================
scheduled time: 23.515625
actual time: 23.472
==========================
scheduled time: 23.75
actual time: 23.712
========================== *Note: Again, multiple notes should play here
scheduled time: 24
actual time: 24.021333333333335
==========================
scheduled time: 24.25
actual time: 24.2
==========================
scheduled time: 25
actual time: 24.941333333333333
==========================
scheduled time: 25.5
actual time: 25.450666666666667
==========================
scheduled time: 25.98828125
actual time: 25.92
==========================
scheduled time: 26.03125
actual time: 25.992
==========================
scheduled time: 26.5
actual time: 26.44
==========================
scheduled time: 26.75
actual time: 26.690666666666665
==========================
scheduled time: 27
actual time: 26.941333333333333
==========================
scheduled time: 27.5
actual time: 27.432
==========================
scheduled time: 27.7109375
actual time: 27.650666666666666
==========================
scheduled time: 27.75390625
actual time: 27.712
========================== *Note: Again, multiple notes should play here
scheduled time: 28
actual time: 28.010666666666665
==========================
scheduled time: 28.25
actual time: 28.2
==========================
scheduled time: 29
actual time: 28.930666666666667
==========================
scheduled time: 29.5
actual time: 29.450666666666667
==========================
scheduled time: 30
actual time: 29.952
==========================
scheduled time: 30.125
actual time: 30.08
==========================
scheduled time: 30.25
actual time: 30.2
==========================
scheduled time: 30.375
actual time: 30.330666666666666
==========================
scheduled time: 30.5
actual time: 30.450666666666667
==========================
scheduled time: 30.75
actual time: 30.701333333333334
==========================
scheduled time: 31
actual time: 30.952
==========================
scheduled time: 31.25
actual time: 31.2
==========================
scheduled time: 31.49609375
actual time: 31.44
==========================
scheduled time: 31.53515625
actual time: 31.48
==========================
scheduled time: 31.75
actual time: 31.701333333333334

Midi: Couldn't attach .midi file... here is the base64 encoding... TVRoZAAAAAYAAAABAIBNVHJrAAANhACQRnkAkCRRAJAwUQCQN1EAkDNRAJBGeQCQJFEAkDBRAJA3UQCQM1EAkEZ5AJAkUQCQMFEAkDdRAJAzUUCARnkAgEZ5AIBGeQCQRnIAkEZyAJBGckCARnIAgEZyAIBGcoEAkER3AJBEdwCQRHeBAIBEdwCARHcAgER3AJBDcgCQQ3IAkENyeJA/VACQP1QAkD9UCIBDcgCAQ3IAgENyA4A/VACAP1QAgD9UAJBBdwCQQXcAkEF3fYBBdwCAQXcAgEF3AJA+cQCQPnEAkD5xQIA+cQCAPnEAgD5xAJA+ZgCQPmYAkD5mQIA+ZgCAPmYAgD5mAJA+ewCQPnsAkD57QIA+ewCAPnsAgD57AJA+dQCQPnUAkD51QIA+dQC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