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
359 stars 52 forks source link

How can I know the duration of each note? #26

Closed weiwei-ww closed 6 years ago

weiwei-ww commented 6 years ago

I am building a midi player using MidiPlayerJS. The player will play a note when a 'Note On' event is fired. Is it possible to get the duration (length) of the notes?

grimmdude commented 6 years ago

Hi @weiwei-ww,

Duration values are not explicitly specified within Note On event objects. To know when a note should be turned off you should use the matching Note Off event, or in the case of some MIDI files, a corresponding Note On event with a velocity of 0. Let me know if you have any questions on that,

-Garrett

weiwei-ww commented 6 years ago

Hi @grimmdude ,

Thanks for your help. Does this player contain a parser so that I can first go through all the events and match every 'Note on' event with a 'Note off' event?

ghost commented 6 years ago

@weiwei-ww yes, use Player.dryRun(), and then all the events are ready in Player.events

grimmdude commented 6 years ago

Thanks @ledlamp.

That's correct, Player.dryRun() will parse and populate events in Player.events. Though, this is called automatically when loading a file so in most cases you can just access Player.events from the start.

-Garrett