2 summary:: non event stream reference definition
3 categories:: Libraries>JITLib>Patterns
7 Pdefn provides an interface to its superclass link::Classes/PatternProxy::, keeping a reference to a stream that can be replaced while playing. One pattern may be used in many streams in different places. A change in the pattern definition propagates through all streams.
10 Pdefn(key) //returns the instance
11 Pdefn(key, pat) //defines the pattern and returns the instance, like Pdef, Tdef and Ndef.
14 It is very similar to link::Classes/PatternProxy::.
16 Pdefn can be used to store value patterns globally (for strong::event patterns::, see link::Classes/Pdef::). Overview: link::Overviews/JITLib::
18 subsection::First Example
23 Pdefn(\x, Pbrown(0, 6, 0.1, inf));
24 Pbind(\note, Pdefn(\x), \dur, 0.3).play;
25 Pbind(\note, Pdefn(\x), \dur, 0.1, \ctranspose, 15).play;
26 // now change the definition
27 Pdefn(\x, Pseq([0, 3, 5, 7, 9, 11], inf));
28 Pdefn(\x, Pseq([0, 3, 3, 7], inf) + Pseq([0, [0, 3], [0, 5, 7]], inf));
38 Store the pattern in a global dictionary under key, replacing its pattern with the new one. If the pattern is a strong::function::, Pdefn creates a link::Classes/Prout:: with this function, passing in the envir, if given(see below).
40 Using strong::*new(key):: you can access the pattern at that key (if none is given, a default silent event is created)
43 Default source, if none is given. The default is a Pattern that returns 1.0 (This is 1 and not 0 to avoid deadlocks when used as a duration pattern. In a sense, 1 is just as generic as 0).
46 Remove all proxies from the global dictionary ( link::#*all::)
49 Clear all proxies, setting their source to silence.
52 Set or return the environment ( link::Classes/IdentityDictionary:: ) that stores all Pdefns.
60 subsection::Pdefn in expressions
63 Pdefn(\c, Pdefn(\a) + Pdefn(\b));
65 t = Pdefn(\c).asStream; // create a stream from Pdefn(\c)
67 t.value; // default value for a Pdefn is 1, so that it is a good time value default.
69 Pdefn(\a, 100); // (re)define Pdefn(\a) as 100
73 Pdefn(\b, Pseq([1, 2, 3], inf)); // (re)define Pdefn(\b) as Pseq([1, 2, 3], inf)
75 3.do { t.value.postln };
77 Pdefn(\c, Pdefn(\a) * Pdefn(\b) - Pdefn(\a)); // (re)define Pdefn(\c)
79 8.do { t.value.postln };
81 Pdefn(\a, Prand([1, 4, 2], inf)); // (re)define Pdefn(\a)
84 subsection::Embedding Pdefn in other patterns
87 Pdefn(\x, Pseq([1, 2, 3], inf));
89 x = Pseq([0, 0, Pdefn(\x)], inf).asStream;
91 t = Task({ loop({ x.next.postln; 0.3.wait }) }).play;
94 Pdefn(\x, Pseq([55, 66, 77],inf));
95 Pdefn(\x, Pseq([55, 66, 77],1));
101 // Pdefn can be accessed in multiple streams
104 SynthDef("Pdefhelp", { arg out, freq, sustain=1, amp=1, pan;
106 env = EnvGen.kr(Env.perc(0.03, sustain), 1, doneAction:2);
107 5.do { var d; d = exprand(0.01, 1); u = SinOsc.ar(d * 300, u, rrand(0.1,1.2) * d, 1) };
108 Out.ar(out, Pan2.ar(SinOsc.ar(u + 1 * freq, 0, amp * env), pan));
115 Pdefn(\deg, Pseq([0, 3, 2],inf));
117 Pset(\instrument, \Pdefhelp,
119 Pbind(\degree, Pdefn(\deg), \pan, -0.8),
120 Pbind(\degree, Pdefn(\deg), \dur, 1/3, \pan, 0.8)
125 Pdefn(\deg, Prand([0, 3, [1s, 4]],inf));
127 Pdefn(\deg, Pn(Pshuf([0, 3, 2, 7, 6],2),inf));
130 Pdefn(\deg, Plazy { var pat;
131 pat = [Pshuf([0, 3, 2, 7, 6],2), Pseries(0, 1, 11), Pseries(11, -1, 11)].choose;
139 When does the definition change?
141 If quant is set, the update is done at the next beat or whatever is specified:
144 Pdefn(\deg).quant = 4;
145 Pdefn(\deg, Pn(Pseries(0, 1, 8),inf));
147 Pdefn(\deg).quant = nil; // activate immediately again
153 #[1, 3, 4].choose.yield;
154 #[5, 0, 12].choose.yield;
155 #[14, 3, 4].choose.do { |j| (i % j).postln.yield };
162 subsection::update condition
164 In order to be able to switch to a new pattern under a certain condition, the instance variable strong::condition:: can be set to a function that returns a boolean. Value and a count index are passed to the function. The condition is always valid for the strong::next pattern:: inserted. For stuck conditions, the strong::reset:: message can be used.
166 As counting up (such as emphasis::"every nth event, a swap can happen"::) is a common task, there is a method for this, called strong::count(n)::.
169 z = Pbind(\degree, Pdefn(\x, \), \dur, 0.25).play;
170 Pdefn(\x, Pseq((0..5), inf)).condition_({ |val, i| i.postln % 6 == 0 });
171 Pdefn(\x, Pseq((7..0), inf)).condition_({ |val, i| i.postln % 8 == 0 });
174 // the above is equvalent to:
175 Pdefn(\x, Pseq((7..0), inf)).count(8);
181 // reset to change immediately:
187 subsection::Functions as arguments to Pdefn
190 Pdefn(\deg, { loop { yield(0.1.rand.round(0.01) + [2, 3, 9].choose) } });
194 Pdefn(\deg, Proutine { loop { yield(0.1.rand.round(0.01) + [2, 3, 9].choose) } });
196 // this is not exactly true, see below..
199 subsection::The (inner) environment
202 // set() creates a local environment that overrides the outer currentEnvironment
204 Pdefn(\z).set(\a, 1, \b, 5);
207 loop { yield((e.a + e.b) + 0.1.rand.round(0.01)) }
211 t = Pdefn(\z).asStream;
217 //(e.a + e.b) + 0.1.rand.round(0.01) 1
222 Pdefn(\z, Pseq([1, 2, 3], 1));
227 Pdefn(\z).set(\a, 3);
231 Pdefn(\z).set(\a, Pseq([1, 2, 3], inf));
236 Pdefn(\z).envir; // post the envir
240 // if you want to keep using the currentEnvironment at the same time,
241 // assign the currentEnvironment to the envir's parent (or proto) field
242 // (this shouldn't be a proxy space of course.)
244 Pdefn(\z).envir.parent = currentEnvironment;