(
var
p1, p2, p3, p4,
controls, button1, button2, button3, sineplay,
patern;

Server.default = s = Server.internal;

s.waitForBoot({

var
amp=0.2;

// synth para sinosóides 
SynthDef(\flies1, {|out= 0, gate=1, freq=440, length=10, pan= 0.1, amp=0.01|
var env=  EnvGen.kr(Env.sine(length, amp), gate, doneAction:2);
var src= SinOsc.ar(freq+SinOsc.kr(0.01, 0, 2));
Out.ar(out, Pan2.ar(src*env, pan));
}).add;

s.sync;

//controlos
controls = SCWindow("sinewaves", Rect(640, 0, 350, 270));
controls.view.background = Color.grey(0.1);

//butões
// 2-toca faixa
button2 = Button(controls, Rect(50, 50, 100, 100))
    .canFocus_(false)
    .states_([
	["play"],
	["stop"]
    ])
    .action_({ arg it;
    var num = it.value;

if(num == 1) 

{

sineplay= Task(
	{
// coloca reverb no fim da lista de plays
//x = Synth.tail(s, \verb, [\in, 2, \out, 0, \time, 2, \room, 20]);

// define 4 Pbinds
p1 = Pbind(
	\instrument, \flies1,
	\dur, 50,
	\length, 40,
	\freq, Pseq([
	
	Pn(Prand([523, 131, 65], 1), 1), 
	Pn(Prand([784, 196], 1), 1), 
	Pn(Prand([587, 147], 1), 1), 
	Pn(Prand([440, 110], 1), 1), 
	Pn(Prand([1319, 330, 82], 1), 1), 
	Pn(Prand([988, 247], 1), 1), 
	
	Pn(Prand([740, 185], 1), 1), 
	Pn(Prand([554, 139], 1), 1), 
	Pn(Prand([1661, 415, 104], 1), 1), 
	Pn(Prand([1175, 311, 78], 1), 1), 
	Pn(Prand([932, 233], 1), 1), 
	Pn(Prand([699, 175], 1), 1)
	
	], inf, {rrand(0, 11)}),
	\amp, 0.2*amp,
	\pan, -0.9,
	\out, {30}
);

p2 = Pbind(
	\instrument, \flies1,
	\dur, 100,
	\length, 90,
	\freq, Pseq([
	
	Pn(Prand([523, 131, 65], 1), 1), 
	Pn(Prand([784, 196], 1), 1), 
	Pn(Prand([587, 147], 1), 1), 
	Pn(Prand([440, 110], 1), 1), 
	Pn(Prand([1319, 330, 82], 1), 1), 
	Pn(Prand([988, 247], 1), 1), 
	
	Pn(Prand([740, 185], 1), 1), 
	Pn(Prand([554, 139], 1), 1), 
	Pn(Prand([1661, 415, 104], 1), 1), 
	Pn(Prand([1175, 311, 78], 1), 1), 
	Pn(Prand([932, 233], 1), 1), 
	Pn(Prand([699, 175], 1), 1)
		
	], inf, {rrand(0, 11)}),
	\amp, 0.2*amp,
	\pan, -0.3,
	\out, {30}
);

p3 = Pbind(
	\instrument, \flies1,
	\dur, 150,
	\amp, 0.2*amp,
	\length, 140,
	\freq, Pseq([
	
	Pn(Prand([523, 131, 65], 1), 1), 
	Pn(Prand([784, 196], 1), 1), 
	Pn(Prand([587, 147], 1), 1), 
	Pn(Prand([440, 110], 1), 1), 
	Pn(Prand([1319, 330, 82], 1), 1), 
	Pn(Prand([988, 247], 1), 1), 
	
	Pn(Prand([740, 185], 1), 1), 
	Pn(Prand([554, 139], 1), 1), 
	Pn(Prand([1661, 415, 104], 1), 1), 
	Pn(Prand([1175, 311, 78], 1), 1), 
	Pn(Prand([932, 233], 1), 1), 
	Pn(Prand([699, 175], 1), 1)

	], inf, {rrand(0, 11)}),
	\pan, 0.3,
	\out, {30}
);

p4 = Pbind(
	\instrument, \flies1,
	\dur, 200,
	\amp, 0.2*amp,
	\length, 190,
	\freq, Pseq([
	
	Pn(Prand([523, 131, 65], 1), 1), 
	Pn(Prand([784, 196], 1), 1), 
	Pn(Prand([587, 147], 1), 1), 
	Pn(Prand([440, 110], 1), 1), 
	Pn(Prand([1319, 330, 82], 1), 1), 
	Pn(Prand([988, 247], 1), 1), 
	
	Pn(Prand([740, 185], 1), 1), 
	Pn(Prand([554, 139], 1), 1), 
	Pn(Prand([1661, 415, 104], 1), 1), 
	Pn(Prand([1175, 311, 78], 1), 1), 
	Pn(Prand([932, 233], 1), 1), 
	Pn(Prand([699, 175], 1), 1)
	
	], inf, {rrand(0, 11)}),
	\pan, 0.9,
	\out, {30}
);

// lê os Pbinds em paralelo
patern = Ppar([p1, p2, p3, p4]).play;

580.wait;
patern.stop;

/*
20.wait;
x.free;
*/
}).play; 

};

if(num == 0) 
	{
		
patern.stop;
sineplay.stop;
//controls.close;
sineplay.free;
s.freeAll;

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

//butões
// 3-stop
button3 = Button(controls, Rect(200, 50, 100, 100))
    .canFocus_(false)
    .states_([
	["finish pattern"],
	["resume pattern"]
    ])
    .action_({ arg it;
    var num = it.value;

if(num == 0) 
	{
		
patern.play;

	};
	
if(num == 1) 
	{
		
patern.stop;

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

controls.front;

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

})
)