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 { TouchOSC {
var <ip, <port=9000, controls; var <net, <controls;
*new { | ip, port | *new { | ip, port |
^super.new.init(ip, port) ^super.new.init(ip, port)
} }
init { | aip, aport | init { | ip, port |
ip = aip; net = NetAddr(ip, port);
port = aport;
controls = (); controls = ();
} }
add1d { | url, scale, apply | send { | url ... oargs |
controls.put(url, TouchOSCControl(url, scale, apply)); 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 | addxy { | url, default, scale1, scale2, apply |
controls.put(url, TouchOSCControl2d(url, 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 { TouchOSCControl {
var <url, <value, apply, scale; var <touchOSC, <url, apply, <value;
*new { | url, scale, apply | *new { | touchOSC, url, apply, value |
^super.new.init(url, scale, apply) ^super.newCopyArgs(touchOSC, url, apply, value)
} }
init { | aurl, ascale, aapply | init {
url = aurl;
scale = ascale;
apply = aapply;
value = scale.default;
OSCdef.new( OSCdef.new(
'osc' ++ url, 'osc' ++ url,
{ | msg | { | msg |
value = scale.tovalue(msg[1]); value = msg[1];
apply.value(value) apply.value(value)
}, },
url 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 { *new { | touchOSC, url, apply, value, scale |
var <url, <value, scale1, scale2, apply; ^super.newCopyArgs(touchOSC, url, apply, value, scale)
*new { | url, scale1, scale2, apply |
^super.new.init(url, scale1, scale2, apply)
} }
init { | aurl, ascale1, ascale2, aapply | init {
url = aurl; OSCdef.new(
scale1 = ascale1; 'osc' ++ url,
scale2 = ascale2; { | msg |
apply = aapply; value = scale.tovalue(msg[1]);
value = [ scale1.default, scale2.default ]; 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( OSCdef.new(
'osc' ++ url, 'osc' ++ url,
{ | msg | { | msg |
@ -53,46 +67,22 @@ TouchOSCControl2d {
}, },
url url
); );
this.send();
} }
} send {
touchOSC.send(url, value[0], value[1]);
// 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)
} }
init { | aurl, aapply |
url = aurl;
apply = aapply;
value = 0;
OSCdef.new(
'osc' ++ url,
{ | msg | apply.value(msg[1]) },
url
);
}
} }
TouchOSCScale { TouchOSCScale {
var <min, <max, <default; var <min, <max;
*new { | min, max, default | *new { | min, max |
^super.new.init(min, max, default) ^super.newCopyArgs(min, max)
}
init { | amin, amax, adefault |
min = amin;
max = amax;
default = adefault;
} }
tovalue { | c | ^c.linlin(0, 1, 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;
// };