(
// before boot, raise memory?
#[\internal, \local].do { |s|
	s = Server.perform(s);
	s.options.memSize = 2097152; // 2 gigs
};

Server.default.waitForBoot {
	fork {

		// SynthDefs ////////////////////////////////////////////
		
		// sawtoothwave-based droney pad
		SynthDef(\sawLine, {
			arg out=0, freq=440, gate=1, amp=1, cutoff=10;
			var snd, env, gen, numosc;

			numosc = 10;
			cutoff = cutoff * 0.1;
			env = Env.adsr(4, 0, 1, 10, 1, \lin);
			gen = EnvGen.kr(env, gate, doneAction:2);

			// using Array.fill multiplies number of oscilators
			snd = Array.fill(numosc, {
				var local, lfreq;
				lfreq = [freq, freq*2.01, freq/1.99 ];
				local = Saw.ar( rrand(lfreq, lfreq * 1.02)
					* LFNoise1.kr(0.3).range(1, 1.01), -10.dbamp * [0.5, 0.3, 0.3]);
				local = Mix(local);
			});
			snd = Splay.ar(snd); // Splay mixes-down all arrays to 2ch - stereo
			snd = LPF.ar(snd, LFNoise1.ar(0.06).exprange(3000,10000) * cutoff);

			// another delay with variing delay times to add to harmonic offsets
			snd = CombL.ar(snd, 1, LFNoise1.ar(0.1).range([0.5,0.65],[0.53,0.68]), 15, -1.dbamp) + snd;

			snd = snd * gen * amp;
			Out.ar(out, snd);
		}).add;


		// a sinewave-ish beep with a home-made reverb
		SynthDef(\revBeep, {
			arg freq=440, amp = 0.3, gate=1, pan=0, revwet=0.1;
			var snd, env, rev, del, in;
			snd = LFCub.ar(freq);
			env = EnvGen.ar(Env.asr(0.01, 1, 0.1), gate,
				doneAction:0);
			snd = snd * env * amp;

			// ## reverb ##
			del = DelayN.ar(snd, 0.048);
			// mix/fill 16 instances of short delay with CombL:
			rev = Mix.ar(Array.fill(16,{ CombL.ar(del, 0.1, LFNoise1.kr(0.001.rand, 0.04, 0.05), 15) }));
			// add delays to the signal recursively with AllPassN 4x:
			6.do({ rev = AllpassN.ar(rev, 0.050, [0.050.rand, 0.050.rand], 1) });
			// adjust volume:
			rev = rev * revwet * 0.1; // carefull
			snd = snd + rev;

			// the end of the sound determined by silence. free synth.
			DetectSilence.ar(snd, amp: 0.0001, doneAction:2);
			
			Out.ar(0, snd);
		}).add;

		// s.scope(16)

		// a beep sound with a delay/echo
		SynthDef(\delBeep, {
			arg freq=200, amp = 0.3, gate = 1, pan = 0;
			var snd, env;

			snd = Pulse.ar(freq)!2;
			env = EnvGen.ar(Env.perc(0.01, 0.1), doneAction:0);
			snd = snd * env * amp;

			// delay:
			snd = CombN.ar(snd, 1, [0.39,0.51], 10, mul:0.3) + snd;
			DetectSilence.ar(snd, time:0.5, doneAction:2);
			
			Out.ar(0, snd);
		}).add;


		SynthDef(\hh, {
			arg amp=0.1, outBus=0, gate=1, sustain=0.2, cutoff=2000, pan=0;
			var snd, env;

			snd = WhiteNoise.ar;
			snd = HPF.ar(snd, cutoff);
			env = EnvGen.ar(Env.perc(0, sustain), doneAction:2);
			snd = snd * env * amp;
			snd = Pan2.ar(snd, pan);
			
			Out.ar(outBus, snd);
		}).add;

		// end of SynthDef-initions. /////////////////////////////////////////

		s.sync; // sync server before moving on //

		// let's pattern! ////////////////////////////////////////////////////

		Pbindef(\delBeepPat,
			\instrument, \delBeep,
			\degree, Pseq([7,0], inf),
			\dur, Pseq([0.1,15.9], inf),
			\sustain, 0.1,
			\amp, 0.05
		).play;

		Pbindef(\hhPat,
			\instrument, \hh,
			\dur, 2,
			\amp, Pseq([0.5,0.3],inf),
			\pan, Pseq([-1,1], inf),
			\sustain, 0.1,
			\cutoff, 12000
		).play;

		Pbindef(\revBeepPat,
			\instrument, \beep1,
			
			\dur, Pseq([1,2,3] * 4, inf) * Prand([1/4,1/8,1/2,1],inf),	
			\degree, Prand([0,3,-2,4],inf),

			\octave, 6,
			\sustain, Prand((2..6)*0.1, inf),
			\amp, 0.04,
			\revwet, 1,
			\scale, Scale.minor(\just),
			\pan, Pseg([-1, 1, -1], 20, \lin, inf)
		).play;

		Pbindef(\padPat,
			\instrument, \sawLine,
			\dur, Pseq([Rest(32), Pn(8)]),
			\degree, Prand([[0,9],[0,8],[0,10]],inf),
			\octave, [4,5,6],
			
			\amp, Pseq([0, Pseq((1..5) ++ (4..2) * 0.1, inf)]),
			\cutoff, Pseq((1..7)++(6..2),inf),
			\scale, Scale.minor(\just)
		).play;


	} // end of fork.
}
)

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