(/*
	Copyright (c) 2020 Luka Prinčič, All rights reserved.
	This program is free software distributed under
	GNU General Public Licence. See COPYING for more info.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - -
                                                                                   */
// RHIZOSPHERE - STEKLENIK 2020 - CONA //////////////////////////////////////////////

postln("

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
/*
>>> Hello. Starting RHIZOSPHERE                                                 <<<<<

██████╗ ██╗  ██╗██╗███████╗ ██████╗ ███████╗██████╗ ██╗  ██╗███████╗██████╗ ███████╗
██╔══██╗██║  ██║██║╚══███╔╝██╔═══██╗██╔════╝██╔══██╗██║  ██║██╔════╝██╔══██╗██╔════╝
██████╔╝███████║██║  ███╔╝ ██║   ██║███████╗██████╔╝███████║█████╗  ██████╔╝█████╗  
██╔══██╗██╔══██║██║ ███╔╝  ██║   ██║╚════██║██╔═══╝ ██╔══██║██╔══╝  ██╔══██╗██╔══╝  
██║  ██║██║  ██║██║███████╗╚██████╔╝███████║██║     ██║  ██║███████╗██║  ██║███████╗
╚═╝  ╚═╝╚═╝  ╚═╝╚═╝╚══════╝ ╚═════╝ ╚══════╝╚═╝     ╚═╝  ╚═╝╚══════╝╚═╝  ╚═╝╚══════╝


");
	*/

Server.default.waitForBoot {

	// library path
	var libPath = PathName(thisProcess.nowExecutingPath.dirname +/+ "lib");

	// for each files in that lib folder 
	libPath.filesDo({|afile|
		// tell me what you're executing:
		postln("   ." + afile.fileName);

		// execute it:
		this.executeFile(afile.fullPath);	
	});
	
	// important for patterns
	s.latency = 0.05; // default server's is 0.2

});


// octoPan busses //////////////
(
// free busses and panners when re-trying things?
~octoBus1.free; ~octoBus2.free; ~octoBus3.free; ~octoBus4.free;
~panBus1x.free; ~panBus1y.free; ~panBus1r.free;
~panBus2x.free; ~panBus2y.free; ~panBus2r.free;
~panBus3x.free; ~panBus3y.free; ~panBus3r.free;
~panBus4x.free; ~panBus4y.free; ~panBus4r.free;
~revBus.free;

// octoBus1 + panners and control busses:
~octoBus1 = Bus.audio(s,1);
~panBus1x = Bus.control(s,1);
~panBus1y = Bus.control(s,1);
~panBus1r = Bus.control(s,1);
~panBus1y.value = 0.14; // set a fixed value to a bus
~panBus1x.value = 0;
~panBus1r.value = 1;

// octoBus2 + panners and control busses:
~octoBus2 = Bus.audio(s,1);
~panBus2x = Bus.control(s,1);
~panBus2y = Bus.control(s,1);
~panBus2r = Bus.control(s,1);
~panBus2y.value = 0; // set a fixed value to a bus
~panBus2x.value = 0;
~panBus2r.value = 0.1;

// octoBus3 + panners and control busses:
~octoBus3 = Bus.audio(s,1);
~panBus3x = Bus.control(s,1);
~panBus3y = Bus.control(s,1);
~panBus3r = Bus.control(s,1);
~panBus3y.value = 0.29; // set a fixed value to a bus
~panBus3x.value = 0;
~panBus3r.value = 0.1;

// octoBus4 + panners and control busses:
~octoBus4 = Bus.audio(s,1);
~panBus4x = Bus.control(s,1);
~panBus4y = Bus.control(s,1);
~panBus4r = Bus.control(s,1);
~panBus4y.value = 0.29; // set a fixed value to a bus
~panBus4x.value = 0;
~panBus4r.value = 0.1;

// 8chan reverb bus
~revBus = Bus.audio(s,8);
">>> loaded all busses".postln;
);

(
var timeLine, r;

// CmdPeriod frees these, but not the busses, so only these need to be re-inst.

"--- freeing old octoPanners ...".postln;
~octoPanner1.free; ~octoPanner2.free; ~octoPanner3.free; ~octoPanner4.free;

"--- loading octoPanners ...".postln;
~octoPanner1 = Synth("octoPanner", [\inBus, ~octoBus1, \outBus, ~revBus], addAction: \addToTail);
~octoPanner1.map(\x, ~panBus1x);
~octoPanner1.map(\y, ~panBus1y);
~octoPanner1.map(\radius, ~panBus1r); 

~octoPanner2 = Synth("octoPanner", [\inBus, ~octoBus2, \outBus, ~revBus], addAction: \addToTail);
~octoPanner2.map(\x, ~panBus2x);
~octoPanner2.map(\y, ~panBus2y);
~octoPanner2.map(\radius, ~panBus2r); 

~octoPanner3 = Synth("octoPanner", [\inBus, ~octoBus3, \outBus, ~revBus], addAction: \addToTail);
~octoPanner3.map(\x, ~panBus3x);
~octoPanner3.map(\y, ~panBus3y);
~octoPanner3.map(\radius, ~panBus3r); 

~octoPanner4 = Synth("octoPanner", [\inBus, ~octoBus4, \outBus, ~revBus], addAction: \addToTail);
~octoPanner4.map(\x, ~panBus4x);
~octoPanner4.map(\y, ~panBus4y);
~octoPanner4.map(\radius, ~panBus4r);

~reverb = Synth("reverBusser", [\inBus, ~revBus, \outBus, 0], addAction: \addToTail);

timeLine = Routine {
	"--- starting main routine ...".postln;

	//1.wait;

	/*
~panBus1y.value = 0.2; // set a fixed value to a bus
~panBus1x.value = 0;


	~darkHenon1 = Synth(\henonLsynth2, [\out, ~octoBus1, \fadeTime, 3]);
	~darkHenon1.set(\gate, 0);
	~darkHenon1.free
	~darkHenon1.set(\gate, 0);
	~darkHenon1.set(\amp, 0);
	~noiseCr1.set(\fadeTime, 20, \gate, 0);

	~darkHenon2 = Synth(\henonLsynth2, [\out, ~octoBus2]);

	~darkHenon1.free
	~darkHenon2.free


	*/

	
	/* ***** ************************

	">>> markov1 ...".postln;
	~markov1 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 440, \tsize, 3, \amp, 0.9, \clip, 0.69, \fadeTime, 0.01 ]);
	wait(50);
	
	~markov1.set(\gate, 0, \fadeTime, 0.01);
	wait(10);
	
	~markov2 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 440, \tsize, 3, \amp, 0.9, \clip, 0.69, \fadeTime, 0.01 ]);
	wait(8);
	
	~markov2.set(\gate, 0, \fadeTime, 0.1);
	wait(4);
	
	~markov3 = Synth(\markovS1,  [ \out, 1,
		\freq, 440, \tsize, 3, \amp, 0.9, \clip, 0.69, \fadeTime, 1 ]);
	wait(8);
	
	~markov3.set(\gate, 0, \fadeTime, 0.1);
	~markov4 = Synth(\markovS1,  [ \out, 0,
		\freq, 1441, \tsize, 3, \amp, 0.2, \clip, 0.69, \fadeTime, 1 ]);
	wait(8);
	
	~markov5 = Synth(\markovS1,  [ \out, 1,
		\freq, 2440, \tsize, 3, \amp, 0.05, \clip, 0.69, \fadeTime, 1 ]);
	wait(15);
	
	~markov4.set(\gate,0, \fadeTime, 10);
	~markov6 = Synth(\markovS1,  [ \out, 0,
		\freq, 842, \tsize, 10, \amp, 0.1, \clip, 0.69, \fadeTime, 1 ]);
	wait(20);
	
	~markov5.set(\gate,0, \fadeTime, 10);
	
	~markov2 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 440, \tsize, 3, \amp, 0.9, \clip, 0.69, \fadeTime, 20 ]);

	wait(10);

	~markov6.set(\gate,0, \fadeTime, 10);
	wait(20);
	
	~markov2.set(\gate, 0, \fadeTime, 0.1);
	wait(10);

	~markov7 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 740, \tsize, 3, \amp, 0.5, \clip, 0.9, \fadeTime, 0.01 ]);
	~markov8 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 1140, \tsize, 3, \amp, 0.5, \clip, 0.9, \fadeTime, 0.01 ]);
	~markov9 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 1940, \tsize, 3, \amp, 0.5, \clip, 0.9, \fadeTime, 0.01 ]);
	wait(20);
	
	~markov7.set(\gate, 0);
	~markov8.set(\gate, 0);
	~markov9.set(\gate, 0);
	wait(1);

	~markov7 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 141, \tsize, 3, \amp, 0.5, \clip, 0.9, \fadeTime, 0.01 ]);
	~markov8 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 830, \tsize, 9, \amp, 0.5, \clip, 0.9, \fadeTime, 0.01 ]);
	~markov9 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 2941, \tsize, 10, \amp, 0.5, \clip, 0.9, \fadeTime, 0.01 ]);
	wait(10);

	~markov5 = Synth(\markovS1,  [ \out, 1,
		\freq, 2440, \tsize, 3, \amp, 0.09, \clip, 0.69, \fadeTime, 1 ]);
	wait(10);
	
	~markov6 = Synth(\markovS1,  [ \out, 0,
		\freq, 4440, \tsize, 3, \amp, 0.1, \clip, 0.69, \fadeTime, 1 ]);
	wait(5);
	

	~markov7.set(\gate, 0);
	~markov8.set(\gate, 0);
	~markov9.set(\gate, 0);
	wait(1);

	~markov7 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 900, \tsize, 3, \amp, 0.1, \clip, 0.9, \fadeTime, 10 ]);
	~markov8 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 1800, \tsize, 3, \amp, 0.1, \clip, 0.9, \fadeTime, 10 ]);
	~markov9 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 5000, \tsize, 3, \amp, 0.1, \clip, 0.9, \fadeTime, 10 ]);
	wait(30);
	
	~markov10 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 501, \tsize, 3, \amp, 0.1, \clip, 0.9, \fadeTime, 10 ]);
	~markov11 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 1101, \tsize, 3, \amp, 0.1, \clip, 0.9, \fadeTime, 10 ]);
	~markov12 = Synth(\markovS1,  [ \out, ~octoBus1,
		\freq, 3001, \tsize, 3, \amp, 0.1, \clip, 0.9, \fadeTime, 10 ]);

	~markov5.set(\gate, 0);
	~markov6.set(\gate, 0);
	
	wait(30);

	~markov7.set(\gate, 0, \fadeTime, 40);
	~markov8.set(\gate, 0, \fadeTime, 40);
	~markov9.set(\gate, 0, \fadeTime, 40);
	wait(60);
	
	~markov10.set(\gate, 0, \fadeTime, 40);
	~markov11.set(\gate, 0, \fadeTime, 40);
	~markov12.set(\gate, 0, \fadeTime, 40);
	wait(20);
	

	~noiseCr1 = Synth(\noiseCrackle, [\out, ~octoBus1, \fadeTime, 10 ]);
	wait(30);

	~noiseCr1.set(\lpfa,1,\hpfa,0);
	wait(10);

	~noiseCr1.set(\lpfa,0,\hpfa,1);
	wait(5);

	~noiseCr1.set(\lpfa,1,\hpfa,1);
	wait(30);

	~noiseCr1.set(\fadeTime, 20, \gate,0);

		**********/

	
	/*
	~latooWan1 = Synth(\latooWanderings, [\out, ~octoBus4, \fadeTime, 10]);
	30.wait;
	~latooWan1.set(\gate, 0, \fadeTime, 5);
	10.wait;
	"done".postln;
	*/
	//Pbind(\freq, Prand([300, 500, 231.2, 399.2], 80), \dur, 0.1).play;

	//20.wait;
	/*
	( // with routine synth*env can be 'run' multiple times in succession!!
		r = Routine { 200.do({
			x = Synth(\fm_grainer, [ "modfreq", rrand(10,1000),	"carfreq", rrand(40,100),
				\out, ~octoBus1 ]); // should we go through panning?
			1.wait; });
			"done".postln;
		}.play;
	);
	*/
	
	"--- end of the TIMELINE ---".postln
	
};

timeLine.play;

);


( // metering/analysis
s.meter;
s.scope;
s.plotTree;
FreqScope.new(400, 200, 0, server: s);
) /////////////////////////////////////


// testing stuff! ///////////////////////////////////////////////////////////////////////////////////////

~lorenzTrig1 = Synth(\lorenzTrigRing, [\out, ~octoBus1, \freq, 1400, \amp, 0.5]);
~reverb.set(\verb1wet, 0.3);
~reverb.set(\verb2wet, 1);

// 'set' values - disconnects mapping!
~octoPanner1.set(\x, 0, \y, 0.28, \radius, 0.001); 

~panner1y.free;
~panner1y = {Out.kr(~panBus1y, SinOsc.kr(0.1).range(0,0.28))}.play;
~panner1y = SynthDef(\randomPanY, { Out.kr(~panBus1y, LFNoise0.kr(9).range(0,0.28))}).play;
// convert to SynthDef + Synth for common oscilations?

~lorenzTrig1.free;


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






// synths

~henonS1 = Synth(\henonSynth1, [\out, ~octoBus1], ~octoPanner1, \addBefore);
~henonS1 = Synth(\henonSynth1, [\out, ~octoBus1], addAction:\addToHead);// addToHead is default action
~henonS1 = Synth(\henonSynth1, [\out, ~octoBus1]);
~Henons1 = Synth(\henonSynth1, [\out, 0]);
~henonS1.free;

~darkHenon = Synth(\henonLsynth2, [\out, ~octoBus2], ~octoPanner2, \addBefore);
~darkHenon = Synth(\henonLsynth2, [\out, ~octoBus2]);
~darkHenon = Synth(\henonLsynth2, [\out, 0]);
~darkHenon.free;

~hpfHenon = Synth(\henonLsynth1, [\out, ~octoBus3, \hpfreqmin, 100]);
~hpfHenon = Synth(\henonLsynth1, [\out, ~octoBus1, \hpfreqmin, 100]);
~hpfHenon.set(\hpfreqmin, 1000)
~hpfHenon.free;

~distbleeps = Synth(\latooTriggers, [\out, ~octoBus1, \trigA, 1.1], ~octoPanner1, \addBefore);
~distbleeps = Synth(\latooTriggers, [\out, ~octoBus4, \trigA, 1.1]);
~distbleeps.free

~bassLatoo = Synth(\latooThroBass, [\out, ~octoBus1, \amp, 0.2]);
~bassLatoo.free;

~henonSquare = Synth(\henonSquare, [\out, ~octoBus1, \amp, 0.2]);
~henonSquare.set(\gate,0);

~latooWan1 = Synth(\latooWanderings, [\out, ~octoBus4, \fadeTime, 0.1]);
~latooWan1.set(\out,1)
~latooWan1.set(\amp,1)
~latooWan1.set(\gate, 0, \fadeTime, 10);
~latooWan1.free;



(~markov1 = Synth(\markovS1,  [\out, ~octoBus1, \freq, 400, \tsize, 3, \amp, 0.7, \clip, 0.99]);)
~markov1.free;
~markov2 = Synth(\markovS1,  [\out, ~octoBus1, \freq, 1801, \tsize, 3, \amp, 0.7, \clip, 0.99], ~octoPanner1, \addBefore);
~markov2.free;
~markov3 = Synth(\markovS1,  [\out, ~octoBus1, \freq, 799, \tsize, 3, \amp, 1, \clip, 0.98]);
~markov3.free;
~markov4 = Synth(\markovS1,  [\out, ~octoBus1, \freq, 1600, \tsize, 9, \amp, 1, \clip, 0.98]);
~markov4.free;


~noiseCr1 = Synth(\noiseCrackle, [\out, ~octoBus1, \fadeTime, 5 ]);
~noiseCr1 = Synth(\noiseCrackle, [\out, 0, \fadeTime, 5 ]);
~noiseCr2 = Synth(\noiseCrackle, [\out, 1, \fadeTime, 5 ]);
~noiseCr1.set(\lpfa,1,\hpfa,0);
~noiseCr1.set(\hpfa,1,\lpfa,0);
~noiseCr1.set(\amp,0.3);

~noiseCr1.set(\fadeTime, 20, \gate,0);
~noiseCr2.set(\fadeTime, 20, \gate,0);
~noiseCr1.free;



////// ring resonator ringing the clicks from triggers

~stdTrig1 = Synth(\stndTrigRing, [\out, ~octoBus1, \freq,1900]);
~stdTrig1.free

(~stdTrig2 = Synth(\stndTrigRing, [\freq, 70, \min, 0, \max, 2, \decay, 2, \ffreq, 200, \amp, 0.9,
\out, ~octoBus1]) )
~stdTrig2.free

~henoTrig1 = Synth(\henoTrigRing, [\out, ~octoBus1, \freq, 700, \amp, 0.5], ~octoPanner1, \addBefore);
~henoTrig1.free;

~gbmanTrig1 = Synth(\gbmanTrigRing, [\out, ~octoBus1, \freq, 900, \amp, 0.5], ~octoPanner1, \addBefore);
~gbmanTrig1.free;

~latooTrig1 = Synth(\latooTrigRing, [\out, ~octoBus1, \freq, 300], ~octoPanner1, \addBefore);
~latooTrig1.free;

~lorenzTrig1 = Synth(\lorenzTrigRing, [\out, ~octoBus1, \freq, 1400, \amp, 0.5]);
~lorenzTrig1.free;

~fhnTrig1 = Synth(\fhnTrigRing, [\out, ~octoBus1, \freq, 2100, \amp, 0.5], ~octoPanner1, \addBefore);
~fhnTrig1.free;




// forestSoil granulation
~soil1 = Synth(\granSoil, [\out, ~octoBus1, \sndbuf, ~forestSoilBuf]);

~soil1.set(\rate, 0.2);
~soil1.set(\out, 1);
~soil1.set(\dur, 0.01);
~soil1.set(\amp, 0.1);
~soil1.set(\out, 1);
~soil1.set(\envbuf, ~planotaBuf);

// send SinOscilation to a argument via bus
~foreskrBus = Bus.control(s, 1);
~soil1.map(\dur, ~foreskrBus);
~durOsc = {Out.kr(~foreskrBus, SinOsc.kr(0.1).range(0.007,0.1))}.play;
~durOsc.free;

// another granulator
~soil2 = Synth(\granSoil, [\out, ~octoBus2, \sndbuf, ~forestSoilBuf]);
~soil2.map(\dur, ~foreskrBus);

~soil2.set(\amp, 0.1)

~soil1.set(\gate, 0, \fadeTime, 2); // use envelope to fade out
~soil2.set(\gate, 0, \fadeTime, 2); // use envelope to fade out

// stop:free
~soil1.free // stop.
~soil2.free // stop.






// a different approach with routine 
// FM grainers all over the place?
( // with routine synth*env can be 'run' multiple times in succession!!
var r;
r = Routine { 200.do({
	x = Synth(\fm_grainer, [ "modfreq", rrand(10,1000),	"carfreq", rrand(40,100),
		\out, 0 ]); // should we go through panning?
	1.wait; });
    "done".postln;
}.play;
)


r = Recorder(s);
r.recHeaderFormat = "wav";
{ GVerb.ar(Dust.ar(4)) }.play; // play on bus 64
r.record(numChannels:2);
r.stopRecording;







//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 