home/ atoms/ sc-patterns-stream-abstraction

SuperCollider Patterns are lazy descriptions of streams; asStream materialises them one value at a time

In SuperCollider’s Pattern library, a Pattern object like Pseq([0,1,2]) is a reusable template. Calling .asStream on it creates a Stream that produces values one at a time via .next. This separation of description from execution means the same Pattern can drive many independent streams simultaneously. Patterns can be combined arithmetically — (Pseq([0,1,2]) * 10 + Pseq([0,1,2])) creates a new Pattern. Embedding one Pattern inside another creates hierarchical musical structures. The stream ends when .next returns nil; inf as a repeat count loops forever.

Examples

~pat = Pseq((0..2), 3, 1); ~stream = ~pat.asStream; ~stream.nextN(10); // returns: [ 1, 2, 0, 1, 2, 0, 1, 2, 0, nil ]

Assessment

Create a Pseq of 4 notes and derive from it (without re-listing them) a version transposed up by 7 semitones. Run both streams in parallel inside a Pbind. Explain when .next returns nil.

“~pat = Pseq((0..2), 3, 1); // the pattern ~stream = ~pat.asStream; // the stream ~stream.nextN(10); // obtain 1 extra value // ~stream returns: [ 1, 2, 0, 1, 2, 0, 1, 2, 0, nil]”
corpus · the-supercollider-book-official-code-examples-scbookcode-gpl · chunk 145