1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
title:: Pattern Guide 06a: Repetition Constraint Patterns
summary:: Patterns that repeat values, or cut other patterns off early
related:: Tutorials/A-Practical-Guide/PG_060_Filter_Patterns, Tutorials/A-Practical-Guide/PG_06b_Time_Based_Patterns
categories:: Streams-Patterns-Events>A-Practical-Guide
section::Repetition and Constraint patterns
These are essentially flow of control patterns. Each one takes a source pattern and repeats values from it, or stops the stream early based on a preset constraint.
subsection::Repetition patterns
These patterns allow you to repeat single values, or (in the case of Pn) entire patterns.
definitionList::
## code::Pclutch(pattern, connected):: || If the code::connected:: pattern is true, link::Classes/Pclutch:: returns the next value from code::pattern::. If code::connected:: is false, the previous pattern value is repeated. It's like a clutch in a car: when engaged, the pattern moves forward; when disconnected, it stays where it is.
## code::Pn(pattern, repeats):: || Embeds the source pattern code::repeats:: times: simple repetition. This also works on single values: code::Pn(1, 5):: outputs the number 1 5 times.
## code::Pdup(n, pattern):: || code::n:: and code::pattern:: are both patterns. Each value from code::pattern:: is repeated code::n:: times. If code::n:: is a pattern, each value can be repeated a different number of times.
## code::Psubdivide(n, pattern):: || Like Pdup, except the pattern value is divided by the number of repeats (so that the total time for the repeat cycle is the duration value from the source pattern).
See also link::Classes/Pstep::, described in link::Tutorials/A-Practical-Guide/PG_06b_Time_Based_Patterns::. Pstep can be used like link::Classes/Pdup::, but repetition is controlled by time rather than number of repeats per item.
code::
// play repeated notes with a different rhythmic value per new pitch
// using Pdup
p = Pbind(
// making 'n' a separate stream so that degree and dur can share it
\n, Pwhite(3, 10, inf),
\degree, Pdup(Pkey(\n), Pwhite(-4, 11, inf)),
\dur, Pdup(Pkey(\n), Pwhite(0.1, 0.4, inf)),
\legato, 0.3
).play;
p.stop;
// using Pfin / Pn
// Pn loops the Pbind infinitely
// Plazy builds a new Pbind for each iteration
// Pfin cuts off the Pbind when it's time for a new value
p = Pn(
Plazy {
Pbind(
\degree, Pfin(rrand(3, 10), rrand(-4, 11)),
\dur, rrand(0.1, 0.4)
)
},
inf
).play;
p.stop;
// using Pclutch
// the rule is, when degree changes, dur should change also
// if Pdiff returns 0, degree has not changed
// so here, nonzero Pdiff values "connect" the clutch and allow a new dur to be generated
// otherwise the old one is held
p = Pbind(
\degree, Pdup(Pwhite(3, 10, inf), Pwhite(-4, 11, inf)),
\dur, Pclutch(Pwhite(0.1, 0.4, inf), Pdiff(Pkey(\degree)).abs > 0),
\legato, 0.3
).play;
p.stop;
::
::
subsection::Constraint (or interruption) patterns
Instead of prolonging a stream by repetition, these patterns use different methods to stop a stream dynamically. They are especially useful for modularizing pattern construction. One section of your code might be responsible for generating numeric or event patterns. By using constraint patterns, that part of the code doesn't have to know how many events or how long to play. It can just return an infinite pattern, and another part of the code can wrap it in one of these to stop it based on the appropriate condition.
definitionList::
## code::Pfin(count, pattern):: || Returns exactly code::count:: values from the source pattern, then stops. (link::Classes/Pfin:: has a cousin, Pfinval, that is deprecated.)
## code::Pconst(sum, pattern, tolerance):: || Output numbers until the sum goes over a predefined limit. The last output value is adjusted so that the sum matches the limit exactly.
## code::Pfindur(dur, pattern, tolerance):: || Like Pconst, but applying the "constrain" behavior to the event's rhythmic values. The source pattern runs up to the specified duration, then stops. This is very useful if you know how long a musical behavior should go on, but the number of events to fill up that time is not known.
code::
// Two variants on the same thing
// Use Pconst or Pfindur to create 4-beat segments with randomized rhythm
// Pconst and Pfindur both can ensure the total rhythm doesn't go above 4.0
p = Pn(Pbind(
// always a low C on the downbeat
\degree, Pseq([-7, Pwhite(0, 11, inf)], 1),
\dur, Pconst(4, Pwhite(1, 4, inf) * 0.25)
), inf).play;
p.stop;
p = Pn(Pfindur(4, Pbind(
\degree, Pseq([-7, Pwhite(0, 11, inf)], 1),
\dur, Pwhite(1, 4, inf) * 0.25
)), inf).play;
p.stop;
::
## code::Psync(pattern, quant, maxdur, tolerance):: || Like Pfindur, but does not have a fixed duration limit. Instead, it plays until either it reaches code::maxdur:: (in which case it behaves like Pfindur, adjusting the last event so the total duration matches code::maxdur::), or the pattern stops early and the last event is rounded up to the next integer multiple of code::quant::. This is hard to explain; a couple of examples might make it clearer.
code::
(
// in this case, the pattern stops by reaching maxdur
// elapsed time = 4
var startTime;
p = (Psync(Pbind(
\dur, 0.25, // total duration = infinite
\time, Pfunc { startTime = startTime ? (thisThread.clock.beats.debug("time")) }
), 1, 4) ++ Pfuncn({
thisThread.clock.beats.debug("finish time");
(thisThread.clock.beats - startTime).debug("elapsed");
nil
}, 1)).play;
)
(
// in this case, the pattern stops itself before maxdur (4)
// the Pbind's duration (1.25) gets rounded up to 2 (next multiple of 1)
var startTime;
p = (Psync(Pbind(
\dur, Pn(0.25, 5), // total duration = 0.25 * 5 = 1.25
\time, Pfunc { startTime = startTime ? (thisThread.clock.beats.debug("time")) }
), 1, 4) ++ Pfuncn({
thisThread.clock.beats.debug("finish time");
(thisThread.clock.beats - startTime).debug("elapsed");
nil
}, 1)).play;
)
::
::
Previous: link::Tutorials/A-Practical-Guide/PG_060_Filter_Patterns::
Next: link::Tutorials/A-Practical-Guide/PG_06b_Time_Based_Patterns::
|