Converting MIDI ticks to actual playback seconds
You need two pieces of information:
- PPQ (pulses per quarter note), which is defined in the header of a midi file, once.
- Tempo (in microseconds per quarter note), which is defined by "Set Tempo" meta events and can change during the musical piece.
Ticks can be converted to playback seconds as follows:
ticks_per_quarter = <PPQ from the header>
µs_per_quarter = <Tempo in latest Set Tempo event>
µs_per_tick = µs_per_quarter / ticks_per_quarter
seconds_per_tick = µs_per_tick / 1.000.000
seconds = ticks * seconds_per_tick
Note that PPQ is also called "division" or "ticks per quarter note" in the document linked above.
Note that Tempo is commonly represented in BPM (a frequency) but raw MIDI represents it in µs per quarter (a period).
The formula is 60000 / (BPM * PPQ)
(milliseconds).
Where BPM is the tempo of the track (Beats Per Minute).
(i.e. a 120 BPM track would have a MIDI time of (60000 / (120 * 192))
or 2.604 ms for 1 tick.
If you don't know the BPM then you'll have to determine that first. MIDI times are entirely dependent on the track tempo.