-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMantaCLI.sc
427 lines (382 loc) · 14.2 KB
/
MantaCLI.sc
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
/*
MantaCLI
This class is intended to use with the MantaOSC or MantaMIDI command-line
applications that come with the libmanta library. It allows you to register
callbacks for touch events and control the leds.
*/
MantaCLI {
classvar <padMaps;
// global callbacks (called regardless of selected page)
var >onPadVelocity;
var >onButtonVelocity;
var >onPadValue;
var >onSliderValue;
var >onSliderAccum;
var device;
var pages;
// we need to keep track of the touch state so we can freeze things with the pedal
var pedalDown = false;
var activePads;
var sustainedPads;
var activePageIdx;
var lastSliderValue;
var sliderAccum;
var >debug = false;
// the scale is tweaked here to make it easier for a full swipe of the
// slider to generate about a 0-1 accumulated value. This is used both
// within MantaCLI and MantaPage
const <sliderScale = 1.17;
// maximum value reported for a Pad Value/Velocity. We use these to scale
// the values to 0-1 in the callbacks
const maxPadValue = 208;
const maxPadVelocity = 105;
*initClass {
// TODO: make pad mapping more convenient - probably add a `setPadMap` function
// that makes it so all indices in the callbacks are mapped
padMaps = (
\wickihayden: #[
0, 2, 4, 6, 8, 10, 12, 14,
7, 9, 11, 13, 15, 17, 19, 21,
12, 14, 16, 18, 20, 22, 24, 26,
19, 21, 23, 25, 27, 29, 31, 33,
24, 26, 28, 30, 32, 34, 36, 38,
31, 33, 35, 37, 39, 41, 43, 45]);
}
*new {| protocol=\midi ...deviceArgs |
var device;
switch (protocol,
\osc, { device = MantaOSCDevice(*deviceArgs) },
\midi, { device = MantaMIDIDevice(*deviceArgs) },
{"Protocol must be one of \\osc or \\midi".error; ^nil; });
if(device.isNil, {
"Couldn't initialize device".error; ^nil;
})
^super.new.init(device);
}
init { | dev |
device = dev;
pages = [];
lastSliderValue = [nil, nil];
sliderAccum = [0, 0];
activePads = ();
sustainedPads = ();
device.onPadVelocity = { | idx, rawVelocity |
pedalDown.if {
sustainedPads[idx].notNil.if {
// this is a sustained pad, so we just update the state
sustainedPads[idx] = rawVelocity;
} {
// pedal is down but this isn't a sustained pad, so it behaves as normal
this.triggerPadVelocity(idx, rawVelocity);
activePads[idx] = (rawVelocity > 0).if(rawVelocity, nil);
}
} {
this.triggerPadVelocity(idx, rawVelocity);
activePads[idx] = (rawVelocity > 0).if(rawVelocity, nil);
}
};
device.onPadValue = { | idx, rawValue |
sustainedPads[idx].isNil.if {
this.triggerPadValue(idx, rawValue);
}
};
device.onButtonVelocity = { | idx, rawVelocity |
this.triggerButtonVelocity(idx, rawVelocity);
};
device.onSliderValue = { | idx, rawValue |
this.triggerSliderValue(idx, rawValue);
};
}
newPage {
var page = MantaPage.new(device);
pages = pages.add(page);
if(activePageIdx.isNil, { activePageIdx = 0 });
^page;
}
enableLedControl {
| enabled=true |
device.enableLedControl(enabled);
if(debug, {"MantaCLI: LED Control %\n".postf(enabled)});
}
draw {
if(activePageIdx.notNil, { pages[activePageIdx].draw; });
}
pedal_ {
| enabled |
pedalDown = enabled;
enabled.if {
sustainedPads = activePads;
activePads = ();
} {
sustainedPads.keys.do {
| idx |
(sustainedPads[idx] > 0).if {
activePads[idx] = sustainedPads[idx];
} {
// this pad was active when the pedal went down but was inactive when the pedal went up
this.triggerPadVelocity(idx, 0);
}
};
sustainedPads = ();
}
}
triggerPadVelocity {| idx, rawVelocity |
var row = idx.div(8);
var column = idx.mod(8);
var value = min(1.0, rawVelocity / maxPadVelocity);
onPadVelocity.value(idx, value, row, column);
if(activePageIdx.notNil, {
pages[activePageIdx].onPadVelocity.value(idx, value, row, column);
});
if(debug, {"MantaCLI: Row %, Column %, Velocity %\n".postf(row, column, value)});
}
triggerPadValue {| idx, rawValue |
var row = idx.div(8);
var column = idx.mod(8);
var value = min(1.0, rawValue / maxPadValue);
onPadValue.value(idx, value, row, column);
if(activePageIdx.notNil, {
pages[activePageIdx].onPadValue.value(idx, value, row, column);
});
if(debug, {"MantaCLI: Pad % (%, %), Value %\n".postf(idx, row, column, value)});
}
triggerButtonVelocity {| idx, rawVelocity |
var velocity = min(1.0, rawVelocity / maxPadVelocity);
onButtonVelocity.value(idx, velocity);
if(activePageIdx.notNil, { pages[activePageIdx].onButtonVelocity.value(idx, velocity); });
if(activePageIdx.notNil && (idx == 0) && (velocity > 0), {
// cycle through the pages
activePageIdx = (activePageIdx + 1).mod(pages.size);
this.draw;
});
if(debug, {"MantaCLI: Button %, Velocity %\n".postf(idx, velocity)});
}
triggerSliderValue {| idx, rawValue |
var value = if(rawValue == 65535, nil, rawValue/4096);
if(value.notNil && lastSliderValue[idx].notNil, {
var diff = value - lastSliderValue[idx];
sliderAccum[idx] = sliderAccum[idx] + (diff*sliderScale);
onSliderAccum.value(idx, sliderAccum[idx]);
if(debug, {"MantaCLI: Slider %, AccumValue %\n".postf(idx, sliderAccum[idx])});
});
onSliderValue.value(idx, value);
if(activePageIdx.notNil, {
pages[activePageIdx].onSliderValue.value(idx, value);
pages[activePageIdx].updateSlider(idx, value);
});
lastSliderValue[idx] = value;
if(debug, {"MantaCLI: Slider %, Value %\n".postf(idx, value)});
}
}
// internal class used for protocol-specific behavior
MantaMIDIDevice {
var midiSender;
// device-level callbacks
var >onPadVelocity;
var >onButtonVelocity;
var >onPadValue;
var >onSliderValue;
var currentLEDState; // 0: off, 1: amber, 2: red
const sendChan = 0;
const receiveChan = 0;
*new { | receivePort=#["Manta Out", "Manta Out"],
sendPort=#["Manta In", "Manta In"] |
^super.new.init(receivePort, sendPort);
}
init { | receivePort, sendPort |
var port;
midiSender = MIDIOut.newByName(sendPort[0], sendPort[1]);
if(midiSender.isNil, {
"Couldn't find output port \"%\":\"%\". Is MIDIClient Initialized?"
.format(*sendPort)
.error;
^nil;
});
port = MIDIIn.findPort(receivePort[0], receivePort[1]);
if(port.isNil, {
"Couldn't find input port \"%\":\"%\". Is MIDIClient Initialized?"
.format(*receivePort)
.error;
^nil;
});
// make these MIDIdefs if you create a new MantaCLI you don't end up
// with leftover methods firing. The receivePort is in the names so you
// can have multiple MantaCLIs with different Mantas
MIDIdef.noteOn(("noteon" ++ receivePort).asSymbol,
{ | val, num, chan, src |
case(
{num < 48}, { onPadVelocity.value(num, val); },
{(100 <= num) && (num < 104)}, { onButtonVelocity.value(num-100, val); },
{"MantaCLI: Got unexpected noteOn: note %, vel %".format(num, val).warn }
)
}, nil, receiveChan, port.uid);
MIDIdef.noteOff(("noteoff" ++ receivePort).asSymbol,
{ | val, num, chan, src |
case(
{num < 48}, { onPadVelocity.value(num, val); },
{(100 <= num) && (num < 104)}, { onButtonVelocity.value(num-100, val); },
{"MantaCLI: Got unexpected noteOff: note %, vel %".format(num, val).warn }
)
}, nil, receiveChan, port.uid);
MIDIdef.cc(("cc" ++ receivePort).asSymbol,
{ | val, num, chan, src |
case(
{num < 48}, { onPadValue.value(num, val); },
{(100 <= num) && (num < 104)}, { /* ignore button value*/ },
{(104 <= num) && (num < 106)}, { this.triggerSliderValue(num-104, val); },
{"MantaCLI: Got unexpected MIDI CC: param %, vel %".format(num, val).warn }
)
}, nil, receiveChan, port.uid);
currentLEDState = 0!48;
}
enableLedControl {
| enabled=true |
"MantaCLI: enableLedControl has no effect when communicating with the Manta over MIDI".warn;
}
// takes an array for the amber LEDs and redLEDs. Each element of the array
// is the number of active requests to turn on that LED
draw { | amberLeds, redLeds |
48.do { | idx |
var desiredState = case(
{redLeds[idx] > 0}, {2},
{amberLeds[idx] > 0}, {1},
{0}
);
if(desiredState != currentLEDState[idx], {
// 0 turns LED off, 1-64 (or 65?) turns it amber, >65 turns it red
midiSender.noteOn(sendChan, idx+1, desiredState*50);
currentLEDState[idx] = desiredState;
})
}
}
triggerSliderValue { | idx, value |
// values get squished down to 0-127 to fit through MIDI. :(
if(value == 127, {
// this signals when the user takes their finger off
onSliderValue.value(idx, 65535)
}, {
onSliderValue.value(idx, value * 4096 / 127)
})
}
}
// internal class used for protocol-specific behavior
MantaOSCDevice {
var oscSender;
// device-level callbacks
var >onPadVelocity;
var >onButtonVelocity;
var >onPadValue;
var >onSliderValue;
*new { | receivePort=31416, sendPort=31417 |
^super.new.init(receivePort, sendPort);
}
// we put most of the setup in this init method so we have access to the
// internal properties
init { | receivePort, sendPort |
oscSender = NetAddr("localhost", sendPort);
// we include the receive port in the OSCdef name to make sure that
// there's only 1 OSCdef per receive port, but we can have multiple
// mantas on different ports
OSCdef(("padvalue-" ++ receivePort).asSymbol, {
| msg |
onPadValue.value(msg[1], msg[2])
}, '/manta/continuous/pad', recvPort: receivePort);
OSCdef(("slidervalue-" ++ receivePort).asSymbol, {
| msg |
onSliderValue.value(msg[1], msg[2]);
}, '/manta/continuous/slider', recvPort: receivePort);
OSCdef(("padvelocity-" ++ receivePort).asSymbol, {
| msg |
onPadVelocity.value(msg[1], msg[2]);
}, '/manta/velocity/pad', recvPort: receivePort);
OSCdef(("buttonvelocity-" ++ receivePort).asSymbol, {
| msg |
onButtonVelocity.value(msg[1], msg[2]);
}, '/manta/velocity/button', recvPort: receivePort);
}
// takes an array for the amber LEDs and redLEDs. Each element of the array
// is the number of active requests to turn on that LED
draw { | amberLeds, redLeds |
// each byte of the mask represents a row of LEDs. First is the 6 rows of amber, then the 6 rows of red
var mask = Int8Array[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
[[amberLeds, 0], [redLeds, 6]].do {
| args |
var leds = args[0];
var maskOffset = args[1];
leds.do {
| padcount, idx |
if(padcount > 0, {
var row = idx.div(8);
var column = idx.mod(8);
mask[row+maskOffset] = mask[row+maskOffset] | (0x80 >> column);
})
};
};
oscSender.sendMsg('/manta/led/pad/frame', "all", mask);
}
enableLedControl {
| enabled=true |
oscSender.sendMsg('/manta/ledcontrol', "padandbutton", if(enabled, 1, 0));
}
}
/*
Often Manta-based apps will want several pages to manipulate different aspects of the app's state. A MantaPage holds all the LED state for that page, as well as allows the user to register callbacks that only apply when that page is active.
*/
MantaPage {
var device;
var padLeds;
var <>onPadVelocity; // callback for pad velocity (note on/off) events
var <>onButtonVelocity; // callback for button velocity (note on/off) events
var <>onPadValue; // callback for pad value events
var <>onSliderValue; // callback for slider value events
var <>onSliderAccum; // callback for accumulated slider events
var lastSliderValue;
var sliderAccum;
*new {
| device |
^super.new.init(device);
}
init { | dev |
device = dev;
padLeds = (amber: 0!48, red: 0!48);
lastSliderValue = [nil, nil];
sliderAccum = [0, 0];
}
setPadByIndex { | idx, color=\amber |
padLeds[color][idx] = padLeds[color][idx] + 1;
}
setPadByRowCol { | row, col, color=\amber |
this.setPadByIndex(row*8+col, color);
}
clearPadByIndex { | idx, color=\amber |
if(padLeds[color][idx] > 0, {
padLeds[color][idx] = padLeds[color][idx] - 1;
}, {
"% Pad % (row %, column %) cleared too many times\n"
.format(color, idx, idx.div(8), idx.mod(8))
.warn;
});
}
clearPadByRowCol { | row, col, color=\amber |
this.clearPadByIndex(row*8+col, color);
}
resetPadByIndex { | idx |
padLeds[\amber][idx] = 0;
padLeds[\red][idx] = 0;
}
resetPadByRowCol { | row, col |
this.resetPadByIndex(row*8+col);
}
draw {
device.draw(padLeds[\amber], padLeds[\red]);
}
updateSlider {
| id, value |
if(value.notNil && lastSliderValue[id].notNil, {
var diff = value - lastSliderValue[id];
sliderAccum[id] = sliderAccum[id] + (diff * MantaCLI.sliderScale);
onSliderAccum.value(id, sliderAccum[id]);
});
lastSliderValue[id] = value;
}
}