// Schrittwechsel Controller



(

Server.default = s = Server.internal;

s.boot;


q = ();


//q.directory = "/localvol/sound/share/audiosamples/koelnKalk";

q.directory = "/Users/till/Documents/koelnKalk";

q.subDirs = (footsteps: 6, doorClose: 4, doorOpen: 4, activity: 1);


// the audiochannels to which the cube speakers are attached to

q.cubeChannels = (

yellow: 0,

lightGreen: 1,

blue: 2

/* orange: 3,

red: 4,

*/);


q.visualAddr = NetAddr("localhost", 12000);

//q.visualAddr = NetAddr("192.168.84.14", 12000);

//q.visualAddr = NetAddr("localhost", 57120);


s.waitForBoot{

q.buffers = q.subDirs.collect{|num, dirName|

num.collect{|i|

Buffer.readChannel(s, "%/%/%.aiff".format(q.directory, dirName, i+1), channels: [0], action:{|b| "file %/%.aiff read.\n".postf(dirName, i+1);  b.normalize});

}

};


HomeBlock.sendSynth;

BlockPerson.sendSynth;

}


)

/*

q.visualAddr.sendMsg(\test) // */

(

// FIXME: record / find activities

// OPTIONAL: record / find different door sounds for each cube side

q.cubes = q.cubeChannels.collect{|outChan, color| 

HomeBlock(color, 0, s, q.buffers.activity, q.buffers.doorOpen, q.buffers.doorClose, outChan, q.visualAddr);

};



// create persons

q.persons = (

green: 

BlockPerson(s, q.buffers.footsteps[[0, 2, 4]], q.cubes.lightGreen, q.cubes.lightGreen, q.visualAddr),

yellow: 

BlockPerson(s, q.buffers.footsteps[[1, 3, 5]], q.cubes.yellow, q.cubes.yellow, q.visualAddr)

);



q.cubes.do{|q| q.activitySynthParams.masterAmp = 1};

//q.cubes.do{|q| q.activitySynthParams.amp = 0.1};

q.persons.do{|p| p.synthParams.masterAmp = 1};



/*

q.cubes.red.getActive


q.cubes.red.getInactive(1)

q.cubes.red.getInactiveImmediatly // force inactivity

*/


(

q.cubes.do{|cube|

cube.fittingAction = {|cube, numPersons|

"active".postln;

};


cube.overfullAction = {|cube, numPersons, supernumerousPersons|


// choose a person and send it to another cube. 

// FIXME: search for a strategy to choose the cube 

// FIXME: set duration according to distance 

{

cube.getInactive;

3.wait;

supernumerousPersons.postln;

supernumerousPersons.do{

rrand(1, 2.0).wait;

cube.persons.choose.transite(cube.others.choose, dur: 5, dt: 1);

};

6.wait;

cube.getActive;


}.fork

};

cube.invisibleAction = {|cube|

"% invisible".format(cube.color).postln;

};

cube.faceChangeAction = {|cube, face|

[cube.color, face].postln;

cube.transitePersonsToNext;

};

q.cubes.do{|q| 

q

.setActivityParam(\masterAmp, 1)

.setActivityParam(\dampFreq, 600)

//.setActivityParam(\amp, 1)

};


/* cube.cubeUpdateAction = {|cube|

};

*/

cube.fUpThresh = 0.11;

}

);


q.god = BlockGod(s, q.cubes, q.persons);

)



/*

q.cubes.orange.upFace 

q.cubes.orange.faceStates.selectIndex{|v| v > q.cubes.orange.fUpThresh}



*/

/*

q.persons.orange.transite(q.cubes.lightGreen, 10) // */


//////////////////////

// Start SETO


(

q.seto = SETO_OSCTUIOServer("2Dobj", nil, JITseto, SETOIDistance); 

q.seto.start;



JITseto.action = {|me|

var cube = q.cubes.detect{|cube| cube.ids.includes(me.id)};

cube.notNil.if{

cube.faceSeenAs(me);

}

};


q.god.start;


/*

JITseto.action = nil; // */

)


///////////////////////

// Midi Mixer


MIDIClient.init;

MIDIIn.connect;


(

MIDIIn.control = {|port, chan, ctlNum, val| 

[chan, ctlNum, val].postln;

(ctlNum == 7).if{ // fader == cubes

(chan == 8).if{ // master (right-most)

q.cubes.do{|cube|

q.god.ampAll(val.linlin(0, 127, 0, 1));

}

};


(chan < 8).if{ 

q.cubes.asArray[chan].setActivityParam(\amp, val.linlin(0, 127, 0, 1));

};

};


(ctlNum == 10).if{ // knobs == persons

(chan < 8).if{ 

q.persons.asArray[chan].setParam(\amp, val.linlin(0, 127, 0, 1));

};

};


(ctlNum == 17).if{ // lower button -> mute

(chan == 8).if{ // master (right-most)

q.cubes.do{|cube|

q.god.muteAll(val.linlin(0, 127, 0, 1));

}

};


(chan < 8).if{ 

q.cubes.detect{|a| a.out == chan}.setActivityParam(\mute, val.linlin(0, 127, 0, 1));

};

};

}

)