/////////// MOL Group 2 //////////////////

// programmers: Till Bovermann, Florian Grond


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

// Read Data


// an envir

q = ();


(

q.paths = pathMatch("data_10x10/*".resolveRelative);

q.allRawData = q.paths.collect { |path| 

FileReader.read(path, true, true, { |x| x.asInteger })

};

q.allRawData.shape.postln;

// special operation!!

q.allData = q.allRawData.collect { |x|

//x.flat.clump(4).clump(1000).collect { |x| x.clump(100).collect { |x| x.clump(10) } }

x.flat.clump(4).clump(10).clump(10).clump(10);

};

q.mono = q.allData.at(0); // many many loops

q.mono = q.allData.at(19); // below phase trans

q.mono = q.allData.at(21); // above phase trans

q.mono = q.allData.at(30); // very few, small loops

// q.allData.size;

)

q.data; // winkel (Float, summed over loop)

q.mono; // loops  (Integer)



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

// main strategy

// sonify one specific item and a randomly chosen other item in its next neighbourhood


(

// current index

q.idx = [1, 1, 1, 1];


// get values for the current index

q.item = q.mono.slice(*q.idx)

)



(

// lower frequencies

q.lowFreqs = [100, 300, 500, 700];

q.higFreqs = [200, 400, 600, 800];



SynthDef(\point, {|out = 0, amp = 0.05|

var freqs = \freqs.kr([100, 200, 300, 400]);

//var freqs = q.lowFreqs;

var amps = \amps.kr([1, 1, 1, 1]);

var env = EnvGen.kr(Env.perc(0.01, 2), doneAction: 2);


Out.ar(out, SinOsc.ar(freqs, 0, amps).sum * env * amp!2);

}).memStore

)


/*test

x = Synth(\point).setn(\freqs, q.higFreqs);

x = Synth(\point).setn(\freqs, q.lowFreqs); // */


(

// get index of an item near the specified item

q.nearTo = {|q, idx|

idx.collect{|it|

it + 1.rand2

};

};


// get specific item (given as an idx (array)) and a randomly chosen item in a 2-element list

q.getItems = {|q, idx|

[q.mono.slice(*idx) , q.mono.slice(*q.nearTo(idx))]

};


/*test

q.getItems(q.idx) // */

)



(

// render one iteration, frequency is slightly varied for the arbitrary chosen item

q.renderIt = {|q, item, vary = 0|

var freqs, amps, gliss;


freqs = item.collect{|it, i|

(it > 0).if({

q.lowFreqs[i]

}, {

q.higFreqs[i]

})

};

gliss = item;


// all

amps = item.abs * 0.5;

// only positive

//amps = (item.abs - item) * 0.25;


//"freqs: %\namps: %\n".postf(freqs, amps);

(vary > 0).if({

Synth(\point).setn(\freqs, freqs * rrand(0.9, 1.1), \amps, amps);

}, {

Synth(\point).setn(\freqs, freqs, \amps, amps);

})

}

)


/* test

q.renderIt(q.item)

q.renderIt([0, 0, 0, 0]);

q.renderIt([1, 1, 0, 0]);

q.renderIt([1, 1, 1, 1].neg); // */



(

Tdef(\player, {loop{

q.getItems(q.idx).do{|item, i|

item.postln;

q.renderIt(item, i);

0.1.wait

}

}})

)



Tdef(\player).play

Tdef(\player).stop




// sliders to navigate through the 4D-space

(

q.win = Window.new.front;

q.sliders = 4.collect{|i|

a = Slider(q.win, Rect(20 + (i*25), 60, 20, 220));

a.action = {|me| q.idx[i] =  (me.value * 8).round(1) + 1; q.idx.postln};

}

)




///// scnd ////



// other freqs

q.lowFreqs = [100, 200, 400, 800]*2;

q.higFreqs = [100, 200, 400, 800]*2;



// other synth, now with glissando

