I currently have a midi file parsed into a JSON object. Below is an image of the midi object in my browser console.
My goal is to take this data (marked in red) and convert each item in the list to timestamps in milliseconds.
From my research, I know the meaning of the "deltaTime
" property and I believe the "timeDivision
" value of 192
(underlined in the image above) should be related to the tempo of the midi file.
However, because of my sparse knowledge of the maths behind midi file values, I don't know how to convert all that information to milliseconds. I'm certain it must be a relatively simple math equation. But I'm really lost. I'm trying to build an object that should look like this and match the current millisecond time of the JSON object
How do I convert the "timeDivision
" value combined with the list of individual "deltaTime
" values to milliseconds?
Even a basic answer would suffice. I just need to understand the math behind those values.
Delta value units
Midi files may contain one or more meta events to specify tempo in terms of the number of microseconds per quarter note. The default tempo is 120 BPM.
In MIDI a "quarter note" corresponds to a single beat. This may be because crotchet beats in 4/4 time are quarter the length of a semibreve, also known as a "whole note".
Each quarter note is divided into a number of "pulses per quarter note" (PPQN) where each pulse generates a playback timing tick. (As an aside, high values of the PPQN clock period lead to quantization of playback note lengths, whereas low periods can reproduce more subtle timing variations a musician might have used when recording a piece.)
The value of PPQN is set in the "master track header" (MThd) chunk, in the last two bytes of the chunk, which has a length of 6 bytes. The field name of the PPQN value shown in documentation is Division
, while in the proxy object listing it has been labelled "timeDivision", with a value of 192.
Delta values are the number of PPQN ticks since the previous Midi event or meta-event in the source MIDI file.
Delta time conversion to microseconds
So to convert delta times to microseconds, you could use the formula
microSeconds = deltaTime / Division // delta time in quarter notes
* microSeconds_per_quarter_note; // taken from the binary value supplied in a tempo meta event.
The microSeconds_per_quarter_note
for the default tempo of 120 BPM would be
(60 / BPM) // length of 1 beat in seconds
* 1000000 // in microseconds
Warning
Detecting tempo changes within a midi file requires full and accurate parsing of raw MIDI data: the byte sequences for tempo change may occur within System exclusive messages (perhaps others?) as a valid part of their content. I am not in a position to comment on the accuracy or completeness of JSON file data shown in the post.
Variations: Some midi files may contain SMPTE timing information not covered above.
See Also