-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (56 loc) · 2 KB
/
Copy pathmain.py
File metadata and controls
72 lines (56 loc) · 2 KB
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
67
68
69
70
71
72
from mido import MidiFile, tick2second
from arduino_code import ArduinoCode
from buzzer import Buzzer
from lights import Lights
from piezo_orchestra import PiezoOrchestra
mid = MidiFile("/Users/boberchik342/Downloads/UT-Megalovania.mid")
current_time = 0
stats = {"important notes on": 0}
code = ArduinoCode()
buzzers = [
Buzzer(0, code, 5, stats),
Buzzer(1, code, 5, stats),
Buzzer(2, code, 5, stats),
]
lights = Lights([5, 6], code)
orchestra = PiezoOrchestra(buzzers)
def pitch_multiplier(note):
if note < 45:
return 0.7 # bass
elif note < 70:
return 1.6 # melody
else:
return 0.3 # high
tempo = 500000
events = []
notes = {}
for msg in mid:
current_time += msg.time * 1000
if msg.type == "note_on" and msg.velocity > 0:
note = notes.pop(msg.note, None)
if note is not None:
note["length"] = current_time - note["time"]
new_note = {"type": "note_on", "note": msg.note, "time": current_time, "length": 100, "velocity": msg.velocity}
events.append(new_note)
notes[msg.note] = new_note
elif msg.type == "note_off" or (msg.type == "note_on" and msg.velocity == 0):
note = notes.pop(msg.note, None)
if note is not None:
note["length"] = current_time - note["time"]
events.append({"type": "note_off", "time": current_time, "note": msg.note})
for event in events:
if event["type"] == "note_on":
if event["note"] > 75 or event["length"] > 3000:
# print("\nignored note")
continue
priority = event["velocity"] * pitch_multiplier(event["note"])
if priority < 20:
continue
# if event["note"] > 65:
# print("\nclamped")
orchestra.play_note(min(event["note"], 65), priority, event["time"])
elif event["type"] == "note_off":
if event["note"] > 65:
continue
orchestra.stop_note(event["note"], event["time"])
lights.set_light_amount(stats["important notes on"] // 2)