collaborative_capacitive/sc/live_touch_instrument.scd

282 lines
9.3 KiB
Plaintext

/*RUN*/
s.options.numInputBusChannels = 4;
s.options.numOutputBusChannels = 4;
b = NetAddr.new("192.168.0.116", 8888); // create the NetAddr
//b.sendMsg("/led", [0,1].choose); // send the application the message "hello" with the parameter "there"
//OSCFunc.trace(true); // Turn posting on
//OSCFunc.trace(false); // Turn posting off
s.waitForBoot{
///////////////////////////////////////////////////////////////////////////
// set path for sample location and make dictionary of subdirectories
//~samplepath = thisProcess.nowExecutingPath.dirname ++ "/samples";
~samplepath = "/home/rob/pifcamp2023/sc/samples"; // CHANGE THIS TO YOUR OWN PATH
~smp = Dictionary.new;
PathName(~samplepath).entries.do{
arg subfolder;
~smp.add(
subfolder.folderName.asSymbol ->
Array.fill(
subfolder.entries.size,
{ arg i; Buffer.read(s, subfolder.entries[i].fullPath).normalize;}
)
);
};
// e.g. green sample subdirectory of ~samplepath: ~smp[\green][1] //
///////////////////////////////////////////////////////////////////////
// allocate busses
~reverbBus = Bus.audio(s, 1);
~ringModBus = Bus.audio(s, 1);
// create group order
~sGA = Group.new; ~sGB = Group.new; ~sGC = Group.new; ~sGD = Group.new;
s.sync;
//// SYNTHS //////////////////////////////////
SynthDef.new(\splay, {| out=0, bufnum=0, tpse=1, t_trig=0, amp=0.8, rate=1,
atk=1, rel=1, loop=0, spos=0, gate=0, panRate=0.2 |
var env, sig;
env= EnvGen.kr(Env.asr(atk,1,rel),gate, doneAction:2);
sig = PlayBuf.ar(1, bufnum, rate*tpse, t_trig, startPos: spos, loop:loop, doneAction: 0);
sig = sig * env * amp;
sig = Pan2.ar(sig, FSinOsc.kr(panRate));
Out.ar(out, sig);
}).add;
s.sync;
~oPA = 8.collect({
arg itt, rate=1, out=0, sdir=\bowl,
amp=0.9, loop=1, panRate=rrand(0.05,0.75), group=~sGA;
Synth.new(\splay, [\bufnum, ~smp[sdir][rrand(0,~smp[sdir].size-1)], \out, out, \loop, loop, panRate: panRate], group);
});
~oPB = 8.collect({
arg itt, rate=1, out=0, sdir=\green,
amp=0.5, loop=1, panRate=rrand(0.05,0.75), group=~sGB;
Synth.new(\splay, [\bufnum, ~smp[sdir][rrand(0,~smp[sdir].size-1)], \out, out, \loop, loop, panRate: panRate], group);
});
// RECEIVE OSC MESSAGES FROM ESP32s
~modeA = 0;
~modeB = 0;
~modeC = 0;
~modeD = 0;
~shiftA = 0; // shift state
~shiftB = 0; // shift state
~shiftC = 0; // shift state
~shiftD = 0; // shift state
~scaleMode = [Scale.partch_o1, Scale.major, Scale.minor, Scale.chromatic];
~scaleA = ~scaleMode[0];
~scaleB = ~scaleMode[3];
~lsA=1; // loop state
~lsB=1; // loop state
~ssA = 0;
~ssB = 0;
//~scales[2].tuning; ~scales[2].size; ~scales[2].pitchesPerOcatave;
s.sync;
~esp32Receive = { arg name ,mac; OSCdef(name, { arg msg, time;
// msg[1] sender mac address
// msg[2] which of the [0 - 11] sensors is touched
// msg[3] is it a touch [1] or a release [0]
// msg[4] which of the connected mpr121 boards is sending
if (msg[1] == mac && msg[3] == 1 && msg[4] == 0 && ~shiftA == 0 ) {
//12.0.rand.round;
// use an array to spread different patterns over the keys
~rpitch = rrand(0,~scaleA.size -1);
postln("first board" + msg[2] + "pitchclass: " + ~rpitch );
b.sendMsg("/led", msg[2], ~rpitch, msg[4]);
w = case
{ msg[2] == 0} { ~oPA[0].set(\rate, ~scaleA[~rpitch].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 1} { ~oPA[1].set(\rate, ~scaleA[0].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
// always have a know root
{ msg[2] == 2} { ~oPA[2].set(\rate, ~scaleA[~rpitch].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 3} { ~oPA[3].set(\rate, ~scaleA[~rpitch].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 4} { ~oPA[4].set(\rate, ~scaleA[~rpitch].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 5} { ~oPA[5].set(\rate, ~scaleA[~rpitch].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 6} { ~oPA[6].set(\rate, ~scaleA[~rpitch].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 7} { ~oPA[7].set(\rate, ~scaleA[~rpitch].midiratio + 1 , \t_trig, 1, \gate, 1); }
// LOOP mode on off
{ msg[2] == 8 } { // touch 7 = toggle looping of all samplers
~lsA = (~lsA + 1)% 2;
~sGA.set(\loop, ~lsA ); // toggle this
b.sendMsg("/led", msg[2], ~lsA, msg[4]);
postln("loop state set as:" + ~lsA + " 7 " + msg[2] );
};
};
// SHIFT logic for board A - 3 function= shift off on extra
///if SHIFT IS DEPRESSED on board 0 - shift enabled while 11 depressed
if (msg[1] == mac && msg[2] == 11 && msg[4] == 0 ) {
if (msg[3]==1){
~shiftA = (~shiftA + 1)% 3;
postln("SHIFT is: " + ~shiftA);
b.sendMsg("/led", msg[2], ~shiftA, msg[4]);
};
};
if (~shiftA==1 && msg[3]==1) {
postln(" shift is pressed " + msg[2]);
w = case
{ msg[2] == 0} { ~oPA[0].set(\tpse, 2, \t_trig, 1); }
{ msg[2] == 1} { ~oPA[1].set(\tpse, 2, \t_trig, 1); }
{ msg[2] == 2} { ~oPA[2].set(\tpse, 2, \t_trig, 1); }
{ msg[2] == 3} { ~oPA[3].set(\tpse, 2, \t_trig, 1); }
{ msg[2] == 4} { ~oPA[4].set(\tpse, 2, \t_trig, 1); }
{ msg[2] == 5} { ~oPA[5].set(\tpse, 2, \t_trig, 1); }
{ msg[2] == 6} { ~oPA[6].set(\tpse, 2, \t_trig, 1); }
{ msg[2] == 7} { ~oPA[7].set(\tpse, 2, \t_trig, 1); }
};
// SCALE CHANGER
if (~shiftA==1 && msg[2] == 8 && msg[3] == 1) {
~ssA = (~ssA + 1) % 4;
// use key release to
~scaleA = ~scaleMode[~ssA];
postln(" shift is pressed - scale changed to: " + ~ssA);
b.sendMsg("/led", 9, ~ssA, msg[4]);
};
//////////////////////////////////////////////////////////////////////////
// MODE logic for board A
if (msg[1] == mac && msg[2] == 10 && msg[4] == 0 ) {
if (msg[3]==1){
// send the esp32 an osc message
~modeA = ~modeA+1 % 3;
b.sendMsg("/led", msg[2], ~modeA, msg[4]); postln(~modeA); };
};
if (msg[1] == mac && msg[3] == 1 && msg[4] == 0 && ~modeA == 1 ) {
w = case
{ msg[2] == 0} { ~oPA[0].set(\tpse, -1); }
{ msg[2] == 1} { ~oPA[1].set(\tpse, -1 ); }
{ msg[2] == 2} { ~oPA[1].set(\tpse, -1 ); }
{ msg[2] == 3} { ~oPA[1].set(\tpse, -1 ); }
{ msg[2] == 4} { ~oPA[1].set(\tpse, -1 ); }
{ msg[2] == 5} { ~oPA[1].set(\tpse, -1 ); }
{ msg[2] == 6} { ~oPA[1].set(\tpse, -1 ); }
{ msg[2] == 7} { ~oPA[1].set(\tpse, -1 ); }
};
// mode 2 randomize transposition
if (msg[1] == mac && msg[3] == 1 && msg[4] == 0 && ~modeA == 2 ) {
w = case
{ msg[2] == 0} { ~oPA[0].set(\tpse, [0.25].choose); }
{ msg[2] == 1} { ~oPA[1].set(\tpse, [0.25].choose ); }
{ msg[2] == 2} { ~oPA[2].set(\tpse, [0.25].choose); }
{ msg[2] == 3} { ~oPA[3].set(\tpse, [0.25].choose ); }
{ msg[2] == 4} { ~oPA[4].set(\tpse, [0.25].choose ); }
{ msg[2] == 5} { ~oPA[5].set(\tpse, [0.25].choose ); }
{ msg[2] == 6} { ~oPA[6].set(\tpse, [0.25].choose ); }
{ msg[2] == 7} { ~oPA[7].set(\tpse, [0.25].choose ); }
};
//////////////////////////////////////////////////////////////////////////
//////////// second board
if (msg[1] == mac && msg[3] == 1 && msg[4] == 1) {
~rpitch = rrand(0,47);
~rpitch = ~rpitch%11; postln("second board" + msg[2] + "color: " + ~rpitch );
b.sendMsg("/led", msg[2], ~rpitch, msg[4]);
w = case
{ msg[2] == 0} { ~oPB[0].set(\rate, Scale.major[0].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 1} { ~oPB[1].set(\rate, Scale.major[1].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 2} { ~oPB[2].set(\rate, Scale.major[2].midiratio, \t_trig, 1, \gate, 1, \tpse, 1); }
{ msg[2] == 3} { ~oPB[3].set(\rate, Scale.major[3].midiratio, \t_trig, 1, \gate, 1,\tpse, 1); }
{ msg[2] == 4} { ~oPB[4].set(\rate, Scale.major[4].midiratio, \t_trig, 1, \gate, 1,\tpse, 1); }
{ msg[2] == 5} { ~oPB[5].set(\rate, Scale.major[5].midiratio, \t_trig, 1, \gate, 1,\tpse, 1); }
{ msg[2] == 6} { ~oPB[6].set(\rate, Scale.major[6].midiratio, \t_trig, 1, \gate, 1,\tpse, 1); }
{ msg[2] == 7} { ~oPB[7].set(\rate, Scale.major[7].midiratio, \t_trig, 1, \gate, 1,\tpse, 1); }
{ msg[2] == 8 } { // touch 7 = toggle looping of all samplers
~lsB = (~lsB + 1)% 2;
~sGB.set(\loop, ~lsB ); // toggle this
b.sendMsg("/led", msg[2], ~lsB, msg[4]);
postln("loop state set as:" + ~lsB + " 7 " + msg[2] );
};
};
// select with SHIFT key - toggle effect = apply with enter key
// set a state // release of shift key acts as enter
// msg[1] sender mac address
// msg[2] which of the [0 - 11] sensors is touched
// msg[3] is it a touch [1] or a release [0]
// msg[4] which of the connected mpr121 boards is sending
// if SHIFT IS DEPRESSED on board 1 - shift enabled while 11 depressed
if (msg[1] == mac && msg[2] == 11 && msg[4] == 1 ) {
if (msg[3]==1){
~shiftB = 1; postln("shift is enabled" )}
{ ~shiftB = 0; postln("shift is disabled" )}
};
if (~shift==1) {
postln(" shift is pressed " + msg[2]);
w = case
{ msg[2] == 0} { ~oPB[0].set(\rate, 16); }
{ msg[2] == 1} { ~oPB[1].set(\rate, 14); }
{ msg[2] == 2} { ~oPB[2].set(\rate, 12); }
{ msg[2] == 3} { ~oPB[3].set(\rate, 10); }
{ msg[2] == 4} { ~oPB[4].set(\rate, 8); }
{ msg[2] == 5} { ~oPB[5].set(\rate, 6); }
{ msg[2] == 6} { ~oPB[6].set(\rate, 4); }
{ msg[2] == 7} { ~oPB[7].set(\rate, 2); }
}
//////////////////////////////////////////////////////////////////////////
},'/touch') } ;
s.sync;
~esp32Receive.('anything', '3C:E9:0E:AD:A5:48');
~sG.set(\amp, 1);
~sG.set(\rate, -1);
~sGB.set(\amp, 0.2);
"patch loaded and ready...".postln;
};