-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.py
executable file
·47 lines (40 loc) · 1.33 KB
/
controller.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
#!/usr/bin/env python3
import mido
from time import time
from pythonosc import udp_client
from pythonosc import osc_message_builder
client = udp_client.SimpleUDPClient("127.0.0.1", 6010)
# We want to avoid jamming the OSC channel with events, so we rate-limit them
# This map has the control as key and the timestamp of the last event as value,
# so we can decide if we should send an event or discard it if it's too often
last_times = {}
with mido.open_input('LPD8') as inport:
for msg in inport:
prefix = None
value = None
key = None
if msg.type == "control_change":
prefix = "cc"
value = (msg.value + 1) / 128
key = str(msg.control)
elif msg.type == "note_on":
prefix = "t"
value = 1.0
key = str(msg.note)
elif msg.type == "note_off":
prefix = "t"
value = 0.0
key = str(msg.note)
elif msg.type == "program_change":
prefix = "p"
value = 1.0
key = str(msg.program)
else:
print(msg)
continue
k = prefix + key
now = time()
if k not in last_times or now - last_times[k] > 0.03:
print([k, value])
client.send_message("/ctrl", [k, value])
last_times[k] = now