-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
58 lines (46 loc) · 1.87 KB
/
main.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
from mido import MidiFile
from os import mkdir, path
if not path.isdir("code"): mkdir("code")
proc_num = 1
commands = []
code = "sensor en switch1 @enabled\njump 0 notEqual en 1\ncontrol enabled switch1 0 0 0 0\n"
cur_len = 0
tempo = 5000000
mid = MidiFile("midi/march1.mid")
for i, track in enumerate(mid.tracks):
for msg in track:
time = msg.time * (60 / (mid.ticks_per_beat * (60000000 / tempo)))
if msg.type == "set_tempo":
tempo = msg.tempo
elif msg.type == "note_on":
if time != 0 and cur_len != 0:
if cur_len > 0 and isinstance(commands[cur_len - 1], float):
commands[cur_len - 1] += time
else:
commands.append(time)
cur_len += 1
commands.append(f"block{msg.note - 20}") # midi note to piano
cur_len += 1
elif msg.type == "note_off" and time != 0:
if cur_len > 0 and isinstance(commands[cur_len - 1], float):
commands[cur_len - 1] += time
else:
commands.append(time)
cur_len += 1
if cur_len > 950:
for cmd in commands:
code += f"control enabled {cmd} 1 0 0 0\n" if isinstance(cmd, str) else f"wait {cmd}\n"
code += f"write 1 cell1 {proc_num + 1}\nwrite 0 cell1 {proc_num}\n"
with open(f"code/code{proc_num}.txt", "w") as f:
f.write(code)
proc_num += 1
commands = []
cur_len = 0
code = f"read en cell1 {proc_num}\njump 0 notEqual en 1\n"
print(msg)
for cmd in commands:
code += f"control enabled {cmd} 1 0 0 0\n" if isinstance(cmd, str) else f"wait {cmd}\n"
code += f"write 1 cell1 {proc_num + 1}\nwrite 0 cell1 {proc_num}\n"
with open(f"code/code{proc_num}.txt", "w") as f:
f.write(code)
print(proc_num)