-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunchpad_controller_object.js
624 lines (512 loc) · 14.3 KB
/
launchpad_controller_object.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/**
* Copyright 2014-2015 Alan Drees
*
* Purpose:
* Stripped-down core implementation of the Launchpad script.
*
* Dependencies:
* launchpad_constants.js
* launchpad_grid.js
*/
var Launchpad = Launchpad || {};
/**\fn Launchpad.LaunchpadController
*
* LaunchpadController consutructor
*
* @param options options object to set the options of the controller for
* @param instance controller instance
*
* @returns None
*/
Launchpad.LaunchpadController = function(options, instance)
{
var self = this;
this.set_options(options);
this.instance = instance;
this.arm = initArray(0, this.options.tracks);
this.trackExists = initArray(0, this.options.tracks);
this.isSelected = initArray(0, this.options.tracks);
this.hasContent = initArray(0, this.options.channels * this.options.scenes);
this.isPlaying = initArray(0, this.options.channels * this.options.scenes);
this.isRecording = initArray(0, this.options.channels * this.options.scenes);
this.isQueued = initArray(0, this.options.channels * this.options.scenes);
this.launchpad_io = Array(this.options.devices);
this.io_grid_array = Launchpad.create_2d_array(this.options.layout_order.length,
this.options.layout_order[0].length);
this.gridPage = new Launchpad.GridPage(this);
var lp_io;
var lp_io_counter = 0;
for(var x = 0; x < this.options.layout_order.length; x++)
{
for(var y = 0; y < this.options.layout_order[0].length; y++)
{
lp_io = new Launchpad.LaunchpadIO(this.options,
x,
y,
lp_io_counter);
this.launchpad_io[lp_io_counter++] = lp_io;
this.io_grid_array[x][y] = lp_io;
}
}
//these coordinates correspond to the top left corner of the highlighting section
this.activePage = {};
this.logoPhase = 0;
this.showStealthascopeLogo = false;
this.force_optimized_flush = false;
}
/**\fn Launchpad.LaunchpadController.prototype.init
*
* Initalization function
*
* @param banks banks object generated externally
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.init = function(banks)
{
var self = this;
this.banks = {};
if(typeof banks === 'undefined')
{
this.banks.transport = host.createTransport();
this.banks.application = host.createApplication();
this.banks.trackbank = host.createMainTrackBank(this.options.tracks,
0,
this.options.scenes);
}
else
{
this.banks = banks;
}
for(var c = 0; c < this.options.channels; c++)
{
var track = this.banks.trackbank.getChannel(c);
track.getArm().addValueObserver(this.getChannelObserverFunc(c, this.arm));
track.exists().addValueObserver(this.getChannelObserverFunc(c, this.trackExists));
track.addIsSelectedInEditorObserver(this.getChannelObserverFunc(c, this.isSelected));
var cliplauncher = track.getClipLauncherSlots();
cliplauncher.addHasContentObserver(this.getGridObserverFunc(c, this.options.channels, this.hasContent));
cliplauncher.addIsPlayingObserver(this.getGridObserverFunc(c, this.options.channels, this.isPlaying));
cliplauncher.addIsRecordingObserver(this.getGridObserverFunc(c, this.options.channels, this.isRecording));
cliplauncher.addIsPlaybackQueuedObserver(this.getGridObserverFunc(c, this.options.channels, this.isQueued));
}
this.banks.trackbank.addCanScrollChannelsUpObserver(function(canScroll)
{
self.gridPage.canScrollTracksUp = canScroll;
});
this.banks.trackbank.addCanScrollChannelsDownObserver(function(canScroll)
{
self.gridPage.canScrollTracksDown = canScroll;
});
this.banks.trackbank.addCanScrollScenesUpObserver(function(canScroll)
{
self.gridPage.canScrollScenesUp = canScroll;
});
this.banks.trackbank.addCanScrollScenesDownObserver(function(canScroll)
{
self.gridPage.canScrollScenesDown = canScroll;
});
for(var i = 0; i < this.launchpad_io.length; i++)
{
this.launchpad_io[i].init(self);
}
if(this.showStealthascopeLogo)
{
this.animateLogo();
}
this.setActivePage(this.gridPage);
}
/**\fn Launchpad.LaunchpadController.prototype.setActivePage
*
* This function takes a page object, and makes it the currently active page
*
* @param page new page to change to
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.setActivePage = function(page)
{
var isInit = this.activePage == null;
if (page != this.activePage)
{
this.activePage = page;
// Update indications in the app
for(var p = 0; p < this.options.channels; p++)
{
var track = this.banks.trackbank.getChannel(p);
track.getClipLauncherSlots().setIndication(this.activePage == this.gridPage);
}
}
}
/**\fn Launchpad.LaunchpadController.prototype.getChannelObserverFunc
*
* Creates a closure for the channel observer functions
*
* @param channel (integer) track with which to run the callback on
* @param varToStore (array) variable to reference with respect to the track
*
* @returns (function) function which will be passed to the channel observer function
*/
Launchpad.LaunchpadController.prototype.getChannelObserverFunc = function(channel, varToStore)
{
return function(value)
{
varToStore[channel] = value;
}
}
/**\fn Launchpad.LaunchpadController.prototype.getGridObserverFunc
*
* Creates a closure for the grid observer functions
*
* @param track (integer) track with which to run the callback on
* @param width (integer) width of the controller in grid units
* @param varToStore (array) array to store the value in
*
* @returns (function) function which will be passed to the grid observer function
*/
Launchpad.LaunchpadController.prototype.getGridObserverFunc = function(track, width, varToStore)
{
return function(scene, value)
{
varToStore[scene * width + track ] = value;
}
}
/**\fn Launchpad.LaunchpadController.prototype.animateLogo
*
* Animation callback function for the startup animation passed to the task scheduler
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.animateLogo = function()
{
var self = this;
if (this.logoPhase > 7)
{
this.setDutyCycle(2, 6);
return;
}
else if (this.logoPhase > 6)
{
this.showStealthascopeLogo = false;
var i = 0.5 - 0.5 * Math.cos(this.logoPhase * Math.PI);
this.setDutyCycle(Math.floor(1 + 5 * i), 18);
}
else
{
var i = 0.5 - 0.5 * Math.cos(this.logoPhase * Math.PI);
this.setDutyCycle(Math.floor(1 + 15 * i), 18);
}
this.logoPhase += 0.2;
host.scheduleTask(function(){self.animateLogo()}, null, 90);
}
/**\fn Launchpad.LaunchpadController.prototype.exit
*
* Exit function for the controller
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.exit = function()
{
for(var i = 0; i < this.launchpad_io.length; i++)
{
this.launchpad_io[i].resetDevice();
}
}
/**\fn Launchpad.LaunchpadController.prototype.flush
*
* Updates the controller with new data
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.flush = function()
{
if (this.showStealthascopeLogo)
{
this.drawStealthascopeLogo();
}
else
{
if(typeof this.activePage.updateOutputState === 'function')
{
this.activePage.updateOutputState();
}
}
/*TODO: ADD LOGIC TO DETERMINE IF ONLY ONE CONTROLLER REQUIRES UPDATING*/
for(var i = 0; i < this.launchpad_io.length; i++)
{
this.launchpad_io[i].flushLEDs();
}
}
/**\fn Launchpad.LaunchpadController.prototype.setCellLED
*
* Interface method for the controller IO subsystem to manipulate the state
* of the grid cell LEDs
*
* @param column (integer) column index of LED
* @param row (integer) row index of LED
* @param value (integer) LED value to set
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.setCellLED = function(column, row, value)
{
var output = this._map_io_object(column, row);
column = column % Launchpad.NUM_SCENES;
row = row % Launchpad.NUM_CHANNELS;
output.setCellLED(column,
row,
value);
}
/**\fn Launchpad.LaunchpadController.prototype.setTopLED
*
* Interface method for the controller IO subsystem to manipulate the state
* of the top (mode) LEDs
*
* @param index (integer) index of top-row CC to set
* @param value (integer) value to set the LED to
* @param ctrl (integer) controller to output to [OPTIONAL]
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.setTopLED = function(index, value, ctrl)
{
if(typeof ctrl === 'undefined'){var ctrl = 0;}
if(this.options.shared_navigation)
{
for(var i = 0; i < this.launchpad_io.length; i++)
{
this.launchpad_io[i].setTopLED(index, value);
}
}
else
{
this.launchpad_io[ctrl].setTopLED(index, value);
}
}
/**\fn Launchpad.LaunchpadController.prototype.setRightLED
*
* Interface method for the controller IO subsystem to manipulate the state
* of the right-side LEDs
*
* @param index (integer) control index to update on the controller
* @param value (integer) value to set the control to
* @param ctrl (integer) controller
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.setRightLED = function(index, value, ctrl)
{
if(typeof ctrl === 'integer')
{
this.launchpad_io[ctrl].setRightLED(index, value);
}
else
{
for(var i = 0; i < this.launchpad_io.length; i++)
{
this.launchpad_io[i].setRightLED(index, value);
}
}
}
/**\fn Launchpad.LaunchpadController.prototype.clear
*
* Interface method for the controller IO subsystem to clear an entire controller grid of values
*
* @param index (integer) controller index to clear [OPTIONAL]
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.clear = function(index)
{
if(typeof index === 'undefined')
{
for(var i = 0; i < this.launchpad_io.length; i++)
{
this.launchpad_io[i].clear();
}
}
else
{
this.launchpad_io[i].clear();
}
}
/**\fn Launchpad.LaunchpadController.prototype._map_io_object
*
* Maps grid coordinates to a particularly underlying physical controller object
*
* @param grid_x (integer) x-coordinate of the vector pair
* @param grid_y (integer) y-coordinate of the vector pair
*
* @returns (LaunchpadIO) LaunchpadIO object representing a physical controller
*/
Launchpad.LaunchpadController.prototype._map_io_object = function(grid_x, grid_y)
{
var ctrl_x = 0;
var ctrl_y = 0;
if(this.launchpad_io.length === 1)
{
return this.launchpad_io[0];
}
else
{
if((grid_x) % Launchpad.NUM_TRACKS == 0)
{
ctrl_x = grid_x / Launchpad.NUM_TRACKS;
}
else
{
ctrl_x = ( grid_x - ((grid_x) % Launchpad.NUM_TRACKS) ) / Launchpad.NUM_TRACKS
}
if((grid_y) % Launchpad.NUM_SCENES == 0)
{
ctrl_y = grid_y / Launchpad.NUM_SCENES;
}
else
{
ctrl_y = ( grid_y - ((grid_y) % Launchpad.NUM_SCENES) ) / Launchpad.NUM_SCENES
}
return this.io_grid_array[ctrl_x][ctrl_y];
}
}
/**\fn Launchpad.LaunchpadController.prototype.drawStealthascopeLogo
*
* Draws the Stealthascope logo
*
* @param None
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.drawStealthascopeLogo = function()
{
var left_offset = 1;
var right_limit = 6;
var c = Launchpad.mixColour(7, 1, false);
//vertical
for(var i = left_offset; i < right_limit - 1; i++){
this.setCellLED(i, 1, c);
}
//left
for(var i = 2; i < 4; i++){
this.setCellLED(left_offset,i,c)
}
//vertical
for(var i = left_offset; i < right_limit -1 ; i++){
this.setCellLED(i, 4, c);
}
//right
for(var i = 5; i < 7; i++){
this.setCellLED(right_limit - 2,i,c);
}
//bottom
for(var i = left_offset; i < right_limit - 1; i++){
this.setCellLED(i, 7, c);
}
}
/**\fn Launchpad.LaunchpadController.prototype.onMidi
*
* Callback function to be called when midi input is detected
*
* @param status (integer) status byte (type/channel) of incoming message
* @param data1 (integer) data1 byte of incoming message
* @param data2 (integer) data2 byte of incoming message
* @param grid_x (integer) x-index of hardware component
* @param grid_y (integer) y-index of hardware component
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.onMidi = function(status, data1, data2, grid_x, grid_y)
{
if (MIDIChannel(status) != 0) return;
if (isChannelController(status))
{
var isPressed = data2 > 0;
switch(data1)
{
case Launchpad.TopButton.SESSION:
if (isPressed)
{
if(this.activePage.name !== "grid"){
this.setActivePage(this.gridPage);
}
else
{
this.gridPage.setTempMode(Launchpad.TEMPMODE.TRACK);
}
}
else
{
this.gridPage.setTempMode(Launchpad.TEMPMODE.OFF);
}
break;
case Launchpad.TopButton.USER1:
if (isPressed)
{
this.gridPage.setTempMode(Launchpad.TEMPMODE.STOP);
}
else
{
this.gridPage.setTempMode(Launchpad.TEMPMODE.OFF);
}
break;
case Launchpad.TopButton.USER2:
break;
case Launchpad.TopButton.MIXER:
this.activePage.onMixerButton(isPressed);
break;
case Launchpad.TopButton.CURSOR_LEFT:
this.activePage.onLeft(isPressed);
break;
case Launchpad.TopButton.CURSOR_RIGHT:
this.activePage.onRight(isPressed);
break;
case Launchpad.TopButton.CURSOR_UP:
this.activePage.onUp(isPressed);
break;
case Launchpad.TopButton.CURSOR_DOWN:
this.activePage.onDown(isPressed);
break;
}
}
if (isNoteOn(status) || isNoteOff(status, data2))
{
var row = data1 >> 4;
var column = data1 & 0xF;
if (column < Launchpad.NUM_TRACKS)
{
row = (row + (grid_y * Launchpad.NUM_SCENES));
column = (column + (grid_x * Launchpad.NUM_TRACKS));
this.activePage.onGridButton(row, column, data2 > 0);
}
else
{
this.activePage.onSceneButton(row + (grid_x * Launchpad.NUM_TRACKS), data2 > 0);
}
}
}
/**\fn Launchpad.LaunchpadController.prototype.set_options
*
* Sets controller options
*
* @param options options with which to set (use defaults if not set)
*
* @returns None
*/
Launchpad.LaunchpadController.prototype.set_options = function(options)
{
this.options = { 'tracks' : Launchpad.NUM_TRACKS,
'scenes' : Launchpad.NUM_SCENES,
'instance' : 1 };
if(typeof options === 'object')
{
for(var option in options)
{
this.options[option] = options[option];
}
}
}