Questions about scheduling

questions about scheduling

hello all!
the following code works, but I would like to add some features:

(
SynthDef(\player, {
var env, sig;

env = Env.asr(
	0.01, 1.0, 0.01, -4
).ar(2, \gate.kr(1));

sig = PlayBuf.ar(
	1, \bufnum.kr(0), \pbRate.kr(1), loop: 0
);

sig = sig * env;
sig = sig * \amp.kr(1.0);

OffsetOut.ar(\out.kr(0), sig)

}).add;
)

~testPlayer = Synth(\player, [\amp, 1]);

~testPlayer.set(\gate, 0);

(
~bufnum = Pseq([0, Pseq((1…8).scramble, 1), 9], 1).asStream;

~previousPlayer = nil;
~sendSwitchTrigger = {
~previousPlayer !? {|p| p.set(\gate, 0) };
~previousPlayer = Synth(\player, [\bufnum, ~bufnum.next]);
};
)

~sendSwitchTrigger.set(0);

(
~play = Routine{

~bufnum = Pseq([0, Pseq((1…8).scramble, 1), 9], 1).asStream;
10.do{
~sendSwitchTrigger.set(0);
rrand(0.5, 5).yield;
};
};
)

~play.next;

This code derives an electroacoustic installation. There are 10 buffers located, which will be triggered by an on/off switch via MIDI (this code is not included, but it already works).

  1. After all 10 buffers have been played (or a timeout has been exceeded), the Array, that contains the sequence of buffer numbers should be reseted / a new one should be generated: where do I have to put which operation to confirm that?
  2. The „set“ message to start the next buffer should be delayed: I was not able to do this via the yield times in the Routine. Where do I have to put a related method, so that the next buffer starts after a random time after the „set“ message (a. if a buffer is still playing, it should not interrupt i. b. if the last buffer is finished, there should be silence according to the time of the „set“ message + the random delay).
  3. random timeout: = each new „set“ message creates a random timeout - when no „set“ message for the next buffer is set within this limit, the Routine should be reset to its beginn. Where do I have to put which sort of method?

Thanks for any suggestions!

best

Rainer