-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyLab Essential.control.js
411 lines (344 loc) · 12.3 KB
/
KeyLab Essential.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
loadAPI(6);
load("lib/InputMIDI.js");
load("lib/InputDAW.js");
load("lib/MidiData.js");
host.setShouldFailOnDeprecatedUse(false);
host.defineController("Arturia", "KeyLab Essential 61", "0.1", "ddbaf2f0-93cd-468c-bf19-0cccdeb74e0b", "cellrok");
host.defineMidiPorts(2, 2);
if (host.platformIsWindows()) {
// TODO
// host.addDeviceNameBasedDiscoveryPair(["Input Port 0", "Input Port 1"], ["Output Port 0", "Output Port 1"]);
}
else if (host.platformIsMac()) {
host.addDeviceNameBasedDiscoveryPair(["Arturia KeyLab Essential 61 MIDI In", "Arturia KeyLab Essential 61 DAW In"], ["Arturia KeyLab Essential 61 MIDI Out", "Arturia KeyLab Essential 61 DAW Out"]);
}
else if (host.platformIsLinux()) {
// TODO
// host.addDeviceNameBasedDiscoveryPair(["Input Port 0", "Input Port 1"], ["Output Port 0", "Output Port 1"]);
}
// Send Text to KeyLab Display:
function sendTextToKeyLab(line1, line2) {
sendSysex("F0 00 20 6B 7F 42 04 00 60 01 " + line1.toHex(16) + " 00 02 " + line2.toHex(16) + " 00 F7");
}
var portMIDI, portMIDIinput;
var portDAW, portDAWoutput;
var BROWSER_IS_OPEN = 0;
var HOLDING_LIVEBANK = false;
var HOLDING_METRO = false;
var NUM_FILTERS = 1;
var NUM_FILTERS_ITEMS = 16;
var presetHasChanged = false;
var pageNames = [];
var browserColumns = [];
var browserColumnsItems = [];
var browserCursors = [];
var browserColumnName = '';
var browserCursorName = '';
// knob 1-9
var knobBank = [74, 71, 76, 77, 93, 18, 19, 16, 17];
var knobIndex = {74:0,71:1,76:2,77:3,93:4,18:5,19:6,16:7,17:8};
// fader 1-9
var faderBank = [73, 75, 79, 72, 80, 81, 82, 83, 85];
var faderIndex = {73:0,75:1,79:2,72:3,80:4,81:5,82:6,83:7,85:8};
var presetColumns = {};
function init() {
var p, i, j, item;
transport = host.createTransport();
portMIDI = host.getMidiInPort(0);
portDAW = host.getMidiInPort(1);
portMIDI.setMidiCallback(onMidi0);
portMIDI.setSysexCallback(onSysex0);
portDAW.setMidiCallback(onMidi1);
portDAW.setSysexCallback(onSysex1);
/**
* Instances
* -------------------------------------------------------
*/
portMIDIinput = host.getMidiInPort(0).createNoteInput("All Channels", "??????");
portMIDIinput.setShouldConsumeEvents(false);
//portMIDIinput.assignPolyphonicAftertouchToExpression(0, NoteExpression.TIMBRE_UP, 2);
application = host.createApplication();
masterTrack = host.createMasterTrack(0);
cursorTrack = host.createCursorTrack(3, 0);
cursorDevice = cursorTrack.createCursorDevice();
trackBank = host.createMainTrackBank(8, 0, 0);
deviceBrowser = cursorDevice.createDeviceBrowser(NUM_FILTERS,NUM_FILTERS_ITEMS);
presetBrowser = deviceBrowser.getPresetSession();
browserFilterBank = presetBrowser.createFilterBank(NUM_FILTERS);
remoteControls = cursorDevice.createCursorRemoteControlsPage(9);
userControls = host.createUserControls(9);
/**
* Browser Filter
* -------------------------------------------------------
*/
/**
* @type {API.BrowserFilterColumn}
*/
/**
for (i = 0; i < NUM_FILTERS; i++) {
browserColumns[i] = browserFilterBank.getItem(i);
browserColumns[i].addNameObserver(20, "", function(name){
presetColumns[name] = i;
if (BROWSER_IS_OPEN && name !== browserColumnName) {
sendTextToKeyLab(browserColumnName, browserCursorName);
}
browserColumnName = name;
});
browserColumnsItems[i] = browserColumns[i].createItemBank(NUM_FILTERS_ITEMS);
browserColumnsItems[i].addCanScrollDownObserver(function (canScroll) {
//println('browserColumnsItems canScroll ' + (canScroll ? 'yes' : 'no'));
});
browserCursors[i] = browserColumns[i].createCursorItem();
browserCursors[i].addValueObserver(20, "", function(name){
if (BROWSER_IS_OPEN && name !== browserCursorName) {
sendTextToKeyLab(browserColumnName, browserCursorName);
}
browserCursorName = name;
});
browserCursors[i].addValueObserver(20, '', function (itemName) {
//println('browserCursors addValueObserver ' + itemName);
});
for (j = 0; j < NUM_FILTERS_ITEMS; j++) {
item = browserColumnsItems[i].getItem(j);
item.isSelected().addValueObserver(function (exists) {
println('item isSelected ' + (exists ? 'yes' : 'no'));
});
item.addValueObserver(20, '', function (itemName) {
//println('browserColumnsItems addValueObserver ' + itemName);
});
}
}
*/
/**
* Remote Controls
* -------------------------------------------------------
*/
// initialize RemoteControlsPage
for (i = 0; i < 8; i++) {
p = remoteControls.getParameter(i).getAmount();
p.setIndication(true);
p.setLabel("P" + (i + 1));
}
// initialize UserControlBank
for (i = 0; i < faderBank.length; i++) {
userControls.getControl(i).setLabel("CC" + faderBank[i]);
}
/**
* Observers
* -------------------------------------------------------
*/
deviceBrowser.addIsBrowsingObserver(function (status) {
BROWSER_IS_OPEN = status;
});
transport.tempo().addRawValueObserver(function (status){
sendTextToKeyLab("Transport:", 'Tempo ' + transport.tempo().value().getRaw() + ' BPM');
});
transport.addIsRecordingObserver(function (status) {
if (status) {
sendTextToKeyLab("Transport:", "Record Enabled");
} else {
sendTextToKeyLab("Transport:", "Record Disabled");
}
});
/**
* Say hello
* -------------------------------------------------------
*/
sendTextToKeyLab('Bitwig', 'Essential');
host.showPopupNotification("KeyLab Essential 61 plugged in");
}
// -------------------------------------------------------------------
// GENERAL MIDI INPUT
// -------------------------------------------------------------------
function onMidi0(status, data1, data2) {
var index, midi = new MidiData(status, data1, data2);
if (midi.isChannelController())
{
// -------------------------------------------------------------------
// DEVICE MACRO CC (knobs)
// -------------------------------------------------------------------
if (typeof knobIndex[data1] !== 'undefined') {
index = knobIndex[data1];
if (index === 8) {
masterTrack.getPan().set(data2, 128);
} else {
if (HOLDING_LIVEBANK) {
trackBank.getTrack(index).getPan().set(data2, 128);
} else {
remoteControls.getParameter(index).getAmount().value().set(data2, 128);
}
}
}
// -------------------------------------------------------------------
// USER CC (faders)
// -------------------------------------------------------------------
if (typeof faderIndex[data1] !== 'undefined') {
index = faderIndex[data1];
if (index === 8) {
masterTrack.getVolume().set(data2, 128);
} else {
if (HOLDING_LIVEBANK) {
trackBank.getTrack(index).getVolume().set(data2, 128);
} else {
userControls.getControl(index).value().set(data2, 128);
}
}
}
// -------------------------------------------------------------------
// PRESET WHEEL
// -------------------------------------------------------------------
if (midi.isPressed()) {
switch (data1)
{
case InputMIDI.CC_JOG_PRESS_CATEGORY: // open browser and focus "Category" column
println('CC_JOG_PRESS_CATEGORY');
if (BROWSER_IS_OPEN) {
deviceBrowser.commitSelectedResult();
} else {
deviceBrowser.startBrowsing();
presetBrowser.activate();
application.arrowKeyDown();
}
break;
case InputMIDI.CC_JOG_PRESS_PRESET: // open browser and focus "Preset" column
println('CC_JOG_PRESS_PRESET');
if (BROWSER_IS_OPEN) {
deviceBrowser.commitSelectedResult();
} else {
deviceBrowser.startBrowsing();
presetBrowser.activate();
application.arrowKeyDown();
}
break;
case InputMIDI.CC_BTN_ARROW_LEFT: // move between filter columns
println('CC_BTN_ARROW_LEFT');
application.arrowKeyUp();
break;
case InputMIDI.CC_BTN_ARROW_RIGHT: // move between filter columns
println('CC_BTN_ARROW_RIGHT');
application.arrowKeyDown();
break;
case InputMIDI.CC_BTN_CATEGORY: // select column "Category" and focus first item
println('CC_BTN_CATEGORY');
break;
case InputMIDI.CC_BTN_PRESET: // select column "Preset" and focus first item
println('CC_BTN_PRESET');
break;
case InputMIDI.CC_BTN_BANK_NEXT:
remoteControls.selectNextPage(false);
break;
case InputMIDI.CC_BTN_BANK_PREV:
remoteControls.selectPreviousPage(false);
break;
case InputMIDI.CC_BTN_BANK_LIVE:
HOLDING_LIVEBANK = true; // Used as "SHIFT" for Faders and Knobs
break;
}
}
else if (midi.isReleased()) { // button release
switch (data1)
{
case InputMIDI.CC_BTN_BANK_LIVE:
HOLDING_LIVEBANK = false;
break;
}
}
// -------------------------------------------------------------------
// PRESET WHEEL MOVEMENT
// -------------------------------------------------------------------
if (BROWSER_IS_OPEN) {
// wheel turned right
if ((data1 === InputMIDI.CC_JOG_DIAL_CATEGORY && midi.isOff()) || (data1 === InputMIDI.CC_JOG_DIAL_PRESET && midi.isOff())) {
application.arrowKeyUp();
println('arrow key up (wheel right)');
}
// wheel turned left
if ((data1 === InputMIDI.CC_JOG_DIAL_CATEGORY && midi.isOn()) || (data1 === InputMIDI.CC_JOG_DIAL_PRESET && midi.isOn())) {
application.arrowKeyDown();
println('arrow key down (wheel left)');
}
}
// -------------------------------------------------------------------
// TEMPO WHEEL (when holding down METRO button)
// -------------------------------------------------------------------
if (HOLDING_METRO) { // wheel adjusts tempo
// wheel turned right
if ((data1 === InputMIDI.CC_JOG_DIAL_CATEGORY && midi.isOff()) || (data1 === InputMIDI.CC_JOG_DIAL_PRESET && midi.isOff())) {
transport.tempo().value().setRaw(transport.tempo().value().getRaw() - 1);
}
// wheel turned left
if ((data1 === InputMIDI.CC_JOG_DIAL_CATEGORY && midi.isOn()) || (data1 === InputMIDI.CC_JOG_DIAL_PRESET && midi.isOn())) {
transport.tempo().value().setRaw(transport.tempo().value().getRaw() + 1);
}
}
}
}
// -------------------------------------------------------------------
// SYSEX
// -------------------------------------------------------------------
function onSysex0(data) {
switch (data) {
case InputMIDI.SYSEX_BTN_MIDI_CH: // midi channel button
break;
}
}
// -------------------------------------------------------------------
// DAW COMMAND CENTER
// -------------------------------------------------------------------
function onMidi1(status, data1, data2) {
var midi = new MidiData(status, data1, data2);
println(midi.type() + ' ' + data1 + ' ' + data2);
if (midi.isPressed()) {
switch (data1)
{
case InputDAW.CC_SAVE:
break;
case InputDAW.CC_UNDO:
application.undo();
break;
case InputDAW.CC_LOOP:
transport.isArrangerLoopEnabled().toggle();
break;
case InputDAW.CC_PUNCH:
transport.isArrangerOverdubEnabled().toggle();
transport.isClipLauncherOverdubEnabled().toggle();
break;
case InputDAW.CC_METRONOME:
transport.isMetronomeEnabled().toggle();
HOLDING_METRO = true;
break;
case InputDAW.CC_REWIND:
transport.rewind();
break;
case InputDAW.CC_FORWARD:
transport.fastForward();
break;
case InputDAW.CC_STOP:
// println("Stop");
transport.stop();
break;
case InputDAW.CC_PLAY:
// println("Play/Pause");
transport.play();
break;
case InputDAW.CC_RECORD:
// println("Record");
transport.record();
break;
}
}
else if (midi.isReleased()) {
switch (data1)
{
case InputDAW.CC_METRONOME:
HOLDING_METRO = false;
break;
}
}
}
function onSysex1(data) {
}
function flush() {
}
function exit() {
}