1

I'm using the Mido library to read a simple MIDI file in python.

My MIDI file is the following: https://www.dropbox.com/s/t80kg9l2k525g0h/file.mid?dl=0

It's just a dummy MIDI file I created with basic notes.

I opened it with the Mido library and printed its content:

from mido import MidiFile
mid = MidiFile('file.mid')
for i, track in enumerate(mid.tracks):
print('Track {}: {}'.format(i, track.name))
for msg in track:
    print(msg)

This is what i get:

Track 0: 
<meta message track_name name='\x00' time=0>

<meta message time_signature numerator=4 denominator=4 clocks_per_click=36 notated_32nd_notes_per_beat=8 time=0>
<meta message time_signature numerator=4 denominator=4 clocks_per_click=36 notated_32nd_notes_per_beat=8 time=0>
note_on channel=0 note=60 velocity=100 time=0
note_on channel=0 note=64 velocity=100 time=0
note_off channel=0 note=60 velocity=64 time=384
note_on channel=0 note=62 velocity=100 time=0
note_on channel=0 note=67 velocity=100 time=0
note_off channel=0 note=62 velocity=64 time=384
note_off channel=0 note=64 velocity=64 time=0
note_on channel=0 note=64 velocity=100 time=0
note_off channel=0 note=67 velocity=64 time=0
note_off channel=0 note=64 velocity=64 time=384
note_on channel=0 note=67 velocity=100 time=0
note_on channel=0 note=66 velocity=100 time=384
note_off channel=0 note=67 velocity=64 time=0
note_off channel=0 note=66 velocity=64 time=384
note_on channel=0 note=67 velocity=100 time=0
note_off channel=0 note=67 velocity=64 time=384
note_on channel=0 note=69 velocity=100 time=0
note_off channel=0 note=69 velocity=64 time=384
note_on channel=0 note=71 velocity=100 time=0
note_on channel=0 note=60 velocity=100 time=384
note_off channel=0 note=71 velocity=64 time=0
note_off channel=0 note=60 velocity=64 time=384
note_on channel=0 note=62 velocity=100 time=0
note_off channel=0 note=62 velocity=64 time=384
note_on channel=0 note=64 velocity=100 time=0
note_off channel=0 note=64 velocity=64 time=375
note_on channel=0 note=67 velocity=100 time=9
note_on channel=0 note=66 velocity=100 time=384
note_off channel=0 note=67 velocity=64 time=0
note_off channel=0 note=66 velocity=64 time=384
note_on channel=0 note=67 velocity=100 time=0
note_off channel=0 note=67 velocity=64 time=384
note_on channel=0 note=69 velocity=100 time=0
note_off channel=0 note=69 velocity=64 time=384
note_on channel=0 note=71 velocity=100 time=0
note_off channel=0 note=71 velocity=64 time=384
<meta message end_of_track time=0>

Doing some experiment i kind of understood that the time is expressed in ticks relative to the previous event (note_on - note_off).

How can i re-order the notes using an absolute time reference (in ticks)?

I would like to have an absolute timeline of my notes, but i can't figure out how to "extract" it from the data i have.

Are there any other libraries that already implement this function? I saw this library: Python-midi, but unfortunately it's only available for Python 2.

Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49

2 Answers2

4

The delta time is not relative to the corresponding note-on event, but relative to the previous event in the same track.

Just add up all the delta times, in order.

CL.
  • 173,858
  • 17
  • 217
  • 259
  • Can i ask you what do you mean with "track"? I read it also in the documentation code of the library, but i can't figure out what it represents – Mattia Surricchio Apr 13 '20 at 14:20
  • 1
    @MattiaSurricchio You need to read Standard MIDI File specification first before using the library. MIDI file contains track chunks which contain events. Absolute time of an event is the sum of delta-times of all previous events. – Maxim Apr 13 '20 at 14:46
  • @Maxim Do you have a useful link to those resources? I tried a couple of sources but i found them pretty ambiguous and hard to understand. I'd really appreciate any help – Mattia Surricchio Apr 13 '20 at 15:41
  • [the official specification](https://www.midi.org/specifications-old/item/the-midi-1-0-specification) – CL. Apr 13 '20 at 15:42
2

Unfortunately, absolute midi tick values haven't been implemented yet as feature... https://github.com/mido/mido/issues/185

ATH
  • 666
  • 6
  • 13