forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraktor-Kontrol-S4-MK3.js
More file actions
3872 lines (3604 loc) · 153 KB
/
Copy pathTraktor-Kontrol-S4-MK3.js
File metadata and controls
3872 lines (3604 loc) · 153 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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// Created by Be <be@mixxx.org> and A. Colombier <mixxx@acolombier.dev>
const LedColors = {
off: 0,
red: 4,
carrot: 8,
orange: 12,
honey: 16,
yellow: 20,
lime: 24,
green: 28,
aqua: 32,
celeste: 36,
sky: 40,
blue: 44,
purple: 48,
fuscia: 52,
magenta: 56,
azalea: 60,
salmon: 64,
white: 68,
};
const LedColorMap = {
0xCC0000: LedColors.red,
0xCC5E00: LedColors.carrot,
0xCC7800: LedColors.orange,
0xCC9200: LedColors.honey,
0xCCCC00: LedColors.yellow,
0x81CC00: LedColors.lime,
0x00CC00: LedColors.green,
0x00CC49: LedColors.aqua,
0x00CCCC: LedColors.celeste,
0x0091CC: LedColors.sky,
0x0000CC: LedColors.blue,
0xCC00CC: LedColors.purple,
0xAD65FF: LedColors.fuscia,
0xCC0079: LedColors.magenta,
0xCC477E: LedColors.azalea,
0xCC4761: LedColors.salmon,
0xCCCCCC: LedColors.white,
};
// This define the sequence of color to use for pad button when in keyboard mode. This should make them look like an actual keyboard keyboard octave, except for C, which is green to help spotting it.
const KeyboardColors = [
LedColors.green,
LedColors.off,
LedColors.white,
LedColors.off,
LedColors.white,
LedColors.white,
LedColors.off,
LedColors.white,
LedColors.off,
LedColors.white,
LedColors.off,
LedColors.white,
];
// Constant used to define custom default pad layout
const DefaultPadLayoutHotcue = "hotcue";
const DefaultPadLayoutSamplerBeatloop = "samplerBeatloop";
const DefaultPadLayoutKeyboard = "keyboard";
/*
* USER CONFIGURABLE SETTINGS
* Change settings in the preferences
*/
const DeckColors = [
LedColors[engine.getSetting("deckA")] || LedColors.red,
LedColors[engine.getSetting("deckB")] || LedColors.blue,
LedColors[engine.getSetting("deckC")] || LedColors.yellow,
LedColors[engine.getSetting("deckD")] || LedColors.purple,
];
const LibrarySortableColumns = [
engine.getSetting("librarySortableColumns1Value"),
engine.getSetting("librarySortableColumns2Value"),
engine.getSetting("librarySortableColumns3Value"),
engine.getSetting("librarySortableColumns4Value"),
engine.getSetting("librarySortableColumns5Value"),
engine.getSetting("librarySortableColumns6Value"),
].map(c => parseInt(c)).filter(c => c); // Filter '0' column, equivalent to '---' value in the UI or disabled
const LoopWheelMoveFactor = engine.getSetting("loopWheelMoveFactor") || 50;
const LoopEncoderMoveFactor = engine.getSetting("loopEncoderMoveFactor") || 500;
const LoopEncoderShiftMoveFactor = engine.getSetting("loopEncoderShiftMoveFactor") || 2500;
const TempoFaderSoftTakeoverColorLow = LedColors[engine.getSetting("tempoFaderSoftTakeoverColorLow")] || LedColors.white;
const TempoFaderSoftTakeoverColorHigh = LedColors[engine.getSetting("tempoFaderSoftTakeoverColorHigh")] || LedColors.green;
// Tempo fader center snap range
// Transform user value (mm) into upper/lower values
const TempoCenterRangeMm = engine.getSetting("tempoCenterRangeMm") || 1.0;
// In theory we have an 80 mm fader. Though, the usable range appears to be only ~77 mm.
// Value range is 0..4096, but we only get 510 steps with 8/9 ticks resolution
// and therefore need to make sure the center zone can actually be reached.
// A diff of 15 should be safe, this corresponds to .3 mm.
const TempoFaderTicksPerMm = 4096 / 77; // 53.1948..
const TempoCenterRangeTicks = TempoFaderTicksPerMm * TempoCenterRangeMm;
// Value center may be off the labeled center.
// Use this setting to compensate per device.
const TempoCenterValueOffsetLeft = (engine.getSetting("tempoCenterOffsetMmLeft") || 0.0) * TempoFaderTicksPerMm;
const TempoCenterValueOffsetRright = (engine.getSetting("tempoCenterOffsetMmRight") || 0.0) * TempoFaderTicksPerMm;
const TempoCenterUpperLeft = (4096 / 2) + (TempoCenterRangeTicks / 2) + TempoCenterValueOffsetLeft;
const TempoCenterLowerLeft = (4096 / 2) - (TempoCenterRangeTicks / 2) + TempoCenterValueOffsetLeft;
const TempoCenterUpperRight = (4096 / 2) + (TempoCenterRangeTicks / 2) + TempoCenterValueOffsetRright;
const TempoCenterLowerRight = (4096 / 2) - (TempoCenterRangeTicks / 2) + TempoCenterValueOffsetRright;
// Define whether or not to keep LED that have only one color (reverse, flux, play, shift) dimmed if they are inactive.
// 'true' will keep them dimmed, 'false' will turn them off. Default: true
const InactiveLightsAlwaysBacklit = !!engine.getSetting("inactiveLightsAlwaysBacklit");
// Keep both deck select buttons backlit and do not fully turn off the inactive deck button.
// 'true' will keep the unselected deck dimmed, 'false' to fully turn it off. Default: true
const DeckSelectAlwaysBacklit = !!engine.getSetting("deckSelectAlwaysBacklit");
// Define whether the keylock is mapped when doing "shift+master" (on press) or "shift+sync" (on release since long push copies the key)".
// 'true' will use "sync+master", 'false' will use "shift+sync". Default: false
const UseKeylockOnMaster = !!engine.getSetting("useKeylockOnMaster");
// Define whether the grid button would blink when the playback is going over a detected beat. Can help to adjust beat grid.
// Default: false
const GridButtonBlinkOverBeat = !!engine.getSetting("gridButtonBlinkOverBeat");
// Wheel led blinking if reaching the end of track warning (default 30 seconds, can be changed in the settings, under "Waveforms" > "End of track warning").
// Default: true
const WheelLedBlinkOnTrackEnd = !!engine.getSetting("wheelLedBlinkOnTrackEnd");
// When shifting either decks, the mixer will control microphones or auxiliary lines. If there is both a mic and an configure on the same channel, the mixer will control the auxiliary.
// Default: false
const MixerControlsMixAuxOnShift = !!engine.getSetting("mixerControlsMicAuxOnShift");
// Make the sampler tab a beatlooproll tab instead
// Default: false
const UseBeatloopRollInsteadOfSampler = !!engine.getSetting("useBeatloopRollInsteadOfSampler");
// Predefined beatlooproll sizes.
const BeatLoopRolls = [
engine.getSetting("beatLoopRollsSize1") || 1/8,
engine.getSetting("beatLoopRollsSize2") || 1/4,
engine.getSetting("beatLoopRollsSize3") || 1/2,
engine.getSetting("beatLoopRollsSize4") || 1,
engine.getSetting("beatLoopRollsSize5") || 2,
engine.getSetting("beatLoopRollsSize6") || 4,
engine.getSetting("beatLoopRollsSize7") || "half",
engine.getSetting("beatLoopRollsSize8") || "double"
];
// Predefined beatjump.
const BeatJumps = [
engine.getSetting("beatJumpSize1"), // Default to 1
engine.getSetting("beatJumpSize2"), // Default to 2
engine.getSetting("beatJumpSize3"), // Default to 4
engine.getSetting("beatJumpSize4"), // Default to 8
engine.getSetting("beatJumpSize5"), // Default to 16
engine.getSetting("beatJumpSize6"), // Default to 32
engine.getSetting("beatJumpSize7"), // Default to 64
engine.getSetting("beatJumpSize8"), // Default to "beatjump"
];
// Define the speed of the jogwheel. This will impact the speed of the LED playback indicator, the scratch, and the speed of
// the motor if enable. Recommended value are 33 + 1/3 or 45.
// Default: 33 + 1/3
const BaseRevolutionsPerMinute = engine.getSetting("baseRevolutionsPerMinute") || 33 + 1/3;
// Define whether or not to use motors.
// This is a BETA feature! Please use at your own risk. Setting this off means that below settings are inactive
// Default: false
const UseMotors = !!engine.getSetting("useMotors");
// Define whether or not the jog wheel plater provide a haptic feedback when going over the cue point.
// Default: true
const CueHapticFeedback = UseMotors && !!engine.getSetting("cueHapticFeedback");
// Define how much the wheel will resist. It is a similar setting that the Grid+Wheel in Tracktor
// Value must defined between 0 to 1. 0 is very tight, 1 is very loose.
// Default: 0.5
const TightnessFactor = engine.getSetting("tightnessFactor") || 0.5;
// Define how much force can the motor use. This defines how much the wheel will "fight" you when you block it in TT mode
// This will also affect how quick the wheel starts spinning when enabling motor mode, or starting a deck with motor mode on
const MaxWheelForce = engine.getSetting("maxWheelForce") || 25000; // Traktor seems to cap the max value at 60000, which just sounds insane
// Map the mixer potentiometers to different components of the software mixer in Mixxx, on top of the physical control of the hardware
// mixer embedded in the S4 Mk3. This is useful if you are not using certain S4 Mk3 outputs.
const SoftwareMixerMain = !!engine.getSetting("softwareMixerMain");
const SoftwareMixerBooth = !!engine.getSetting("softwareMixerBooth");
const SoftwareMixerHeadphone = !!engine.getSetting("softwareMixerHeadphone");
// Define custom default layout used by the pads, instead of intro/outro and first 4 hotcues.
const DefaultPadLayout = engine.getSetting("defaultPadLayout");
// The LEDs only support 16 base colors. Adding 1 in addition to
// the normal 2 for Button.prototype.brightnessOn changes the color
// slightly, so use that get 25 different colors to include the Filter
// button as a 5th effect chain preset selector.
const QuickEffectPresetColors = [
LedColors.red,
LedColors.green,
LedColors.blue,
LedColors.yellow,
LedColors.orange,
LedColors.purple,
LedColors.white,
LedColors.magenta,
LedColors.azalea,
LedColors.salmon,
LedColors.red + 1,
LedColors.sky,
LedColors.celeste,
LedColors.fuscia,
LedColors.blue + 1,
LedColors.carrot,
LedColors.honey,
LedColors.yellow + 1,
LedColors.lime,
LedColors.aqua,
LedColors.purple + 1,
LedColors.magenta + 1,
LedColors.azalea + 1,
LedColors.salmon + 1,
LedColors.fuscia + 1,
];
// assign samplers to the crossfader on startup
const SamplerCrossfaderAssign = true;
const MotorWindUpMilliseconds = 1200;
const MotorWindDownMilliseconds = 900;
/*
* Kontrol S4 Mk3 hardware-specific constants
*/
const wheelRelativeMax = 2 ** 32 - 1;
const wheelAbsoluteMax = 2879;
const wheelTimerMax = 2 ** 32 - 1;
const baseRevolutionsPerSecond = BaseRevolutionsPerMinute / 60;
// The active tab ID. This is used when SharedDataAPI is active, to communicate with the screens which tab is currently selected.
const ActiveTabPadID = {
jump: 1,
hotcue: 2,
roll: 3,
samples: 4,
loop: 5,
mute: 7,
record: 8,
tone: 11,
fxbank1: 12,
fxbank2: 13,
};
const wheelLEDmodes = {
off: 0,
dimFlash: 1,
spot: 2,
ringFlash: 3,
dimSpot: 4,
individuallyAddressable: 5, // set byte 4 to 0 and set byes 8 - 40 to color values
};
// The mode available, which the wheel can be used for.
const wheelModes = {
jog: 0,
vinyl: 1,
motor: 2,
loopIn: 3,
loopOut: 4,
};
const moveModes = {
beat: 0,
bpm: 1,
grid: 2,
keyboard: 3,
hotcueColor: 4,
};
// tracks state across input reports
let wheelTimer = null;
// This is a global variable so the S4Mk3Deck Components have access
// to it and it is guaranteed to be calculated before processing
// input for the Components.
let wheelTimerDelta = 0;
/*
* helper function
*/
const quickFxChannel = (group) => {
return `[QuickEffectRack1_${group}]`;
};
const stemChannel = (group, idx) => {
return `${group.substr(0, group.length - 1)}_Stem${idx + 1}]`;
};
const isObject = (item) => {
return (item && typeof item === "object" && !Array.isArray(item));
};
const mergeDeep = (target, ...sources) => {
if (!sources.length) { return target; }
const source = sources.shift();
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) { Object.assign(target, {[key]: {}}); }
mergeDeep(target[key], source[key]);
} else {
Object.assign(target, {[key]: source[key]});
}
}
}
return mergeDeep(target, ...sources);
};
const hasRuntimeDataAPI = () => typeof engine.getSharedData === "function";
const updateRuntimeData = (patch) => {
if (!hasRuntimeDataAPI()) {
return;
}
engine.setSharedData(mergeDeep(engine.getSharedData() || {}, patch));
};
/*
* HID report parsing library
*/
class HIDInputReport {
constructor(reportId) {
this.reportId = reportId;
this.fields = [];
}
registerCallback(callback, byteOffset, bitOffset = 0, bitLength = 1, defaultOldData = undefined) {
if (typeof callback !== "function") {
throw Error("callback must be a function");
}
if (!Number.isInteger(byteOffset)) {
throw Error("byteOffset must be 0 or a positive integer");
}
if (!Number.isInteger(bitOffset) || bitOffset < 0) {
throw Error("bitOffset must be 0 or a positive integer");
}
if (!Number.isInteger(bitOffset) || bitLength < 1 || bitLength > 32) {
throw Error("bitLength must be an integer between 1 and 32");
}
const field = {
callback: callback,
byteOffset: byteOffset,
bitOffset: bitOffset,
bitLength: bitLength,
oldData: defaultOldData
};
this.fields.push(field);
return {
disconnect: () => {
this.fields = this.fields.filter((element) => {
return element !== field;
});
}
};
}
handleInput(reportData) {
const view = new DataView(reportData);
for (const field of this.fields) {
const numBytes = Math.ceil(field.bitLength / 8);
let data;
// Little endianness is specified by the HID standard.
// The HID standard allows signed integers as well, but I am not aware
// of any HID DJ controllers which use signed integers.
if (numBytes === 1) {
data = view.getUint8(field.byteOffset);
} else if (numBytes === 2) {
data = view.getUint16(field.byteOffset, true);
} else if (numBytes === 3) {
data = view.getUint32(field.byteOffset, true) >>> 8;
} else if (numBytes === 4) {
data = view.getUint32(field.byteOffset, true);
} else {
throw Error("field bitLength must be between 1 and 32");
}
// The >>> 0 is required for 32 bit unsigned ints to not magically turn negative
// because all Numbers are really 32 bit signed floats. Because JavaScript.
data = ((data >> field.bitOffset) & (2 ** field.bitLength - 1)) >>> 0;
if (field.oldData !== data) {
field.callback(data);
field.oldData = data;
}
}
}
}
class HIDOutputReport {
constructor(reportId, length) {
this.reportId = reportId;
this.data = new Uint8Array(length).fill(0);
}
send() {
controller.sendOutputReport(this.reportId, this.data.buffer);
}
}
/*
* Components library
*/
class Component {
constructor(options) {
if (options) {
Object.keys(options).forEach(function(key) {
if (options[key] === undefined) { delete options[key]; }
});
Object.assign(this, options);
}
this.outConnections = [];
if (typeof this.key === "string") {
this.inKey = this.key;
this.outKey = this.key;
}
if (typeof this.unshift === "function" && this.unshift.length === 0) {
this.unshift();
}
this.shifted = false;
if (typeof this.input === "function" && this.inReport instanceof HIDInputReport && this.inReport.length === 0) {
this.inConnect();
}
this.outConnect();
}
inConnect(callback) {
if (this.inByte === undefined
|| this.inBit === undefined
|| this.inBitLength === undefined
|| this.inReport === undefined) {
return;
}
if (typeof callback === "function") {
this.input = callback;
}
this.inConnection = this.inReport.registerCallback(this.input.bind(this), this.inByte, this.inBit, this.inBitLength, this.oldDataDefault);
}
inDisconnect() {
if (this.inConnection !== undefined) {
this.inConnection.disconnect();
}
}
send(value) {
if (this.outReport !== undefined && this.outByte !== undefined) {
this.outReport.data[this.outByte] = value;
this.outReport.send();
}
}
output(value) {
this.send(value);
}
outConnect() {
if (this.outKey !== undefined && this.group !== undefined && this.outConnections.length === 0) {
const connection = engine.makeConnection(this.group, this.outKey, this.output.bind(this));
// This is useful for case where effect would have been fully disabled in Mixxx. This appears to be the case during unit tests.
if (connection) {
this.outConnections.push(connection);
} else {
console.warn(`Unable to connect '${this.group}.${this.outKey}' to the controller output. The control appears to be unavailable.`);
}
}
}
outDisconnect() {
for (const connection of this.outConnections) {
connection.disconnect();
}
this.outConnections = [];
}
outTrigger() {
for (const connection of this.outConnections) {
if (!connection) { continue; }
connection.trigger();
}
}
}
class ComponentContainer extends Component {
constructor() {
super();
}
*[Symbol.iterator]() {
// can't use for...of here because it would create an infinite loop
for (const property in this) {
if (Object.prototype.hasOwnProperty.call(this, property)) {
const obj = this[property];
if (obj instanceof Component) {
yield obj;
} else if (Array.isArray(obj)) {
for (const objectInArray of obj) {
if (objectInArray instanceof Component) {
yield objectInArray;
}
}
}
}
}
}
reconnectComponents(callback) {
for (const component of this) {
if (typeof component.unshift === "function" && component.unshift.length === 0) {
component.unshift();
}
if (typeof component.outDisconnect === "function" && component.outDisconnect.length === 0) {
component.outDisconnect();
}
if (typeof callback === "function" && callback.length === 1) {
callback.call(this, component);
}
if (typeof component.outConnect === "function" && component.outConnect.length === 0) {
component.outConnect();
}
component.outTrigger();
if (typeof component.unshift === "function" && component.unshift.length === 0) {
component.unshift();
}
}
}
unshift() {
for (const component of this) {
if (typeof component.unshift === "function" && component.unshift.length === 0) {
component.unshift();
}
component.shifted = false;
}
this.shifted = false;
}
shift() {
for (const component of this) {
if (typeof component.shift === "function" && component.shift.length === 0) {
component.shift();
}
component.shifted = true;
}
this.shifted = true;
}
}
/* eslint no-redeclare: "off" */
class Deck extends ComponentContainer {
constructor(decks, colors, settings) {
super();
if (typeof decks === "number") {
this.group = Deck.groupForNumber(decks);
} else if (Array.isArray(decks)) {
this.decks = decks;
this.currentDeckNumber = decks[0];
this.group = Deck.groupForNumber(decks[0]);
}
if (colors !== undefined && Array.isArray(colors)) {
this.groupsToColors = {};
let index = 0;
for (const deck of this.decks) {
this.groupsToColors[Deck.groupForNumber(deck)] = colors[index];
index++;
}
this.color = colors[0];
}
this.settings = settings;
this.secondDeckModes = {
moveMode: this.moveMode,
wheelMode: this.wheelMode,
};
this.selectedHotcue = null;
updateRuntimeData({
selectedHotcue: {
[this.group]: this.selectedHotcue
}
});
}
toggleDeck() {
if (this.decks === undefined) {
throw Error("toggleDeck can only be used with Decks constructed with an Array of deck numbers, for example [1, 3]");
}
const currentDeckIndex = this.decks.indexOf(this.currentDeckNumber);
let newDeckIndex = currentDeckIndex + 1;
if (currentDeckIndex >= this.decks.length) {
newDeckIndex = 0;
}
this.switchDeck(this.decks[newDeckIndex]);
}
switchDeck(newDeck) {
const newGroup = Deck.groupForNumber(newDeck);
const currentModes = {
wheelMode: this.wheelMode,
moveMode: this.moveMode,
};
switch (this.moveMode) {
case moveModes.beat:
case moveModes.bpm:
case moveModes.grid:
case moveModes.hotcueColor:
this.moveMode = null;
this.selectedHotcue = null;
updateRuntimeData({
selectedHotcue: {
[this.group]: this.selectedHotcue
}
});
break;
}
this.selectedStem.fill(false);
engine.setValue(this.group, "scratch2_enable", false);
this.group = newGroup;
this.color = this.groupsToColors[newGroup];
if (this.secondDeckModes !== null) {
this.wheelMode = this.secondDeckModes.wheelMode;
this.moveMode = this.secondDeckModes.moveMode;
if (this.wheelMode === wheelModes.motor) {
engine.beginTimer(MotorWindUpMilliseconds, () => {
engine.setValue(newGroup, "scratch2_enable", true);
}, true);
}
}
this.reconnectComponents(function(component) {
if (component.group === undefined
|| component.group.search(script.channelRegEx) !== -1) {
component.group = newGroup;
} else if (component.group.search(script.eqRegEx) !== -1) {
component.group = `[EqualizerRack1_${newGroup}_Effect1]`;
} else if (component.group.search(script.quickEffectRegEx) !== -1) {
component.group = quickFxChannel(newGroup);
}
component.color = this.groupsToColors[newGroup];
});
this.secondDeckModes = currentModes;
this.currentDeckNumber = newDeck;
updateRuntimeData({
group: {
[this.decks[0] === 1 ? "leftdeck":"rightdeck"]: this.group
}
});
}
static groupForNumber(deckNumber) {
return `[Channel${deckNumber}]`;
}
}
class Button extends Component {
constructor(options) {
options.oldDataDefault = 0;
super(options);
if (this.input === undefined
|| (typeof this.onLongPress === "function" && this.onLongPress.length === 0)
|| (typeof this.onLongRelease === "function" && this.onLongRelease.length === 0)
|| (typeof this.onShortPress === "function" && this.onShortPress.length === 0)
|| (typeof this.onShortRelease === "function" && this.onShortRelease.length === 0)
|| (typeof this.onPress === "function" && this.onPress.length === 0)
|| (typeof this.onRelease === "function" && this.onRelease.length === 0)) {
this.input = this.defaultInput;
}
if (typeof this.input === "function"
&& this.inReport instanceof HIDInputReport
&& this.input.length === 0) {
this.inConnect();
}
if (this.longPressTimeOutMillis === undefined) {
this.longPressTimeOutMillis = 225;
}
if (this.indicatorIntervalMillis === undefined) {
this.indicatorIntervalMillis = 350;
}
this.longPressTimer = 0;
this.indicatorTimer = 0;
this.indicatorState = false;
this.isLongPress = false;
if (this.inBitLength === undefined) {
this.inBitLength = 1;
}
}
setKey(key) {
this.inKey = key;
if (key === this.outKey) {
return;
}
this.outDisconnect();
this.outKey = key;
this.outConnect();
this.outTrigger();
}
setGroup(group) {
if (group === this.group) {
return;
}
this.outDisconnect();
this.group = group;
this.outConnect();
this.outTrigger();
}
output(value) {
if (this.indicatorTimer !== 0) {
return;
}
const brightness = (value > 0) ? this.brightnessOn : this.brightnessOff;
this.send((this.color || LedColors.white) + brightness);
}
indicatorCallback() {
this.indicatorState = !this.indicatorState;
this.send((this.indicatorColor ?? this.color ?? LedColors.white) + (this.indicatorState ? this.brightnessOn : this.brightnessOff));
}
indicator(on) {
if (on && this.indicatorTimer === 0) {
this.outDisconnect();
this.indicatorTimer = engine.beginTimer(this.indicatorIntervalMillis, this.indicatorCallback.bind(this));
} else if (!on && this.indicatorTimer !== 0) {
engine.stopTimer(this.indicatorTimer);
this.indicatorTimer = 0;
this.indicatorState = false;
this.outConnect();
this.outTrigger();
}
}
defaultInput(pressed) {
this.pressed = pressed;
if (pressed) {
this.isShortPress = true;
this.isLongPress = false;
if (typeof this.onPress === "function" && this.onPress.length === 0) { this.onPress(); }
if (typeof this.onShortPress === "function" && this.onShortPress.length === 0) { this.onShortPress(); }
if ((typeof this.onLongPress === "function" && this.onLongPress.length === 0) || (typeof this.onLongRelease === "function" && this.onLongRelease.length === 0)) {
this.longPressTimer = engine.beginTimer(this.longPressTimeOutMillis, () => {
this.isLongPress = true;
this.isShortPress = false;
this.longPressTimer = 0;
if (typeof this.onLongPress !== "function") { return; }
this.onLongPress(this);
}, true);
}
} else if (this.isLongPress) {
this.isLongPress = false;
if (typeof this.onRelease === "function" && this.onRelease.length === 0) { this.onRelease(); }
if (typeof this.onLongRelease === "function" && this.onLongRelease.length === 0) { this.onLongRelease(); }
} else {
this.isShortPress = false;
if (this.longPressTimer !== 0) {
engine.stopTimer(this.longPressTimer);
this.longPressTimer = 0;
}
if (typeof this.onRelease === "function" && this.onRelease.length === 0) { this.onRelease(); }
if (typeof this.onShortRelease === "function" && this.onShortRelease.length === 0) { this.onShortRelease(); }
}
}
}
class PushButton extends Button {
constructor(options) {
super(options);
}
input(pressed) {
engine.setValue(this.group, this.inKey, pressed);
}
}
class ToggleButton extends Button {
constructor(options) {
super(options);
}
onShortPress() {
script.toggleControl(this.group, this.inKey, true);
}
}
class TriggerButton extends Button {
constructor(options) {
super(options);
}
onShortPress() {
engine.setValue(this.group, this.inKey, true);
}
onShortRelease() {
engine.setValue(this.group, this.inKey, false);
}
}
class PowerWindowButton extends Button {
constructor(options) {
super(options);
}
onShortPress() {
script.toggleControl(this.group, this.inKey);
}
onLongRelease() {
script.toggleControl(this.group, this.inKey);
}
}
class PlayButton extends Button {
constructor(options) {
// Prevent accidental ejection/duplication accident
options.longPressTimeOutMillis = 800;
super(options);
this.inKey = "play";
this.outKey = "play_indicator";
this.outConnect();
}
onShortPress() {
script.toggleControl(this.group, this.inKey, true);
}
onLongPress() {
if (this.shifted) {
engine.setValue(this.group, this.inKey, false);
script.triggerControl(this.group, "eject");
} else if (!engine.getValue(this.group, this.inKey)) {
script.triggerControl(this.group, "CloneFromDeck");
}
}
}
class CueButton extends PushButton {
constructor(options) {
super(options);
this.outKey = "cue_indicator";
this.outConnect();
}
unshift() {
this.inKey = "cue_default";
}
shift() {
this.inKey = "start_stop";
}
input(pressed) {
if (this.deck.moveMode === moveModes.keyboard && !this.deck.keyboardPlayMode) {
this.deck.assignKeyboardPlayMode(this.group, this.inKey);
} else if (this.deck.wheelMode === wheelModes.motor && engine.getValue(this.group, "play") && pressed) {
engine.setValue(this.group, "cue_goto", pressed);
} else {
engine.setValue(this.group, this.inKey, pressed);
if (this.deck.wheelMode === wheelModes.motor) {
engine.setValue(this.group, "scratch2_enable", false);
engine.beginTimer(MotorWindDownMilliseconds, () => {
engine.setValue(this.group, "scratch2_enable", true);
}, true);
}
}
}
}
class Encoder extends Component {
constructor(options) {
super(options);
this.lastValue = null;
}
input(value) {
const oldValue = this.lastValue;
this.lastValue = value;
if (oldValue === null || typeof this.onChange !== "function") {
// This scenario happens at the controller initialisation. No real input to proceed
return;
}
let isRight;
if (oldValue === this.max && value === 0) {
isRight = true;
} else if (oldValue === 0 && value === this.max) {
isRight = false;
} else {
isRight = value > oldValue;
}
this.onChange(isRight);
}
}
/*
* Represent a pad button that interact with a hotcue (set, activate or clear)
*/
class HotcueButton extends PushButton {
constructor(options) {
super(options);
if (this.number === undefined || !Number.isInteger(this.number) || this.number < 1 || this.number > 32) {
throw Error("HotcueButton must have a number property of an integer between 1 and 32");
}
this.outKey = `hotcue_${this.number}_status`;
this.colorKey = `hotcue_${this.number}_color`;
this.indicatorColor = LedColors.off;
this.outConnect();
}
unshift() {
this.inKey = `hotcue_${this.number}_activate`;
this.indicator(false);
}
shift() {
this.inKey = `hotcue_${this.number}_clear`;
this.indicator(true);
}
input(pressed) {
if (this.deck.moveMode === moveModes.hotcueColor) {
this.deck.selectedHotcue = pressed ? this.number : null;
updateRuntimeData({
selectedHotcue: {
[this.group]: this.deck.selectedHotcue
}
});
} else if (this.deck.libraryPlayButton.pressed) {
engine.setValue(this.deck.libraryPlayButton.group, this.inKey, pressed);
} else {
if (this.deck.wheelMode === wheelModes.vinyl && !this.deck.wheelTouch.touched) {
engine.setValue(this.group, "scratch2_enable", false);
}
engine.setValue(this.group, this.inKey, pressed);
if (this.shifted) {
this.indicatorColor = LedColors.off;
}
}
}
output(value) {
if (value) {
this.indicatorColor = LedColors.red;
this.send(this.color + this.brightnessOn);
} else {
this.indicatorColor = LedColors.off;
this.send(LedColors.off);
}
}
outConnect() {
if (undefined !== this.group && this.outConnections.length === 0) {
const connection0 = engine.makeConnection(this.group, this.outKey, this.output.bind(this));
if (connection0) {
this.outConnections.push(connection0);
} else {
console.warn(`Unable to connect ${this.group}.${this.outKey}' to the controller output. The control appears to be unavailable.`);
}
const connection1 = engine.makeConnection(this.group, this.colorKey, (colorCode) => {
this.color = this.colorMap.getValueForNearestColor(colorCode);
this.output(engine.getValue(this.group, this.outKey));
});
if (connection1) {
this.outConnections.push(connection1);
} else {
console.warn(`Unable to connect ${this.group}.${this.colorKey}' to the controller output. The control appears to be unavailable.`);
}
}
}
outDisconnect() {
this.indicator(false);
super.outDisconnect();
}
}
/*
* Represent a pad button that acts as a keyboard key. Depending the deck keyboard mode, it will either change the key, or play the cue with the button's key
*/
class KeyboardButton extends PushButton {
constructor(options) {
super(options);
if (this.number === undefined || !Number.isInteger(this.number) || this.number < 1 || this.number > 8) {
throw Error("KeyboardButton must have a number property of an integer between 1 and 8");
}
if (this.deck === undefined) {
throw Error("KeyboardButton must have a deck attached to it");
}
this.outConnect();
}
unshift() {