-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
105 lines (84 loc) · 2.85 KB
/
__init__.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
import pygame.midi as midi
import threading
class Init:
def __init__(self, mode="live"):
self.functions = {}
self.keys = False #Amount of buttons / leds
self.mode = mode.lower()
if(not midi.get_init()):
midi.init()
self.input = self.input_port()
self.output = self.output_port()
self.clear()
def poll(self):
try:
while True:
if(self.input.poll()):
data = self.input.read(1)
status = data[0][0][0]
data1 = data[0][0][1] #key
data2 = data[0][0][2] #event
data3 = data[0][0][3]
time = data[0][1]
ob = {
"status": status,
"data": [data1, data2, data3],
"timestamp": time
}
self.processData(ob)
except KeyboardInterrupt:
pass
def processData(self, data):
funcs = self.functions
if("ondata" in funcs):
funcs["ondata"](data)
if(data["data"][1] == 127):
if("keypress" in funcs):
funcs["keypress"](data["data"][0])
if(data["data"][1] == 0):
if("keyup" in funcs):
funcs["keyup"](data["data"][0])
if(data["status"] == 2):
if("modechanged" in funcs):
funcs["modechanged"]()
if(data["status"] == 247):
if("modeready" in funcs):
funcs["modeready"]()
def on(self, key, color):
self.output.note_on(key, velocity=color, channel=0)
def off(self, key):
self.output.note_off(key, velocity=0, channel=0)
def clear(self):
count = 100
if(self.keys):
count = self.keys
for a in range(1, count + 1):
self.off(a)
def feature(self, func):
self.functions[func.__name__.lower()] = func
def input_port(self):
for a in range(midi.get_count()):
device = midi.get_device_info(a)
name = str(device[1]).lower()
if("launchpad" in name and device[2]):
id = a
if(self.mode != "live"):
id = a + 1
return midi.Input(id)
return False
def output_port(self):
for a in range(midi.get_count()):
device = midi.get_device_info(a)
name = str(device[1]).lower()
if("launchpad" in name and device[3]):
id = a
if(self.mode != "live"):
id = a + 1
return midi.Output(id)
return False
def close(self):
self.clear()
if(self.input):
self.input.close()
if(self.output):
self.output.close()