Python: How to label .midi file using MIDO library

237 Views Asked by At

I have these .midi files and I want to label them with pitch (in MIDI numbers) per 0.032 second. Example of the label I desired can be found here. In this task, I use Python mido library, the docs can be found here.

From what I know now, I need to iterate all the message available from the file first before processing the .midi. I take 1 example from this file and this is the code + result.

# Code to iterate .midi message
import mido
from mido import MidiFile
from mido import Message
mid_14 = MidiFile('00014 Twinkle, Twinkle, Little Star.mid', clip=True)
for i, track in enumerate(mid_14.tracks):
    print('Track {}: {}'.format(i, track.name))
    print("-"*100)
    for msg in track: #print message in tracks
        print(msg)
    print("="*100)
# Result
Track 0: untitled
----------------------------------------------------------------------------------------------------
<meta message track_name name='untitled' time=0>
<meta message time_signature numerator=4 denominator=4 clocks_per_click=24 notated_32nd_notes_per_beat=8 time=0>
<meta message key_signature key='C' time=0>
<meta message set_tempo tempo=500000 time=0>
<meta message sequencer_specific data=(5, 15, 28, 50, 48, 48, 49, 46, 49, 49, 46, 48, 57) time=0>
<meta message sequencer_specific data=(5, 15, 18, 0, 0, 127, 127, 0) time=0>
<meta message end_of_track time=0>
====================================================================================================
Track 1: MAJOR_TRACK
----------------------------------------------------------------------------------------------------
<meta message track_name name='MAJOR_TRACK' time=0>
program_change channel=0 program=0 time=0
<meta message sequencer_specific data=(5, 15, 9, 64, 72) time=0>
<meta message sequencer_specific data=(5, 15, 6, 71, 101, 110, 101, 114, 97, 108, 32, 77, 73, 68, 73) time=0>
note_on channel=0 note=60 velocity=110 time=0
note_on channel=0 note=60 velocity=0 time=480
note_on channel=0 note=60 velocity=110 time=0
note_on channel=0 note=60 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=69 velocity=110 time=0
note_on channel=0 note=69 velocity=0 time=480
note_on channel=0 note=69 velocity=110 time=0
note_on channel=0 note=69 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=960
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=62 velocity=110 time=0
note_on channel=0 note=62 velocity=0 time=480
note_on channel=0 note=62 velocity=110 time=0
note_on channel=0 note=62 velocity=0 time=480
note_on channel=0 note=60 velocity=110 time=0
note_on channel=0 note=60 velocity=0 time=960
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=62 velocity=110 time=0
note_on channel=0 note=62 velocity=0 time=960
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=62 velocity=110 time=0
note_on channel=0 note=62 velocity=0 time=960
note_on channel=0 note=60 velocity=110 time=0
note_on channel=0 note=60 velocity=0 time=480
note_on channel=0 note=60 velocity=110 time=0
note_on channel=0 note=60 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=480
note_on channel=0 note=69 velocity=110 time=0
note_on channel=0 note=69 velocity=0 time=480
note_on channel=0 note=69 velocity=110 time=0
note_on channel=0 note=69 velocity=0 time=480
note_on channel=0 note=67 velocity=110 time=0
note_on channel=0 note=67 velocity=0 time=960
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=65 velocity=110 time=0
note_on channel=0 note=65 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=64 velocity=110 time=0
note_on channel=0 note=64 velocity=0 time=480
note_on channel=0 note=62 velocity=110 time=0
note_on channel=0 note=62 velocity=0 time=480
note_on channel=0 note=62 velocity=110 time=0
note_on channel=0 note=62 velocity=0 time=480
note_on channel=0 note=60 velocity=110 time=0
note_on channel=0 note=60 velocity=0 time=960
<meta message end_of_track time=0>
====================================================================================================

My question is: How can I divide them in 0.032 second chunk with the information (tempo, velocity) available? Should I gather more information from the file before I start labelling them?

0

There are 0 best solutions below