This is a common request - good advice from @jamshark70.
It’s probably easier to experiment with this than one might expect… The core function in EventStreamPlayer
, where new Event
s are pulled and the ESP is rescheduled, is prNext
. The implementation is pretty readable… in particular, you can see where the next time is calculated, or where we bail out if we have a nil duration:
nextTime = outEvent.playAndDelta(cleanup, muteCount > 0);
if (nextTime.isNil) { this.removedFromScheduler; ^nil };
nextBeat = inTime + nextTime; // inval is current logical beat
^nextTime
Observe that, in general, Routine
's can easily be restarted after a nil
is returned:
r = Routine({
[1, 1, nil, 2, 2].do {
|i|
i.postln;
i.yield
}
});
r.play; // pauses after nil
r.play; // run this to begin playback again
If you want to experiment with custom scheduling / playback, just subclass EventStreamPlayer
and override prNext
to provide your own behavior. For example, you might try to allow events to use a Condition
as \delta
value, which could look something like this:
// Assuming we return an Event like: (delta: ~resume = Condition(false))
// We could then resume by doing ~resume.unhang() elsewhere.
prNext { arg inTime;
var nextTime;
var outEvent = stream.next(event.copy);
if (outEvent.isNil) {
streamHasEnded = stream.notNil;
cleanup.clear;
this.removedFromScheduler;
^nil
}{
nextTime = outEvent.playAndDelta(cleanup, muteCount > 0);
if (nextTime.isKindOf(Condition)) {
fork {
nextTime.wait(); // wait for condition
nextTime = thisThread.beats; // reset time
routine.play(); // and resume our routine....
};
^nil // This will stop our routine from resuming on the clock
} {
if (nextTime.isNil) { this.removedFromScheduler; ^nil };
nextBeat = inTime + nextTime; // inval is current logical beat
^nextTime
}
};
}
Of course, you can detect anything you want in your outEvent
in order to do the “pause” behavior - you might have a special key, etc. I haven’t tested this, but I hope it’s a place to start for anyone that wants to experiment - please post if you have any interesting success (or failure)!