//////Cicatriz remix by @joseCao5 [at] www.mixfuckedup.net
/////original samples by @Huixtralizer
////written aug 2012, gedit / puredyne 
///for www.nucleoroto.org.mx
///
///
s.boot;
s.quit;
//analisis
s.scope;
FreqScope.new(150,100);
//stems
(
a=Buffer.read(s,"/home/josecaos/Desktop/Cicatriz+W.T.0846 Bliztik (Steams+Track)/audiostuff/cicatriz-stem 1.wav");
b=Buffer.read(s,"/home/josecaos/Desktop/Cicatriz+W.T.0846 Bliztik (Steams+Track)/audiostuff/cicatriz-stem 2.wav");
c=Buffer.read(s,"/home/josecaos/Desktop/Cicatriz+W.T.0846 Bliztik (Steams+Track)/audiostuff/cicatriz-stem 3.wav");
d=Buffer.read(s,"/home/josecaos/Desktop/Cicatriz+W.T.0846 Bliztik (Steams+Track)/audiostuff/cicatriz-stem 4.wav");
);
//a.free;
//b.free;
//c.free;
//d.free;
~r=Buffer.alloc(s, s.sampleRate * 16, 1);
//~r.free;
~r.plot
////
(// * evalua, si da error o algo, evalua otra vez, si no, evalua cada funcion o grupo de funciones
(// rec + granular
(
~rec={|gate=1,ent=90|
	var in,env;
		in=In.ar(ent);
		in=RecordBuf.ar(in,~r,0,1);
		env=EnvGen.ar(Env.asr(0.01,0.98,0.01),gate,doneAction: 2);
	Out.ar(70,Pan2.ar(in*env,[1, -0.9]))
}.play;

);
(
////grains
~g={|vol=0,d=4,rat=1|
	var stems,dur,trate;
		trate=MouseX.kr(1,16);
		dur=d/trate;
		stems=TGrains.ar(2,Impulse.kr(trate),~r,MouseY.kr(-1,3),0,dur,0.5);
	Out.ar(0,Normalizer.ar(Pan2.ar(stems,[1, -0.95]),vol));
}.play;
);
);
(/////play * samples
(
SynthDef(\clean,{|gate=1,out=0,vol=0,ent=20,maxdelaytime=0.25,delaytime=0,revmix=0.5,revtime=0.1,damp=0.05,cfreq=2000,rq=1|
	var in,env;
		in=Limiter.ar(RLPF.ar(In.ar(20),cfreq,rq),vol);
		in=CombC.ar(in,maxdelaytime,delaytime);
		in=HPF.ar(FreeVerb2.ar(in,in - 0.1,revmix,revtime,damp),92,0.25);
		env=EnvGen.ar(Env.asr(0.01,0.98,0.01),gate,doneAction:2);
	Out.ar(out,Pan2.ar(in*env,[-0.95, 1]))
}).add;
);
(
SynthDef(\frase1,{|gate=1,rate=1,out=#[90,91],out2=22|
	var sam,envl;
		sam=PlayBuf.ar(2,a,rate,1,0,1);
		envl=EnvGen.ar(Env.asr(0.01,0.98,0.01),gate,doneAction:2);
	Out.ar([out,out2],Pan2.ar(sam*envl,[1, -0.9]))
}).add;
);
(
SynthDef(\frase2,{|gate=1,rate=1,out=#[92,93],out2=22|
	var sam,envl;
		sam=PlayBuf.ar(2,b,rate,1,0,1);
		envl=EnvGen.ar(Env.asr(0.01,0.98,0.01),gate,doneAction:2);
	Out.ar([out,out2],Pan2.ar(sam*envl,[1, -0.9]))
}).add;
);
(
SynthDef(\frase3,{|gate=1,rate=1,out=#[94,95],out2=22|
	var sam,envl;
		sam=PlayBuf.ar(2,c,rate,1,0,1);
		envl=EnvGen.ar(Env.asr(0.01,0.98,0.01),gate,doneAction:2);
	Out.ar([out,out2],Pan2.ar(sam*envl,[1, -0.9]))
}).add;
);
(
SynthDef(\frase4,{|gate=1,rate=1,out=#[96,97],out2=22|
	var sam,envl;
		sam=PlayBuf.ar(2,d,rate,1,0,1);
		envl=EnvGen.ar(Env.asr(0.01,0.98,0.01),gate,doneAction:2);
	Out.ar([out,out2],Pan2.ar(sam*envl,[1, -0.9]))
}).add;
);
(
SynthDef(\bombo,{|gate=0,rel=0.1,out=0,mfreq=2,maxfrq=60,krev=0.025|
	var in,env;
		in=Limiter.ar(Pulse.ar([60,160,280]/Pulse.kr(mfreq-1,0.5,60,62),0.03)+LFTri.ar(Pulse.ar(mfreq,0.1,60,62),0.1,0.4),0.3);
		in=LPF.ar(in,740,0.2);
		in=HPF.ar(in,40,0.2);
		in=GVerb.ar(in,6,0.4,0.2);
		in=Compander.ar(in,in*1.1,0.5,0.5,0.5,0.5,0.2,1);
		env=EnvGen.ar(Env.perc(krev,rel),gate,doneAction:2);
	Out.ar(out,Pan2.ar(in*env,[-0.9,1]))
}).add;
);
(//synth variables
~fx=Synth(\clean);
~stem1=Synth(\frase1);
~stem2=Synth(\frase2);
~stem3=Synth(\frase3);
~stem4=Synth(\frase4);
);
);
(//tareas
(// selecconador de samples 
 ~task=Tdef(\samples,{
	inf.do({
	        var tiempo=[1,0.25,0.5].scramble.choose;
		a=[0,1,2,3].choose;
		w=0;		
		x=1;
		y=2;
		z=3;
		if(a==w,{"stem1".postln;~rec.set(\ent,[90,91]);
			~stem1.set(\out2,20);
			~stem2.set(\out2,82);
			~stem3.set(\out2,84);	
			~stem4.set(\out2,86)},
			{if(a==x,{"stem2".postln;~rec.set(\ent,[92,93]);
				~stem1.set(\out2,80);
				~stem2.set(\out2,20);
				~stem3.set(\out2,84);
				~stem4.set(\out2,86)},
				{if(a==y,{"stem3".postln;~rec.set(\ent,[94,95]);
					~stem2.set(\out2,82);
					~stem3.set(\out2,20);
					~stem1.set(\out2,80);
					~steam4.set(\out2,86)},
					{if(a==z,{"stem4".postln;~rec.set(\ent,[96,97]);
						~stem3.set(\out2,84);
						~stem4.set(\out2,20);
						~stem1.set(\out2,80);
						~stem2.set(\out2,82)}
					)}
				)}
			)}
		);
		["huixtralizer","remix","by","mixfuckedup.net"].scramble.postln;
		tiempo.wait;
	});
})
);
(
~rit=Tdef(\rythm,{
	var t=Pxrand([0.5,0.25,0.5,0.5,0.25,0.75,0.75],inf).asStream;
	var r=Pxrand([0.1,0.35,0.15,0.1,0.05],inf).asStream;
		inf.do({
			~kick=Synth(\bombo);
			~kick.set(\gate,1,\rel,r.resume);
			t.resume.wait;
			});
});
);
(
~rit2=Tdef(\rythm2,{
	var t=Pseq([0.25,0.5,0.5,0.25,0.5],inf).asStream;
		inf.do({
	                ~kick=Synth(\bombo);
			~kick.set(\gate,1,\rel,0.5);
			t.resume.wait;
			});
});
);
);
);
//controles
(//kicks + task
~task.play;//asignando las salidas al bufrec
(//kicks
~rit.play;//kick1
~rit2.play;//kick2
);
);
~task.stop;
~rit.stop;
~rit2.stop;
//MIDI
MIDIIn.connect;
MIDIIn.connect(0,MIDIClient.sources.at(3).postln);//selecciona el driver del controlador 
//MIDIIn.disconnect(0,MIDIClient.sources.at(3).free.postcln);
(
MIDIIn.control={|src,chan,num,val| //cambia los valores de "num" por los de tu controlador
		[src,chan,num,val].postln;
			switch(num,12,{~fx.set(\vol,val.linlin(0,127,0,0.3))},
				   11,{~g.set(\vol,val.linlin(0,127,0,0.2))},
				   15,{~kick.set(\rel,val.linlin(0,127,0.05,1))},
				   14,{~kick.set(\krev,val.linlin(0,127,0.05,1))},
				   24,{~fx.set(\maxdelaytime,val.linexp(0,127,0.1,0.5))},
				   27,{~fx.set(\delaytime,val.linlin(0,127,0,0.5))},
				   25,{~fx.set(\revmix,val.linexp(0,127,0.1,1))},
				   28,{~fx.set(\revtime,val.linexp(0,127,0.1,1))},
				   35,{~fx.set(\damp,val.linexp(0,127,0.01,0.25))},
				   26,{~fx.set(\cfreq,val.linexp(0,127,60,11000))},
				   29,{~fx.set(\rq,val.linlin(0,127,1,0.1))},
				   32,{~stem1.set(\rate,val.linlin(0,127,1,8))};{~stem2.set(\rate,val.linlin(0,127,1,8))};
					{~stem3.set(\rate,val.linlin(0,127,1,8))};{~stem4.set(\rate,val.linlin(0,127,1,8))}
				)
		};
);
s.record;
s.stopRecording;
