-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpadKONTROL-neko.control.js
500 lines (435 loc) · 10.3 KB
/
padKONTROL-neko.control.js
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
loadAPI(2);
host.defineController("Korg", "padKONTROL NKO", "1.0", "d5eaa780-f0e1-11e3-ac10-0800200c9a66");
host.defineMidiPorts(2, 1);
if (host.platformIsLinux()) {
host.addDeviceNameBasedDiscoveryPair(["padKONTROL MIDI 2", "padKONTROL MIDI 3"], ["padKONTROL MIDI 2"]);
}
else if (host.platformIsMac()) {
host.addDeviceNameBasedDiscoveryPair(["padKONTROL PORT A", "padKONTROL PORT B"], ["padKONTROL CTRL"]);
}
else if (host.platformIsWindows()) {
host.addDeviceNameBasedDiscoveryPair(["MIDIIN2 (padKONTROL)", "MIDIIN3 (padKONTROL)"], ["MIDIOUT2 (padKONTROL)"]);
}
var Mode =
{
Drum: -1,
Scene: 0,
Message: 1,
Pedal: 15,
};
var ModeTitle =
{
"-1": "DRM",
0: "SCN",
1: "MSG",
15: "PDL"
}
var mode = Mode.Undefined;
var pedal = {
root: 36,
offset: 0,
velocity: 127
}
var Light =
{
PAD01: 0,
PAD02: 1,
PAD03: 2,
PAD04: 3,
PAD05: 4,
PAD06: 5,
PAD07: 6,
PAD08: 7,
PAD09: 8,
PAD10: 9,
PAD11: 10,
PAD12: 11,
PAD13: 12,
PAD14: 13,
PAD15: 14,
PAD16: 15,
SCENE: 16,
MESSAGE: 17,
SETTING: 18,
NOTECC: 19,
MIDICH: 20,
SWTYPE: 21,
RELVAL: 22,
VEL: 23,
PORT: 24,
FIXEDVEL: 25,
PROGCHANGE: 26,
X: 27,
Y: 28,
KNOB1: 29,
KNOB2: 30,
PEDAL: 31,
ROLL: 32,
FLAM: 33,
HOLD: 34,
COUNT: 35
};
var LightState =
{
OFF: 0,
ON: 32,
BLINKING: 0x63,
BLINK_SHORTEST: 0x41,
BLINK_LONGEST: 0x5f
};
var Button =
{
UNDEFINED: -1,
SCENE: 0,
MESSAGE: 1,
SETTING: 2,
NOTE_CC: 3,
MIDI_CH: 4,
SW_TYPE: 5,
REL_VAL: 6,
VELOCITY: 7,
PORT: 8,
FIXED_VELOCITY: 9,
PROG_CHANGE: 10,
X: 11,
Y: 12,
KNOB1_ASSIGN: 13,
KNOB2_ASSIGN: 14,
PEDAL: 15,
ROLL: 16,
FLAM: 17,
HOLD: 18,
XY_PRESS: 32
};
var tempMode = Button.UNDEFINED;
var Observer =
{
IS_PLAYING: 0,
IS_RECORDING: 1,
TRACK_NAME: 2
};
var isPlaying = initArray(0, 16);
var isQueued = initArray(0, 16);
var hold = false;
var isPlay = false;
var keyTranslationTable, keyTranslationTableOff;
function init() {
noteInput = host.getMidiInPort(1).createNoteInput("", "8A????", "9A????");
//host.getMidiInPort(0).setMidiCallback(onMidi);
host.getMidiInPort(0).setSysexCallback(onSysex);
//noteInput.setShouldConsumeEvents(false);
// default notes
keyTranslationTable = initArray(0, 128);
for (var i = 0; i < 16; i++) {
var col = i & 3;
var row = i >> 2;
keyTranslationTable[i + 1] = 36 + col + (3 - row) * 4; // flip y-direction of pads to match the "standard" 16-pad layout
}
// All notes off
keyTranslationTableOff = initArray(0, 128);
for (var i = 0; i < 16; i++) {
keyTranslationTableOff[i + 1] = -1
}
noteInput.setKeyTranslationTable(keyTranslationTable);
// transport
transport = host.createTransportSection();
transport.addIsPlayingObserver(function (on) {
isPlay = on;
});
cursorTrack = host.createCursorTrackSection(2, 0);
cursorTrack.addNoteObserver(onNote);
primaryDevice = cursorTrack.getPrimaryDevice();
remotes = primaryDevice.createCursorRemoteControlsPage("remotes", 8, "");
xyControls = primaryDevice.createCursorRemoteControlsPage("xy", 2, "xy");
/* CURSOR TRACK */
// Enter Native Mode
sendSysex("f0 42 40 6e 08 00 00 01 f7");
// packet comm. #1 (section 3-1, fig 17, pg. 6)
// see table 3 for the packet format
sendSysex("f0 42 40 6e 08 3f 2a 00 00 05 05 05 7f 7e 7f 7f 03 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 0a 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 f7");
// packet comm. #2 (section 3-1, fig 19, pg. 7)
// see table 4 for the packet format
sendSysex("F0 42 40 6E 08 3F 0A 01 00 00 00 00 00 00 29 29 29 F7");
setMode(Mode.Drum);
updateIndications();
}
var playingKeys = initArray(false, 128);
function onNote(state, key, vel) {
//println("onNote:" + key + "," + state + "," + vel);
playingKeys[key] = state;
}
function exit() {
sendSysex("f0 42 40 6e 08 00 00 00 f7"); // Turn off native mode
sendSysex("f0 42 40 6e 08 1f 14 00 f7"); // Select scene 0
}
var pendingState = initArray(false, Light.COUNT);
var outputState = initArray(-1, Light.COUNT);
function prepareDisplay() {
for (var i = 0; i < 16; i++) {
var key = 36 + i;
var row = 3 - (i >> 2);
var column = i & 3;
setButtonLight(Light.PAD01 + column + 4 * row, playingKeys[key]);
}
setButtonLight(Light.KNOB1, macroOffset != 0);
setButtonLight(Light.KNOB2, macroOffset != 6);
setButtonLight(Light.HOLD, hold);
setButtonLight(Light.PROGCHANGE, tempMode == Button.PROG_CHANGE);
setButtonLight(Light.MIDICH, tempMode == Button.MIDI_CH);
setButtonLight(Light.SCENE, tempMode == Button.SCENE);
setButtonLight(Light.MESSAGE, tempMode == Button.MESSAGE);
setButtonLight(Light.PEDAL, tempMode == Button.PEDAL);
}
var macroOffset = 0;
function setButtonLight(light, state) {
pendingState[light] = state ? LightState.ON : LightState.OFF;
}
function sendButtonLight(light, state) {
sendSysex("f0 42 40 6e 08 01 " + uint7ToHex(light) + " " + uint7ToHex(state) + " f7");
}
function flush() {
prepareDisplay();
for (var i = 0; i < Light.COUNT; i++) {
if (pendingState[i] != outputState[i]) {
sendButtonLight(i, pendingState[i]);
outputState[i] = pendingState[i];
}
}
}
/**
* @param {MidiMessage} msg
*/
function onMidi(status, data1, data2) {
printMidi(status, data1, data2);
}
/**
* @override
*/
function onSysex(data) {
//printSysex(data);
if (data.matchesHexPattern("f042406e0848????f7")) // NORMAL BUTTONS
{
var button = data.hexByteAt(6);
var value = data.hexByteAt(7);
var buttonPressed = value > 0;
println([button, value])
switch (button) {
case Button.SCENE:
setTempMode(button, buttonPressed);
break;
case Button.MESSAGE:
setTempMode(button, buttonPressed, true);
break;
case Button.FIXED_VELOCITY:
break;
case Button.PROG_CHANGE:
setTempMode(button, buttonPressed);
// setButtonLight(button, buttonPressed);
break;
case Button.XY_PRESS:
pressXY(buttonPressed);
break;
case Button.MIDI_CH:
break;
case Button.X:
break;
case Button.Y:
break;
case Button.PEDAL:
setTempMode(button, buttonPressed);
// setButtonLight(button, buttonPressed);
break;
case Button.VELOCITY:
break;
case Button.REL_VAL:
break;
case Button.PORT:
break;
case Button.NOTE_CC:
break;
case Button.SW_TYPE:
break;
case Button.HOLD:
if (buttonPressed) {
hold = !hold;
if (!hold && !isXYPressed) {
xyControls.getParameter(0).set(64, 128);
xyControls.getParameter(1).set(64, 128);
}
}
break;
case Button.FLAM:
break;
case Button.ROLL:
break;
case Button.KNOB1_ASSIGN:
if (buttonPressed) {
macroOffset = Math.max(0, macroOffset - 2);
showMacroOffsetInDisplay();
updateIndications();
}
break;
case Button.KNOB2_ASSIGN:
if (buttonPressed) {
macroOffset = Math.min(6, macroOffset + 2);
showMacroOffsetInDisplay();
updateIndications();
}
break;
}
}
else if (data.matchesHexPattern("F042406E084300??F7")) // DATA WHEEL
{
var value = data.hexByteAt(7);
var delta = uint7ToInt7(value);
onDataWheel(delta);
}
else if (data.matchesHexPattern("F042406E084B????F7")) // XY position
{
var x = data.hexByteAt(6);
var y = data.hexByteAt(7);
if (hold || isXYPressed) {
xyControls.getParameter(0).set(x, 128);
xyControls.getParameter(1).set(y, 128);
}
}
else if (data.matchesHexPattern("F042406E0849????F7")) // Knob
{
var index = data.hexByteAt(6);
var value = data.hexByteAt(7);
remotes.getParameter(index + macroOffset).getAmount().set(value, 128);
}
else if (data.matchesHexPattern("F042406E0845????F7")) // Pads
{
var padInfo = data.hexByteAt(6);
var velocity = data.hexByteAt(7);
println(['padInfo', padInfo]);
onPad(padInfo & 0xF, (padInfo & 0x40), velocity);
}
else if (data.matchesHexPattern("F042406E084700??F7")) // Pedal
{
noteInput.sendRawMidiEvent(
data.hexByteAt(7) ? 0x91 : 0x81,
pedal.root + pedal.offset,
pedal.velocity
);
}
else {
printSysex(data);
}
}
function onDataWheel(delta) {
switch (tempMode) {
case Button.PROG_CHANGE:
if (delta > 0) primaryDevice.switchToNextPreset();
else primaryDevice.switchToPreviousPreset();
break;
case Button.UNDEFINED:
if (delta > 0) cursorTrack.selectNext();
else cursorTrack.selectPrevious();
break;
}
}
function setTempMode(button, pressed, noteOff) {
if (pressed) {
noteInput.setKeyTranslationTable(noteOff ? keyTranslationTableOff : keyTranslationTable);
tempMode = button;
}
else {
noteInput.setKeyTranslationTable(keyTranslationTable);
tempMode = Button.UNDEFINED;
}
if (ModeTitle[tempMode] != undefined) {
setDisplay(ModeTitle[tempMode], false);
}
}
function updateIndications() {
for (var i = 0; i < 8; i++) {
remotes.getParameter(i).getAmount().setIndication((i & 0xE) == macroOffset);
}
}
function showMacroOffsetInDisplay() {
switch (macroOffset) {
case 0:
setDisplay("M12", false);
break;
case 2:
setDisplay("M34", false);
break;
case 4:
setDisplay("M56", false);
break;
case 6:
setDisplay("M78", false);
break;
}
}
function onPad(pad, isOn, velocity) {
var x = pad & 0x3;
var y = pad >> 2;
var noteIndex = x + (3 - y) * 4;
switch (tempMode) {
case Mode.Message:
switch (pad + 1) {
// tap tempo
case 4:
isOn ? transport.tapTempo() : false;
break;
// stop
case 9:
isOn ? transport.stop() : false;
break;
case 11:
isOn ? transport.incPosition(-8, false) : false;
break;
case 12:
isOn ? transport.incPosition(8, false) : false;
break;
// play
case 13:
isOn ? transport.stop() : transport.play();
break;
// pause / play
case 14:
isOn ? transport.play() : false;
break;
case 15:
isOn ? transport.rewind() : false;
break;
case 16:
isOn ? transport.fastForward() : false;
break;
}
break;
case Mode.Pedal:
if (isOn) {
pedal.offset = noteIndex;
pedal.velocity = velocity;
println([pedal.offset, pedal.velocity])
}
break;
}
}
function setDisplay(text, blink) {
var blinkHex = blink ? "01" : "00";
sendSysex("f0 42 40 6e 08 22 04 " + blinkHex + text.toHex(3) + "f7");
}
function setMode(m) {
mode = m;
switch (m) {
case Mode.Drum:
setDisplay("DRM", false);
noteInput.setKeyTranslationTable(keyTranslationTable);
break;
}
}
var isXYPressed = false;
function pressXY(isPressed) {
if (isPressed != isXYPressed) {
if (!isPressed && !hold) {
xyControls.getParameter(0).set(64, 128);
xyControls.getParameter(1).set(64, 128);
}
}
isXYPressed = isPressed;
}