Pattern Guide 06d: Parallel Patterns:
Tutorials/A-Practical-Guide | Streams-Patterns-Events > A-Practical-Guide

Pattern Guide 06d: Parallel Patterns

Running multiple event patterns simultaneously

Parallelizing event patterns

There are a couple of different ways to have several patterns playing at the same time. The most obvious is to play them separately. The patterns' events get scheduled independently on their clock(s) and run concurrently. None of these patterns need to have any knowledge of the others. One advantage of this approach is that the patterns can be stopped and started independently.

The other is to combine them into a parallel stream. The result is a single pattern object that can be played or stopped only as one unit. Some degree of interactive control is lost, but there are times when merging several patterns is necessary -- for instance, converting a pattern into a Score object for NRT rendering.

Ppar(list, repeats)
Start each of the event patterns in the list at the same time. When the last one finishes, the Ppar also stops. If repeats > 1, all the subpatterns start over again from the beginning.
Ptpar(list, repeats)
Here, the list consists of [timeOffset0, pattern0, timeOffset1, pattern1...] . Each pattern starts after the number of beats given as its time offset. The patterns can start at different times relative to each other.
Pgpar(list, repeats)
Like Ppar, but it creates a separate group for each subpattern.
Pgtpar(list, repeats)
This is like Ptpar with separate groups for the subpatterns.

An excellent example of Ppar and Pseq used together to structure an entire piece (Kraftwerk's "Spacelab") can be found in examples/pieces/spacelab.scd.

Dynamic parallelizing

Ppar and its cousins are good for a fixed set of parallel patterns -- that is, you need to know in advance how many patterns will be parallelized. Once the parallel pattern starts, there is no way to add more streams to it. To keep adding streams, use Pspawner and Pspawn. For the purpose of this overview, some basic features will be illustrated in a couple of simple examples. These classes have more capabilities; refer to their help files for specifics.

The function is run in a Routine. A Spawner object gets passed into this Routine, and this object is used to add or remove streams to/from the parallel stream. New patterns can be added in sequence or in parallel.
Pspawn(pattern, spawnProtoEvent)
Supports most of the features of Pspawner, but uses a pattern to control the Spawner object instead of a Routine function.

This example uses Pspawner to trigger overlapping scale segments at different speeds. Unlike Ppar, which could handle a fixed number before stopping, Pspawner can keep going indefinitely.

The same, written using Pspawn :

Previous: Pattern Guide 06c: Composition of Patterns

Next: Pattern Guide 06e: Language Control