How to correctly convert MIDI ticks to milliseconds?

后端 未结 2 1459
被撕碎了的回忆
被撕碎了的回忆 2021-02-13 18:36

I\'m trying to convert MIDI ticks/delta time to milliseconds and have found a few helpful resources already:

  1. MIDI Delta Time Ticks to Seconds
  2. How to conve
2条回答
  •  一个人的身影
    2021-02-13 19:09

    First, you have to merge all tracks, to ensure that the tempo change events are processed properly. (This is probably easier if you convert delta times to absolute tick values first; otherwise, you'd have to recompute the delta times whenever an event is inserted between events of another track.)

    Then you have to compute, for each event, the relative time to the last event, like in the following pseudocode. It is important that the computation must use relative times because the tempo could have changed at any time:

    tempo = 500000        # default: 120 BPM
    ticks_per_beat = ...  # from the file header
    
    last_event_ticks = 0
    microseconds = 0
    for each event:
        delta_ticks = event.ticks - last_event_ticks
        last_event_ticks = event.ticks
        delta_microseconds = tempo * delta_ticks / ticks_per_beat
        microseconds += delta_microseconds
        if event is a tempo event:
            tempo = event.new_tempo
        # ... handle event ...
    

提交回复
热议问题