How to correctly convert MIDI ticks to milliseconds?

后端 未结 2 1449
被撕碎了的回忆
被撕碎了的回忆 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 ...
    
    0 讨论(0)
  • 2021-02-13 19:22

    You might want to increase the frame rate. On my system, increasing clock.tick(30) to clock.tick(300) gives good results. You can measure this by printing how much your timing is off:

    print self.t0[self.event_id].text, millis - self.now - deltaMillis
    

    With 30 ticks the cues are lagging 20 to 30 millisecond behind. With 300 ticks they are at most 2 milliseconds behind. You might want to increase this even further.

    Just to be safe you should run python with the -u switch to prevent stdout from buffering (this might be unnecessary, since lines end with newline).

    I have a hard time determining the timing, but judging from the "Ah ha ha ha"'s it seems to be correct with these changes.

    0 讨论(0)
提交回复
热议问题