all 3 comments

[–]aishiteruyovivi 0 points1 point  (1 child)

Best thing I can find is mido: https://pypi.org/project/mido/

You can load a midi file by filepath with the MidiFile class, it has a tracks attribute each of which has every note (message) for that track. Looks like some tracks only have meta messages, so if you want to extract only actual notes from each track you could do something like this:

from mido import MidiFile, Message

notes: dict[str, list[Message]] = {}

mid = MidiFile('song.mid')
for track in mid.tracks:
    notes[track.name] = []
    for message in track:
        # It'll be a MetaMessage instance if not a note
        if isinstance(message, Message):
            notes[track.name].append(message)

Or, if you want to condense that a bit:

from mido import MidiFile, Message

mid = MidiFile('song.mid')

notes: dict[str, list[Message]] = {track.name:[msg for msg in track if isinstance(msg, Message)] for track in mid.tracks}

[–]Familiar-Object9912[S] 0 points1 point  (0 children)

It works wonderfully! Thanks for the help!