-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepareMidi.py
70 lines (44 loc) · 1.74 KB
/
prepareMidi.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
66
import mido
import os
song = "rick_e_morty"
midi = mido.MidiFile(f"midi/{song}.mid", clip=True)
print("Midi file:\n", midi, "\n")
newFile = mido.MidiFile(ticks_per_beat = midi.ticks_per_beat)
for track in midi.tracks:
noteAlreadyOn = dict()
newTrack = mido.MidiTrack()
newTrack.name = track.name
offsetByNotMeta = 0
for msg in track:
if msg.is_meta:
if msg.type != "unknown_meta":
newTrack.append(msg)
continue
if msg.type == "note_on":
if msg.velocity == 0:
msg = mido.Message("note_off", note = msg.note, time=msg.time+offsetByNotMeta, channel = msg.channel)
elif noteAlreadyOn.get(msg.note):
newTrack.append(mido.Message("note_off", note = msg.note, time=msg.time+offsetByNotMeta, channel = msg.channel))
newTrack.append(mido.Message("note_on", note = msg.note, velocity = msg.velocity, time = 0, channel = msg.channel))
else:
newTrack.append(mido.Message("note_on", note = msg.note, time=msg.time+offsetByNotMeta, channel = msg.channel, velocity = msg.velocity))
noteAlreadyOn[msg.note] = True
if msg.type == "note_off":
noteAlreadyOn[msg.note] = False
newTrack.append(mido.Message("note_off", note = msg.note, time=msg.time+offsetByNotMeta, channel = msg.channel))
if msg.type not in ["note_on", "note_off"]:
offsetByNotMeta += msg.time
else:
offsetByNotMeta = 0
newFile.tracks.append(newTrack)
#Se non esiste creo la directory dove collezionare i backups dei file midi
if not os.path.exists("backups"):
os.mkdir("backups")
print(newFile, "\n")
from functions import MidiInterface
# m = MidiInterface(midi)
# n = MidiInterface(newFile)
# print(midi.length, m.totalTicks)
# print(newFile.length, n.totalTicks)
midi.save(f"backups/{song}.mid")
newFile.save(f"midi/{song}.mid")