abique / midi-parser

Very simple midi parser
MIT License
40 stars 9 forks source link

How to know time of every notes? #1

Open cgoxopx opened 6 years ago

ell1e commented 3 years ago

I have looked into this now, it's kind of a two step thing and I think this is how to do it:

Musical offset of a note: Check out how many midi clock ticks a quarter note has via parser->header.time_division, and each midi event has the vtime which specifies the offset to the last in midi clock ticks. With this, you can calculate each note's exact offset easily in relation to musical quarter notes as e.g. a fractional double number.

Find out how long a quarter note length is in seconds: You have to track tempo change events yourself and compute how long each quarter note is yourself from that at any point in the song, and multiply that with your musical offset. (I know this sounds strongly simplified, and can be a bit involved with multiple tempo changes, but I think it's the simplest approach.)

Maybe there is a way to add all of this into the parser itself some day, due to how simple it is it basically just returns all the above pieces as-is from the midi file and leaves the rest to you. Midi is just very musically oriented, so it usually doesn't have direct seconds timing offsets but its all via the abstract midi clock/quarter notes.