-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Behringer-Extension-scripts.js
1618 lines (1535 loc) · 64.6 KB
/
Behringer-Extension-scripts.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
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
/**
* Additional JS components for Mixxx
*/
(function(global) {
/** @private */
var components = global.components;
/** @private */
var engine = global.engine;
/**
* Contains functions to print a message to the log.
* `debug` output is suppressed unless the caller owns a truthy property `debug`.
*
* @param {string} message Message
* @private
*/
var log = {
debug: function(message) {
if (this.debug) {
print("[DEBUG] " + message);
}
},
warn: function(message) {
print("[WARN] " + message);
},
error: function(message) {
print("[ERROR] " + message);
},
};
/**
* Determine an ID from a component's MIDI address
*
* @param {Array<number>} midiAddress MIDI address consisting of two integers
* @return {string} ID for the MIDI address; `undefined` on error
* @private
*/
var findComponentId = function(midiAddress) {
if (Array.isArray(midiAddress) && midiAddress.length === 2
&& typeof midiAddress[0] === "number" && typeof midiAddress[1] === "number") {
return "[" + midiAddress.map(function(x) {
return "0x" + ("00" + x.toString(16).toUpperCase()).slice(-2);
}).join(", ") + "]";
}
log.error("Invalid MIDI address: " + global.stringifyObject(midiAddress));
};
/**
* Create a human-readable string to identify a component.
*
* @param {components.Component} component A component
* @return {string} A short string that describes the component; `undefined` on error
* @private
*/
var stringifyComponent = function(component) {
if (!component) {
return;
}
var key = component.inKey || component.outKey;
var value = component.group + "," + key;
if (component.midi) {
var id = findComponentId(component.midi);
if (id !== undefined) {
value = id + ": " + value;
}
}
return "(" + value + ")";
};
/**
* Convert a parameter value (0..1) to a MIDI value (0..this.max).
*
* @param {number} value A number between 0 and 1.
* @private
*/
var convertToMidiValue = function(value) {
/*
* Math.round() is important to keep input and output in sync.
* Example:
* - Mixxx receives input value 35 and calculates input parameter (35 / 127) == 0.27559
* - Mixxx sends output value of 0.27559
* - Component.outScaleValue(0.27559) = 0.27559 * 127 = 34.99
* - Without round, 34 is sent to the controller.
*/
return Math.round(value * this.max);
};
/**
* Derive a prototype from a parent.
*
* @param {object} parent Constructor of parent whose prototype is used as base
* @param {object} members Own members that are not inherited
* @return {object} A new prototype based on parent with the given members
* @private
*/
var deriveFrom = function(parent, members) {
return _.merge(Object.create(parent.prototype), members || {});
};
/**
* Perform an action, throttled if the owner supports throttling.
*
* @param {function} action The action to perform
* @param {object} owner Object used as `this` for the action
* @private
* @see `Throttler`
*/
var throttle = function(action, owner) {
if (owner.throttler) {
owner.throttler.schedule(action, owner);
} else {
action.call(owner);
}
};
/**
* A component that uses the parameter instead of the value as output.
*
* @constructor
* @extends {components.Component}
* @param {object} options Options object
* @public
*/
var ParameterComponent = function(options) {
components.Component.call(this, options);
};
ParameterComponent.prototype = deriveFrom(components.Component, {
outValueScale: function(_value) {
/*
* We ignore the argument and use the parameter (0..1) instead because value scale is
* arbitrary and thus cannot be mapped to MIDI values (0..127) properly.
*/
return convertToMidiValue.call(this, this.outGetParameter());
},
});
/**
* A button to toggle un-/shift on a target component.
*
* @constructor
* @extends {components.Button}
* @param {object} options Options object
* @param {components.Component|components.ComponentContainer} options.target Target component
* @public
*/
var ShiftButton = function(options) {
components.Button.call(this, options);
};
ShiftButton.prototype = deriveFrom(components.Button, {
inSetValue: function(value) {
if (value) {
this.target.shift();
} else {
this.target.unshift();
}
components.Button.prototype.inSetValue.call(this, value);
},
});
/**
* A component that handles every incoming value.
*
* @constructor
* @extends {components.Component}
* @param {object} options Options object
* @public
*/
var Trigger = function(options) {
components.Component.call(this, options);
};
Trigger.prototype = deriveFrom(components.Component, {
inValueScale: function() { return true; },
});
/**
* A button with configurable Mixxx control values for `on` and `off`.
*
* @constructor
* @extends {components.Button}
* @param {number} options.onValue Value for `on`; optional, default: `1`
* @param {number} options.offValue Value for `off`; optional, default: opposite of `onValue`
* @public
*/
var CustomButton = function(options) {
options = options || {};
if (options.onValue === undefined) { // do not use '||' to allow 0
options.onValue = 1;
}
if (options.offValue === undefined) { // do not use '!' to keep number type
options.offValue = options.onValue ? 0 : 1;
}
components.Button.call(this, options);
};
CustomButton.prototype = deriveFrom(components.Button, {
isOn: function(engineValue) { // engine -> boolean
return engineValue === this.onValue;
},
inGetValue: function() { // engine -> boolean
return this.isOn(engine.getValue(this.group, this.inKey));
},
inSetValue: function(on) { // boolean -> engine
engine.setValue(this.group, this.inKey, on ? this.onValue : this.offValue);
},
outGetValue: function() { // engine -> boolean
return this.isOn(engine.getValue(this.group, this.outKey));
},
outSetValue: function(on) { // boolean -> engine
engine.setValue(this.group, this.outKey, on ? this.onValue : this.offValue);
},
outValueScale: function(engineValue) { // engine -> MIDI
return this.isOn(engineValue) ? this.on : this.off;
},
inValueScale: function(midiValue) { // MIDI -> engine
return midiValue === this.on ? this.onValue : this.offValue;
}
});
/**
* An object that simplifies using a timer safely.
* Use `start(action)` to start and `reset()` to reset.
*
* @constructor
* @param {number} options.timeout Duration between start and action (in ms)
* @param {boolean} options.oneShot If `true`, the action is run once;
* otherwise, it is run periodically until the timer is reset.
* @param {function} options.action Function that is executed whenever the timer expires
* @param {object} options.owner Owner object of the `action` function (assigned to `this`)
* @public
* @see https://github.com/mixxxdj/mixxx/wiki/Script-Timers
*/
var Timer = function(options) {
_.assign(this, options);
this.disable();
};
Timer.prototype = {
disable: function() { this.id = 0; },
isEnabled: function() { return this.id !== 0; },
start: function() {
this.reset();
this.id = engine.beginTimer(this.timeout, function() {
if (this.oneShot) {
this.disable();
}
this.action.call(this.owner);
}.bind(this), this.oneShot); // .bind(this) is required instead of arrow function for Qt < 6.2.4 due to QTBUG-95677
},
reset: function() {
if (this.isEnabled()) {
engine.stopTimer(this.id);
this.disable();
}
},
setState: function(active) {
if (active) {
this.start();
} else {
this.reset();
}
}
};
/**
* An object that enforces a constant delay between the execution of consecutive actions.
*
* Use `schedule(action, owner)` to perform an action on the owner as soon as the delay has
* elapsed after the preceding action has finished.
*
* @constructor
* @param {number} options.delay Minimal delay between two consecutive actions (in ms)
* @public
*/
var Throttler = function(options) {
options = options || {};
options.delay = options.delay || 0;
_.assign(this, options);
this.locked = false;
this.jobs = [];
this.unlockTimer = new Timer(
{timeout: this.delay, oneShot: true, action: this.unlock, owner: this});
};
Throttler.prototype = {
schedule: function(action, owner) {
this.jobs.push({action: action, owner: owner});
this.notify();
},
notify: function() {
if (this.jobs.length > 0 && this.acquireLock()) {
var job = this.jobs.shift();
job.action.call(job.owner);
this.unlockTimer.start();
}
},
acquireLock: function() {
var unlocked = !this.locked;
if (unlocked) {
this.locked = true;
}
return unlocked;
},
unlock: function() {
this.locked = false;
this.notify();
}
};
/**
* A button that supports different actions on short and long press.
*
* @constructor
* @extends {components.Button}
* @param {object} options Options object
* @public
*/
var LongPressButton = function(options) {
components.Button.call(this, options);
var action = function() {
this.isLongPressed = true;
this.onLongPress();
};
this.longPressTimer = new Timer(
{timeout: this.longPressTimeout, oneShot: true, action: action, owner: this});
};
LongPressButton.prototype = deriveFrom(components.Button, {
isLongPressed: false,
input: function(channel, control, value, status, _group) {
if (this.isPress(channel, control, value, status)) {
this.handlePress();
} else {
this.handleRelease();
}
},
handlePress: function() {
this.onShortPress();
this.longPressTimer.start();
},
handleRelease: function() {
this.longPressTimer.reset();
this.onRelease();
this.isLongPressed = false;
},
onShortPress: function(_value) {},
onLongPress: function(_value) {},
onRelease: function(_value) {},
});
/**
* A button that blinks when `on`.
*
* @constructor
* @extends {components.Button}
* @param {number} options.blinkDuration Blink duration in ms; optional, default: 500
* @public
*/
var BlinkingButton = function(options) {
options = options || {};
var blinkAction = function() {
this.send(components.Button.prototype.outValueScale.call(
this, this.flashing = !this.flashing));
};
this.blinkTimer = new Timer(
{timeout: options.blinkDuration || 500, action: blinkAction, owner: this});
components.Button.call(this, options);
};
BlinkingButton.prototype = deriveFrom(components.Button, {
flashing: false,
outValueScale: function(value) {
this.blinkTimer.setState(this.flashing = value);
return components.Button.prototype.outValueScale.call(this, value);
},
});
/**
* An encoder for directions.
*
* Turning the encoder to the right means "forwards" and returns 1;
* turning it to the left means "backwards" and returns -1.
*
* This component supports an optional relative mode as an alternative to
* dealing with soft takeover. To use it, set the `relative` property to
* `true` in the options object for the constructor. In this mode, moving
* the Pot will adjust the Mixxx Control relative to its current value.
* Holding shift and moving the encoder will not affect the Mixxx Control.
* This allows the user to continue adjusting the Mixxx Control after
* the encoder has reached the end of its physical range.
*
* @constructor
* @extends {components.Encoder}
* @param {object} options Options object
* @public
*/
var DirectionEncoder = function(options) {
components.Encoder.call(this, options);
this.previousValue = this.inGetValue(); // available only after call of Encoder constructor
};
DirectionEncoder.prototype = deriveFrom(components.Encoder, {
min: 0,
inValueScale: function(value) {
var direction = 0;
if (!(this.relative && this.isShifted)) {
if (value > this.previousValue || value === this.max) {
direction = 1;
} else if (value < this.previousValue || value === this.min) {
direction = -1;
}
this.previousValue = value;
}
return direction;
},
shift: function() {
this.isShifted = true;
},
unshift: function() {
this.isShifted = false;
},
});
/**
* An encoder for a value range of [-bound..0..+bound].
*
* @constructor
* @extends {components.Encoder}
* @param {object} options Options object
* @param {number} options.bound A positive integer defining the range bounds
* @public
*/
var RangeAwareEncoder = function(options) {
components.Encoder.call(this, options);
};
RangeAwareEncoder.prototype = deriveFrom(components.Encoder, {
outValueScale: function(value) {
/* -bound..+bound => 0..1 */
var normalizedValue = (value + this.bound) / (2 * this.bound);
/* 0..1 => 0..127 */
return convertToMidiValue.call(this, normalizedValue);
},
});
/**
* A pot for a value range of [-bound..0..+bound].
*
* @constructor
* @extends {components.Pot}
* @param {object} options Options object
* @param {number} options.bound A positive integer defining the range bounds
* @public
*/
var RangeAwarePot = function(options) {
components.Pot.call(this, options);
};
RangeAwarePot.prototype = deriveFrom(components.Pot, {
outValueScale: RangeAwareEncoder.prototype.outValueScale
});
/**
* A button to cycle through the values of an enumeration.
*
* The enumeration values may be defined either explicitly by an array
* or implicitly by a `maxValue` so that the values are `[0..maxValue]`.
*
* @constructor
* @extends {components.Button}
* @param {object} options Options object
* @param {Array} options.values An array of enumeration values
* @param {number} options.maxValue A positive integer defining the maximum enumeration value
* @public
*/
var EnumToggleButton = function(options) {
options = options || {};
if (options.maxValue === undefined && options.values === undefined) {
log.error("An EnumToggleButton requires either `values` or a `maxValue`.");
this.maxValue = 0;
}
if (options.type === undefined) { // do not use '||' to allow 0
options.type = components.Button.prototype.types.toggle;
}
components.Button.call(this, options);
};
EnumToggleButton.prototype = deriveFrom(components.Button, {
input: function(channel, control, value, status, _group) {
if (this.isPress(channel, control, value, status)) {
var newValue;
if (this.values) {
var index = this.values.indexOf(this.inGetValue());
newValue = this.values[(index + 1) % this.values.length];
} else {
newValue = (this.inGetValue() + 1) % (this.maxValue + 1);
}
this.inSetValue(newValue);
}
}
});
/**
* An encoder for enumeration values.
*
* @constructor
* @extends {components.Encoder}
* @param {object} options Options object
* @param {Array} options.values An array containing the enumeration values
* @param {boolean} options.softTakeover (optional) Enable soft-takeover; default: `true`
* @public
* @see https://github.com/mixxxdj/mixxx/wiki/Midi-Scripting#soft-takeover
*/
var EnumEncoder = function(options) {
options = options || {};
if (options.values === undefined) {
log.error("EnumEncoder constructor was called without specifying enum values.");
options.values = [];
}
if (options.softTakeover === undefined) { // do not use '||' to allow false
options.softTakeover = true;
}
options.maxIndex = options.values.length - 1;
components.Encoder.call(this, options);
};
EnumEncoder.prototype = deriveFrom(components.Encoder, {
input: function(_channel, _control, value, _status, _group) {
var scaledValue = this.inValueScale(value);
if (!this.softTakeover
|| this.previousValue === undefined
|| this.previousValue === this.inGetValue()) {
this.inSetParameter(scaledValue);
}
this.previousValue = scaledValue;
},
inValueScale: function(value) {
var normalizedValue = value / this.max;
var index = Math.round(normalizedValue * this.maxIndex);
return this.values[index];
},
outValueScale: function(value) {
var index = this.values.indexOf(value);
if (index !== -1) {
var normalizedValue = index / this.maxIndex;
return convertToMidiValue.call(this, normalizedValue);
} else {
log.warn("'" + value + "' is not in supported values " + "[" + this.values + "]");
}
},
});
/**
* An EnumEncoder for a loop control that uses beat sizes as enumeration.
*
* @constructor
* @extends {EnumEncoder}
* @param {object} options Options object
* @public
*/
var LoopEncoder = function(options) {
options = options || {};
if (options.values === undefined) {
/* taken from src/engine/controls/loopingcontrol.cpp */
options.values
= [0.03125, 0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512];
}
EnumEncoder.call(this, options);
};
LoopEncoder.prototype = deriveFrom(EnumEncoder);
/**
* An encoder that moves a loop.
*
* Turning the encoder to the right will move the loop forwards; turning it to the left will
* move it backwards. The amount of movement may be given by either `size` or `sizeControl`,
* `sizeControl` being preferred.
*
* @constructor
* @extends {DirectionEncoder}
* @param {object} options Options object
* @param {number} options.size (optional) Size given in number of beats; default: 0.5
* @param {string} options.sizeControl (optional) Name of a control that contains `size`
* @public
*/
var LoopMoveEncoder = function(options) {
options = options || {};
options.inKey = options.inKey || "loop_move";
options.size = options.size || 0.5;
DirectionEncoder.call(this, options);
};
LoopMoveEncoder.prototype = deriveFrom(DirectionEncoder, {
inValueScale: function(value) {
var direction = DirectionEncoder.prototype.inValueScale.call(this, value);
var beats = this.sizeControl
? engine.getValue(this.group, this.sizeControl)
: this.size;
return direction * beats;
},
});
/**
* A button that toggles a beatloop that ends at the current play position.
*
* @constructor
* @extends {components.Button}
* @param {object} options Options object
* @public
*/
var BackLoopButton = function(options) {
options = options || {};
options.key = options.key || "loop_enabled";
components.Button.call(this, options);
};
BackLoopButton.prototype = deriveFrom(components.Button, {
inSetValue: function(value) {
var script = global.script;
var group = this.group;
if (value) {
var loopSize = engine.getValue(group, "beatloop_size");
var beatjumpSize = engine.getValue(group, "beatjump_size");
engine.setValue(group, "beatjump_size", loopSize);
script.triggerControl(group, "beatjump_backward");
script.triggerControl(group, "beatloop_activate");
engine.setValue(group, "beatjump_size", beatjumpSize);
} else {
script.triggerControl(group, "reloop_toggle");
}
}
});
/**
* A pot for the crossfader curve.
*
* @constructor
* @extends {components.Pot}
* @param {number} options.mode Crossfader mode; optional, default: `0`.
* (`0`: additive, `1`: constant)
* @public
*/
var CrossfaderCurvePot = function(options) {
options = options || {};
options.group = options.group || "[Mixer Profile]";
if (options.mode) {
options.inKey = options.inKey || options.key || "xFaderCalibration";
options.low = options.low || 0.5;
options.high = options.high || 0.962;
engine.setValue("[Mixer Profile]", "xFaderMode", options.mode);
} else {
options.inKey = options.inKey || options.key || "xFaderCurve";
options.low = options.low || 1;
options.high = options.high || 2;
}
components.Pot.call(this, options);
};
CrossfaderCurvePot.prototype = deriveFrom(components.Pot, {
modes: {ADDITIVE: 0, CONSTANT: 1},
min: 0,
input: function(_channel, _control, value, _status, _group) {
engine.setValue(this.group, this.inKey,
script.absoluteLin(value, this.low, this.high, this.min, this.max));
}
});
/**
* A component that sends the values of a source component to a MIDI controller even if the
* source component uses its `outKey` property for other purposes.
*
* Note: most components send output properly out of the box so that no Publisher is required.
* This component allows to add functionality to some special components, e.g. effect unit
* controls.
*
* This component offers a `bind()` function that allows for re-binding to the source component
* when its internal state changes.
*
* @constructor
* @extends {components.Component}
* @param {object} options Options object
* @param {components.Component} options.source Source component whose values are sent to the
* controller
* @public
*/
var Publisher = function(options) {
if (options.source === undefined) {
log.error("Missing source component");
return;
}
this.source = options.source;
this.sync();
ParameterComponent.call(this, options);
};
Publisher.prototype = deriveFrom(ParameterComponent, {
sync: function() {
this.midi = this.source.midi;
this.group = this.source.group;
this.outKey = this.source.inKey;
},
bind: function() {
if (this.group !== this.source.group || this.outKey !== this.source.inKey) {
log.debug("Binding publisher " + stringifyComponent(this)
+ " to " + stringifyComponent(this.source));
this.disconnect();
this.sync();
this.connect();
this.trigger();
}
},
});
var EffectUnit = function(rack, deckGroup) {
components.ComponentContainer.call(this);
var effectGroup = "[" + rack + "_" + deckGroup + "_Effect1]";
var channelGroup = "[" + rack + "_" + deckGroup + "]";
var ParameterKnob = function(parameterNumber) {
components.Pot.call(this, {group: effectGroup, key: "parameter" + parameterNumber});
};
ParameterKnob.prototype = deriveFrom(components.Pot);
var ParameterButton = function(parameterNumber) {
components.Button.call(this, {group: effectGroup, key: "button_parameter" + parameterNumber});
};
ParameterButton.prototype = deriveFrom(
components.Button, {type: components.Button.prototype.types.powerWindow});
this.enabled = new components.Button(
{group: effectGroup, key: "enabled", type: components.Button.prototype.types.powerWindow});
this.meta = new components.Pot({group: effectGroup, key: "meta"});
this.super1 = new components.Pot({group: channelGroup, key: "super1"});
this.mix = new components.Pot({group: channelGroup, key: "mix"});
this.parameterKnobs = new components.ComponentContainer();
var parameterKnobCount = engine.getValue(effectGroup, "num_parameters");
for (var knobIndex = 1; knobIndex <= parameterKnobCount; knobIndex++) {
this.parameterKnobs[knobIndex] = new ParameterKnob(knobIndex);
}
this.parameterButtons = new components.ComponentContainer();
var parameterButtonCount = engine.getValue(effectGroup, "num_button_parameters");
for (var buttonIndex = 1; buttonIndex <= parameterButtonCount; buttonIndex++) {
this.parameterButtons[buttonIndex] = new ParameterButton(buttonIndex);
}
};
EffectUnit.prototype = deriveFrom(components.ComponentContainer);
/**
* @typedef {components.ComponentContainer} EqualizerUnit
*
* @property {components.Button} enabled En-/disable equalizer unit
* @property {components.Pot} parameterKnobs.1 Low knob
* @property {components.Pot} parameterKnobs.2 Mid knob
* @property {components.Pot} parameterKnobs.3 High knob
* @property {components.Button} parameterButtons.1 Mute low
* @property {components.Button} parameterButtons.2 Mute mid
* @property {components.Button} parameterButtons.3 Mute high
*/
/**
* A component container for equalizer controls.
*
* @constructor
* @extends {components.ComponentContainer}
* @param {string} deckGroup Group of the deck this unit belongs to (e.g. `[Channel1]`)
* @yields {EqualizerUnit}
* @public
*/
var EqualizerUnit = function(deckGroup) {
EffectUnit.call(this, "EqualizerRack1", deckGroup);
};
EqualizerUnit.prototype = deriveFrom(EffectUnit);
/**
* @typedef {components.ComponentContainer} QuickEffectUnit
*
* @property {components.Button} enabled En-/disable quick effect unit
* @property {components.Pot} meta Meta knob
* @property {components.Pot} super1 Super knob
* @property {components.Pot} parameterKnobs.1 Parameter 1
* @property {components.Pot} parameterKnobs.2 Parameter 2
* @property {components.Pot} parameterKnobs.3 Parameter 3
* @property {components.Pot} parameterKnobs.4 Parameter 4
* @property {components.Pot} parameterKnobs.5 Parameter 5
* @property {components.Button} parameterButtons.1 Parameter Button 1
* @property {components.Button} parameterButtons.2 Parameter Button 2
*/
/**
* A component container for quick effect controls.
*
* @constructor
* @extends {components.ComponentContainer}
* @param {string} deckGroup Group of the deck this unit belongs to (e.g. `[Channel1]`)
* @yields {QuickEffectUnit}
* @public
*/
var QuickEffectUnit = function(deckGroup) {
EffectUnit.call(this, "QuickEffectRack1", deckGroup);
};
QuickEffectUnit.prototype = deriveFrom(EffectUnit);
/**
* Manage Components in named ComponentContainers.
*
* @constructor
* @param {Array<string>} initialContainers Initial container names
* @public
*/
var ComponentRegistry = function(initialContainers) {
this.containers = new components.ComponentContainer();
if (Array.isArray(initialContainers)) {
initialContainers.forEach(function(name) { this.createContainer(name); }, this);
}
};
ComponentRegistry.prototype = {
/**
* Create a new ComponentContainer within this registry.
*
* @param {string} name Name of the ComponentContainer
* @return {components.ComponentContainer} The ComponentContainer; `undefined` on error
* @public
*/
createContainer: function(name) {
if (!Object.prototype.hasOwnProperty.call(this.containers, name)) {
log.debug("Creating ComponentContainer '" + name + "'");
this.containers[name] = new components.ComponentContainer();
return this.containers[name];
} else {
log.error("ComponentContainer '" + name + "' already exists.");
}
},
/**
* Retrieve an existing ComponentContainer.
*
* @param {string} name Name of an existing ComponentContainer
* @return {components.ComponentContainer} The ComponentContainer; `undefined` on error
* @public
*/
getContainer: function(name) {
if (!Object.prototype.hasOwnProperty.call(this.containers, name)) {
log.error("ComponentContainer '" + name + "' does not exist.");
}
return this.containers[name];
},
/**
* Disconnect all components in a ComponentContainer.
*
* @param {components.ComponentContainer} container An existing ComponentContainer
* @public
*/
disconnectContainer: function(container) {
container.forEachComponent(function(component) {
component.disconnect();
});
},
/**
* Remove a ComponentContainer, optionally disconnecting its components
*
* @param {string} name Name of an existing ComponentContainer
* @param {boolean} disconnect Iff truthy, components are disconnected
* @public
*/
destroyContainer: function(name, disconnect) {
log.debug("Destroying ComponentContainer '" + name + "'");
if (disconnect) {
this.disconnectContainer(this.getContainer(name));
}
delete this.containers[name];
},
/**
* Remove all ComponentContainers, optionally disconnecting their components.
*
* @param {boolean} disconnect Iff truthy, components are disconnected
* @public
*/
destroy: function(disconnect) {
Object.keys(this.containers).forEach(function(name) {
this.destroyContainer(name, disconnect);
}, this);
},
/**
* Store a component in a ComponentContainer using the component's MIDI address as key.
* If the ComponentContainer is missing, it is created. If the ComponentContainer contains
* a component with the same key, the existing component is unregistered first.
* Note: components are not connected or disconnected.
*
* @param {components.Component} component A component
* @param {string} containerName Name of a ComponentContainer
* @return {string} ID of the stored component; `undefined` on error
* @public
*/
register: function(component, containerName) {
if (component === undefined) {
log.error("Missing component");
return;
}
if (!component.midi) {
log.debug(containerName + ": ignore "
+ stringifyComponent(component) + " without MIDI address");
return;
}
var id = findComponentId(component.midi);
if (!Object.prototype.hasOwnProperty.call(this.containers, containerName)) {
this.createContainer(containerName);
}
var container = this.getContainer(containerName);
var store = true;
if (Object.prototype.hasOwnProperty.call(container, id)) {
var old = container[id];
if (old !== component) {
this.unregister(old, containerName);
} else {
store = false;
log.debug(containerName + ": ignore re-register of "
+ stringifyComponent(component));
}
}
if (store) {
log.debug(containerName + ": register " + stringifyComponent(component));
container[id] = component;
}
return id;
},
/**
* Remove a component from a ComponentContainer.
* Note: components are not connected or disconnected.
*
* @param {components.Component} component A component
* @param {string} containerName Name of an existing ComponentContainer
* @return {string} ID of the removed component; `undefined` on error
* @public
*/
unregister: function(component, containerName) {
log.debug(containerName + ": unregister " + stringifyComponent(component));
var container = this.getContainer(containerName);
var id = findComponentId(component.midi);
delete container[id];
return id;
},
};
/**
* A component that manages components in two layers named "Default" and "Shift".
*
* Components may be added to either layer. One of the layers has the role of the "active"
* layer (initially: Default). The active layer is changed by calling the functions `shift()`
* and `unshift()`.
*
* Once after addition of all components, `init()` must be called.
*
* This object is itself a component offering an `input()` function. This function delegates to
* the component with the matching MIDI address on the active layer. When there's no matching
* component, the Default layer is used as fallback.
*
* Note: when two components on different layers share the same MIDI address, they are
* dis-/connected on layer switch. Components are not dis-/connected when they are
* un-/registered to a layer.
*
* @constructor
* @extends {components.Component}
* @param {object} options Options object
* @param {boolean} options.debug Optional flag to emit debug messages to the log
* @public
*/
var LayerManager = function(options) {
this.componentRegistry = new ComponentRegistry([
LayerManager.prototype.defaultContainerName,
LayerManager.prototype.shiftContainerName]);
this.activeLayer = new components.ComponentContainer();
components.Component.call(this, options);
};
LayerManager.prototype = deriveFrom(components.Component, {
/** @private */
defaultContainerName: "Default",
/** @private */
shiftContainerName: "Shift",
/**
* Retrieve the Default layer.
*
* @return {object} The Default layer
* @private
*/
defaultLayer: function() {
var defaultContainer = this.componentRegistry.getContainer(this.defaultContainerName);
return Object.keys(this.shiftLayer()).reduce(
function(shiftCounterparts, name) {
shiftCounterparts[name] = defaultContainer[name];
return shiftCounterparts;
}, {});
},
/**
* Retrieve the Shift layer.
*
* @return {object} The Shift layer
* @private
*/
shiftLayer: function() {
return this.componentRegistry.getContainer(this.shiftContainerName);
},