Collision Network

OpenFrameworks application with Box2d Physics library and Osc comunications.
When a collision is detected start an Osc message Form OpenFrameworks to Supercollider. The particle informations are send to a granular SynthDef.

Code


v = GCN.new(\collision,"files_path");

///////CLASS
GrainToken {
var <>startPos, <>grDur, <>rate, <>speed, <>atk, <>sus, <>rel ,<>grSize,<>pos,<>pan;
*new {|startPos, grDur, rate, speed, atk, sus, rel,grSize,pos,pan|
^super.newCopyArgs(startPos, grDur, rate, speed, atk, sus, rel,grSize,pos,pan);
}
}

Grain {
var <>buf, <>control,<>synth;
*new {|buf, control|
^super.new.init(buf, control);
}
init {|buf, control|
this.buf = buf;
this.control = control;
}
update{|control|
this.control = control;
}
play {arg synth;
^synth = Synth(synth, [
\buf, this.buf,
\start, this.control.startPos,
\grDur, this.control.grDur,
\grSize,this.control.grSize,
\rate, this.control.rate,
\speed, this.control.speed,
\atk, this.control.atk,
\sus, this.control.sus,
\rel, this.control.rel,
\pos,this.control.pos,
\pan,this.control.pan
]);
}
}

GCN{
var <>oscListener,<>oscListener_n,<>buffers,<>particles;
*new { arg osc_address,samples_path;
^super.new.init(osc_address,samples_path);
}
load_samples{arg path;
var fullpath,files,temp;
fullpath = path ++ “*”;
files = SoundFile.collect(fullpath);
this.buffers = files.collect { |sf|
temp = Buffer.readChannel(Server.local, sf.path,channels: [0])
};
“> Samples loaded”.postln;
this.buffers.postln;
}
init_synth{
SynthDef(\granulator, {|buf=0, dur=0.3, amp=2 , speed=10, rate=1, atk=0.01,
sus=0.5, rel=0.5, pos=0.125,pan=0|
var grSize=1.2, sig, shape, env;
shape = Env([0, amp, amp, 0], [atk, sus, rel], \sine);
env = EnvGen.ar(shape, doneAction: 2);
sig = TGrains.ar(2, Impulse.kr(speed), buf, rate, pos, grSize,pan);
OffsetOut.ar(0, Pan2.ar(sig * env),pan);
}).add;
}
init {arg osc_address,samples_path;
var frq,rt;
this.load_samples(samples_path);
this.init_synth;
this.particles = Array.new(100);
“> Starting Osc…”.postln;
oscListener = OSCresponder.new(nil, osc_address , { |time, resp, msg|
var id = msg[1];
this.update(msg);
this.play(id,\granulator);
msg.postln;
}).add;
oscListener_n = OSCresponder.new(nil, \n , { |time, resp, msg|
this.create(msg);
msg.postln;
}).add;
“> Listening address: “.post;
osc_address. postln;
}
create{arg msg;
var id = msg[1];
//CREATE GRAIN IF NOT EXIST
if(this.particles[id].isNil){
var node;
var token = GrainToken.new(
1.0.rand,
msg[3],
rrand(0.1, 0.8),
rrand(1, 10),
rrand(0.1, 2),
rrand(0.5, 1),
rrand(0.1, 2),
msg[3]
);
node = Grain.new(this.buffers.choose, token);
this.particles.add(node);
“> Grain created”.postln;
};
}
update{arg msg;
var id = msg[1];
//UPDATE GRAIN IF NOT EXIST
if(this.particles[id].isNil == false){
var node;
var token = GrainToken.new(
1.0.rand,
abs(msg[3]).linlin(0,20,0.1,0.8),
abs(msg[3]).linlin(0,20,0.5,1.2),
abs(msg[3]),
abs(msg[3]).linlin(0,20,0.1,0.4),
abs(msg[3]).linlin(0,20,0.1,0.6),
abs(msg[3]).linlin(0,20,0.1,0.8),
abs(msg[3]).linlin(0,20,0.1,0.8),
rrand(0.1, 0.8),
msg[4]
);
this.particles[id].update(token);
//node = Grain.new(this.buffers.choose, token);
//this.particles.add(node);
“> Grain uptade”.postln;
};
}
play{arg index,synth;
//PLAY GRAIN
^this.particles[index].play(synth);
}
reset{
this.particles = nil;
this.particles = Array.new(100);
}
clearBuffers{
buffers = nil;
}
}