GameOfLife / Unit-Lib

The Unit Library is a system that provides high level abstractions on top of the SuperCollider language.
25 stars 6 forks source link

integration with touch surfaces via osc #44

Open miguel-negrao opened 10 years ago

miguel-negrao commented 10 years ago

Would be nice to be able to click on a button on the gui of a UChain and make all the parameters appear as virtual sliders on tablet / smartphone or via repositioning midi faders/encoders.

miguel-negrao commented 10 years ago

A sketch. It uses a touchosc layout for the iphone available here. Namepace (from FPLib) can be changed to a dictionary or event, in which case passing of functions has to be done with keys, i.e. q[\updateFunc]. The last page of the layout has buttons, pressing the first button will load the first unit of UChain.current, the second button will load the second unit, etc...

//debug osc
OSCFunc.trace(false, true)

//osc <-> uchain
//setup
v = NetAddr("172.20.10.1", 57130)

//clean
(
q.controller !? _.remove;
q.oscfuncs.do{ |x| x.postln.free };
q.updateOscFunc.free;
)

//start
(
(4*12).do{ |i|
        v.sendMsg("/%/fader%".format(i.div(12)+1,i+1).asSymbol.postln,0);
        v.sendMsg("/%/label%".format(i.div(12)+1,i+1).asSymbol,"");
    };
)

(
q !? _.updateOscFuncs !? { |x| x.do(_.free) };

q = Namespace();

q.oscfuncs = [];

q.init = { |chain, i|

    var unit = chain.units[i];
    var keys = unit.keys.select{ |x| "u_.*".matchRegexp(x.asString).not };

    q.controller !? _.remove;
    q.oscfuncs.do{ |x| x.free };

    //clean state
    (4*12).do{ |i|
        v.sendMsg("/%/fader%".format(i.div(12)+1,i+1).asSymbol.postln,0);
        v.sendMsg("/%/label%".format(i.div(12)+1,i+1).asSymbol,"");
    };

    //supercollider -> touchosc
    q.controller = SimpleController(unit);
    keys.do{ |key, i|
        var oscpath = "/%/fader%".format(i.div(12)+1,i+1).asSymbol;
        q.controller.put(key, { |obj, key, value|
            v.sendMsg(oscpath, unit.mapGet(key))
        });
        v.sendMsg(oscpath.postln, unit.mapGet(key).postln);
        v.sendMsg( "/%/label%".format(i.div(12)+1,i+1).asSymbol.postln, key.asString.postln);
    };

    //touchosc -> supercollider
    q.oscfuncs = keys.collect{ |key, i|
        var oscpath = "/%/fader%".format(i.div(12)+1,i+1).asSymbol;
        OSCFunc({ |msg|
            unit.mapSet(key, msg[1])
        },oscpath);
    };
};

//change unit and chain
q.updateFunc = { |i|
    var x = UChain.current;
    if( x.notNil ) {
        "OSC: Switching to unit % of %".format(i+1,x.cs).postln;
        q.init.(x,i)
    } {
        "OSC: No active UChain".postln;
    }
};

q.updateOscFuncs = 14.collect{ |i|
    OSCFunc({ q.updateFunc.(i) },"/5/push%".format(i+1).asSymbol);
};
)