
(
// function to partially match filename for buffers
~getSmp = { |regexp|
	~smpBuffers.detect { |buf|
		regexp.matchRegexp(buf.path)
	}
};
)

(
SynthDef(\bufCyc, {
	arg bufnum, gate=1, freq=440, chanDetune=1, out=0, amp = 1, attackTime=0.01, releaseTime=1;
	
	var snd, env;
	
	env = Linen.kr(gate, attackTime:attackTime, releaseTime:releaseTime, doneAction:2);

	freq = [freq, freq * (1 + (chanDetune * 0.0005))];
	
	snd = BufRd.ar(
		numChannels: 1,
		bufnum: bufnum,
		//phase: LFSaw.ar(BufDur.ir(bufnum).reciprocal).range(0, BufFrames.ir(bufnum)),
		phase: LFSaw.ar(freq).range(0, BufFrames.ir(bufnum)),
		loop: 1,
		interpolation: 2);

	snd = snd * amp * env;
	Out.ar(out, snd); 

}).add;
)

Pdef(\x32).play
Pdef(\x32).fadeTime = 0;
(
Pdef(\x32,
	Pbind(
		\instrument, 	\bufCyc,
		\bufnum, 		[~getSmp.value("0043"),~getSmp.value("0043")],
		\octave,		[3,2,4],
		\degree, 		Pseq([2,2,4,1], inf),
		\mtranspose,	Pseq([Pn([0],10),Pn([1],5),Pn([0],10),Pn([3],4)],inf),
		\ctranspose,	Pseq([Pn([0],50), Pn([5],5)],inf),
		\detune,		[0.1,0],
		\chanDetune,	20,
		//\dur,			Pseq([0.20,0.40,0.60],inf),
		\dur,			Pseq([0.20,0.40,Prand([0.60,0.20])],inf),
		\legato,		0.8,
		\amp,			[0.3,0.5,0.1],
		\releaseTime,	0.06
	));
)

Pdef(\x33).play
Pdef(\x33).fadeTime = 0;
(
Pdef(\x33,
	Pbind(
		\instrument, 	\bufCyc,
		\bufnum, 		[~getSmp.value("0246"),~getSmp.value("0229")],
		\octave,		[4,5,6],
		\degree, 		Pseq([[4,9],2,8,4,1], inf),
		\mtranspose,	Pseq([Pn([0],10),Pn([3],10),Pn([0],10),Pn([5],5)],inf),
		\ctranspose,	Pseq([Pn([0],50), Pn([5],10)],inf),
		\detune,		[0,0.3,-0.3],
		\chanDetune,	Pseq([-10,0,5,10,-5],inf),
		\dur,			Pseq([0.20,0.40,Prand([0.60,0.20])],inf),
		\legato,		0.1,
		\amp,			[0.7,0.5,0.1] * 0.1,
		\releaseTime,	1
	));
)
