// arp galore!

// set bus for the fx-delay
~delay1 = Bus.audio(s, 2);

( // synthdefs:

SynthDef(\sin1, {
	arg freq=440, out=0, gate=1, amp=0.1, release=0, cutoff=10, rq=0.5;
	var snd, env;
	
	freq = [freq,freq*1.01];
	env = Linen.kr(gate, attackTime: 0.001, releaseTime: release, doneAction:2);
	snd = SinOsc.ar(freq) + Saw.ar(freq*1.004, mul:0.3) + Pulse.ar(freq*0.996, mul:0.4);
	snd = snd + snd.fold2(0.65);
    snd = (snd * 0.5) + (snd.wrap2(0.64) * 0.5) * 2;
	cutoff = cutoff  * 0.1;
	cutoff = freq.pow(cutoff);
	cutoff = cutoff.max(100);
	cutoff = cutoff.min(18000);
	snd = RLPF.ar(snd, cutoff, rq);
	snd = snd * env;
	snd = snd * amp;

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

SynthDef(\dlywrangler, {
	arg out=0, in, wet=0.1, wet2=0.5;
	var snd, dly2;
	
	snd = In.ar(in,2);
	dly2 = CombN.ar(HPF.ar(snd,300), 1, [0.5,0.77], 5, mul: LFNoise1.kr(0.1).range(0.4,0.1) * 2 * wet2);
	snd = CombN.ar(snd, 1, Lag.ar(LFNoise0.ar(1).range(0.01,0.1),0.1), 3, mul:wet) + snd;
	snd = Limiter.ar(snd, 0.8);
	snd = snd + dly2;
	
	Out.ar(out, snd);
}).add;
)

// create delaymangler comb // on ctrl+. you need to reeval this
~dlywrang1 = Synth(\dlywrangler, [\out, 0, \in, ~delay1]);
~dlywrang1.set(\wet2, 0.8); // normal space delay


( // basics
Pbindef(\y3, 
	\instrument, \sin1,
	\degree, Pseq( [ 0, 2, -1, 5, -2, 6 ], inf),
	\octave, 4, 
	\scale, Scale.minor(\just),
	\mtranspose, 0,

	\cutoff, Pstutter(16, Pseq([Pseq((13..18)),Pseq((17..12))].min(18),inf)),
	\rq, Prand([0.2,0.3,0.5]*1,inf),
	\legato, Pseq([Prand([1,0.7]), 0.6, 0.9, 0.4, 0.6] * 0.2, inf),
	\release, 0.1,

	\dur, 1/8,
	\amp, [0.2,0.2,0.1] * Pseq([Prand([1,0.7]), 0.6, 0.9, 0.7, 0.6], inf) * 0.5,
	
	\dlywet, Pstutter(8, Pfunc({~dlywrang1.set(\wet, rrand(0, ((0..1) * 0.1).choose ) ) })),	
	\out, ~delay1,
)
)
Pdef(\y3).quant=4;
Pdef(\y3).play

// PLAY HERE:
// change these freely without restarting other patterns

// shuffle?
Pbindef(\y3, \dur, Pseq([2.1/16, 1.9/16],inf))
Pbindef(\y3, \dur, Pseq([2.3/16, 1.7/16],inf))
Pbindef(\y3, \dur, Pseq([2.5/16, 1.5/16],inf))
Pbindef(\y3, \dur, 1/8)

// shorter/longer notes:
Pbindef(\y3, \legato, Pseq([Prand([1,0.7]), 0.6, 0.9, 0.4, 0.6] * 0.5, inf))
Pbindef(\y3, \legato, Pseq([Prand([1,0.7]), 0.6, 0.9, 0.4, 0.6] * 1, inf))
Pbindef(\y3, \legato, Pseq([Prand([1,0.7]), 0.6, 0.9, 0.4, 0.6] * 0.1, inf))

// degree
Pbindef(\y3, \degree, Pseq( [ 0, 2, -1, 5, -2, 6 ], inf)) // default
Pbindef(\y3, \degree, Pseq( [ [7,0], 2, -1, 5, Prand([-2,Pseq([2,6])]) ], inf))
Pbindef(\y3, \degree, Pseq( [ [7,0], 2, -1, Prand([4,5]), Prand([-2,Pseq([2,6])]) ], inf))

// octave
Pbindef(\y3, \octave, 4)
Pbindef(\y3, \octave, [4,5])
Pbindef(\y3, \octave, [4,5,6])
Pbindef(\y3, \octave, [4,5,6] + Pwrand([0,0,1,-1],[0.7,0.1,0.1,0.1],inf))
Pbindef(\y3, \octave, [4,5,6] + Pwrand([0,2,1,-1],[0.7,0.1,0.1,0.1],inf))
Pbindef(\y3, \octave, [4,5,6] + Pwrand([0,2,1, 3],[0.7,0.1,0.1,0.1],inf))
Pbindef(\y3, \octave, Pstutter(32, Pseq( [5, [4,5], [4,5,6] ], inf), inf))
Pbindef(\y3, \octave, Pstutter(32, Pseq( [5, [4,5], [4,5,6] ], inf), inf) + Pwrand( [0,1,1,-1], [0.7,0.1,0.1,0.1], inf))
Pbindef(\y3, \octave, Pstutter(32, Pseq( [5, [4,5], [4,5,6] ], inf), inf) + Pwrand( [0,1,2,-1], [0.7,0.1,0.1,0.1], inf))

// mtranspose
Pbindef(\y3, \mtranspose, 0)
Pbindef(\y3, \mtranspose, Pstutter(16, Pseq([Pn(0,2), Pn(3,2)],inf)))
Pbindef(\y3, \mtranspose, Pstutter(16, Pseq([Pn(0,4), 3,5],inf)))
Pbindef(\y3, \mtranspose, Pstutter(16, Pseq([7, Pn(0,3), 3, 5 ],inf)))

// gritty it with more 'wetness' to wrangler delay
Pbindef(\y3, \dlywet, Pstutter(8, Pfunc({~dlywrang1.set(\wet, rrand(0, ((0..1) * 0.1).choose ) ) }))) // default
Pbindef(\y3, \dlywet, Pstutter(8, Pfunc({~dlywrang1.set(\wet, rrand(0, ((0..4) * 0.1).choose ) ) })))
Pbindef(\y3, \dlywet, Pstutter(8, Pfunc({~dlywrang1.set(\wet, rrand(0, ((5..9) * 0.1).choose ) ) })))
Pbindef(\y3, \dlywet, Pstutter(8, Pfunc({~dlywrang1.set(\wet, rrand(0, 1 ) ) })))
Pbindef(\y3, \dlywet, Pstutter(8, Pfunc({~dlywrang1.set(\wet, rrand(0, 0.2 ) ) })))

// filter
Pbindef(\y3, \cutoff, Pstutter(16, Pseq([Pseq((13..18)),Pseq((17..12))].min(18),inf)))
Pbindef(\y3, \cutoff, Pstutter(16, Pseq([Pseq((13..18)),Pseq((17..12))].min(13),inf)))
Pbindef(\y3, \cutoff, Pstutter(16, Pseq([Pseq((13..18)),Pseq((17..12))].min(11),inf)))
Pbindef(\y3, \cutoff, Pstutter(16, Pseq([Pseq((13..18)),Pseq((17..12))].min(10),inf)))

Pbindef(\y3).stop
