(
Server.default = s = Server.internal;

s.waitForBoot({

var 

button1, button2, controls, fliesbuf, pflies;

// synth para buffer
SynthDef(\timefly, {|pan= 0, amp= 0.5, rate= 1, atk= 0.01, rel= 0, gate= 1, curve= -4, bufnum, off= 0|
	var env= EnvGen.ar(Env.asr(atk, amp, rel, curve), gate, doneAction:2);
	var src= PlayBuf.ar(1, bufnum, BufRateScale.kr(bufnum)*rate, 1, off*BufFrames.ir(bufnum));
	Out.ar(34, Pan2.ar(src*env, pan));
}).add;

fliesbuf = Buffer.read(s, "/Users/tiagocutileiro/Music/em trabalho/1 - esbocos/opera 2/tese/multimedia elements /3. Electronic Patches/spoken text.wav");

s.sync;

// lê o texto
pflies = Pbind(
	\instrument, \timefly,
	#[\off, \dur], Pseq([ [1, 25], [0, 6], Pn(Pshuf([ [0.035, 3], [0.052, 2.5], [0.065, 3.4], [0.085, 2.3], [0.098, 3.4], [0.117, 2], [0.127, 3.2], [0.145, 2], [0.153, 3.3], [0.17, 3.2], [0.189, 2], [0.2, 2], [0.208, 3], [0.22, 3.3], [0.238, 3], [0.254, 2.5], [0.267, 5], [0.3, 2.1], [0.315, 2.5], [0.33, 2.55], [0.345, 1.8], [0.355, 1.5], [0.364, 2.1], [0.375, 3.9], [0.399, 1], [0.404, 1.8], [0.416, 1.8], [0.425, 2.9], [0.442, 2.36], [0.457, 1.2], [0.464, 1.5], [0.473, 3], [0.49, 3], [0.505, 4.9], [0.534, 1.5], [0.543, 1.2], [0.55, 1.2], [0.558, 1.2], [0.565, 2.5], [0.576, 2.45], [0.59, 1.5], [0.598, 3], [0.615, 3], [0.63, 1.5], [0.64, 2], [0.655, 2], [0.669, 2], [0.675, 2], [0.69, 2], [0.7, 1.9], [0.712, 1.2], [0.719, 2.3], [0.733, 2], [0.745, 3], [0.763, 1.8], [0.772, 2.2], [0.784, 0.8], [0.79, 2.1], [0.802, 0.7], [0.807, 3.8], [0.829, 2.35], [0.842, 1.75], [0.852, 1], [0.858, 1.7], [0.867, 4], [0.887, 1.5], [0.897, 4], [0.918, 3], [0.94, 3] ], 1), 3), [0.96, 6], [1, 25] ], 1).trace,
	\amp, 0.7,
	\bufnum, fliesbuf,
	\pan, Pseg( Pseq([-0.85, 0.85], inf), Pseq([20, 20], inf), \linear) // pan R/L em 20''
).play.stop;

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

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

//butões
// 1-play
button1 = Button(controls, Rect(50, 50, 100, 100))
    .canFocus_(false)
    .states_([
	["play: p", Color.black, Color.grey],
	["pause: P", Color.black, Color.red],
    ])
    .action_({ arg it;
    var num = it.value;

    if(num == 1) 
	{

pflies.play

	};
    if(num == 0) 
	{
		
pflies.pause;

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

// 2-stop reset
button2 = Button(controls, Rect(200, 50, 100, 100))
    .canFocus_(false)
    .states_([
	["stop: S", Color.black, Color.grey],
	["stop: S", Color.black, Color.red],

    ])
    .action_({ arg it;
    var num = it.value;

    if(num == 1) 
	{
		
pflies.stop;
pflies.reset;
button2.value_(0);
button1.value_(0)

	};
    if(num == 0) 
	{

	};

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

//teclado
controls.view.keyDownAction_( { |view, char, mod, uni, key|
	if(char == $P, { button1.valueAction_(0) }, { nil });
	if(char == $p, { button1.valueAction_(1) }, { nil });
	if(char == $S, { button2.valueAction_(1) }, { nil });
});

controls.front;

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

p.play