
( // synthdefs

SynthDef(\dlywrangler, {
	arg out=0, in;
	var snd;
	snd = In.ar(in,2);
	snd = CombL.ar(snd, 1, Lag.ar(LFNoise0.ar(1).range(0.01,0.1),0.01), 3, mul:0.1) + snd;
	Out.ar(out, snd);
}).add;


SynthDef(\wiDel, {
	var snd = In.ar(\in.kr, 2);
	snd = CombL.ar(snd, 1, SinOsc.ar(0.02,[0,pi/2]).range(0.5,0.6)+[0,0.22], 10, mul:1) + snd;
	Out.ar(\out.kr(0), snd);
}).add;


SynthDef(\softSaw, {
	arg out = 0, freq = 40, oscdetune = 0.1, amp = 1, pan = 0,
	gate = 1, attackTime = 0.1, susLevel = 1.0, releaseTime = 1.0;
	var env, snd;
	oscdetune = oscdetune * 0.1;
	amp = amp * 0.4;

	env = Linen.kr(gate, attackTime, susLevel, releaseTime, doneAction:2);

	snd = VarSaw.ar(
		freq: [freq, freq * (1.003 + oscdetune)],
		width: SinOsc.kr(0.01,0.2).range(0.05,0.095));
	snd = snd + VarSaw.ar(
		freq: [(freq*2 * (1.001 - oscdetune)),(freq*2 * (1 + oscdetune))],
		width: SinOsc.kr(0.06,0.8).range(0.2,0.8));

	
	snd = Balance2.ar(snd.at(0), snd.at(1), pos: pan);
	
	snd = snd * amp * env;

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


// "LFTri bass drum kick" - by luka@lukaprincic.si
SynthDef(\tribd, {
	arg out=0, gate=1, freq=77, amp=0.1;
	var snd, aenv, fenv, senv;

	fenv = Env(levels:[2, 1, 0], times: [0.1, 0.5]).kr;
	freq = freq * fenv;
	freq = freq.max(10);

	snd = LFTri.ar(freq) + SinOsc.ar(freq);
	snd = LPF.ar(snd, freq * 4);

	snd = snd + RLPF.ar( BrownNoise.ar(1),
		Env(levels:[2000,500,90], times:[0.01,0.05]).kr, 0.3 );

	snd = snd + ( BPF.ar(WhiteNoise.ar(0.9), 3000) 
		* Env([1,1,0], [0.004,0.005]).kr );
	snd = snd + ( BPF.ar(WhiteNoise.ar(0.1), 9000) 
		* Env([1,1,0], [0.002,0.002]).kr );
	
	aenv = Env([1,1,0], [0.2,2], curve: -9).kr;
	snd = snd * aenv;
	
	senv = Linen.kr(releaseTime: 0.1, gate: gate, doneAction: Done.freeSelf);
	snd = LeakDC.ar(snd);
	snd = snd * amp * senv;
	snd = Pan2.ar(snd, pos:0);
	Out.ar(out, snd);
}).add;

// SC808 drum snare
SynthDef.new(\sc808sn, {
	arg amp=2, tone=340, tone2=189, snappy=0.3, gate=0, amp2=1, out=0;
	var noiseEnv, atkEnv, sig, noise, osc1, osc2, sum;
	noiseEnv = EnvGen.kr(Env.perc(0.001, 4.2, 1, -115), doneAction:2);
	atkEnv = EnvGen.kr(Env.perc(0.001, 0.8,curve:-95), doneAction:0);
	noise = WhiteNoise.ar;
	noise = HPF.ar(noise, 1800);
	noise = LPF.ar(noise, 8850);
	noise = noise * noiseEnv * snappy;
	osc1 = SinOsc.ar(tone2, pi/2) * 0.6;
	osc2 = SinOsc.ar(tone, pi/2) * 0.7;
	sum = (osc1+osc2) * atkEnv * amp2;
	sig = Pan2.ar((noise + sum) * amp * 2.5, 0);
	sig = HPF.ar(sig, 340);
	Out.ar(out, sig);
}).add;

SynthDef.new(\sc808hh, {
	arg decay=0.42, amp=1, pan=0, out=0;
	var sig, sighi,siglow, sum, env, osc1, osc2, osc3, osc4, osc5, osc6;
	env = EnvGen.kr(Env.perc(0.005, decay, 1, -30),doneAction:2);
	osc1 = LFPulse.ar(203.52);
	osc2 = LFPulse.ar(366.31);
	osc3 = LFPulse.ar(301.77);
	osc4 = LFPulse.ar(518.19);
	osc5 = LFPulse.ar(811.16);
	osc6 = LFPulse.ar(538.75);
	sighi = (osc1 + osc2 + osc3 + osc4 + osc5 + osc6);
    siglow = (osc1 + osc2 + osc3 + osc4 + osc5 + osc6);
    sighi = BPF.ar(sighi, 8900, 1);
    sighi = HPF.ar(sighi, 9000);
    siglow = BBandPass.ar(siglow, 8900, 0.8);
    siglow = BHiPass.ar(siglow, 9000, 0.3);
    sig = BPeakEQ.ar((siglow+sighi), 9700, 0.8, 0.7);
    sig = sig * env * amp;
    sig = Pan2.ar(sig, pan);
    Out.ar(out, sig);
}).add;


// fx buses
~delay1 = Bus.audio(s, 2);
~delay2 = Bus.audio(s,2);


/////////////////////////////////////////////////////////////////////
// timeline of events

r = Routine {

	1.wait;
	
	// delays
	~dlywrang1 = Synth(\dlywrangler, [\out, 0, \in, ~delay1]);
	~widel1 = Synth(\wiDel, [\in, ~delay2, \out, 0]);

	1.wait;
	(
		Pbindef(\x99,
			* [
				instrument: \tribd,
				octave:3,
				degree: Prand([1,2,3]/10,inf),
				dur: Pseq([3,2,Prand([2,3]),3,2,2]/8, inf),
				amp: 0.2
			]
		).play;
	);
	7.wait;

	( // snare
		Pbindef(\snp1,
			*[
				instrument: \sc808sn,
				dur: Pseq([Rest(1),Pxrand([1,1.5,0.5,Pn(0.5,4),Pn(0.25,2),Pn(0.25,2),Pn(0.25,2)])]/2,inf),
				amp: Prand([1,2,3]*0.05+0.2,inf),
				out: Prand([0,~delay1],inf)
			]
		).play;
	);
	7.wait;
	
	( // highhat
		Pbindef(\hhp1,
			* [
				instrument: \sc808hh,
				dur: 1/8,
				amp: Prand([1,2,3]/10, inf),
				out: Pwrand([~delay1,0],[0.7,0.3],inf),
				pan: Pfunc({rrand(-0.6,0.6)})
			]
		).play;
	);

	21.wait;

	( // dark saw
		Pbindef(\sawp1,
			*[
				instrument: \softSaw,
				dur: 10,
				attackTime: 5,
				releaseTime:5,
				legato:1,
				degree: Pseq([ [-2,3,11,20], [-2,5,7,21] ],inf),
				octave: 3,
				amp: [1,0.5,0.2,0.1] * 0.2,
				out: ~delay2
			]
		).play;
	);

	77.wait;

	Pbindef(\hhp1).stop;

	10.wait;

	Pbindef(\snp1).stop;

	10.wait;

	Pbindef(\x99).stop;

	10.wait;

	Pbindef(\sawp1).stop;
	


}.play;





);
