432 lines
16 KiB
Text
432 lines
16 KiB
Text
|
#lang scribble/manual
|
||
|
@(require (for-label racket))
|
||
|
|
||
|
@title{Pattern Guide Reference 01: Pattern Internals}
|
||
|
Details of pattern implementation, with guidance on writing new pattern classes@section{related}
|
||
|
Tutorials/A-Practical-Guide/PG_Cookbook08_Swing
|
||
|
@section{categories}
|
||
|
Streams-Patterns-Events>A-Practical-Guide
|
||
|
|
||
|
@section{section}
|
||
|
Inner workings of patterns
|
||
|
|
||
|
@section{subsection}
|
||
|
Patterns as streams
|
||
|
|
||
|
As noted, patterns by themselves don't do much. They have to be turned into streams first; then, values are requested from a stream, not from the pattern.
|
||
|
|
||
|
For most patterns, the stream is an instance of link::Classes/Routine::. Routines (formally known in computer science as "coroutines") are important because they can yield control back to the caller but still remember exactly where they were, so they can resume in the middle on the next call without having to start over. A few exceptional patterns use FuncStream, which is simply a wrapper around a function that allows a function to act like a stream by responding to
|
||
|
@racketblock[next:: and other Stream methods.
|
||
|
|
||
|
Every pattern class must respond to ]
|
||
|
|
||
|
@racketblock[asStream::; however, most patterns do not directly implement ]
|
||
|
|
||
|
@racketblock[asStream::. Instead, they use the generic ]
|
||
|
|
||
|
@racketblock[asStream:: implementation from link::Classes/Pattern::.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
asStream { ^Routine({ arg inval; this.embedInStream(inval) }) }
|
||
|
::
|
||
|
|
||
|
This line creates a Routine whose job is simply to embed the pattern into its stream. "Embedding" means for the pattern to do its assigned work, and return control to the parent level when it's finished. When a simple pattern finishes, its parent level is the Routine itself. After ]
|
||
|
|
||
|
@racketblock[embedInStream:: returns, there is nothing else for the Routine to do, so that stream is over; it can only yield nil thereafter.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
p = Pseries(0, 1, 3).asStream; // this will yield exactly 3 values
|
||
|
4.do { p.next.postln }; // 4th value is nil
|
||
|
::
|
||
|
|
||
|
We saw that list patterns can contain other patterns, and that the inner patterns are treated like "subroutines." List patterns do this by calling ]
|
||
|
|
||
|
@racketblock[embedInStream:: on their list items. Most objects are embedded into the stream just by yielding the object:
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
// in Object
|
||
|
embedInStream { ^this.yield; }
|
||
|
::
|
||
|
|
||
|
But if the item is a pattern itself, control enters into the subpattern and stays there until the subpattern ends. Then control goes back to the list pattern to get the next item, which is embedded and so on.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
p = Pseq([Pseries(0, 1, 3), Pgeom(10, 2, 3)], 1).asStream;
|
||
|
|
||
|
p.next; // Pseq is embedded; first item is Pseries(0...), also embedded
|
||
|
// Control is now in the Pseries
|
||
|
p.next; // second item from Pseries
|
||
|
p.next; // third item from Pseries
|
||
|
p.next; // no more Pseries items; control goes back to Pseq
|
||
|
// Pseq gets the next item (Pgeom) and embeds it, yielding 10
|
||
|
p.next; // second item from Pgeom
|
||
|
p.next; // third item from Pgeom
|
||
|
p.next; // no more Pgeom items; Pseq has no more items, so it returns to Routine
|
||
|
// Routine has nothing left to do, so the result is nil
|
||
|
::
|
||
|
|
||
|
To write a new pattern class, then, the bare minimum required is:
|
||
|
|
||
|
]
|
||
|
@section{list}
|
||
|
|
||
|
## strong::Instance variables:: for the pattern's parameters
|
||
|
## A
|
||
|
@racketblock[*new:: method to initialize those variables
|
||
|
## An ]
|
||
|
|
||
|
@racketblock[embedInStream:: method to do the pattern's work
|
||
|
::
|
||
|
|
||
|
One of the simpler pattern definitions in the main library is Prand:
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
Prand : ListPattern {
|
||
|
embedInStream { arg inval;
|
||
|
var item;
|
||
|
|
||
|
repeats.value.do({ arg i;
|
||
|
item = list.at(list.size.rand);
|
||
|
inval = item.embedInStream(inval);
|
||
|
});
|
||
|
^inval;
|
||
|
}
|
||
|
}
|
||
|
::
|
||
|
|
||
|
This definition doesn't show the instance variables or ]
|
||
|
|
||
|
@racketblock[*new:: method. Where are they? They are inherited from the superclass, ListPattern.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
ListPattern : Pattern {
|
||
|
var <>list, <>repeats=1;
|
||
|
|
||
|
*new { arg list, repeats=1;
|
||
|
if (list.size > 0) {
|
||
|
^super.new.list_(list).repeats_(repeats)
|
||
|
}{
|
||
|
Error("ListPattern (" ++ this.name ++ ") requires a non-empty collection; received "
|
||
|
++ list ++ ".").throw;
|
||
|
}
|
||
|
}
|
||
|
// some misc. methods omitted in this document
|
||
|
}
|
||
|
::
|
||
|
|
||
|
Because of this inheritance, Prand simply expresses its behavior as a ]
|
||
|
|
||
|
@racketblock[do:: loop, choosing ]
|
||
|
|
||
|
@racketblock[repeats:: items randomly from the list and embedding them into the stream. When the loop is finished, the method returns the input value (see below).
|
||
|
|
||
|
]
|
||
|
@section{subsection}
|
||
|
Streams' input values (inval, inevent)
|
||
|
|
||
|
Before discussing input values in patterns, let's take a step back and discuss how it works for Routines.
|
||
|
|
||
|
link::Classes/Routine::'s
|
||
|
@racketblock[next:: method takes one argument, which is passed into the stream (Routine). The catch is that the routine doesn't start over from the beginning -- if it did, it would lose its unique advantage of remembering its position and resuming on demand. So it isn't sufficient to receive the argument using the routine function's argument variable.
|
||
|
|
||
|
In reality, when a Routine yields a value, its execution is interrupted after calling ]
|
||
|
|
||
|
@racketblock[yield::, but before ]
|
||
|
|
||
|
@racketblock[yield:: returns. Then, when the Routine is asked for its next value, execution resumes by providing a return value from the ]
|
||
|
|
||
|
@racketblock[yield:: method. (This behavior isn't visible in the SuperCollider code in the class library; ]
|
||
|
|
||
|
@racketblock[yield:: is a primitive in the C++ backend, which is how it's able to do something that is otherwise impossible in the language.)
|
||
|
|
||
|
For a quick example, consider a routine that is supposed to multiply the input value by two. First, the wrong way, assuming that everything is done by the function argument ]
|
||
|
|
||
|
@racketblock[inval::. In reality, the first ]
|
||
|
|
||
|
@racketblock[inval:: to come in is ]
|
||
|
|
||
|
@racketblock[1::. Since nothing in the routine changes the value of ]
|
||
|
|
||
|
@racketblock[inval::, the routine yields the same value each time.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
r = Routine({ |inval|
|
||
|
loop {
|
||
|
yield(inval * 2)
|
||
|
}
|
||
|
});
|
||
|
|
||
|
(1..3).do { |x| r.next(x).postln };
|
||
|
::
|
||
|
|
||
|
If, instead, the routine saves the result of ]
|
||
|
|
||
|
@racketblock[yield:: into the ]
|
||
|
|
||
|
@racketblock[inval:: variable, the routine becomes aware of the successive input values and returns the expected results.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
r = Routine({ |inval|
|
||
|
loop {
|
||
|
// here is where the 2nd, 3rd, 4th etc. input values come in
|
||
|
inval = yield(inval * 2);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
(1..3).do { |x| r.next(x).postln };
|
||
|
::
|
||
|
|
||
|
This convention -- receiving the first input value as an argument, and subsequent input values as a result of a method call -- holds true for the ]
|
||
|
|
||
|
@racketblock[embedInStream:: method in patterns also. The rules are:
|
||
|
|
||
|
]
|
||
|
@section{list}
|
||
|
|
||
|
##
|
||
|
@racketblock[embedInStream:: takes strong::one ]
|
||
|
@section{argument}
|
||
|
, which is the first input value.
|
||
|
## When the pattern needs to yield a value directly, or embed an item into the stream, it receives the next input value as the result of
|
||
|
@racketblock[yield:: or ]
|
||
|
|
||
|
@racketblock[embedInStream:: : ]
|
||
|
|
||
|
@racketblock[inval = output.yield:: or ]
|
||
|
|
||
|
@racketblock[output.embedInStream(inval)::.
|
||
|
## When the pattern exits, it must return the last input value, so that the parent pattern will get the input value as the result of its ]
|
||
|
|
||
|
@racketblock[embedInStream:: call: ]
|
||
|
|
||
|
@racketblock[^inval::.
|
||
|
::
|
||
|
|
||
|
By following these rules, ]
|
||
|
|
||
|
@racketblock[embedInStream:: becomes a near twin of ]
|
||
|
|
||
|
@racketblock[yield::. Both do essentially the same thing: spit values out to the user, and come back with the next input value. The only difference is that yield can return only one object to the ]
|
||
|
|
||
|
@racketblock[next:: caller, while ]
|
||
|
|
||
|
@racketblock[embedInStream:: can yield several in succession.
|
||
|
|
||
|
Take a moment to go back and look at how Prand's ]
|
||
|
|
||
|
@racketblock[embedInStream:: method does it.
|
||
|
|
||
|
]
|
||
|
@section{subsection}
|
||
|
embedInStream vs. asStream + next
|
||
|
|
||
|
If a pattern class needs to use values from another pattern, should it evaluate that pattern using
|
||
|
@racketblock[embedInStream::, or should it make a separate stream ( ]
|
||
|
|
||
|
@racketblock[asStream:: ) and pull values from that stream using ]
|
||
|
|
||
|
@racketblock[next::? Both approaches are used in the class library.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[embedInStream:: turns control over to the subpattern completely. The outer pattern is effectively suspended until the subpattern gives control back. This is the intended behavior of most list patterns, for example. There is no opportunity for the parent to do anything to the value yielded back to the caller.
|
||
|
|
||
|
This pattern demonstrates what it means to give control over to the subpattern. The first pattern in the link::Classes/Pseq:: list is infinite; consequently, the second subpattern will never execute because the infinite pattern never gives control back to Pseq.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
p = Pseq([Pwhite(0, 9, inf), Pwhite(100, 109, inf)], 1).asStream;
|
||
|
p.nextN(20); // no matter how long you do this, it'll never be > 9!
|
||
|
::
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[asStream:: should be used if the parent pattern needs to perform some other operation on the yield value before yielding, or if it needs to keep track of multiple child streams at the same time. For instance, link::Classes/Pdiff:: takes the difference between the current value and last value. Since the subtraction comes between evaluating the child pattern and yielding the difference, the child pattern must be used as a stream.
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
Pdiff : FilterPattern {
|
||
|
embedInStream { arg event;
|
||
|
// here is the stream!
|
||
|
var stream = pattern.asStream;
|
||
|
var next, prev = stream.next(event);
|
||
|
while {
|
||
|
next = stream.next(event);
|
||
|
next.notNil;
|
||
|
}{
|
||
|
// and here is the return value
|
||
|
event = (next - prev).yield;
|
||
|
prev = next;
|
||
|
}
|
||
|
^event
|
||
|
}
|
||
|
}
|
||
|
::
|
||
|
|
||
|
]
|
||
|
@section{subsection}
|
||
|
Writing patterns: other factors
|
||
|
|
||
|
Pattern objects are supposed to be emphasis::stateless::, meaning that the pattern object itself should undergo no changes based on any stream running the pattern. (There are some exceptions, such as link::Classes/Ppatmod::, which exists specifically to perform some modification on a pattern object. But, even this special case makes a separate copy of the pattern to be modified for each stream; the original pattern is insulated from the streams' behavior.) emphasis::Be very careful if you're thinking about breaking this rule::, and before doing so, think about whether there might be another way to accomplish the goal without breaking it.
|
||
|
|
||
|
Because of this rule, emphasis::all variables reflecting the state of a particular stream should be local to the embedInStream @section{method}
|
||
|
. If you look through existing pattern classes for examples, you will see in virtually every case that
|
||
|
@racketblock[embedInStream:: does not alter the instance variables defined in the class. It uses them as parameters, but does not change them. Anything that changes while a stream is being evaluated is a local method variable.
|
||
|
|
||
|
To initialize the pattern's parameters (instance variables), typical practice in the library is to give getter and setter methods to all instance variables, and use the setters in the ]
|
||
|
|
||
|
@racketblock[*new:: method (or, use ]
|
||
|
|
||
|
@racketblock[^super.newCopyArgs(...))::. It's not typical to have an init method populate the instance variables. E.g.,
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
Pn : FilterPattern {
|
||
|
var <>repeats;
|
||
|
*new { arg pattern, repeats=inf;
|
||
|
// setter method used here for repeats
|
||
|
^super.new(pattern).repeats_(repeats)
|
||
|
}
|
||
|
...
|
||
|
}
|
||
|
::
|
||
|
|
||
|
Consider carefully whether a parameter can change in each ]
|
||
|
|
||
|
@racketblock[next:: call. If so, make a stream from that parameter and call ]
|
||
|
|
||
|
@racketblock[.next(inval):: on it for each iteration. Parameters that should not change, such as number of repeats, should call ]
|
||
|
|
||
|
@racketblock[.value(inval):: so that a function may be given. link::Classes/Pwhite:: demonstrates both of these features.
|
||
|
|
||
|
- strong::Exercise for the reader:: : Why does ]
|
||
|
|
||
|
@racketblock[Pwhite(0.0, 1.0, inf):: work, even with the ]
|
||
|
|
||
|
@racketblock[asStream:: and next calls?
|
||
|
|
||
|
]
|
||
|
|
||
|
@racketblock[
|
||
|
Pwhite : Pattern {
|
||
|
var <>lo, <>hi, <>length;
|
||
|
*new { arg lo=0.0, hi=1.0, length=inf;
|
||
|
^super.newCopyArgs(lo, hi, length)
|
||
|
}
|
||
|
storeArgs { ^[lo,hi,length] }
|
||
|
embedInStream { arg inval;
|
||
|
// lo and hi streams
|
||
|
var loStr = lo.asStream;
|
||
|
var hiStr = hi.asStream;
|
||
|
var hiVal, loVal;
|
||
|
// length.value -- functions allowed for length
|
||
|
// e.g., Pwhite could give a random number of values for each embed
|
||
|
length.value.do({
|
||
|
hiVal = hiStr.next(inval);
|
||
|
loVal = loStr.next(inval);
|
||
|
if(hiVal.isNil or: { loVal.isNil }) { ^inval };
|
||
|
inval = rrand(loVal, hiVal).yield;
|
||
|
});
|
||
|
^inval;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// the plot rises b/c the lo and hi values increase on every 'next' value
|
||
|
Pwhite(Pseries(0.0, 0.01, inf), Pseries(0.2, 0.01, inf), inf).asStream.nextN(200).plot;
|
||
|
::
|
||
|
|
||
|
]
|
||
|
@section{subsection}
|
||
|
Cleaning up event pattern resources
|
||
|
|
||
|
Some event patterns create server or other objects that need to be explicitly removed when they come to a stop. This is handled by the link::Classes/EventStreamCleanup:: object. This class stores a set of functions that will run at the pattern's end. It also uses special keys in the current event to communicate cleanup functions upward to parent patterns, and ultimately to the link::Classes/EventStreamPlayer:: that executes the events.
|
||
|
|
||
|
Basic usage involves 4 stages:
|
||
|
|
||
|
|
||
|
@racketblock[
|
||
|
embedInStream { |inval|
|
||
|
var outputEvent;
|
||
|
|
||
|
// #1 - make the EventStreamCleanup instance
|
||
|
var cleanup = EventStreamCleanup.new;
|
||
|
|
||
|
// #2 - make persistent resource, and add cleanup function
|
||
|
// could be some kind of resource other than a Synth
|
||
|
synth = (... make the Synth here...);
|
||
|
cleanup.addFunction(inval, { |flag|
|
||
|
if(flag) {
|
||
|
synth.release
|
||
|
};
|
||
|
});
|
||
|
|
||
|
loop {
|
||
|
outputEvent = (... get output event...);
|
||
|
|
||
|
// #4 - cleanup.exit
|
||
|
if(outputEvent.isNil) { ^cleanup.exit(inval) };
|
||
|
|
||
|
// #3 - update the EventStreamCleanup before yield
|
||
|
cleanup.update(outputEvent);
|
||
|
inval = outputEvent.yield;
|
||
|
}
|
||
|
}
|
||
|
::
|
||
|
|
||
|
]
|
||
|
@section{numberedList}
|
||
|
|
||
|
## The embedInStream method should create its own instance of EventStreamCleanup. (Alternately, it may receive the cleanup object as the second argument, but it should not assume that the cleanup object will be passed in. It should always check for its existence and create the instance if needed. Note that the pattern should also reimplement
|
||
|
@racketblock[asStream:: as shown.) It's much simpler for the pattern just to create its own instance.
|
||
|
## When the pattern creates the objects that will need to be cleaned up, it should also use the ]
|
||
|
|
||
|
@racketblock[addFunction:: method on the EventStreamCleanup with a function that will remove the resource(s). (The example above is deliberately oversimplified. In practice, attention to the timing of server actions is important. Several pattern classes give good examples of how to do this, e.g., link::Classes/Pmono::, link::Classes/Pfx::.)
|
||
|
|
||
|
The flag should be used when removing Synth or Group nodes. Normally the flag is true; but, if the pattern's EventStreamPlayer gets stopped by cmd-., the nodes will already be gone from the server. If your function tries to remove them again, the user will see FAILURE messages from the server and then get confused, thinking that she did something wrong when in fact the error is preventable in the class.
|
||
|
## Before calling ]
|
||
|
|
||
|
@racketblock[.yield:: with the return event, also call ]
|
||
|
|
||
|
@racketblock[cleanup.update(outputEvent)::.
|
||
|
## When ]
|
||
|
|
||
|
@racketblock[embedInStream:: returns control back to the parent, normally this is done with ]
|
||
|
|
||
|
@racketblock[^inval::. When an EventStreamCleanup is involved, it should be ]
|
||
|
|
||
|
@racketblock[^cleanup.exit(inval)::. This executes the cleanup functions and also removes them from EventStreamCleanups at any parent level.
|
||
|
::
|
||
|
|
||
|
]
|
||
|
@section{subsection}
|
||
|
When does a pattern need an EventStreamCleanup?
|
||
|
|
||
|
If the pattern creates something on the server (bus, group, synth, buffer etc.), it must use an EventStreamCleanup as shown to make sure those resources are properly garbage collected.
|
||
|
|
||
|
Or, if there is a chance of the pattern stopping before one or more child patterns has stopped on its own, EventStreamCleanup is important so that the pattern is aware of cleanup actions from the children. For example, in a construction like
|
||
|
@racketblock[Pfindur(10, Pmono(name, pairs...)):: , Pmono may continue for more than 10 beats, in which case Pfindur will cut it off. The Pmono needs to end its synth, but it doesn't know that a pattern higher up in the chain is making it stop. It becomes the parent's responsibility to clean up after the children. As illustrated above, EventStreamCleanup handles this with only minimal intrusion into normal pattern logic.
|
||
|
|
||
|
Previous: link::Tutorials/A-Practical-Guide/PG_Cookbook08_Swing::
|
||
|
]
|
||
|
|
||
|
|