Mirai's Miscellaneous Misadventures

M54 / midi.py

1# license: AGPLv3 or later
2# copyright 2024 zamfofex
3
4from mido import MidiFile
5from sys import argv, stderr
6
7repeat = 0
8notes = []
9
10for i, track in enumerate(MidiFile(argv[1]).tracks):
11	index = 0
12	prev = None
13	for msg in track:
14		if msg.is_meta: continue
15		repeat += msg.time // 64
16		if msg.type != "note_on" and msg.type != "note_off": continue
17		if prev != None:
18			length = len(notes)
19			if length < index + repeat:
20				for n in range(index + repeat - length): notes.append([None] * 16)
21			for j in range(repeat):
22				notes[index + j][i] = (prev, -1)
23		index += repeat
24		repeat = 0
25		if msg.type == "note_off": prev = None
26		else: prev = msg.note
27
28print("{")
29for tick in notes:
30	print("{", end="")
31	for note in tick:
32		if note == None: print("{0,0},", end="")
33		else: print(f"{{{note[0] * 2},{note[1]}}},", end="")
34	print("},")
35print("},", len(notes))