(
Server.default = s = Server.internal;

s.waitForBoot({

var

width= 1280, proj= width-(width/16), painel=proj/4, height= width*0.4, 
usr1, usr2, usr3, 
theta=0, theta2=0, theta3=0,
canto1, canto2, canto3, canto4, instr1, instr2,
ctr1a=0, ctr2a=0, ctr3a=0, ctr4a=0, ctr5a=0, ctr6a=0,
ctr1=0, ctr2=0, ctr3=0, ctr4=0, ctr5=0, ctr6=0, 
cmb1=0, cmb2=0, cmb3=0, cmb4=0,cmb5=1280, cmb6=3820, 
coloc=0, coloc2=0, coloc3=0, coloc4=0, coloc5=0, coloc6=0,
passo1=width*0.00037065972222222, 
passo2=width*0.00021690538194445, 
passo3=width*0.0003125,
tex1=width*1.171875, 
tex2=width*(-12.171875), 
tex3=width*1.06640625, 
tex4=width*(-6.7421875), 
tex5=width, 
tex6=width*(-1.4375),
font1=width*0.0296875, 
font2=width/16,
oscresp,
controls, 

slide1 = 0, slide2 = 0, slide3 = 0, slide4 = 0, slide5 = 0, slide6 = 0, slide7 = 0, slide8 = 0, slide9 = 0,
slidemidi1 = 0, slidemidi2 = 0, slidemidi3 = 0, slidemidi4 = 0, slidemidi5 = 0, slidemidi6 = 0, slidemidi7 = 0, slidemidi8 = 0, slidemidi9 = 0,
knob1 = 0, knob2 = 0, knob3 = 0, knob4 = 0, knob5 = 0, knob6 = 0, knob7 = 0, knob8 = 0, knob9 = 0,
knobmidi1 = 0, knobmidi2 = 0, knobmidi3 = 0, knobmidi4 = 0, knobmidi5 = 0, knobmidi6 = 0, knobmidi7 = 0, knobmidi8 = 0, knobmidi9 = 0,

meter, texto1, texto2, texto3, cor, fonte, fonte2,
button1, button2, button3, button4, button2task, button3task;

texto1= "each one who enters imagines himself to be the first to enter whereas he is always the last of a preceding series even if the first term of a succeeding one, each imagining himself to be the first, last, only and alone, whereas he is neither first nor last nor only nor alone in a series originating in and repeated to infinity. passou por aqui aquele que pensava que não ia morrer, a sua sombra seguia-o, macaqueando o seu modo de andar e os seus pensamentos e o seu próprio ser. demasiado cheia de palavras para conseguir exprimi-las, she appeared like an angel. she is alone. they cannot touch her. she ventured a look at him and the face that met her gaze there in the twilight, wan and strangely drawn, seemed to her the saddest she had ever seen. - see this. remember. as suas recordações consumiam-no. ela não sabia. bocejou. era demasiado. ninguém sabia. nunca ninguém saberia. estava tudo acabado. ela tinha dezoito anos e era adorável e estava perdida. what of all the will to do? it has vanished long ago.".asciiCorrect;

texto2= "each one who enters imagines himself to be the first to enter whereas he is always the last of a preceding series even if the first term of a succeeding one, each imagining himself to be the first, last, only and alone, whereas he is neither first nor last nor only nor alone in a series originating in and repeated to infinity. as suas recordações consumiam-no. ela não sabia. bocejou. era demasiado. ninguém sabia. nunca ninguém saberia. estava tudo acabado. ela tinha dezoito anos e era adorável e estava perdida. what of all the will to do? it has vanished long ago.".asciiCorrect;

texto3=  "she said, don't leave me alone; i'll never let go, said he.";

cor = Color.red(0, 1);
fonte = Font("Times", font1);
fonte2 = Font("Times", font2);
	
//declaração micros
SynthDef(\trig, {|id, mic, amp=1|
	var src= SoundIn.ar(mic, amp).abs; 
	SendTrig.kr(Impulse.kr(60),id,src);}).add;

	

s.sync;

//sistema de lag
Routine({
	inf.do{
		slide1= slide1-(slide1-slidemidi1*0.5);
		slide2= slide2-(slide2-slidemidi2*0.5);
		slide3= slide3-(slide3-slidemidi3*0.5);
		slide4= slide4-(slide4-slidemidi4*0.5);
		slide5= slide5-(slide5-slidemidi5*0.5);
		slide6= slide6-(slide6-slidemidi6*0.5);
		slide7= slide7-(slide7-slidemidi7*0.5);
		slide8= slide8-(slide8-slidemidi8*0.5);
		slide9= slide9-(slide9-slidemidi9*0.5);
		
		knob1= knob1-(knob1-knobmidi1*0.5);
		knob2= knob2-(knob2-knobmidi2*0.5);
		knob3= knob3-(knob3-knobmidi3*0.5);
		knob4= knob4-(knob4-knobmidi4*0.5);
		knob5= knob5-(knob5-knobmidi5*0.5);
		knob6= knob6-(knob6-knobmidi6*0.5);
		knob7= knob7-(knob7-knobmidi7*0.5);
		knob8= knob8-(knob8-knobmidi8*0.5);
		knob9= knob9-(knob9-knobmidi9*0.5);
				
		(1/60).wait;
	};
}).play;

//controlador midi
CCResponder.removeAll;
CCResponder({|src,chan,num,val|
	switch(num,
		2, {slidemidi1= val.lincurve(0, 127, 0, 1.5, 3)},
		3, {slidemidi2= val.lincurve(0, 127, 0, 1.5, 3)},
		4, {slidemidi3= val.lincurve(0, 127, 0, 1.5, 3)},
		5, {slidemidi4= val.lincurve(0, 127, 0, 1.5, 3)},
		6, {slidemidi5= val.lincurve(0, 127, 0, 1.5, 3)},
		8, {slidemidi6= val.lincurve(0, 127, 0, 1.5, 3)},
		9, {slidemidi7= val.lincurve(0, 127, 0, 1.5, 3)},
		12, {slidemidi8= val.lincurve(0, 127, 0, 1.5, 3)},
		13, {slidemidi9= val.lincurve(0, 127, 0, 1.5, 3)},
		
		14, {knobmidi1= val.lincurve(0, 127, 0, 40, 3)},
		15, {knobmidi2= val.lincurve(0, 127, 0, 40, 3)},
		16, {knobmidi3= val.lincurve(0, 127, 0, 40, 3)},	
		17, {knobmidi4= val.lincurve(0, 127, 0, 40, 3)},
		18, {knobmidi5= val.lincurve(0, 127, 0, 40, 3)},
		19, {knobmidi6= val.lincurve(0, 127, 0, 40, 3)},
		20, {knobmidi7= val.lincurve(0, 127, 0, 40, 3)},
		21, {knobmidi8= val.lincurve(0, 127, 0, 40, 3)},		
		22, {knobmidi9= val.lincurve(0, 127, 0, 40, 3)}
	)
});

//visuals
w= Window("pasta", Rect(width/32, 800, proj-2, height), false, false);
usr1= UserView(w, Rect(0, 0, proj, height));
usr2= UserView(w, Rect(0, 0, proj, height));
usr3= UserView(w, Rect(0, 0, proj, height));

// sistema de lag
Routine({
	inf.do{
		ctr1= ctr1-(ctr1-ctr1a*0.05);
		ctr2= ctr2-(ctr2-ctr2a*0.05);
		ctr3= ctr3-(ctr3-ctr3a*0.05);
		ctr4= ctr4-(ctr4-ctr4a*0.05);
		ctr5= ctr5-(ctr5-ctr5a*0.05);
		(1/60).wait;
	};
}).play;

//destribuidor do som
OSCresponder(s.addr, \tr, {|t, r, m|
	switch(m[2],
		1, {ctr1a= m[3];},
		2, {ctr2a= m[3];},
		3, {ctr3a= m[3];},
		4, {ctr4a= m[3];},
		5, {ctr5a= m[3];}
	);
}).add;

//random onde fica o texto
coloc=  rrand(width/(-128), height-(font1*4.3));
coloc2= coloc+font1;
coloc3= coloc2+font1;
coloc4= coloc3+font1;

coloc5= rrand(width/(-64), width/3.3862433862434);
coloc6= rrand(coloc5+(width/32), width/3.0622009569378);

//rectângulos
usr1.drawFunc= {
	Pen.smoothing = true;

	1.do{
		Pen.fillColor= Color.gray(ctr1, 1);
		Pen.fillRect(Rect(0, 0, painel, height)); 	

		Pen.fillColor= Color.gray(ctr2, 1);
		Pen.fillRect(Rect(painel, 0, painel, height)); 	

		Pen.fillColor= Color.gray(ctr3, 1);
		Pen.fillRect(Rect(2*painel, 0, painel, height)); 
		
		Pen.fillColor= Color.gray(ctr4, 1);
		Pen.fillRect(Rect(3*painel, 0, painel, height)); 
	};	
		
};		

//texto base
usr2.drawFunc= {
	Pen.smoothing = true;	
	
	Pen.color = cor;
	Pen.font = fonte;
	Pen.stringAtPoint(texto1, Point(cmb1,coloc));
	Pen.stringAtPoint(texto1, Point(cmb2,coloc2));
	Pen.stringAtPoint(texto2, Point(cmb3,coloc3));
	Pen.stringAtPoint(texto2, Point(cmb4,coloc4));

	cmb1=tex1-(theta%80000);
	cmb2=tex2+(theta%80000);
				
	cmb3=tex3-theta2;
	cmb4=tex4+theta2;

};

//texto instrumento
usr3.drawFunc= {
	Pen.smoothing = true;
	
	Pen.color = Color.blue(0.75, ctr5);
	Pen.font = fonte2;
	
	Pen.stringAtPoint(texto3, Point(cmb5,coloc5));
	Pen.stringAtPoint(texto3, Point(cmb6,coloc6));
	
	cmb5=tex5-theta3;
	cmb6=tex6+theta3;
};

// -----------------------------------------------------------------------------------------------------

//controlos
controls = SCWindow("o principio do amor", Rect(640, 0, 500, 270));
controls.view.background = Color.grey(0.1);

//butões
// 1-activa som
button1 = Button(controls, Rect(50, 50, 100, 100))
    .canFocus_(false)
    .states_([
	["anime: q", Color.black, Color.grey],
	["stop anime: Q", Color.black, Color.red],
    ])
    .action_({ arg it;
    var num = it.value;

    if(num == 1) 
	{
		
canto1= Synth(\trig, [\id, 1, \mic, 0]);
canto2= Synth(\trig, [\id, 2, \mic, 1]);
canto3= Synth(\trig, [\id, 3, \mic, 3]);
canto4= Synth(\trig, [\id, 4, \mic, 5]);
instr1= Synth(\trig, [\id, 5, \mic, 7]);

Routine({
	inf.do{

canto1.set(\amp, knob1);
canto2.set(\amp, knob2);
canto3.set(\amp, knob3);
canto4.set(\amp, knob4);
instr1.set(\amp, knob5);
				
		(1/60).wait;
	};
}).play;

	};
    if(num == 0) 
	{
		
canto1.free;
canto2.free;
canto3.free;
canto4.free;
instr1.free;

	};
    });
button1.font_(Font("Lucida Grande", 12));

// 2-activa texto
button2 = Button(controls, Rect(200, 50, 100, 100))
    .canFocus_(false)
    .states_([
	["text1: t", Color.black, Color.grey],
	["stop text: T", Color.black, Color.red],
    ])
    .action_({ arg it;
    var num = it.value;

    if(num == 1) 
	{
		
button2task =  Routine({while({w.isClosed.not}, { (0.016666666666667).wait; theta= theta+passo1; theta2=theta2+passo2})}).play;

	};
    if(num == 0) 
	{
		
button2task.stop;

	};
    });
button2.font_(Font("Lucida Grande", 12));

// 3-activa texto instrumento
button3 = Button(controls, Rect(350, 50, 100, 100))
    .canFocus_(false)
    .states_([
	["text2: i", Color.black, Color.grey],
	["stop text2: I", Color.black, Color.red],
    ])
    .action_({ arg it;
    var num = it.value;

    if(num == 1) 
	{
		
button3task =  Routine({while({w.isClosed.not}, { (0.016666666666667).wait; theta3= theta3+passo3})}).play;

	};
    if(num == 0) 
	{
		
button3task.stop;

	};
    });
button3.font_(Font("Lucida Grande", 12));

// 4-reset theta
button4 = Button(controls, Rect(50, 200, 400, 20))
    .canFocus_(false)
    .states_([
	["reset txt: 0", Color.black, Color.grey],
	["reset txt: 0", Color.black, Color.red],
    ])
    .action_({ arg it;
    var num = it.value;

    if(num == 1) 
	{
		
theta =  0;
theta2 = 0;
theta3 = 0;
button4.value_(0)

	};
    if(num == 0) 
	{

	};
    });
button4.font_(Font("Lucida Grande", 12));

//teclado
controls.view.keyDownAction_( { |view, char, mod, uni, key|
	if(char == $Q, { button1.valueAction_(0) }, { nil });
	if(char == $q, { button1.valueAction_(1) }, { nil });
	if(char == $T, { button2.valueAction_(0) }, { nil });
	if(char == $t, { button2.valueAction_(1) }, { nil });
	if(char == $I, { button3.valueAction_(0) }, { nil });
	if(char == $i, { button3.valueAction_(1) }, { nil });

	if(char == $0, { button4.valueAction_(1) }, { nil });
});

// ----------------------------------------------------------------

meter = AudioMeter.input([0, 1, 2, 3, 4], rect: 1200@0);

w.front;
controls.front;

//animação do som
usr1.animate = true;
usr2.animate = true;
usr3.animate = true;

//t = Routine({while({w.isClosed.not}, {usr1.refresh; usr2.refresh; usr3.refresh; (0.016666666666667).wait})}).play(AppClock);

CmdPeriod.doOnce{if(w.isClosed.not, {w.close; controls.close; meter.quit})}
});
)

SCWindow.closeAll