2 summary:: stream reference definition
3 categories:: Libraries>JITLib>Patterns
4 related:: Classes/Pdefn, Classes/PdefGui
7 Pdef is a class that provides an interface to its superclass link::Classes/EventPatternProxy::, 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.
9 Pdef and Pdefn use separate global collections.
12 Pdef(key) //returns the instance
13 Pdef(key, pat) //stores the pattern and returns the instance, like Tdef and Ndef.
16 It can be used to store event Patterns globally. Changes in this global library have immediate effect.
18 For strong::non-event patterns:: link::Classes/Pdefn:: is used instead. For another use of Pdef see also link::Tutorials/JITLib/recursive_phrasing::. Graphical overview over all current Pdefs: link::Classes/PdefAllGui::. Overview: link::Overviews/JITLib::.
20 subsection::First Example
25 Pdef(\x, Pbind(\note, Pbrown(0, 6, 0.1, inf)));
26 Pdef(\x).quant = 0; // no waiting.
27 Pbindf(Pdef(\x), \dur, 0.03).play;
28 Pbindf(Pdef(\x), \dur, 0.1, \ctranspose, 15).play;
29 Pbindf(Pdef(\x), \dur, 0.3, \ctranspose, 2).play;
30 // now change the definition
31 Pdef(\x, Pbind(\note, Pseq([0, 3, 5, 7, 9, 11], inf)));
32 Pdef(\x, Pbind(\freq, Pseq([1000, 1923, 245.2, 1718] / 1.2 + 0.1, inf)));
42 Store the pattern in a global dictionary under key, replacing its pattern with the new one. If the pattern is a strong::function::, Pdef creates a link::Classes/PlazyEnvir:: internall that dynamically creates the pattern returned from the function, applying the arguments from the inevent.
44 Using strong::*new(key):: you can access the pattern at that key (if none is given, a default silent event is created)
47 Default source, if none is given. The default is an Event.silent of 1.0 beat duration.
50 Remove all proxies from the global dictionary ( link::#*all:: )
53 Clear all proxies, setting their source to silence.
56 Set or return the environment ( link::Classes/IdentityDictionary:: ) that stores all Pdefs.
59 Set the default quantisation for new instances (default: 1.0). This can be an array [quant, phase, timingOffset, outset]
63 subsection::Changing the definition / setting the source
65 One pattern may have many streams in different places. A change in the pattern definition Pdef propagates through all streams. The change does not have to be immediate - there is a scheme to schedule when the change becomes effective: a strong::quant:: and strong::clock:: (like elsewhere) and a strong::condition::.
68 Set the quantisation time for beat accurate scheduling.
71 can be an array strong::[quant, phase, timingOffset, outset] ::, or just strong::[quant, phase]:: etc.
74 Provide a condition under which the pattern is switched when a new one is inserted. The stream value and a count value is passed into the function.
77 Create and update condition that simply counts up to n and switches the pattern then
80 Switch the pattern immediately (stuck conditions can be subverted by this).
83 When the synthdefs that are used contain an code::\amp:: control, the patterns are replaced by crossfading the previous with the new over this time (in beats)
86 Set the event for the Pdef. It is used to filter the incoming stream before it is passed to the source pattern. This is similar to link::Classes/NodeProxy#-nodeMap::. When set for the first time, the pattern is rebuilt.
89 Set arguments in the default event. If there is none, it is created and the pattern is rebuilt.
92 Map Pdefn to the keys in the event.
98 Returns a link::Classes/Prout:: that plays the proxy endlessly, replacing strong::nil:: with a strong::default:: value (silent event). This allows to create streams that idle on until a new pattern is inserted.
100 subsection::Pdef as stream reference
102 A single Pdef may serve as a definition for multiple streams. These methods show how to fork off separate streams from one instance. Even if they run in different contexts, their definition may still be changed.
105 Play an independent stream in parallel.
108 can be an array of [quant, phase, offset], or a link::Classes/Quant:: value.
111 Pass a value (typically an link::Classes/Event::) into the pattern inval, and embed the Pdef in the stream.
114 just like any pattern, embeds itself in stream
116 subsection::Pdef as EventStreamPlayer
118 For live coding, each Pdef also may control one instance that plays one stream off it. This is an link::Classes/EventStreamPlayer::, accessible in the instance variable link::#-player::.
121 Starts the Pdef and creates a player.
124 can be an array of [quant, phase, offset] or a link::Classes/Quant:: value.
130 Return the current player (if the Pdef is simply used in other streams this is nil)
132 method::pause, resume, reset
133 Perform this method on the player.
136 Returns true if player is running. If a Pdef is playing and its stream ends, it will schedule a stream for playing strong::as soon as a new one is assigned to it::. If it is stopped by strong::stop::, it won't.
140 subsection::Pdef as stream reference
144 SynthDef("Pdefhelp", { arg out, freq, sustain=1, amp=1, pan;
146 env = EnvGen.kr(Env.perc(0.03, sustain), 1, doneAction:2);
147 3.do { var d; d = exprand(0.01, 1); u = SinOsc.ar(d * 300, u, rrand(0.1,1.2) * d, 1) };
148 Out.ar(out, Pan2.ar(SinOsc.ar(u + 1 * freq, 0, amp * env), pan));
154 Pdef(\metronom, Pbind(\instrument, \Pdefhelp, \dur, 1, \degree, 16, \legato, 0.1)).play;
156 x = Pseq([Pdef(\a), Pdef(\b), Pdef(\c)], inf).play;
158 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.25, \degree, Pseq(#[0, 5, 4, 3])));
159 Pdef(\b, Pbind(\instrument, \Pdefhelp, \dur, 0.125, \degree, Pseq(#[7, 8, 7, 8])));
160 Pdef(\c, Pbind(\instrument, \Pdefhelp, \dur, 0.25, \degree, Pseq(#[0, 1, 2], 2)));
165 Pdef(\c, Pbind(\instrument, \Pdefhelp, \dur, 0.25, \degree, Pseq(#[4, 3, 1, 2]*3)));
168 // infinite loops are scheduled (to ths clock's next beat by default) and released:
170 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.753, \degree, Pseq(#[0, 5, 4, 3, 2], inf)));
171 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.125, \degree, Pseq(#[0, 5, 4, 3] + 1, 1)));
172 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.25, \degree, Pseq(#[0, 5, 4, 3] - 4, 1)));
174 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.125, \degree, Pseq(#[0, 5] - 1, 1)));
175 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.753, \degree, Pshuf(#[0, 5, 4, 3, 2], inf)));
178 Pdef(\metronom).stop;
180 // Pdef can be used in multiple patterns:
184 Pbindf(Pn(Pdef(\a), inf),
185 \gtranspose, Pstutter(8, Pseq(#[0, 2, 0, 3],inf))
187 Pbindf(Pn(Pdef(\a), inf),
188 \gtranspose, Pstutter(8, Pseq(#[7, 4, 0, 3],inf)),
191 Pbindf(Pn(Pdef(\a), inf),
192 \degree, Pseq(#[0, 5, 4, 3, 2, 3, 2], 1)
197 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.1, \degree, Pseq(#[0, 1, 0, 1, 2], inf)));
199 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.2, \degree, Pseq([0, 4], inf)));
201 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 1, \degree, Pseq([0, 4], inf)));
203 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.2, \degree, Pseq([0, 4, Prand([6, 8b],2)], inf)));
205 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.1, \degree, Pseq(#[0, 1b, 1, 2b, 2, 3, 4b, 4, 5], inf)));
207 // using a fade time, the above changes are crossfaded
208 Pdef(\a).fadeTime = 2;
210 Pdef(\a, Pbind(\instrument, \Pdefhelp, \dur, 0.2, \degree, Pseq([0, 4, Prand([6, 8b],2)], inf)));
214 Pdef(\a).set(\detune, -50); // set environment
215 Pdef(\a).set(\detune, 0);
220 subsection::Pdef as EventStreamPlayer
227 { arg out=0, freq=440, sustain=0.05, amp=0.1, pan;
229 env = EnvGen.kr(Env.perc(0.01, sustain), doneAction:2) * amp;
230 Out.ar(out, Pan2.ar(SinOsc.ar(freq, 0, env), pan))
235 Pdef(\x); // creates a Pdef with a default pattern.
238 Pdef(\x).play; // play it. A silent resting pattern is used.
239 Pdef(\y).play; // play a second one (automatically instantiated)
242 // assign various patterns to it:
244 Pdef(\x, Pbind(\dur, 0.25, \instrument, \gpdef));
245 Pdef(\x, Pbind(\dur, 0.25, \degree, Pseq([3, 4, 5b, 6], inf), \instrument, \gpdef));
246 Pdef(\x, Pbind(\dur, 0.25, \degree, Pseq([3, 4, 5b, 6]+1, inf), \instrument, \gpdef));
247 Pdef(\y, Pbind(\dur, 0.25, \degree, Pseq([3, 4, 5b, 6]-1, inf), \instrument, \gpdef));
248 Pdef(\y, Pbind(\dur, 0.25, \degree, Pseq([3, 4, 5b]-2, inf), \instrument, \gpdef));
252 Pdef(\y).fadeTime = 8.0;
253 Pdef(\y, Pbind(\dur, 0.125, \degree, Pseq([3, 4, 5b, 6]+4.rand, inf), \instrument, \gpdef));
254 Pdef(\y, Pbind(\dur, 0.25, \degree, Pseq([3, 4, 5b, 6]-2, inf), \instrument, \gpdef));
259 \degree, Pseq([3, 4, Prand([8, 2, 3, 9, 10],1) - 5, 6]+1, inf),
267 \degree, Pseq([3, 4, Prand([8, 2, 3, 9, 10],1), 6], inf),
276 TempoClock.default.tempo = 1.3;
277 Pdef(\y, Pbind(\dur, 0.25, \degree, Pseq([3, 4, 5, 6]+1, inf), \instrument, \gpdef));
279 // drop in ending patterns
281 Pdef(\x, Pbind(\dur, 0.25, \degree, Pseq([3, [7,4], 5, 6]-2), \instrument, \gpdef));
282 Pdef(\x, Pbind(\dur, 0.125, \degree, Pseq([3, [7,4], 5, 4]-3), \instrument, \gpdef));
283 Pdef(\x, Pbind(\dur, 0.35, \degree, Pseq([3, [7,4], 5, 4, 3]-3), \instrument, \gpdef));
284 Pdef(\x, Pbind(\dur, 0.25, \degree, Pshuf([3, [7,4], 5, 6]-2), \instrument, \gpdef));
290 TempoClock.default.tempo = 1.0;
293 // GUI example: see also
297 subsection::Recursion
299 Pdefs can be used recursively under the condition that the stream call structure allows it. a structure like the following works:
302 Pdef(\x, Pseq([Pbind(\instrument, \gpdef), Pdef(\x)], inf));
306 but the following would crash, because code::.embedInStream:: is called recursively with no limit:
309 // Pdef(\y, Pseq([Pdef(\y), Pbind(\instrument, \gpdef)], inf));
312 subsection::Quantizing and outset
314 When quantizing to a larger number of beats, the changes become very slow if one has to wait for the next beat. Providing an strong::outset:: quant value is a way to make the change so that it appears as if it had been done at the previous grid point already. The stream is fast forwarded to the current position relative to the quant grid. Providing a number larger than zero, the next possible quant point is used as outset.
316 For example, if quant is 32, and one has just missed the first beat when changing the pattern,
317 one has to wait for 32 beats until the change happens. Using an outset of 1, it is assumed that you had already
318 changed the pattern at the first beat, the stream is fast forwarded to the time it would be at now if you had done so. The new pattern is inserted at the next beat (outset=1).
320 quant can be: strong::[quant, phase, timingOffset, outset]::
323 This fast forwarding might create a cpu peak if the pattern is very complex/fast or quant is very long. This is hard to avoid, so it simply has to be taken into account.
328 Pdef(\x).quant_([8, 0, 0, 1]);
329 Pdef(\y).quant_([8, 0.5, 0, 1]); // phase: half a beat
334 Pdef(\x, Pbind(\degree, Pseq((0..7)+2, inf)));
335 Pdef(\y, Pbind(\degree, Pseq((0..7)-2, inf)));
336 Pdef(\x, Pbind(\degree, Pseq((0..7)+2, inf), \dur, 0.5));
337 Pdef(\y, Pbind(\degree, Pseq((0..7).scramble-2, inf), \dur, 0.25, \legato, 0.3));
338 Pdef(\x, Pbind(\degree, Pseq((0..7), inf)));
340 Pdef(\x, Pbind(\degree, Pseq([ 1, 5, 6, 7, 0, 3, 2, 4 ], inf), \dur, 1));
341 Pdef(\x, Pbind(\degree, Pseq([ 0, 2, 2, 4, 0, 4, 0, 4 ], inf), \dur, 1));
343 Pdef(\x).quant_([8, 1/3, 0, 1]); // phase: 1/6 beat relative to y
344 Pdef(\x, Pbind(\degree, Pseq([ 1, 1, 1, 7, 0, 2, 2, 4 ], inf), \legato, 0.1));
345 Pdef(\x, Pbind(\degree, Pseq([ 3, 3, 3, 4b ], inf), \legato, 0.1));
346 Pdef(\y, Pbind(\degree, Pseq((0..7).scramble-4, inf), \dur, 0.25, \legato, 0.3));
353 var quant = #[8, 0, 0, 1]; // quantise to 8 beats, no phase, insert quant to 1 beat
354 Pdef(\x).quant_(quant);
356 Routine { loop { 8.do { |i| ("uhr:"+i).postln; 1.wait } } }.play(quant:quant);
357 Pbind(\degree, Pseq((0..7), inf)).play(quant:quant);
360 Pdef(\x, Pbind(\degree, Pseq((0..7)+2, inf)).trace(\degree));
361 Pdef(\x, Pbind(\degree, Pseq((0..7), inf) + [0, 3]).trace(\degree));
362 Pdef(\x, Pbind(\degree, Pseq((0..7), inf) + [0, 6], \dur, 0.5).trace(\degree));
365 Pdef(\x).fadeTime = 8;
367 Pdef(\x, Pbind(\degree, Pseq((0..7), inf)).trace(\degree));
368 Pdef(\x, Pbind(\degree, Pseq((0..7).reverse, inf) + [0, 6], \dur, 0.5));
370 Pdef(\x).fadeTime = nil;
373 Pdef(\x, Pbind(\degree, Pseq((0..7), inf)).trace(\degree));
376 Pdef(\x, Pbind(\degree, Pseq((0..7), inf)).trace(\degree));
379 subsection::Update condition
381 In order to be able to switch to a new pattern under a certain link::#-condition::, the instance variable 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 link::#-reset:: message can be used.
383 As counting up (such as emphasis::"every nth event, a swap can happen"::) is a common task, there is a method for this, called link::#-count::(n).
387 Pdef(\x).quant = 0; // we don't want quant here.
388 Pdef(\x, Pbind(\degree, Pseq((0..5), inf), \dur, 0.3)).condition_({ |val, i| i.postln % 6 == 0 });
389 Pdef(\x, Pbind(\degree, Pseq((0..7) + 5.rand, inf), \dur, 0.3)).condition_({ |val, i| (i % 8).postln == 0 });
391 // the above is equivalent to:
392 Pdef(\x, Pbind(\degree, Pseq((0..7) + 5.rand, inf), \dur, 0.3)).count(8);
394 // the value that is sent in is the event, so decisions can be made dependent on the event's fields
400 // reset to change immediately: