-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnanoktl-objects.scd
280 lines (264 loc) · 9.05 KB
/
nanoktl-objects.scd
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
// nanoKontrol interface objects
var saveSubtype = AbstractChuckArray.defaultSubType;
var parentEnvir = currentEnvironment;
/**
Chucklib-livecode: A framework for live-coding improvisation of electronic music
Copyright (C) 2018 Henry James Harkins
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
if(PR.exists(\abstractTouch).not) {
(thisProcess.nowExecutingPath.dirname +/+ "mobile-objects.scd").load;
};
protect {
PR(\mix16Touch).clone {
~uid = nil;
~chan = nil;
// jeez. asPaddedString isn't core. OK. fine. whatever.
~padString = { |string, size, padWithChar($0)|
string = string.asString; // if it's, say, a number as below
if(string.size < size) {
String.fill(size - string.size, padWithChar) ++ string
} {
string
}
};
// flat array of pairs: ccnum, oscpath, ccnum, oscpath etc.
// for GUI definition below, zero-padding the index is important!
~pathTable = [
[(0..7), (16..23)].flat,
[(64..71), (32..39)].flat
].collect { |row, i|
var prefix = #["/1/fader", "/1/toggle"][i];
row.collect { |ccnum, j| [ccnum, (prefix ++ ~padString.(j+1, 2)).asSymbol] }
}.flat;
~keepAlive = false;
~prep = {
~pathsForCC = Array.newClear(128);
~pathTable.pairsDo { |ccnum, path|
~pathsForCC[ccnum] = path;
};
// special case for presetbrowser
~pathsForCC[45] = '/1/push5';
// if we do this now, it delays server init messages
NotificationCenter.registerOneShot(\clInterface, \ready, \nanoKtl, {
if(MIDIClient.initialized.not) { MIDIIn.connectAll };
});
~resp = MIDIFunc.cc(inEnvir { |value, ccnum, chan|
var path;
path = ~pathsForCC[ccnum];
if(path.notNil) {
// downstream may use 'nil' to detect that it's coming from MIDI
~respond.([path, value / 127.0], SystemClock.seconds, nil, nil)
};
}, chan: ~chan, srcID: ~uid);
~data = IdentityDictionary.new; // save all incoming data by oscpath
~syncSigns = IdentityDictionary.new;
if(~labels.isNil) { ~labels = IdentityDictionary.new };
~setDataKeys.();
// touch interface needs to wait for ping; MIDI doesn't
// so signal that it's OK to move ahead
parentEnvir[\pingStatus] = true;
if(parentEnvir[\pingCond].notNil) {
parentEnvir[\pingCond].signalAll;
};
currentEnvironment
};
~freeCleanup = {
~resp.free;
NotificationCenter.notify(currentEnvironment, \modelWasFreed);
};
~setLabel = { |oscpath, label|
if(label.isNil) { label = "" /*oscpath.asString.split($/).last*/ };
~labels[oscpath] = label;
NotificationCenter.notify(currentEnvironment, \any, [[\label, oscpath, label]]);
currentEnvironment
};
// setValue and related methods supply local address
// so, if there's an address, we know we should override saved value
// if replyAddr is nil, the new value comes from MIDI
// and we should sync up if there's a saved value
~respond = { |msg, time, replyAddr, recvPort, guiOnly(false)|
if(msg[0].asString[3] == $f) {
~respondFader.(msg, time, replyAddr, recvPort, guiOnly);
} {
~respondToggle.(msg, time, replyAddr, recvPort, guiOnly);
};
};
// syncSigns:
// nil = not synced, check before changing real value
// 0 = synced, pass value through
// -1 = MIDI value is less than old value
~respondFader = { |msg, time, replyAddr, recvPort, guiOnly(false)|
var args = [msg, time, replyAddr, recvPort];
var sign, data;
// [msg, time, replyAddr, recvPort, guiOnly].debug("respond");
if(replyAddr.notNil) {
if(
(msg[1] ?? { -2 }) absdif: (~data[msg[0]] ?? { -3 }) > 0.007 // 1/127 ~= 0.00787
) {
~syncSigns[msg[0]] = nil;
};
~prUpdateControl.(msg, args, guiOnly);
} {
// MIDI: check sync
sign = ~syncSigns[msg[0]];
case
{ sign == 0 } {
~prUpdateControl.(msg, args, guiOnly);
}
{ sign.isNil } {
data = ~data[msg[0]];
if(data.notNil) {
~syncSigns[msg[0]] = sign(msg[1] - data);
} {
// no existing data, MIDI is first to set
~syncSigns[msg[0]] = 0;
~prUpdateControl.(msg, args, guiOnly);
}
}
{
// sync should get close or cross over
data = ~data[msg[0]];
if(data absdif: msg[1] <= 0.016 or: { // 2/127 ~= 0.016
sign(msg[1] - data) != sign
}) {
~syncSigns[msg[0]] = 0;
~prUpdateControl.(msg, args, guiOnly);
};
};
};
};
// toggle:
// if never used before (nothing in ~data), assume we are switching on
// otherwise read value and flip
~respondToggle = { |msg, time, replyAddr, recvPort, guiOnly(false)|
var args = [msg, time, replyAddr, recvPort];
var old;
if(replyAddr.notNil) { // again, if setting from client (not MIDI)
~prUpdateControl.(msg, args, guiOnly);
} {
if(msg[1] > 0) {
old = ~data[msg[0]];
if(old.isNil) {
msg[1] = 1; // assume flipping on
} {
msg[1] = (old == 0).asInteger;
};
~prUpdateControl.(msg, args, guiOnly);
};
};
};
~prUpdateControl = { |msg, args, guiOnly(false)|
if(~saveKeys.includes(msg[0])) {
~saveValueFromMsg.(msg);
};
NotificationCenter.notify(currentEnvironment, \any, args);
if(guiOnly.not) {
NotificationCenter.notify(currentEnvironment, msg[0], args);
};
};
~saveValueFromMsg = { |msg|
if(msg.size == 2) {
// "saveValueFromMsg".debug;
~data[msg[0]] = msg[1] //.dump
} {
~data[msg[0]] = msg[1..];
};
};
~fullWidth = 200;
~sliderWidth = ~fullWidth - (2 * (~buttonExtent.x + ~gap.x));
~tabSpecs = [
"Tab1", {
var out = Array(64), // 8 rows: button, slider, button, knob with names
origin = ~gap.copy,
oneBound = Rect(0, 0, ~sliderWidth, ~buttonExtent.y),
buttonExtent = ~buttonExtent,
gap = ~gap.y,
// must save colors because GUI object init runs in another proto
ltAqua = ~ltAqua, aqua = ~aqua,
green = ~green, ltGreen = ~ltGreen,
sBkColor = ~sliderBG;
8.do { |i|
out.add("/1/toggle%".format(~padString.(i+1, 2)).asSymbol).add((
bounds: Rect.fromPoints(
origin + Point(gap.neg, 0),
origin + Point(buttonExtent.x - gap, buttonExtent.y)
),
class: Button,
init: { |view|
// init func runs in the touchGui environment
view.states_([[" ", nil, ltAqua], ["", nil, aqua]])
.receiveDragHandler_(inEnvir { |view|
// we don't have access to the 't' touch object
// notification assumes there's just one (untested with multiple)
NotificationCenter.notify(~model, \receiveDrag, [View.currentDrag, i]);
})
},
))
.add("/1/fader%".format(~padString.(i+1, 2)).asSymbol).add((
bounds: Rect.fromPoints(
origin + Point(buttonExtent.x, 0),
origin + oneBound.extent
),
class: Slider,
// I considered implementing receive-drag on the slider too but it didn't work
init: { |view| view.knobColor_(green).background_(sBkColor) },
spec: [0, 1]
))
.add("/1/toggle%".format(~padString.(i+9, 2)).asSymbol).add((
bounds: Rect.fromPoints(
origin + Point(gap + ~sliderWidth, 0),
origin + Point(buttonExtent.x + ~sliderWidth + gap, buttonExtent.y)
),
class: Button,
init: { |view|
// init func runs in the touchGui environment
view.states_([[" ", nil, ltAqua], ["", nil, aqua]])
.receiveDragHandler_(inEnvir { |view|
// we don't have access to the 't' touch object
// notification assumes there's just one (untested with multiple)
NotificationCenter.notify(~model, \receiveDrag, [View.currentDrag, i+8]);
})
},
))
.add("/1/fader%".format(~padString.(i+9, 2)).asSymbol).add((
bounds: Rect.fromPoints(
origin + Point(~fullWidth - buttonExtent.x, 0),
origin + Point(~fullWidth, buttonExtent.y)
),
class: Knob,
// I considered implementing receive-drag on the slider too but it didn't work
init: { |view|
view.color_([ltGreen, green, ltGreen, green])
.mode_(\vert)
},
spec: [0, 1]
));
origin.y = origin.y + oneBound.height + gap;
};
out
}.value
];
~chuckOSCKeys = {
var allKeys = ~saveKeys.collect(_.asString),
keys, faderKeys, toggleKeys;
keys = sort(allKeys.select({ |key|
key[1] == $1 and: { "ft".includes(key[3]) }
}).as(Array));
faderKeys = keys.select { |key| key[3] == $f }.collect(_.asSymbol);
toggleKeys = keys.select { |key| key[3] == $t }.collect(_.asSymbol);
(keys: keys, faderKeys: faderKeys, toggleKeys: toggleKeys)
};
} => PR(\nanoTouch); // 'touch' is not accurate for MIDI, but reflects object design
} {
AbstractChuckArray.defaultSubType = saveSubtype;
};