Setting, getting and sending buttons, sliders and x-y controls

main
Mike Lynch 2022-04-02 14:58:49 +11:00
parent 95a32d81c7
commit 5d3dd866eb
2 changed files with 71 additions and 184 deletions

View File

@ -2,116 +2,42 @@
TouchOSC {
var <ip, <port=9000, controls;
var <net, <controls;
*new { | ip, port |
^super.new.init(ip, port)
}
init { | aip, aport |
ip = aip;
port = aport;
init { | ip, port |
net = NetAddr(ip, port);
controls = ();
}
add1d { | url, scale, apply |
controls.put(url, TouchOSCControl(url, scale, apply));
send { | url ... oargs |
net.sendMsg(url, *oargs)
}
addbutton { | url, default, apply |
var ctrl = TouchOSCControl(this, url, apply, default);
ctrl.init();
controls.put(url, ctrl);
^ctrl;
}
addslider { | url, default, scale, apply |
var ctrl = TouchOSCControlScale(this, url, apply, default, scale);
ctrl.init();
controls.put(url, ctrl);
^ctrl;
}
add2d { | url, scale1, scale2, apply |
controls.put(url, TouchOSCControl2d(url, scale1, scale2, apply));
addxy { | url, default, scale1, scale2, apply |
var ctrl = TouchOSCControlXY(this, url, apply, default, scale1, scale2);
ctrl.init();
controls.put(url, ctrl);
^ctrl;
}
addbutton { | url, apply |
controls.put(url, TouchOSCControlButton(url, apply));
}
}
// // TODO: each of these needs to be able to write its value back
// // to its TouchOSC control - this should be a fairly simple
// // method like ~ctrlset and ~ctrlget
//
// // then call all of those on an iterator at startup to write the
// // defaults to the controller
//
// ~mset = {
// | name, oscurl, min, max, default, apply=({}), ctrlset=(~ctrlset), ctrlget=(~ctrlget), ctrlsend=(~ctrlsend) |
// ~sets.put(name, (
// name: name,
// oscurl: oscurl,
// default: default,
// min: min,
// max: max,
// v: default,
// ctrlset: ctrlset,
// ctrlget: ctrlget,
// ctrlsend: ctrlsend,
// apply: apply
// ));
// OSCdef.new(
// 'osc' ++ name,
// { | msg |
// ~sets.at(name).ctrlset(msg);
// ~sets.at(name).apply() },
// oscurl
// );
// };
//
//
//
//
//
// }
//
//
//
//
// ~sets = ();
//
// // The following two functions are the default methods for going from
// // touchOSC control settings (0-1) to setting values as defined by min,max.
// // Default uses linlin - for linexp or fancier stuff, override them.
// // It's up to you to make sure they're mathematically inverse.
//
//
//
// ~ctrlset = { | self, msg | self.v = msg[1].linlin(0, 1, self.min, self.max); };
//
// ~ctrlget = { | self | self.v.linlin(self.min, self.max, 0, 1) };
//
// ~ctrlexpset = { | self, msg | self.v = msg[1].linexp(0, 1, self.min, self.max); };
//
// ~ctrlexpget = { | self | self.v.linlin(self.min, self.max, 0, 1) };
//
//
// // getter and setter for an x-y control - the default, max and min are arrays
// // of [ x, y ] pairs
//
// // note: msg is what we get from the OSC and x = 1, y = 2
//
// ~ctrlxyset = {
// | self, msg |
// self.v[0] = msg[1].linlin(0, 1, self.min[0], self.max[0]);
// self.v[1] = msg[2].linlin(0, 1, self.min[1], self.max[1]);
// };
//
// ~ctrlxyget = {
// | self |
// var vals = [ 0, 0 ];
// vals[0] = self.v[0].linlin(self.min[0], self.max[0], 0, 1);
// vals[1] = self.v[1].linlin(self.min[1], self.max[1], 0, 1);
// vals;
// };
//
//
// // ~ctrlsend sends the current self.v back to the TouchOSC control,
// // for when we send the defaults or load a patch
//
// ~ctrlsend = {
// | self |
// var ctrlval = self.ctrlget();
// [ "ctrlsend", self.oscurl, ctrlval ].postln;
// ~touchosc.sendMsg(self.oscurl, ctrlval);
// };

View File

@ -1,49 +1,63 @@
// 1d - fader, radial
// 2d - xy, radar
// button
// radio
TouchOSCControl {
var <url, <value, apply, scale;
var <touchOSC, <url, apply, <value;
*new { | url, scale, apply |
^super.new.init(url, scale, apply)
*new { | touchOSC, url, apply, value |
^super.newCopyArgs(touchOSC, url, apply, value)
}
init { | aurl, ascale, aapply |
url = aurl;
scale = ascale;
apply = aapply;
value = scale.default;
init {
OSCdef.new(
'osc' ++ url,
{ | msg |
value = scale.tovalue(msg[1]);
value = msg[1];
apply.value(value)
},
url
);
this.send();
}
value_ { |newval|
value = newval;
this.send();
}
send {
touchOSC.send(url, value);
}
}
// inheritance is too awkward or I don't understand it well enough
TouchOSCControlScale : TouchOSCControl {
var scale;
TouchOSCControl2d {
var <url, <value, scale1, scale2, apply;
*new { | url, scale1, scale2, apply |
^super.new.init(url, scale1, scale2, apply)
*new { | touchOSC, url, apply, value, scale |
^super.newCopyArgs(touchOSC, url, apply, value, scale)
}
init { | aurl, ascale1, ascale2, aapply |
url = aurl;
scale1 = ascale1;
scale2 = ascale2;
apply = aapply;
value = [ scale1.default, scale2.default ];
init {
OSCdef.new(
'osc' ++ url,
{ | msg |
value = scale.tovalue(msg[1]);
apply.value(value);
},
url
);
this.send();
}
}
TouchOSCControlXY : TouchOSCControl {
var scale1, scale2;
*new { | touchOSC, url, apply, value, scale1, scale2 |
^super.newCopyArgs(touchOSC, url, apply, value, scale1, scale2)
}
init {
OSCdef.new(
'osc' ++ url,
{ | msg |
@ -53,46 +67,22 @@ TouchOSCControl2d {
},
url
);
this.send();
}
}
// For buttons and radio - a TouchOSCControl which just gets values
// and doesn't have a scale
TouchOSCControlButton {
var <url, <value, apply;
*new { | url, apply |
^super.new.init(url, apply)
send {
touchOSC.send(url, value[0], value[1]);
}
init { | aurl, aapply |
url = aurl;
apply = aapply;
value = 0;
OSCdef.new(
'osc' ++ url,
{ | msg | apply.value(msg[1]) },
url
);
}
}
TouchOSCScale {
var <min, <max, <default;
var <min, <max;
*new { | min, max, default |
^super.new.init(min, max, default)
}
init { | amin, amax, adefault |
min = amin;
max = amax;
default = adefault;
*new { | min, max |
^super.newCopyArgs(min, max)
}
tovalue { | c | ^c.linlin(0, 1, min, max) }
@ -111,32 +101,3 @@ TouchOSCScaleExp : TouchOSCScale {
}
// ~ctrlset = { | self, msg | self.v = msg[1].linlin(0, 1, self.min, self.max); };
//
// ~ctrlget = { | self | self.v.linlin(self.min, self.max, 0, 1) };
//
// ~ctrlexpset = { | self, msg | self.v = msg[1].linexp(0, 1, self.min, self.max); };
//
// ~ctrlexpget = { | self | self.v.linlin(self.min, self.max, 0, 1) };
//
//
// // getter and setter for an x-y control - the default, max and min are arrays
// // of [ x, y ] pairs
//
// // note: msg is what we get from the OSC and x = 1, y = 2
//
// ~ctrlxyset = {
// | self, msg |
// self.v[0] = msg[1].linlin(0, 1, self.min[0], self.max[0]);
// self.v[1] = msg[2].linlin(0, 1, self.min[1], self.max[1]);
// };
//
// ~ctrlxyget = {
// | self |
// var vals = [ 0, 0 ];
// vals[0] = self.v[0].linlin(self.min[0], self.max[0], 0, 1);
// vals[1] = self.v[1].linlin(self.min[1], self.max[1], 0, 1);
// vals;
// };