(
Server.default.waitForBoot {

	// audio is in the smp/ subfolder from the location of this file
	var path = PathName.new(thisProcess.nowExecutingPath).pathOnly +/+ "smp/contrapoints_gender_critical.wav";
	~contra = Buffer.read(s, path);

	// a mono channel feeding reverb fx
	~revBus = Bus.audio(s, 1);

	// stereo channel feeding granular reverb/delay
	~granBus = Bus.audio(s, 2);

	// two buffers for granluar rev/delay
	~granbufL = Buffer.alloc(s, 6 * s.sampleRate);
	~granbufR = Buffer.alloc(s, 6 * s.sampleRate);


	// SynthDefs

	SynthDef(\bufPlayer, {
		arg out=0, sndbuf, gate = 1, amp = 0.1, pos = 0, rate = 1;
		var snd, com, env;

		env = EnvGen.ar(Env.asr(0.01, 1, 0.01), gate, doneAction:Done.freeSelf);
		snd = PlayBuf.ar(1, sndbuf, rate: rate, startPos: pos);
		com = CombN.ar(snd, 0.5, Rand(0.01, 0.05), mul:0.6);
		snd = snd + (com * Rand(0,1));
		snd = RLPF.ar(snd, Rand(1000, 5000), 0.2);
		snd = HPF.ar(snd, Rand(200,1000));
		snd = Decimator.ar(snd/2, ExpRand(5000,44100), add:snd/2);
		com = CombN.ar(snd, 0.5, Rand(0.1, 0.5));
		snd = snd + (com * Rand(0,1));
		snd = snd * env * amp;

		Out.ar(out, snd);
	}).add;

	
	SynthDef(\revfx, {
		arg inBus, outBus=0, revtime = 5, dry=1, wet=0.2, roomsize=100;
		var snd;

		snd = In.ar(inBus);
		snd = GVerb.ar(snd, roomsize, revtime, drylevel: dry, earlyreflevel: 0, taillevel: wet) ;

		Out.ar(outBus, snd);
	}).add;

	
	SynthDef(\grainIn, { 
		arg inBus, outBus=0, bufL, bufR, amp=0.5, freq=220, fvar=1, 
		dur = 0.5, durvar = 0.05, pos = 0, posvar = 0.05, pitch = 1, pitchvar = 0.01,
		width = 0.8, gain = 0.4, wet = 1, dry = 3 ;
		var inSnd, snd;

		// audio from a bus
		inSnd = In.ar(inBus, 2);

		// continously record into two buffers
		RecordBuf.ar(inSnd[0], bufL);
		RecordBuf.ar(inSnd[1], bufR);

		// granulate the audio in buffers
		snd = GrainBuf.ar(
			numChannels: 1,
			trigger: Impulse.kr(freq + (freq * (fvar * LFNoise2.kr(freq)))), 
			dur: dur + (durvar * LFNoise2.kr(freq)), // in seconds
			sndbuf: [bufL, bufR], // not sure if this works correctly
			rate: pitch + (pitchvar * LFNoise2.kr(5)), 
			pos: SinOsc.ar(0.03, phase:[0,0.1]).range(0,1) + (posvar * LFNoise2.kr(freq)), // 0-1
			interp: 2, 
			//pan: LFNoise1.kr(10).range(width.neg, width), 
			maxGrains: 512,	mul: gain, add: inSnd*2
		);

		// mix granulated and original sound
		snd = (snd * wet) + (inSnd * dry);
		
		snd = snd * amp;
		snd = Limiter.ar(snd, 0.7);
		Out.ar(outBus,snd);
	}).add;
	
}
)

// create fx
~reverb = Synth(\revfx, [\inBus, ~revBus, \outBus, ~granBus]);
~granulator = Synth(\grainIn, [\inBus, ~granBus, \bufL, ~granbufL, \bufR, ~granbufR], addAction:\addToTail)



// play it with a patterns
(
Pbindef(\bufPlat,
	\instrument, \bufPlayer,
	\sndbuf, ~contra,
	\amp, 0.3,
	\dur, Prand([1, 4, 1/2, Pn(1/4, Prand([2,4])), Pn(1/8, Prand([2,4,8])), 2], inf),
	\legato, Prand((3..4) * 0.1, inf),
	\pos, Prand(list: (0..~contra.numFrames), repeats: inf),
	\out, ~revBus
).stop
)


////////////////////////////////////////////////////////////
//play with arguments/parameters
~reverb.set(\wet, 0.5);
~reverb.set(\roomsize, 140);

~granulator.set(\amp, 1)
~granulator.set(\dur, 0.5)
~granulator.set(\wet, 1);
~granulator.set(\dry, 3);
~granulator.free;
////////////////////////////////////////////////////////////


( // free stuff
~reverb.free;
~granulator.free;

~granBufL.free;
~granBufR.free;

~revBus.free;
~granBus.free;
)






thisProcess.platform.recordingsDir = "/home/random";
r = Recorder.new(s);
r.recHeaderFormat = "flac";
r.recSampleFormat = "int24";
r.filePrefix = "SuperCollider_";
