This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
minilab-mk2.control.js
189 lines (159 loc) · 4.47 KB
/
minilab-mk2.control.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Load API v6.
loadAPI(6);
// Basic setup.
host.setShouldFailOnDeprecatedUse(true);
host.defineController("Arturia", "MiniLab Mk II", "1.0", "2fdbbb1f-f771-43f7-83e8-bc01c3e573de", "bequadro");
host.defineMidiPorts(1, 1);
host.addDeviceNameBasedDiscoveryPair(["Arturia MiniLab mkII"], ["Arturia MiniLab mkII"]);
// MIDI messages.
const MIDI_STATUS_PAD_ON = 153;
const MIDI_STATUS_PAD_OFF = 137;
const MIDI_STATUS_KNOBS = 176;
const MIDI_DATA1_PAD_OFFSET = 36;
// Knobs mapping (MIDI data1).
const KNOB1 = 112;
const KNOB1_CLICK = 113;
const KNOB9 = 114;
const KNOB9_CLICK = 115;
const KNOBS_LEFT_ = [74, 71, 76, 18, 19, 16];
const KNOBS_RIGHT = [77, 93, 73, 75, 17, 91, 79, 72];
// MiniLab colors.
const COLOR = {
BLACK: "00",
RED: "01",
BLUE: "10",
GREEN: "04",
CYAN: "14",
PURPLE: "11",
YELLOW: "05",
WHITE: "7F"
};
// Set single pad color via SysEx.
function setPadColor(pad, color) {
let padHex = (112 + pad).toString(16);
sendSysex("F0 00 20 6B 7F 42 02 00 10 " + padHex + " " + color + " F7");
}
// Pad color mapping. Notes = white, Control = color.
const PAD_COLORS = [
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.WHITE,
COLOR.BLACK,
COLOR.BLACK,
COLOR.CYAN,
COLOR.GREEN,
COLOR.YELLOW,
COLOR.RED
];
function init() {
// Create separate inputs for keys and pads. Set aftertouch to pads.
MiniLabKeys = host.getMidiInPort(0).createNoteInput("MiniLab Keys", "80????", "90????", "B001??", "B002??", "B007??", "B00B??", "B040??", "C0????", "D0????", "E0????");
MiniLabKeys.setShouldConsumeEvents(false);
MiniLabPads = host.getMidiInPort(0).createNoteInput("MiniLab Pads", "?9????");
MiniLabPads.setShouldConsumeEvents(false);
MiniLabPads.assignPolyphonicAftertouchToExpression(0, NoteExpression.TIMBRE_UP, 2);
// Only enable the lower 8 pad keys.
let padTranslation = new Array(128);
for (let i = 0; i < 128; i++) {
padTranslation[i] = -1;
}
for (let i = MIDI_DATA1_PAD_OFFSET; i < MIDI_DATA1_PAD_OFFSET + 8; i++) {
padTranslation[i] = i;
}
MiniLabPads.setKeyTranslationTable(padTranslation);
// Define global transport variable.
transport = host.createTransport();
// Currently selected track, device and macros.
track = host.createCursorTrack(1, 1);
device = track.createCursorDevice();
macros = device.createCursorRemoteControlsPage(8);
// Freely assignable controls.
ccKnobs = host.createUserControls(6);
// Define Callbacks (no SysEx).
host.getMidiInPort(0).setMidiCallback(onMidi);
// Set colors for all pads.
for (let i = 0; i < 16; i++) {
setPadColor(i, PAD_COLORS[i]);
}
// Set colored control indicators.
// for (let i = 0; i < 6; i++) {
// ccKnobs.getControl(i).setIndication(true);
// }
// for (let i = 0; i < 8; i++) {
// macros.getParameter(i).setIndication(true);
// }
// Hi.
println("MiniLab MK2 Controller Script by @bequadro.");
}
// Called when a short MIDI message is received on MIDI input port 0.
// Status: Note on/off, CC, PB, ...
// Data1: Note number, CC number
// Data2: Note velocity, pressure, CC data (0-127)
function onMidi(status, data1, data2) {
let padNumber = data1 - MIDI_DATA1_PAD_OFFSET;
if (status === MIDI_STATUS_PAD_ON) {
switch (padNumber) {
case 8:
track.selectPrevious();
break;
case 9:
track.selectNext();
break;
case 12:
transport.isMetronomeEnabled().toggle();
break;
case 13:
transport.play();
break;
case 14:
transport.stop();
break;
case 15:
transport.record();
break;
}
//setPadColor(padNumber, PAD_COLORS[padNumber]);
} else if (status === MIDI_STATUS_PAD_OFF) {
setPadColor(padNumber, PAD_COLORS[padNumber]);
} else if (status === MIDI_STATUS_KNOBS) {
// Relative knob data value between 61 and 67. Multiply with speed parameter?
let increment = (data2 - 64);
switch (data1) {
case KNOB1:
track.pan().inc(increment, 128);
break;
case KNOB1_CLICK:
track.pan().reset();
break;
case KNOB9:
track.volume().inc(increment, 128);
break;
case KNOB9_CLICK:
track.volume().reset();
break;
default: {
let ccIndex = KNOBS_LEFT_.indexOf(data1);
if (ccIndex > -1) {
ccKnobs.getControl(ccIndex).inc(increment, 128);
} else {
let macroIndex = KNOBS_RIGHT.indexOf(data1);
if (macroIndex > -1) {
macros.getParameter(macroIndex).inc(increment, 128);
}
}
}
}
}
//println('Status: ' + status + ', Data1: ' + data1 + ', Data2: ' + data2);
}
function flush() {
}
function exit() {
}