Loop mesurated with granular and delays

Good morning everyone, I’m working on this script, the aim of which is to have an input signal which is processed through granularity and delays and above all to be put in a “measured” loop, i.e. with a beginning and an end ( in sec) pre-established.

The grain and delays part should be complete or in any case it doesn’t cause me too many problems, the real issue is instead the loop, as I would like it to be measured and I would then like to modify in real time what is put in the loop, precisely with grain and delays. Thank you, I hope this is possible and I hope I have been as clear as possible.


ServerOptions.devices;
Server.killAll;
s.options.sampleRate_(48000);

(
//s.options.inDevice_("Microfono (US-16x08)");
//s.options.outDevice_("Cuffie (Realtek High Definition");
)

s.plotTree;
s.boot;
s.scope;
s.meter;

(
// -----------------------------> Synth input

SynthDef.new(\mic, {arg inBus=0,outBus=0,amp=0;
	                var sig;
	                    sig = SoundIn.ar(inBus) * amp.lag(0.02);
	                    sig = LeakDC.ar(sig);
	                Out.ar(outBus,sig)
             }).add;

// -------------------> Synth router

SynthDef(\router, {arg inBus=0, outBus=0, fadeT = 1;
		           var sig, init, gate;
		               sig    = In.ar(inBus,1);
		               init   = Impulse.kr(0);
		               outBus = K2A.ar(outBus);
			           gate   = HPZ1.ar(outBus).abs > 0;

	               Out.ar(outBus,
			              sig * EnvGen.ar(Env([0,0,1], [0,fadeT],\cub),gate+init));
			       Out.ar(DelayN.ar(outBus, fadeT, fadeT),
			              sig * EnvGen.ar(Env([0,1,0], [0,fadeT],\cub),gate));
	       }).add;
)


(
~micBus.free;
~delBus.free;

~micBus = Bus.audio(s,1); // Bus Microfono
~delBus = Bus.audio(s,1); // Bus Delay

~micGrp = Group.new;            // Gruppo sources
~fxGrp  = Group.after(~micGrp); // Gruppo elaborazioni

a = Synth(\mic,    [\inBus,0,       \outBus,~micBus,\amp,1],       ~micGrp); // Sig in
b = Synth(\router, [\inBus,~micBus, \outBus,~delBus, \fadeT, 0.5], ~micGrp, \addToTail);

)

// GRAIN

(
SynthDef(\glive_1,{arg outBus=0, inBus=0,
	                   gden=2, gdur=0.2, amp=0, pan=0, envbuf= -1,
	                   fadein=0.2,fadeout=1,gate=0;
	                var trig, sig, env;

	                    trig = Impulse.ar(gden);
	                    sig  = SoundIn.ar(inBus, 1);   // Segnale in ingresso
	                    sig  = GrainIn.ar(2,
		                                    trig,
		                                    gdur,
		                                    sig ! 2,   // Doppio mono
		                                    pan,
		                                    envbuf,512,amp);
	                    env = Linen.kr(gate,fadein,1,fadeout,doneAction:2);
	                Out.ar(outBus,sig * env)
          }).add
)

~grain = Synth(\glive_1,[\inBus,0,\outBus,0,\gden,10,\gdur,0.1,\amp,1,\gate,1]);
~grain.free;


// DELAY

(
SynthDef(\del_1, {arg inBus=0, outBus=0, maxdel=10, del=0.5, fbk=0.0, mix=0,
	                  amp=0, gate=0, fade=0.2, pan=0, done=2;
	              var in, local, sig, dly, pann, env;
	                  in    = In.ar(inBus);    // Ingresso esterno mono
	                  local = LocalIn.ar(1);   // Ingresso interno mono (per feedback)
	                  sig   = in + local;
	                  dly   = DelayL.ar(sig, maxdel, del);
	                  dly   = LeakDC.ar(dly);
	              LocalOut.ar(dly * fbk);      // Feedback sig
	                  env   = Linen.kr(gate,fade,1,fade,done);
	                  sig   = XFade2.ar(dly, sig, mix) * env * amp; // mix -1 = solo dly,
	                                                                // 0 = entrambi,
	                                                                // 1 = solo suono diretto
	                                                                // valori intermedi = rapporto
	                  pann  = Pan2.ar(sig, pan);
	             Out.ar(outBus, pann);
                 }).add;
)

~delay = Synth(\del_1,[\inBus,~delBus,\outBus,0,\amp,1,\fade,0.5, \gate,1],~fxGrp);    // Delay
~delay.free;

//LOOP
........................