-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidi.py
130 lines (106 loc) · 3.21 KB
/
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import mido
import menu
import screen
import threading
import settings
import led
from time import sleep
_input_port = None
_output_port = None
_midi_event_callbacks = []
_midi_status_active = False
settings.add(settings.BoolSetting("Lights on Send", True))
class MidiStatus():
name = "MIDI STATUS"
@staticmethod
def on_button_pressed():
menu.leave_sub_menu()
@staticmethod
def on_button_long_pressed():
menu.leave_sub_menu()
@staticmethod
def on_enter():
global _midi_status_active
_midi_status_active = True
screen.line1("MIDI STATUS", True)
screen.line2("Checking...", True)
@staticmethod
def on_leave():
global _midi_status_active
_midi_status_active = False
@staticmethod
def init():
midi_thread = threading.Thread(target=port_handler)
midi_thread.daemon = True
midi_thread.start()
def port_handler():
global _input_port
global _output_port
port_message = ""
while True:
input_port_names = mido.get_input_names()
if _input_port and _input_port.name not in input_port_names: _input_port = None
if _input_port == None:
input_port_name = None
for port in input_port_names:
if "Grand" in port:
input_port_name = port
break
if input_port_name == None:
port_message = "NO GRAND PIANO"
on_disconnect()
else:
try:
_input_port = mido.open_input(input_port_name)
_output_port = mido.open_output(input_port_name)
port_message = "CONNECTED"
on_connect()
except:
port_message = "PORT ERROR"
on_disconnect()
if _midi_status_active:
screen.line2(port_message, True)
sleep(1)
_connected = False
def on_connect():
global _connected
if _connected: return
_connected = True
led.pulse((0, 10, 0))
def on_disconnect():
global _connected
if not _connected: return
_connected = False
led.pulse((10, 0, 0))
def stop():
if _input_port:
_input_port.close()
if _output_port:
_output_port.close()
def send(msg):
global _output_port
if _output_port == None:
return
if _output_port.closed or _output_port.name not in mido.get_output_names():
_output_port = None
on_disconnect()
return
if settings.get_value("Lights on Send"):
trigger_callbacks(msg)
_output_port.send(msg)
def loop():
global _input_port
if _input_port == None:
return
if _input_port.closed or _input_port.name not in mido.get_input_names():
_input_port = None
on_disconnect()
return
for msg in _input_port.iter_pending():
trigger_callbacks(msg)
def trigger_callbacks(msg):
for f in _midi_event_callbacks:
f(msg)
def add_callback(f):
_midi_event_callbacks.append(f)
add_callback(settings.NoteSetting.midi_callback)