(
SynthDef(\snap, {
	arg out=0, gate=1, amp=0.1;
	var snd, aenv, cenv;

	snd = WhiteNoise.ar;
	
	aenv = EnvGen.ar(Env([0,1,1,0], [0.001,0.03,0.01]), gate: gate);
	snd = snd * aenv;

	snd = snd + (SinOsc.ar(Rand(6000,15000)) * EnvGen.ar(Env.perc(0, 0.05)));

	snd = HPF.ar(snd, Line.ar(500,3000,0.1));

	// env that contains doneAction MUST BE sustained for Pbind to work!
	cenv = EnvGen.ar(Env.adsr(0.001,0.1,0.9,0.01), gate: gate, doneAction:2);
	snd = CombN.ar(snd, 1, Rand(0.01,0.1), 10, mul:0.3) + snd;
	snd = snd * cenv * amp;
	snd = Pan2.ar(snd, Rand(-1,1) * 1/2);
	Out.ar(out, snd);
}).add;

SynthDef(\bdx, {
	arg out=0, gate=1, amp=0.1, freq=100;
	var snd, aenv, fltenf, frqenv;

	frqenv = EnvGen.ar(Env([5,1,0.5], [0.01,1]), gate);
	freq = freq * frqenv;
	snd = SinOsc.ar(freq) * Pulse.ar(freq/4).range(0.4,1);

	snd = snd +
	(WhiteNoise.ar * EnvGen.ar(Env.adsr(0.001,0.01,0.05), gate, 0.2));
	

	aenv = EnvGen.ar(Env.adsr(0.0001,0.2,0.5,0.01), gate, doneAction:2);
	snd = snd * aenv * amp;
	snd = snd.dup;
	Out.ar(out, snd);
}).add;

SynthDef(\sc808clap, { // from sc-808 project!
        arg amp=1, gate=1;
        var atkenv, atk, decay, sum, denv;
        atkenv = EnvGen.kr(Env.new([0.5,1,0],[0, 0.3], -160), doneAction:0);
        denv = EnvGen.kr(Env.dadsr(0.026, 0, 6, 0, 1, 1, curve:-157), doneAction:0);
        atk = WhiteNoise.ar * atkenv * 1.4;
        decay = WhiteNoise.ar * denv;
        sum = atk + decay * amp;
        sum = HPF.ar(sum, 500);
	sum = BPF.ar(sum, 1062, 0.5);
		sum = sum * EnvGen.ar(Env.asr(0.00001,1,0.0001), gate, doneAction:2);

        Out.ar(0, Pan2.ar(sum * 1.5, 0));
}).add;

SynthDef.new(\sc808sn, {
        arg amp=2, tone=340, tone2=189, snappy=0.3, gate=1, amp2=1;
        var noiseEnv, atkEnv, sig, noise, osc1, osc2, sum;
        noiseEnv = EnvGen.kr(Env.perc(0.001, 4.2, 1, -115), doneAction:0);
        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);
		sig = sig * EnvGen.ar(Env.asr(0.00001,1,0.0001), gate, doneAction:2);
        Out.ar(0, sig);
}).add;

SynthDef.new(\sc808hh, {
        arg decay=0.42, amp=1, pan=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(0, sig);
}).add;

)






/////////// patterns ///////////////////////////////////////////////////
(
Pbindef(\psnap,
	\instrument, \snap,
	\dur, Pseq([3,Prand([3,2,1]),Prand([2,Pseq([1/2],2)])] * 1/4, inf),
	\amp, Prand([1,2,3]/4, inf),
	\legato, Prand([1,2,3]/6, inf)
).play;


Pbindef(\pbdx,
	\instrument, \bdx,
	\dur, Pseq([3,Prand([3,2,1]),Prand([2,Pseq([1/2],2)])] * 1/4, inf),
	\legato, Prand((1..5) * 0.2, inf),
	\freq, 70 * Prand((1..9) + 120 * 0.01,inf),
	\amp, 0.7
).play;


Pbindef(\pcl,
	\instrument, \sc808clap,
	\dur, Pseq([2,Prand([2,2.5])],inf),
	\amp, 2,
).play
;

Pbindef(\psn,
	\instrument, \sc808sn,
	\dur, Prand([1.5,1.25,0.75],inf),
	\amp, 1,
	\legato, 0.04
).play
;

Pbindef(\phh,
	\instrument, \sc808hh,
	\dur, Pseq([Prand([1,1/2]),1,1,3]/4, inf),
	\amp, 0.5
).play
)