(

SynthDef(\point, {|out = 0, amp = 0.05, dur= 0.5|

var freqs = \freqs.kr([100, 200, 300, 400]);

//var freqs = q.lowFreqs;

var amps = \amps.kr([1, 1, 1, 1]);

var gliss = \gliss.kr([1, 1, 1, 1]);

var env = EnvGen.kr(Env.perc(0.01, dur), doneAction: 2);



freqs = Line.kr(freqs, freqs*(1+(gliss*0.5)), dur);


Out.ar(out, SinOsc.ar(freqs, 0, amps).sum * env * amp!2);

}).memStore

)


// take care of gliss in renderIt

(

q.renderIt = {|q, item, vary = 0|

var freqs, amps, gliss;


freqs = item.collect{|it, i|

(it > 0).if({

q.lowFreqs[i]

}, {

q.higFreqs[i]

})

};

gliss = item;

// all

//amps = item.abs * 0.5;

// only positive

amps = (item.abs - item) * 0.25;


//"freqs: %\namps: %\n".postf(freqs, amps);

(vary > 0).if({

Synth(\point).setn(

\freqs, freqs * rrand(0.9, 1.1), 

\amps, amps, 

\gliss, gliss

);

}, {

Synth(\point).setn(

\freqs, freqs, 

\amps, amps, 

\gliss, gliss

);

})

}

)



Tdef(\player).play;

Tdef(\player).stop;



//////////// thrd ////////////////


(

SynthDef(\form, {|basef = 400, form1 = 600, gain1 = 0, form2 = 1200, gain2 = 0, form3 = 2400, gain3 = 0, form4 = 4800, gain4 = 0, cutoff = 2000|


var  fo1, fo2 , fo3 , fo4, env, lop, sumsum ;

fo1 =  Formant.ar(basef, form1, basef * 10, gain1);

fo2 =  Formant.ar(basef, form2, basef * 10, gain2);

fo3 =  Formant.ar(basef, form3, basef * 10, gain3);

fo4 =  Formant.ar(basef, form4, basef * 10, gain4);

env = EnvGen.kr(Env.linen(0.1, 0.1, 0.1, 0.6, 'sine'), 0.1, doneAction: 2);

sumsum = (fo1 + fo2 + fo3 + fo4) * env;

lop = LPF.ar(sumsum, cutoff);

//OffsetOut.ar(0,Pan2.ar( (fo1 + fo2 + fo3 + fo4) * env ));

OffsetOut.ar(0,Pan2.ar( lop ));

}).memStore;

)


// test

(

Synth(\form, [\basef, 100, 

\form1, 300 + 300.rand, \gain1, 1.0.rand,

\form2, 1000 + 1000.rand, \gain2, 1.0.rand,

\form3, 1500 + 1500.rand, \gain3, 1.0.rand,

\form4, 3000  + 3000.rand, \gain4, 1.0.rand

])

)


// take care of renderIt

(

q.renderIt = {|q, item, vary = 0|

var freqs, amps, gliss;


freqs = item.collect{|it, i|

(it > 0).if({

q.lowFreqs[i]

}, {

q.higFreqs[i]

})

};

gliss = item;

amps = item.abs * 0.5;

//amps = (item.abs - item) * 0.25;


//"freqs: %\namps: %\n".postf(freqs, amps);

(vary > 0).if({

Synth(\point).setn(

\freqs, freqs * rrand(0.9, 1.1), 

\amps, amps, 

\gliss, gliss

);

}, {

Synth(\point).setn(

\freqs, freqs, 

\amps, amps, 

\gliss, gliss

);

})

}

)




// new tdef

(

Tdef(\flo, {

var item, item2;

inf.do({

q.getItems(q.idx).do{|item, i|

item.postln;

Synth(\form, [\basef, 50, 

\form1, 300, \gain1, item[0].abs.linlin(0, 2, 0, 0.6),

\form2, 1000, \gain2, item[1].abs.linlin(0, 2, 0, 0.6),

\form3, 1500, \gain3, item[2].abs.linlin(0, 2, 0, 0.6),

\form4, 3000, \gain4, item[3].abs.linlin(0, 2, 0, 0.6),

\cutoff, 4000 - (i*3000)

]);

0.1.wait;

}

});

});

)


Tdef(\flo).play

Tdef(\flo).stop