-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpadkontrol.py
214 lines (168 loc) · 5.9 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
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
# 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 + [0xF0, 0x42, 0x40, 0x6E, 0x08, 0x3F,
0x0A, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x29, 0x29, 0xF7]
# 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]
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, basestring):
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
}
"""
# LED must be reversed
led = ensure_sysex(led)[::-1]
group = [0 for i in range(5)]
for light, value in lights.items():
if value:
group_index = 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()
# 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