vishnubob / python-midi

Python MIDI library
MIT License
1.49k stars 370 forks source link

Read the timing of each event from a MIDI file #146

Open josephernest opened 6 years ago

josephernest commented 6 years ago

When doing:

import midi
for track in midi.read_midifile("song.mid"):
    for e in track:
        if e.__class__.__name__ not in ["NoteOnEvent", "NoteOffEvent"]:
            continue
        print e.tick, "Note", e.data[0], "on" if e.data[1] == 110 else "off"

we get a list of events like this:

3360 Note 60 on
480 Note 60 off
0 Note 65 on
720 Note 65 off
0 Note 67 on
120 Note 67 off
0 Note 68 on
85 Note 68 off
35 Note 68 on
120 Note 68 off
0 Note 67 on
60 Note 67 off
0 Note 68 on

but obviously the tick is not enough to get the timing of the note: in the song, there isn't many so many notes at time 0.

Question: how to properly extract the timing? (no matter the unit, I could do the conversion later by * 10000 or 100000 if needed)

josephernest commented 6 years ago

I noticed that tick is the time difference between each event, and not absolute timing.

Thus, this works:

import midi
for track in midi.read_midifile("song.mid"):
    t = 0
    for e in track:
        if e.__class__.__name__ not in ["NoteOnEvent", "NoteOffEvent"]:
            continue
        t += e.tick
        print t * alpha, "Note", e.data[0], "on" if e.data[1] == 110 else "off"
VincentCusson commented 6 years ago

I am using this library to do something similar. What is the value of 'alpha' on your last line?

belm0 commented 6 years ago

see make_ticks_abs()

sharmaakshat960 commented 6 years ago

I have this error module 'midi' has no attribute 'read_midifile' . I don't understand why my midi has this problem can you please help me.

sharmaakshat960 commented 6 years ago

Joseph if possible can you please share your midi Library from Anaconda3>Lib>site-packages