-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix_midi.py
65 lines (51 loc) · 1.75 KB
/
fix_midi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pathlib import Path
from typing import Optional
import argparse
import mido
def main(input_file: Path, output: Path = None, max_ticks: int = 100):
output = output or input_file.with_stem(input_file.stem + "_fixed")
midi = mido.MidiFile(input_file)
messages = mido.MidiTrack()
time = 0
pending = {}
for message in mido.merge_tracks(midi.tracks):
time += message.time
messages.append(message)
if message.type not in ("note_on", "note_off"):
continue
if message.velocity >= 1: # note turned on
pending[message.note] = time
elif message.type == "note_off" or message.velocity == 0: # note turned off
pending.pop(message.note, None)
for note, timestamp in pending.copy().items():
if time - timestamp < max_ticks:
continue
messages.append(mido.Message(
"note_on",
note=note,
velocity=0,
time=time - timestamp
))
pending.pop(note)
midi.tracks = [messages]
midi.save(output)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fix a MIDI file by adding note_off messages after each note_on message.")
parser.add_argument(
"input_file",
help="The path to a MIDI file.",
type=Path
)
parser.add_argument(
"--max_ticks", "-m",
help="The maximum length of a note in ticks. Defaults to 100.",
default=100,
type=int
)
parser.add_argument(
"--output", "-o",
help="The path to the output file.",
default=None,
type=Path
)
main(**vars(parser.parse_args()))