//routine to load SynthDefs... Use as processes
(

// define a control, a 3 point env to control bus 30
SynthDef(\env, { 
	arg bus=30, p1= 440, p2 = 880, p3 = 440,  t1= 4, t2 = 4;
	Out.kr(bus, EnvGen.kr(Env([p1, p2, p3], [t1, t2], 'exp'), doneAction: 2));
}).load(s);
			 
SynthDef(\bfdecode, {arg gate = 1, bfbus = 16, diskoutbuf = 0;
	var w, x, y, z, out, env, sub;
	#w, x, y, z = In.ar(bfbus, 4);
	env = Env([0.001, 1, 1, 0.001], [0.1, 1, 0.1], \exp, 2);
	//decode to two speakers for now
	out = BFDecode1.ar(w, x, y, z, [-0.25pi, 0.25pi, 0.75pi, 1.25pi], 1) 
		* EnvGen.kr(env, gate, doneAction: 2);
	sub = LRLPF.ar(out.sum * 0.25, 4, 80);
	out = LRLPF.ar(out, 4, 80);
	Out.ar(0, out ++ sub);
	DiskOut.ar(diskoutbuf, [w, x, y, z]);
}).load(s);
	
SynthDef(\inputloop, {arg gate = 1, inbus = 8, loopfreq = 0.5, feedbackpct = 0.1, bufnum = 1, 
		loc = 0, elev = 0, rho = 1, mul = 1;
	var in, out, trig, bufwrite, bufrd;
	in = In.ar(inbus, 1);
	trig = Impulse.ar(loopfreq);
	bufrd = (PlayBuf.ar(1, bufnum, 1, trig, 0, 1) * feedbackpct) + in;
	RecordBuf.ar(bufrd, bufnum, 0, 1, 0, 1, 1, trig);
	Out.ar(16, BFEncode1.ar(bufrd, loc, elev, rho, mul));
	//send a mono version out to the be filtered
	Out.ar(20, bufrd);
}).load(s);

// balance selects what is sent out... 0 clean, 1 filter
SynthDef(\loop, {arg gate = 1, balance = 0, bufnum = 0, delaytime = 2, feedback = 0.1,
		basefreq = 130, outbus = 0, delmul = 0.001, inputamp = 1, del1time = 0.1, 		del2time = 0.15, delran = 1, cleanout = 0, mixout = 0, outlevel = 1, scopeout = 26;
	var in, filts, out, delayin, delauout, bal, del1, del2;
	//in = In.ar(inbus);
	// an envelope to control the randomness of the del1 and del2
	in = AudioIn.ar(1) * Lag2.kr(inputamp, 0.1);
	delayin = LocalIn.ar(1);
	delayin = DelayL.ar(delayin, 2, Lag2.kr(delaytime, 0.2));
	del1time = Lag2.kr(del1time, 0.2);
	del2time = Lag2.kr(del2time, 0.2);
	del1 = DelayL.ar(delayin, 1, 
			(del1time + (LFNoise1.ar(0.2, del1time, del1time) * delran).clip2(1.0))) 
		* delmul;
	del2 = DelayL.ar(delayin, 1, 
			(del2time + (LFNoise1.ar(0.2, del2time, del2time) * delran).clip2(1.0))) 
		* delmul;
	LocalOut.ar((delayin * feedback) + in);
	delayin = delayin + del1 + del2;// + in;
	basefreq = Lag2.kr(basefreq, 0.2);
	balance = Lag2.kr(balance, 0.2);
	filts = ((BPF.ar(delayin, basefreq, 0.05, 0.05) * ( 10 + (SinOsc.ar(0.25, 0, 1)))) + 
		(BPF.ar(delayin, (basefreq)*2, 0.05, 0.05) * ( 5 + (SinOsc.ar(0.33, 0, 3)))) +
		(BPF.ar(delayin, (basefreq)*3, 0.05, 0.05) * ( 3 + (SinOsc.ar(0.1, 0, 3)))) +
		(BPF.ar(delayin, (basefreq)*5, 0.05, 0.05) * ( 5 + (SinOsc.ar(0.05, 0, 1)))) +
		(CombL.ar(delayin, 1.0, basefreq.reciprocal, 0.5, 2)));
	bal = (delayin * (balance*0.5pi).cos) + (filts * (balance*0.5pi).sin);
	Out.ar(outbus, BFEncode1.ar(bal, LFNoise1.kr(0.1, 2pi), 0, 1) * outlevel);
	Out.ar([cleanout, scopeout], delayin);
	Out.ar(mixout, bal);
	}).load(s);
	
SynthDef(\mixgrains, {arg gate = 1, inbus = 0, outbus = 0;
	var grains, env;
	env = Control.names([\env]).kr(Env.newClear(5).asArray);
	grains = MonoGrainBF.ar(In.ar(inbus), 0.1, 10, 0.01, 0, 1);
	Out.ar(outbus, grains * EnvGen.kr(env, gate, doneAction: 2))
	}).load(s);

SynthDef(\controlenv, {arg gate = 1, outbus = 0, doneAction = 2;
	var env;
	env = Control.names([\env]).kr(Env.newClear(16).asArray);
	Out.kr(outbus, EnvGen.kr(env, gate, doneAction: doneAction))
	}).load(s);

SynthDef(\grains, {arg inbus = 21, grainsize = 1, grainfreq = 10, loc = 0, elev = 0, rho = 1, 
		basefreq = 130, dur = 1, mul = 1, bufnum = 0, outbus = 0;
	var in, env, out, freq, grains;
	in = In.ar(inbus, 1);
//	in = PinkNoise.ar(0.1);
	freq = XLine.kr(basefreq, basefreq * Rand.new(0.7, 1.4), dur);
	out = ((BPF.ar(in, freq, 0.05, 0.05) * ( 10 + (SinOsc.ar(0.25, 0, 1)))) + 
		(BPF.ar(in, (freq)*2, 0.05, 0.05) * ( 5 + (SinOsc.ar(0.33, 0, 3)))) +
		(BPF.ar(in, (freq)*3, 0.05, 0.05) * ( 3 + (SinOsc.ar(0.1, 0, 3)))) +
		(BPF.ar(in, (freq)*5, 0.05, 0.05) * ( 5 + (SinOsc.ar(0.05, 0, 1)))) +
		(BufCombL.ar(bufnum, in, freq.reciprocal, 0.5, 2)));
	grains = MonoGrainBF.ar(out, grainsize, grainfreq, 0.01, Line.kr(loc, Rand.new(-pi, pi), 
		dur * 0.3), Line.kr(0.01, 0.2, dur * 0.3), elev, 0, rho);
	env = EnvGen.kr(Env([0, 1, 0], [0.01, 0.99], [\sin, -4]), timeScale: dur, doneAction: 2);
	Out.ar(outbus, grains * env * mul);
}).load(s);

SynthDef(\trigger, {
	SendTrig.kr(Impulse.kr(1), 0, 0);
	Line.kr(1, 1, 0.1, doneAction: 2);
	}).load(s);
	
SynthDef(\trom_buffer,{ arg gate = 1, bufnum, amp = 1, dur = 244, outbus = 16;
	var disk;
	//set envelope later to 244 seconds when testing done
	disk = DiskIn.ar( 3, bufnum ) * EnvGen.kr(Env([0.001, 1, 1, 0.001], [0.001, dur, 1], 'exp', 2),
		gate, doneAction: 2);
	Out.ar(outbus, disk * amp);	
}).load(s);

SynthDef(\hide_buffer,{ arg gate = 1, bufnum, amp = 1, dur = 96, outbus = 16;
	var disk;
	//set envelope to 96 later, when testing is done
	disk = DiskIn.ar( 3, bufnum ) * EnvGen.kr(Env([0.001, 1, 1, 0.001], [0.001, dur, 1], 'exp', 2), 		gate, doneAction: 2);
	Out.ar(outbus, disk * amp);	
}).load(s);

)

/*
Gain (feedback pct)

out - volume out

filtmix- % of filt (0 all straight, 1 all filt)

mic in- 0- 19

rev- not really used

filt scale
*/