Plambda is used to create a namespace for patterns to share data among each other.
Used in combination with Plet and Pget, it allows you to get the output of a pattern from one stream and use it in another stream and as such create a dataspace for your patterns.
pattern |
an event stream. |
scope |
an event with default bindings (can be nil). |
/*
A simple example:
Two patterns playing in parallel,
sharing data between eachother
*/
(
// a melody playing random scale degrees
a = Pbind(
\dur, 0.125,
\octave, 4,
\degree, Plet(\melody, pattern: Pwhite(0,7))
);
// the bass, scale degrees sampled from the \melody variable defined above
b = Pbind(
\dur, 0.5,
\octave, 3,
\degree, Pget(\melody, default: 1, repeats: inf).trace
);
// Play the patterns in parallel
Plambda(
Ppar([a, b], inf)
).play;
)
/*
A more complex example:
Three patterns running in parallel
*/
(
SynthDef(\sine,
{ arg out=0, freq=440, sustain=0.05, pan=0, amp=0.1;
var env;
env = EnvGen.kr(Env.perc(Rand(0.001, 0.02), sustain, AmpCompA.kr(freq)*amp), doneAction: Done.freeSelf);
Out.ar(out, Pan2.ar(SinOsc.ar(freq), pan, env))
}).add;
)
(
a = Plambda(
Pseq([
Pfindur(5,
Ppar([
Pbind(\note, Plet(\x, Prand([1, 5, 1, [10, 14]], inf)), \dur, 8/3, \pan, -1),
Pbind(\note, Plet(\y, Pseq([5, 3, 2, 0, [0, 5, 6, 9]], inf)), \dur, 0.5, \pan,1),
Pbind(\note, Pseq([Pget(\x), Pget(\y)], inf) + 12, \pan, 0, \dur, 2/3)
])
),
Pbind(\note, Pget(\x, 0, 6) + [0, 5], \dur, Pdup(inf, Prand([2/3, 1/6])))
], inf).trace(\eventScope) // internally, the values are shared via \eventScope
);
b = Pbindf(a, \instrument, \sine, \legato, 0.1);
b.play
)
// Play two copies of the complex pattern above in parallel, one of them transposed and playing with shorter durations
Ppar([b, Pbindf(b, \ctranspose, 24, \dur, Pkey(\dur) * 0.25)]).play;