-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpadKONTROL.lua
367 lines (335 loc) · 10.3 KB
/
padKONTROL.lua
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
class "PadKontrolMap" (ControlMap)
function PadKontrolMap:__init()
ControlMap.__init(self)
end
function PadKontrolMap:determine_type(str)
if str == "XYPAD" then
return DEVICE_MESSAGE.OSC
elseif str == "DISPLAY" then
return DEVICE_MESSAGE.OSC
elseif str:sub(1, 4) == "BTN#" then
return DEVICE_MESSAGE.OSC
elseif str:sub(1, 4) == "PAD#" then
return DEVICE_MESSAGE.OSC
elseif str:sub(1, 5) == "KNOB#" then
return DEVICE_MESSAGE.OSC
elseif str:sub(1, 8) == "ENCODER#" then
return DEVICE_MESSAGE.OSC
elseif str:sub(1,4) == "MAP#" then
return DEVICE_MESSAGE.OSC
elseif str:sub(1,7) == "ENABLE#" then
return DEVICE_MESSAGE.OSC
elseif str:sub(1,8) == "CHANNEL#" then
return DEVICE_MESSAGE.OSC
elseif str == "MAPX" then
return DEVICE_MESSAGE.OSC
elseif str == "MODEX" then
return DEVICE_MESSAGE.OSC
elseif str == "CHANNELX" then
return DEVICE_MESSAGE.OSC
elseif str == "MAPY" then
return DEVICE_MESSAGE.OSC
elseif str == "MODEY" then
return DEVICE_MESSAGE.OSC
elseif str == "CHANNELY" then
return DEVICE_MESSAGE.OSC
else
error(("unknown message-type: %s"):format(str or "nil"))
end
end
local buttons = {
"SCENE",
"MESSAGE",
"SETTING",
"NOTE",
"MIDICH",
"SWTYPE",
"RELVAL",
"VELO",
"PORT",
"FIXEDVEL",
"PROGCHANGE",
"X",
"Y",
"KNOB1ASSIGN",
"KNOB2ASSIGN",
"PEDAL",
"ROLL",
"FLAM",
"HOLD",
}
class "padKONTROL" (MidiDevice)
function padKONTROL:__init(display_name, message_stream, port_in, port_out)
self.buttons = {}
for i, v in ipairs(buttons) do
self.buttons[v] = i
end
MidiDevice.__init(self, display_name, message_stream, port_in, port_out)
self.control_map = PadKontrolMap()
end
local XY_MODE_DISABLE = 0
local XY_MODE_CC = 1
local XY_MODE_PITCHBEND = 2
function padKONTROL:open()
local input_devices = renoise.Midi.available_input_devices()
local output_devices = renoise.Midi.available_output_devices()
if table.find(input_devices, self.port_in) then
self.midi_in = renoise.Midi.create_input_device(self.port_in,
{self, padKONTROL.midi_callback},
{self, padKONTROL.sysex_callback}
)
else
LOG("Notice: Could not create MIDI input device ", self.port_in)
end
if table.find(output_devices, self.port_out) then
self.midi_out = renoise.Midi.create_output_device(self.port_out)
else
LOG("Notice: Could not create MIDI output device ", self.port_out)
end
self:send_sysex_message(0x42, 0x40, 0x6E, 0x08, 0x00, 0x00, 0x01)
self.global_channel = 1
self.x_mode = XY_MODE_DISABLE
self.y_mode = XY_MODE_DISABLE
self.x_channel = 1
self.y_channel = 1
self.x_cc = 1
self.y_cc = 1
self.pad_mode = {0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0}
self.pad_channel = {1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1,
1, 1, 1, 1}
self.pad_note = {0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15}
self:sync_setup()
self:send_sysex_message(0x42, 0x40, 0x6E, 0x08, 0x3F, 0x0A, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00)
self:disp("---")
end
function padKONTROL:sync_setup()
local packet = {0x42, 0x40, 0x6E, 0x08, 0x3F, 0x2A, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
packet[7 + 1] = self.global_channel - 1
packet[7 + 2] = self.x_mode + self.y_mode * 4
packet[7 + 3] = self.x_channel - 1
packet[7 + 4] = self.y_channel - 1
packet[7 + 5] = self.x_cc
packet[7 + 6] = self.y_cc
packet[7 + 7] = 0
packet[7 + 8] = 0
packet[7 + 9] = 0
for i=16, 1, -1 do
local a = math.floor((i-1)/7)
packet[7 + 7 + a] = packet[7 + 7 + a] * 2
packet[7 + 7 + a] = packet[7 + 7 + a] + self.pad_mode[i]
packet[7 + 9 + i] = self.pad_channel[i]
packet[7 + 25 + i] = self.pad_note[i]
end
self:send_sysex_message(unpack(packet))
end
function padKONTROL:release()
self:send_sysex_message(0x42, 0x40, 0x6E, 0x08, 0x00, 0x00, 0x00)
MidiDevice.release(self)
end
function padKONTROL:disp(str)
local out = {0x42, 0x40, 0x6E, 0x08, 0x22, 0x04, 0x00, 0x29, 0x29, 0x29}
for i, c in ipairs({str:byte(1, 3)}) do
if c >= string.byte('a') and c <= string.byte('z') then
out[7 + i] = c - string.byte('a') + 0x61
elseif c >= string.byte('A') and c <= string.byte('Z') then
out[7 + i] = c - string.byte('A') + 0x41
elseif c >= string.byte('0') and c <= string.byte('9') then
out[7 + i] = c - string.byte('0') + 0x30
elseif c == string.byte('-') then
out[7 + i] = 0x2d
end
end
self:send_sysex_message(unpack(out))
end
function padKONTROL:send_osc_message(key, value)
if key == "XYPAD" then
elseif key:sub(1, 4) == "BTN#" then
local btn = key:sub(5)
local code = self.buttons[btn]
if (code == nil) then
error(("Button not known: '%s'"):format(key))
end
local send_value
if value == 0 then
send_value = 0
else
send_value = 0x20
end
self:send_sysex_message(0x42, 0x40, 0x6E, 0x08, 0x01, code + 0x10 - 1, send_value)
elseif key:sub(1, 4) == "PAD#" then
local pad = tonumber(key:sub(5))
if value == 127 then
self:send_sysex_message(0x42, 0x40, 0x6E, 0x08, 0x01, pad - 1, 0x20)
else
self:send_sysex_message(0x42, 0x40, 0x6E, 0x08, 0x01, pad - 1, 0x00)
end
elseif key:sub(1, 5) == "KNOB#" then
elseif key:sub(1, 8) == "ENCODER#" then
elseif key == "DISPLAY" then
self:disp(value)
elseif key:sub(1,4) == "MAP#" then
self.pad_note[tonumber(key:sub(5))] = tonumber(value)
self:sync_setup()
elseif key:sub(1,7) == "ENABLE#" then
self.pad_mode[tonumber(key:sub(8))] = tonumber(value)
self:sync_setup()
elseif key:sub(1,8) == "CHANNEL#" then
self.pad_mode[tonumber(key:sub(9))] = tonumber(value)
self:sync_setup()
elseif key == "MAPX" then
self.x_cc = tonumber(value)
self:sync_setup()
elseif key == "MODEX" then
self.x_mode = tonumber(value)
self:sync_setup()
elseif key == "CHANNELX" then
self.x_channel = tonumber(value)
self:sync_setup()
elseif key == "MAPY" then
self.x_cc = tonumber(value)
self:sync_setup()
elseif key == "MODEY" then
self.x_mode = tonumber(value)
self:sync_setup()
elseif key == "CHANNELY" then
self.x_channel = tonumber(value)
self:sync_setup()
else
error(("unknown message: %s"):format(str or "nil"))
end
end
local header = {0xF0, 0x42, 0x40, 0x6E, 0x08}
function padKONTROL:sysex_callback(message)
MidiDevice.sysex_callback(self, message)
if #message <= #header then
error("Bad SYSEX")
end
for i, v in ipairs(header) do
if message[i] ~= v then
error("Bad SYSEX")
end
end
if message[#message] ~= 0xF7 then
error("Bad SYSEX")
end
if message[#header+1] == 0x45 then
if #message ~= (#header + 1 + 3) then
error("Bad SYSEX")
end
local value = message[#header+3]
local n = message[#header+2]
if (n >= 0x40) and (n <= 0x4F) then
local str = ("PAD#%i"):format(n - 0x40 + 1)
for k, v in ipairs(self.control_map:get_params_by_value(str, nil)) do
local msg = Message()
msg.value = value
msg.is_note_off = false
msg.context = DEVICE_MESSAGE.MIDI_NOTE
self:_send_message(msg, v["xarg"])
end
elseif (n >= 0x00) and (n <= 0x0F) then
local str = ("PAD#%i"):format(n + 1)
for k, v in ipairs(self.control_map:get_params_by_value(str, nil)) do
local msg = Message()
msg.value = 0
msg.is_note_off = true
msg.context = DEVICE_MESSAGE.MIDI_NOTE
self:_send_message(msg, v["xarg"])
end
else
error("Bad SYSEX")
end
elseif message[#header+1] == 0x48 then
if #message ~= (#header + 1 + 3) then
error("Bad SYSEX")
end
local on = message[#header+3]
local n = message[#header+2]
local str
if n == 0x20 then
str = "BTN#XYPAD"
else
str = "BTN#" .. buttons[1+n]
end
for k, v in ipairs(self.control_map:get_params_by_value(str, nil)) do
local msg = Message()
msg.value = 1
if on then
msg.is_note_off = false
else
msg.is_note_off = true
end
self:_send_message(msg, v["xarg"])
end
elseif message[#header+1] == 0x4B then
if #message ~= (#header + 1 + 3) then
error("Bad SYSEX")
end
local x = message[#header+2]
local y = message[#header+3]
for k, v in ipairs(self.control_map:get_params_by_value("XYPAD", nil)) do
local msg = Message()
msg.value = {x, y}
self:_send_message(msg, v["xarg"])
end
elseif message[#header+1] == 0x49 then
if #message ~= (#header + 1 + 3) then
error("Bad SYSEX")
end
local knob = message[#header+2]
local value = message[#header+3]
local str = ("KNOB#%i"):format(1 + knob)
for k, v in ipairs(self.control_map:get_params_by_value(str, nil)) do
local msg = Message()
msg.value = value
self:_send_message(msg, v["xarg"])
end
elseif message[#header+1] == 0x43 then
if #message ~= (#header + 1 + 3) then
error("Bad SYSEX")
end
local value = message[#header+3]
local str
if value == 1 then
str = "ENCODER#RIGHT"
else
str = "ENCODER#LEFT"
end
for k, v in ipairs(self.control_map:get_params_by_value(str, nil)) do
local msg = Message()
msg.value = 1
msg.is_note_off = false
self:_send_message(msg, v["xarg"])
end
else
end
end
function padKONTROL:midi_callack(message)
end
function padKONTROL:point_to_value(pt, elm, ceiling)
if type(pt.val) == "table" then
local value = table.create()
for k, v in ipairs(pt.val) do
value:insert((v * (1 / ceiling)) * 1)
end
return value
else
return MidiDevice.point_to_value(self, pt, elm, ceiling)
end
end