-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPC-key25-mk2.js
More file actions
386 lines (314 loc) · 8.86 KB
/
APC-key25-mk2.js
File metadata and controls
386 lines (314 loc) · 8.86 KB
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
// Based on the APC Mini mk2 module by lenschcode
// https://github.com/lenschcode/APC-Mini-mkII-Chataigne-Module/tree/master
var knobNotes = [48, 49, 50, 51, 52, 53, 54, 55]; //CC
var buttonNotes = [
[32, 33, 34, 35, 36, 37, 38, 39],
[24, 25, 26, 27, 28, 29, 30, 31],
[16, 17, 18, 19, 20, 21, 22, 23],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[ 0, 1, 2, 3, 4, 5, 6, 7],
]; // Notes
var softkeyNotes = [82, 83, 84, 85, 86, 81,
64, 65, 66, 67, 68, 69, 70, 71, 98, 91, 93];
var softkeyNames = ["clipStop","solo","mute","recArm","select","stopAllClips",
"up","down","left","right","volume","pan","send","device","shift","play_Pause","record"];
var noteValueObj = [];
var noteColorObj = [];
var notePulsingObj = [];
var ccValueObj = [];
var knobValueObj = [];
var colorParameterObj = [];
var pulsingParameterObj = [];
var pulsingSteps = 128;
var pulsingTable = [0, 13, 26, 40, 53, 67, 80, 93, 107, 120, 134, 147, 161, 174, 187, 201, 214, 228, 255, 255, 252, 250, 247, 245, 243, 240, 238, 236, 233, 231, 229, 226, 224, 222, 219, 217, 215, 212, 210, 208, 205, 203, 201, 198, 196, 194, 191, 189, 187, 184, 182, 180, 177, 175, 173, 170, 168, 166, 163, 161, 159, 156, 154, 152, 149, 147, 145, 142, 140, 138, 135, 133, 131, 128, 126, 123, 121, 119, 116, 114, 112, 109, 107, 105, 102, 100, 98, 95, 93, 91, 88, 86, 84, 81, 79, 77, 74, 72, 70, 67, 65, 63, 60, 58, 56, 53, 51, 49, 46, 44, 42, 39, 37, 35, 32, 30, 28, 25, 23, 21, 18, 16, 14, 11, 9, 7, 4, 0];
var pulsingRate = 1.0;
var previousStep = -1;
// ------ Feedback Functions ------
function calcMSBLSB(value) {
var MSB = (value & 0xFF00) >> 8;
var LSB = value & 0x00FF;
return [MSB, LSB];
}
function sendButtonColorBrightness(id, color, brightness)
{
var note = buttonNotes[id[0]][id[1]];
var r = Math.round(((color[0] * 127) * brightness) / 255);
var g = Math.round(((color[1] * 127) * brightness) / 255);
var b = Math.round(((color[2] * 127) * brightness) / 255);
var rMSBLSB = calcMSBLSB(r);
var gMSBLSB = calcMSBLSB(g);
var bMSBLSB = calcMSBLSB(b);
script.log("Set Color for note " + note + " to rgb:{"+r+","+g+","+b+"} Intensity: "+brightness);
local.sendSysex(0x47, 0x7F, 0x4E, 0x24, 0x00, 0x09, note, note, rMSBLSB[0], rMSBLSB[1], gMSBLSB[0], gMSBLSB[1], bMSBLSB[0], bMSBLSB[1],0x00);
util.delayThreadMS(1);
}
function sendButtonColor(id, color)
{
var note = buttonNotes[id[0]][id[1]];
var r = Math.round(color[0] * 127);
var g = Math.round(color[1] * 127);
var b = Math.round(color[2] * 127);
var rMSBLSB = calcMSBLSB(r);
var gMSBLSB = calcMSBLSB(g);
var bMSBLSB = calcMSBLSB(b);
script.log("Set Color for note " + note + " to rgb:{"+r+","+g+","+b+"}");
local.sendSysex(0x47, 0x7F, 0x4E, 0x24, 0x00, 0x09, note, note, rMSBLSB[0], rMSBLSB[1], gMSBLSB[0], gMSBLSB[1], bMSBLSB[0], bMSBLSB[1],0x00);
util.delayThreadMS(1);
}
function sendButton(name, state)
{
index = softkeyNames.indexOf(name);
local.sendNoteOn(0,softkeyNotes[index],state ? 127 :0);
}
// ---- User Commands ----
function resendColors()
{
for(var i = 0; i < 8; i++)
{
for(var j = 0; j < 8; j++)
{
var color = colorParameterObj[i][j].get();
sendButtonColor([i,j], color);
}
}
}
function resync()
{
// Main Buttons
for (row = 0; row < 5; row++)
{
for (col = 0; col < 8; col ++)
{
var color = colorParameterObj[row][col].get();
var pulsing = pulsingParameterObj[row][col].get();
sendButtonColor([row, col], color);
util.delayThreadMS(1);
}
}
}
// ---- Module Common Functions ----
function init()
{
script.log("Setting up APC Key25 mk2");
// init variable
for (var i = 0; i < 5; i++)
{
colorParameterObj[i] = [];
pulsingParameterObj[i] = [];
}
// Knobs
for (var i = 0; i < 8; i++) {
knobValueObj[i] = local.values.knobs.getChild("knob" + (i+1));
ccValueObj[knobNotes[i]] = local.values.knobs.getChild("knob" + (i+1));
}
// Main Buttons
for (var i = 0; i < 5; i++)
{
for (var column = 0; column < 8; column++)
{
colorParameterObj[i][column] = local.parameters.colors.getChild("Main Pads").getChild("pad" + (i + 1) + (column + 1) + "");
pulsingParameterObj[i][column] = local.parameters.pulsing.getChild("Main Pads").getChild("pad" + (i + 1) + (column + 1) + "");
var note = buttonNotes[i][column];
noteValueObj[note] = local.values.getChild("Main Pads").getChild("pad" + (i + 1) + (column + 1) + "");
noteColorObj[note] = local.parameters.colors.getChild("Main Pads").getChild("pad" + (i + 1) + (column + 1) + "");
notePulsingObj[note] = local.parameters.pulsing.getChild("Main Pads").getChild("pad" + (i + 1) + (column + 1) + "");
}
}
//Softkeys
for ( var i = 0; i < softkeyNotes.length; i++)
{
var note = softkeyNotes[i];
noteValueObj[note] = local.values.softkeys.getChild(softkeyNames[i]);
noteColorObj[note] = local.parameters.colors.softkeys.getChild(softkeyNames[i]);
}
if (local.parameters.isConnected) resync();
}
// ------ Chataigne Events ------
var midiDeviceOutLast;
var resyncReady = false;
function moduleValueChanged(value) {
}
function moduleParameterChanged(param)
{
script.log("Parameter Changed: " + param.name + " Parent: " + param.getParent().name + " GrandParent: " + param.getParent().getParent().name + " Value: " + param.get());
if (param.name == "devices") {
var midiOutDevice = param.get()[1];
if (param.get()[1] != midiDeviceOutLast) {
midiDeviceOutLast = midiOutDevice;
if (midiOutDevice) {
script.log("New Midi out Device detected");
resyncReady = true;
}
}
}
// Color Feedback
if(param.getParent().getParent().name == "colors")
{
var color = param.get();
if(param.getParent().name == "mainPads")
{
var id = [(parseInt(param.name.charAt(3))-1), (parseInt(param.name.charAt(4))-1)];
sendButtonColor(id, color);
}
else if(param.getParent().name == "softkeys")
{
sendButton(param.name, param.get());
}
}
// Pulsing
if(param.getParent().getParent().name == "pulsing")
{
// if pulsing just got turned off send full brightness color again
if (param.get() == false){
if(param.getParent().name == "mainPads")
{
var id = [(parseInt(param.name.charAt(3))-1), (parseInt(param.name.charAt(4))-1)];
var color = colorParameterObj[id[0]][id[1]].get();
sendButtonColor(id, color);
}
}
}
}
function update(delta)
{
if (!local.parameters.isConnected) return;
if (resyncReady)
{
resyncReady = false;
resync();
}
var time = util.getTime(); // time in seconds
var step = Math.round(((time % pulsingRate) / pulsingRate) * pulsingSteps);
if (step !== previousStep)
{
var intensity = pulsingTable[step];
// script.log("Step: "+step+" intensity: "+intensity);
for (var i = 0; i < 5; i ++)
{
for (var j = 0; j < 8; j ++)
{
// script.log(i + " " + j);
if (pulsingParameterObj[i][j].get())
{
// scale color with intensity
var color = colorParameterObj[i][j].get();
sendButtonColorBrightness([i,j], color, intensity);
}
}
}
}
previousStep = step;
}
// ----- Midi Events ------
function noteOnEvent(channel, note, velocity)
{
if (channel != 1) return;
var button = noteValueObj[note];
if (button != undefined)
{
button.set((velocity==127));
}
}
function noteOffEvent(channel, note, velocity)
{
if (channel != 1) return;
var button = noteValueObj[note];
if (button != undefined)
{
button.set(false);
}
}
function ccEvent(channel, note, value)
{
if (channel != 1) return;
script.log(channel, note);
var knob = ccValueObj[note];
if (knob != undefined)
{
if(value>64){
value = -1* (128-value);
}
knob.set(knob.get()+(value/127));
}
}
function sysExEvent(data)
{
script.log("Sysex Message received, "+data.length+" bytes :");
}
// ----- User Functions ------
function setColorEnum(id, color)
{
var colorObj = colorParameterObj[id[0]][id[1]];
if (!!colorObj)
{
colorObj.set(color);
}
}
function setColorByNote(note, color)
{
var colorObj = noteColorObj[note];
if (!!colorObj)
{
colorObj.set(color);
}
}
function setColorByIndex(index, color)
{
var x = Math.floor(index / 8);
var y = index % 8;
var colorObj = colorParameterObj[x][y];
if (!!colorObj)
{
colorObj.set(color);
}
}
function setColorByPosition(x, y, color)
{
var colorObj = colorParameterObj[x-1][y-1];
if (!!colorObj)
{
colorObj.set(color);
}
}
function setPulsingEnum(id, pulsing)
{
var pulsingObj = pulsingParameterObj[id[0]][id[1]];
if (!!pulsingObj)
{
pulsingObj.set(pulsing);
}
}
function setPulsingByNote(note, pulsing)
{
var pulsingObj = notePulsingObj[note];
if (!!pulsingObj)
{
pulsingObj.set(pulsing);
}
}
function setPulsingByIndex(index, pulsing)
{
var x = Math.floor(index / 8);
var y = index % 8;
var pulsingObj = pulsingParameterObj[x][y];
if (!!pulsingObj)
{
pulsingObj.set(pulsing);
}
}
function setPulsingByPosition(x, y, pulsing)
{
var pulsingObj = pulsingParameterObj[x-1][y-1];
if (!!pulsingObj)
{
pulsingObj.set(pulsing);
}
}
function setSoftkeyLed(note, state)
{
var obj = noteColorObj[note];
if (!!obj)
{
obj.set(state);
}
}