Time-based patterns


"Time-based patterns" here are value patterns that use time as part of their calculation. Event patterns are naturally time-driven when played on a clock. (Technically it's possible to request events from an event stream without running it in an EventStreamPlayer, but this is not typical usage.)


Most of these patterns work by remembering the clock's current time at the moment the pattern is embedded into a value stream. The time value used for calculation is, then, the clock's time at the moment of evaluation minus the starting time -- that is, the number of beats elapsed since the patterns started embedding. If the pattern is embedded several times, the starting time is also reset so that the pattern begins again from the beginning.


There is nothing to prevent using these patterns outside of a scheduling context. In these documents, that context would be an event pattern played on a clock, but streams made from these patterns can be used in scheduled routines or functions as well. Only a scheduling context can ensure precise timing of requests for values.



This Pif pattern uses Ptime to get values from the true branch for exactly 4 beats after the first value is requested. After that, the condition will be false and Pif reverts to the false branch, which is nil. That causes the stream to stop. (This is like Pfindur for event patterns, but Pif/Ptime works for value patterns as well.)


// This is a really useful trick: like Pfindur but for value patterns

(

p = Pbind(

\degree, Pif(Ptime(inf) < 4.0, Pwhite(-4, 11, inf)),

\dur, 0.25

).play;

)



// curve is 5 - here's what the curve looks like, ascending first then descending

Env(#[0, 1, 0], #[1, 1], 5).plot;


(

p = Pbind(

// using \note b/c Pseg will give fractional note numbers

// can't use \degree because it handles non-integers differently

\note, Pseg(

Pwhite(-7, 19, inf), // chromatic note numbers

// alternate version for diatonic numbers

// PdegreeToKey does the same conversion as \degree --> \note

// PdegreeToKey(Pwhite(-4, 11, inf), Pkey(\scale), 12),

Pwhite(1, 4, inf) * 0.5,

5, inf),

\dur, 0.125

).play;

)


p.stop;



Using envelopes as patterns


Env supports the stream protocol: 'asStream' turns an Env into a stream, and timed values can be obtained from it using 'next'. The envelope stream returns the value the envelope would have at the elapsed time, in the same way .at(time) returns the envelope value at the specified time.


e = Env.linen(1, 1, 1);

e.at(2); // == 1

e.at(2.5); // == 0.5


// print envelope values

r = fork {

var stream = e.asStream;

12.do({

stream.next.postln;

0.25.wait;

});

};


// Use an envelope to pan notes from left to right and back

p = Pbind(

\degree, Pwhite(-4, 11, 32),

\pan, Env(#[-1, 1, -1], #[2, 2], \sin),

\dur, 0.125

).play;


p.stop;


The releaseNode and loopNode envelope parameters do not take effect, because they are meaningful only when used in a Synth with a gated EnvGen.


When the envelope ends, the stream will hold the final level indefinitely. The Pif(Ptime(inf) < totalTime, Env(...)) trick can make it stop instead.


// Use an envelope to pan notes from left to right and back

// Plays one cycle

(

p = Pbind(

// change to inf: we don't need to know exactly how many events are needed

\degree, Pwhite(-4, 11, inf),

\pan, Pif(Ptime(inf) <= 4.0, Env(#[-1, 1, -1], #[2, 2], \sin)),

\dur, 0.125

).play;

)


p.stop;


// To keep looping the envelope, wrap Pif inside Pn

(

p = Pbind(

\degree, Pwhite(-4, 11, inf),

\pan, Pn(Pif(Ptime(inf) <= 4.0, Env(#[-1, 1, -1], #[2, 2], \sin)), inf),

\dur, 0.125

).play;

)


p.stop;



Previous: PG_06a_Repetition_Contraint_Patterns

Next: PG_06c_Composition_of_Patterns