This repository has been archived by the owner on Oct 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
padKontrol.py
332 lines (269 loc) · 7.23 KB
/
padKontrol.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import math
# button constants
BUTTON_SCENE = 0x10
BUTTON_MESSAGE = 0x11
BUTTON_SETTING = 0x12
BUTTON_NOTE_CC = 0x13
BUTTON_MIDI_CH = 0x14
BUTTON_SW_TYPE = 0x15
BUTTON_REL_VAL = 0x16
BUTTON_VELOCITY = 0x17
BUTTON_PORT = 0x18
BUTTON_FIXED_VELOCITY = 0x19
BUTTON_PROG_CHANGE = 0x1A
BUTTON_X = 0x1B
BUTTON_Y = 0x1C
BUTTON_KNOB_1_ASSIGN = 0x1D
BUTTON_KNOB_2_ASSIGN = 0x1E
BUTTON_PEDAL = 0x1F
BUTTON_ROLL = 0x20
BUTTON_FLAM = 0x21
BUTTON_HOLD = 0x22
BUTTON_PAD = 0x30
# 7 SEGMENT LED DISPLAY
# https://en.wikipedia.org/wiki/Seven-segment_display
# individual segments
FIRST_A = 0x38 # 56 Decimal
FIRST_B = 0x39
FIRST_C = 0x3A
FIRST_D = 0x3B
FIRST_E = 0x3C
FIRST_F = 0x3D
FIRST_G = 0x3E
FIRST_DP = 0x3F # 38-3F
SEC_A = 0x30 # 48 Decimal
SEC_B = 0x31
SEC_C = 0x32
SEC_D = 0x33
SEC_E = 0x34
SEC_F = 0x35
SEC_G = 0x36
SEC_DP = 0x37 # 30-37
THIRD_A = 0x28 # 40 Decimal
THIRD_B = 0x29
THIRD_C = 0x2A
THIRD_D = 0x2B
THIRD_E = 0x2C
THIRD_F = 0x2D
THIRD_G = 0x2E
THIRD_DP = 0x2F # 28 -2F
# knobs
KNOB_1 = 0x00
KNOB_2 = 0x01
ROTARY_KNOB = 0x43
# send values
ROTARY_KNOB_RIGHT = 1
ROTARY_KNOB_LEFT = 127
ALL_PADS = list(range(16))
ALL_BUTTONS = [x for x in range(16, 35)]
ALL_BUTTONS_AND_PADS = ALL_BUTTONS + [48] # button pads #48
# light state constants
LIGHT_STATE_OFF = 0x00
LIGHT_STATE_ON = 0x20
LIGHT_STATE_ONESHOT = 0x40
LIGHT_STATE_BLINK = 0x60
# LED state constants
LED_STATE_ON = 0x00
LED_STATE_BLINK = 0x01
# SYSEX constants
_SYSEX_COMMON = [0xF0, 0x42, 0x40, 0x6E, 0x08]
SYSEX_NATIVE_MODE_ON = _SYSEX_COMMON + [0x00, 0x00, 0x01, 0xF7]
SYSEX_NATIVE_MODE_ENABLE_OUTPUT = _SYSEX_COMMON + [
0x3F,
0x2A,
0x00,
0x00,
0x05,
0x05,
0x05,
0x7F,
0x7E,
0x7F,
0x7F,
0x03,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x0A,
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x07,
0x08,
0x09,
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x0F,
0x10,
0xF7,
]
SYSEX_NATIVE_MODE_INIT = _SYSEX_COMMON + [
0x3F, # packet comm
0x0A, # msg len (9 bytes) +1,
0x01, # comm #2
0x00, # data ( 4 bytes)
0x00,
0x00,
0x00,
0x00, # data (+ 5 bytes = 9 bytes)
0x00,
0x00,
0x00,
0x00,
0xF7, # EOX
]
# displays YES on LED if native mode is enabled properly
SYSEX_NATIVE_MODE_TEST = _SYSEX_COMMON + [0x22, 0x04, 0x00, 0x59, 0x45, 0x53, 0xF7]
SYSEX_NATIVE_MODE_OFF = _SYSEX_COMMON + [0x00, 0x00, 0x00, 0xF7]
# midi constants
NOTE_ON = "note_on"
NOTE_OFF = "note_off"
def string_to_sysex(string):
"""Convert a string to the type required by the PadKontrol.
string -- the string to convert. Must be 3 characters long.
"""
if len(string) != 3:
raise ValueError("String '%s' must be 3 characters long" % string)
return [ord(s) for s in string]
def ensure_sysex(value):
"""Ensure the value is the correct type.
value -- either a string (will be converted) or a list (will be returned
unmodified).
"""
if isinstance(value, str):
return string_to_sysex(value)
else:
return value
def light_group(led, lights):
"""Set the LED readout and multiple lights at once.
led -- either a string or a string converted by string_to_sysex.
lights -- a dictionary. Specify pad numbers (0 to 15) or button constants
as keys, and indicate whether the associated light is on or off
via a truth-y or false-y value. Any missing pads or buttons will
be turned off.
In this example, BUTTON_X and pad #6 will be on, and pad #4 will
be off:
{
BUTTON_X: True,
6: True,
4: False
}
Caution: works only, if midiin port ignore sysex!!
"""
# LED must be reversed
if led:
led = ensure_sysex(led)[::-1]
else:
led = [0x00, 0x00, 0x00] # do not change current LCD text
group = [0 for i in range(5)]
for light, value in list(lights.items()):
if value:
group_index = math.floor(light / 7)
remainder = light % 7
group[group_index] += int(math.pow(2, remainder))
return _SYSEX_COMMON + [0x3F, 0x0A, 0x01] + group + [0x00] + led + [0xF7]
def light(button_or_pad, light_state):
"""Set the state of the specified button or pad's light.
button_or_pad -- a button value constant or pad number (0 to 15).
light_state -- a light state constant or True (on) or False (off).
"""
if light_state is True:
light_state = LIGHT_STATE_ON
elif light_state is False:
light_state = LIGHT_STATE_OFF
return _SYSEX_COMMON + [0x01, button_or_pad, light_state, 0xF7]
def light_flash(button_or_pad, duration):
"""Flash the button's light momentarily.
button_or_pad -- a button value constant or pad number (0 to 15).
duration -- a number between 0 (9ms) and 1.0 (279ms).
"""
speed = LIGHT_STATE_ONESHOT + int(30 * duration)
return light(button_or_pad, speed)
def led(led, led_state=LED_STATE_ON):
"""Set the LED display.
led -- either a string or a list returned from string_to_sysex.
led_state -- a LED state constant (default is on)
"""
led = ensure_sysex(led)
return _SYSEX_COMMON + [0x22, 0x04, led_state] + led + [0xF7]
class PadKontrolInput:
"""Handle the PadKontrol's output.
Extend this class and override the on_* methods to respond to PadKontrol
events. Pass SYSEX data to the process_sysex method to trigger those
methods.
"""
def process_sysex(self, sysex):
"""Inspect the SYSEX and call the relevant handler.
sysex -- a SYSEX message in the form of a list of integers.
Must be a fully formed SYSEX message (begins with 0xF0, ends
with 0xF7).
"""
first = sysex[5]
second = sysex[6]
third = sysex[7]
# pad
if first == 0x45:
if second >= 64:
self.on_pad_down(second - 64, third)
else:
self.on_pad_up(second)
# button
elif first == 0x48:
if third == 127:
self.on_button_down(second + 16)
else:
self.on_button_up(second + 16)
# knob
elif first == 0x49:
self.on_knob(second, third)
# rotary encoder
elif first == 0x43:
if third == 1:
self.on_rotary_right()
else:
self.on_rotary_left()
self.on_rotary(third)
# x/y pad
elif first == 0x4B:
self.on_x_y(second, third)
# invalid SYSEX
else:
self.on_invalid_sysex(sysex)
def on_invalid_sysex(self, sysex):
raise ValueError("unrecognised SYSEX - %s", sysex)
def on_pad_down(self, pad, velocity):
pass
def on_pad_up(self, pad):
pass
def on_button_down(self, button):
pass
def on_button_up(self, button):
pass
def on_knob(self, knob, value):
pass
def on_rotary_left(self):
pass
def on_rotary_right(self):
pass
def on_x_y(self, x, y):
pass
def on_rotary(self, val):
pass