/** 
Data on PLoop bins
[-1,0,1] are physical values, 2 is filtered data (can be ignored)

KV, 02/2010, prepared for SBEII (Data by Christof Gattringer)
**/

// Team 1 sonification by Florian Grond, David Pirro, Iris Rennert, Christoph Gattringer, Bettina Lackner, Robert Höldrich, Marcus Schmickler

// read data here:

(
q = ();
q.myPath = Document.current.dir; 
q.ttcs = [0.63, 0.69, 0.75, 0.82, 0.89, 0.97, 1.01, 1.05, 1.13, 1.22, 1.32];
q.confs = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10"];

q.ttc = q.ttcs[0]; // choose here the data set 0 - 9 from above
q.conf = q.confs[0];
q.path = q.myPath ++ "/files_gattringer/intsect_TTC_" ++ q.ttc ++ "_conf_" ++ q.conf ++".dat";

File.exists(q.path);

q.gitter = [40, 40, 40];

q.data = FileReader.read(q.path, true, true, (_.asInteger), $\n).unbubble.reshape(*(q.gitter));

q.data.shape;

);

/*
we programed a randow walk through the data
the restriction was to always stay within one cluster
by resetting  ~position you can jump to other clusters 
*/

(
// set initial position here
~position = [10,10,10];

// one step random walk
~cval = {|position|
		var val, valnew, randcomp,valindex, positionnew, randvec = [0,0,0];
			val = q.data[position[0]][position[1]][position[2]];
			valnew = 100;
			valindex = 0;
			while({(val != valnew) && (valindex < 100)},
			{
			valindex = valindex + 1; // increment
			randvec = [0,0,0];
			randvec[3.rand] = [-1,1].choose; //  ramndvetor 
			// randvec.postln;
			positionnew = (~position + randvec).mod(40);
			valnew = q.data[positionnew[0]][positionnew[1]][positionnew[2]];
			// [~position, positionnew].postln;
			});
			~position = positionnew.mod(40);
			// val.postln;
			valnew
			};
			
~cval.(~position);	~position.postln;
)

(
~form = SynthDef(\form, {|basef = 400, form1 = 600, gain1 = 0, cutoff = 2000, pan = 0.0|
			var  fo1, env, lop, sumsum ;
			fo1 =  Formant.ar(basef, form1, form1 * 0.3, gain1);
			env = EnvGen.kr(Env.linen(0.1, 0.2, 0.1, 0.6, 'sine'), 0.1, doneAction: 2);
			sumsum = fo1 * env;
			lop = LPF.ar(sumsum, cutoff);
		 OffsetOut.ar(0,Pan2.ar( sumsum, pan, 0.1  ));
	 }).send(s);

)

// test the synth here

Synth(\form, [\basef, 100, 	\form1, 300 + 1000.rand, \gain1, 0.5, \pan, 2.rand2])

(
~position = 39.rand!3;

~t = Task({ 
		inf.do({
		~cval.(~position);	// ~position.postln;
		Synth(\form, [\basef, ~position[0].linexp(0,39,50,400), 	
					\form1, ~position[1].linexp(0,39,300,1300),
					\gain1, 0.5,
					\pan, ~position[2].linlin(0,39,-1,1)]);
							0.008.wait;
			});
	});

~t.start;

)

// choose new position here
~position = 39.rand!3;

~t.start;
~t.pause;
~t.resume;
~t.reset;
~t.stop;