{
   "labels" : [
      "pattern",
      "live coding",
      "abstraction",
      "tutorial"
   ],
   "is_private" : null,
   "id" : "1-4UV",
   "code" : "// For a long time I was thinking of what would be the best way to do this: function composition (functors) in live coding with patterns - it seems all in place anyway.\r\n\r\n// some synthdef\r\n(\r\nSynthDef(\\Pdefhelp, { arg out, freq, sustain=1, amp=1, pan;\r\n    var env, u=1;\r\n    env = EnvGen.kr(Env.perc(0.03, sustain), 1, doneAction:2);\r\n    3.do { var d; d = exprand(0.01, 1); u = SinOsc.ar(d * 300, u, rrand(0.1,1.2) * d, 1) };\r\n    Out.ar(out, Pan2.ar(SinOsc.ar(u + 1 * freq, 0, amp * env), pan));\r\n}).add;\r\n)\r\n\r\nPdef(\\stut, { Pstutter(~stutter ? 1, ~pattern) }); // this defines a function that maps one pattern to another.\r\nPdef(\\x, Pbind(\\instrument, \\Pdefhelp, \\note, Pseq([0, 4, 7, 3, 0, 1, 0], inf)));\r\nPdef(\\y, Pdef(\\stut) <> (pattern: Pdef(\\x), stutter: Pseq([2, 2, 4, 3], inf)) <> (dur: 0.1, legato: 0.2)).play;\r\nPdef(\\y, Pdef(\\stut) <> (pattern: Pdef(\\x), stutter: Pseq([2, 2, 4, 3], inf)) <> (dur: 0.1, legato: 0.2)).play;\r\n\r\n\r\n// Up to a certain point, this also works for non-event-patterns:\r\n\r\n// pattern as an argument\r\nPdefn(\\x, Pseq([1, 2, 5, 6, 7], inf));\r\nPdefn(\\x); // omitting the second argument, we can access the proxy\r\nPdefn(\\x).asStream.nextN(20); // ... play one event stream\r\nPdefn(\\x).source.postcs; // ... and inspect the event pattern itself.\r\n\r\n\r\n// function as an argument, create a new pattern each time it is called\r\nPdefn(\\x, { Pseq({ 10.rand } ! 8) });\r\nPn(Pdefn(\\x)).asStream.nextN(16);\r\n\r\n// the function is called in the incoming event as current environment, so parameters can be passed:\r\nPdef(\\stut, { Pstutter(~stutter ? 1, ~pattern) }); // note that we use a Pdef here, not a Pdefn\r\nPdefn(\\y, Pdef(\\stut) <> (pattern: Pdefn(\\x), stutter: Pseq([2, 2, 4, 3], inf))).asStream.nextN(16);\r\n\r\n::",
   "name" : "functors - Using Pdef for building abstract pattern functions while live coding",
   "author" : "julian.rohrhuber",
   "ancestor_list" : [],
   "description" : "A simple example that shows of how to build higher-level patterns using Pdef"
}
