Python: get correct start and end of note from midi file

575 Views Asked by At

i am playing with python mido library an I am trying to get notes data but I don't understand it.

my code:

    # open midi file
    midi = str(MidiFile('./midi/piano/WA_Mozart_Marche_Turque_Turkish_March_fingered.mid', clip=True))
    
    # copy content of midi file to text file for easier work
    midiTextFile = open("midi.txt", "w")
    midiTextFile.write(midi)

Some example part of output

 Message('note_on', channel=0, note=80, velocity=96, time=1),
    Message('note_on', channel=0, note=80, velocity=0, time=119),
    Message('note_on', channel=0, note=64, velocity=96, time=1),
    Message('note_on', channel=0, note=64, velocity=0, time=119),
    Message('note_on', channel=0, note=76, velocity=96, time=1),
    Message('note_on', channel=0, note=76, velocity=0, time=119),
    MetaMessage('time_signature', numerator=1, denominator=4, clocks_per_click=24, notated_32nd_notes_per_beat=8, time=1),
    Message('note_on', channel=0, note=69, velocity=96, time=0),
    Message('note_on', channel=0, note=81, velocity=96, time=0),
    MetaMessage('set_tempo', tempo=500000, time=479),
    Message('note_on', channel=0, note=69, velocity=0, time=0),
    Message('note_on', channel=0, note=81, velocity=0, time=0),
    MetaMessage('key_signature', key='A', time=1),
    Message('note_on', channel=0, note=69, velocity=96, time=0),
    Message('note_on', channel=0, note=69, velocity=0, time=119),
    Message('note_on', channel=0, note=81, velocity=96, time=1),
    Message('note_on', channel=0, note=81, velocity=0, time=119),
    Message('note_on', channel=0, note=71, velocity=96, time=1),
    Message('note_on', channel=0, note=71, velocity=0, time=119),
    Message('note_on', channel=0, note=83, velocity=96, time=1),
    Message('note_on', channel=0, note=83, velocity=0, time=119),
    MetaMessage('time_signature', numerator=2, denominator=4, clocks_per_click=24, notated_32nd_notes_per_beat=8, time=1),
    Message('note_on', channel=0, note=73, velocity=96, time=0),
    Message('note_on', channel=0, note=73, velocity=0, time=119),
    Message('note_on', channel=0, note=85, velocity=96, time=1),
    Message('note_on', channel=0, note=85, velocity=0, time=119),
    Message('note_on', channel=0, note=69, velocity=96, time=241),
    Message('note_on', channel=0, note=69, velocity=0, time=119),
    Message('note_on', channel=0, note=81, velocity=96, time=1),
    Message('note_on', channel=0, note=81, velocity=0, time=119),
    Message('note_on', channel=0, note=71, velocity=96, time=1),

Now I am not sure about few things. This part means that note 64 starts on 1milisecond and ends on 119 milisecond? And if yes is there any better way how to get start and end of note than this?

Message('note_on', channel=0, note=64, velocity=96, time=1),
Message('note_on', channel=0, note=64, velocity=0, time=119),

And other thing i don't understand is that song in midi editor have 3 minutes while in this midi text file there is no time value bigger then time=500

Thanks for any help.

0

There are 0 best solutions below