EventStreamCleanup : Object

Helper class that collects information about internal state of streams that needs to be released


Event streams created by objects like Pmono or Pfx are special: when they start, they create some state (like a new synth) that is present over the whole period of the stream, or at least over several events. When such a stream ends, it releases this state. There are other streams, however, that may stop their input stream at any time (e.g. Pfindur or Pdef). Them the state of any stream buried in the hierarchy of input streams must be released by them. EventStreamCleanup collects the cleanup functions and can run them when the stream is cut.

NOTE: So all event patterns that can end a subpattern (and streams respectively) have to use an EventStreamCleanup.

Some examples of patterns that may stop an input stream and update an EventStreamCleanup:

Some examples of patterns that create state that lasts over several events. They also release the state via EventStreamCleanup:

// wrap a pattern in a stop condition
f = { |pat, condition|
    Prout { |inval|
        var stream = pat.asStream;
        var cleanup =;
        var outval;
        while {
            outval =;
            outval.notNil and: { condition.value(outval) }
        } {
            inval = outval.yield;

p = Plazy { Pmono(\default, \note, Pgeom(rrand(1, 1.5), rrand(1.05, 2), inf), \harmonic, [0.78, 1, 1.2], \dur, 1/rrand(4, 7), \amp, 0.4) };
x = f.(p, { |outval| outval[\note] < 20 }); // always stop at 20
Pn(x).play; // loop it.

Class Methods

Create a new instance.

Inherited class methods

Instance Methods

.addFunction(event, function)

Add a new cleanup function which will be called when the stream is made to end somewhere downstreams. This is called only in patterns that create resources that need to be released (e.g. Pmono or Pfx).



The outevent that is passed on downstreams and which communicates to any stream-ending pattern what needs to be done to release the resources. It must be yielded after update!


The function that is called for cleanup. E.g. { }.


For every new event, the cleanup must be updated to receive information from any input stream further up. This method is called from all streams that may stop early (e.g. Pmono or Pfindur).



The outevent from the input stream. It must be yielded after update!

.exit(event, freeNodes: true)

Run all functions that have been collected over time, adding appropriate information to the event, in case it is passed on as an inevent.



The inevent that is passed through to the outer stream


Used internally.


An event. In embedInStream, this event must be returned (^cleanup.exit(inevent))


.functions = value

A collections of cleanup functions.


Empty the cleanup functions, without evaluating them.

.terminate(freeNodes: true)

Run all functions that have been collected over time without adding information to an event.



Used internally.

Inherited instance methods

Undocumented instance methods

.addNodeCleanup(event, function)


// some code from the class library

// here is a pattern that can end the stream externally after a number of steps
Pfin : FilterPattern {
    var <>count;
    *new { arg count, pattern;
    storeArgs { ^[count,pattern] }

    embedInStream { arg event;
        var inevent;
        var stream = pattern.asStream;
        var cleanup =;
            inevent = ?? { ^event };
            event = inevent.yield;

// and here is a pattern that adds a resource: a bus in which the events play

Pbus : FilterPattern {
    var <>numChannels, <>rate, <>dur=2.0, <>fadeTime;

    *new { arg pattern, dur=2.0, fadeTime=0.02, numChannels=2, rate=\audio;

    storeArgs { ^[ pattern, dur, fadeTime, numChannels, rate ] }

    embedInStream { arg inevent;
        var server, groupID, linkID, bus, ingroup, cleanup;
        var patterns, event, freeBus, stream, cleanupEvent;

        cleanup =;
        server = inevent[\server] ?? { Server.default };
        groupID = server.nextNodeID;
        linkID = server.nextNodeID;
        ingroup = inevent[\group];

        // could use a special event type for this:
        if(rate == \audio) {
            bus = server.audioBusAllocator.alloc(numChannels);
            freeBus = { };
        } {
            bus = server.controlBusAllocator.alloc(numChannels);
            freeBus = { };


        event = inevent.copy;
        event[\addAction] = 0; // \addToHead
        event[\type] = \group;
        event[\delta] = 0;
        event[\id] = groupID;
        event[\group] = ingroup;

        inevent = event = inevent.copy;

        event[\type] = \on;
        event[\group] = groupID;
        event[\addAction] = 3; // \addBefore
        event[\delta] = 0;
        event[\id] = linkID;
        event[\fadeTime] = fadeTime;
        event[\instrument] = format("system_link_%_%", rate, numChannels);
        event[\in] = bus;
        event[\msgFunc] = #{ |out, in, fadeTime, gate=1|
            [\out, out, \in, in, \fadeTime, fadeTime, \gate, gate, \doneAction, 3]

        cleanupEvent = (type: \off, parent: event, fadeTime: fadeTime.abs, hasGate: true, gate: 0);

        cleanup.addFunction(event, { | flag |
            if(flag) { defer ( {}, dur) };

        cleanup.addFunction(event, { defer({ freeBus.value;}, fadeTime.abs + dur) });

        // doneAction = 3;
        // remove and deallocate both this synth and the preceding node
        // (which is the group).
        inevent = event.yield;

        // now embed the pattern
        stream = Pchain(pattern, (group: groupID, out: bus)).asStream;
        loop {
            event = ?? { ^cleanup.exit(inevent) };
            inevent = event.yield;