{
   "author" : "grirgz",
   "name" : "How to generate random envelope but control it with pattern standard \\dur and \\legato mecanism",
   "description" : "",
   "ancestor_list" : [],
   "labels" : [],
   "id" : "1-534",
   "is_private" : null,
   "code" : "TempoClock.default.tempo = 1.5; \r\n\r\n(\r\nSynthDef(\\plop, { arg out=0, amp=0.1, gate=1, pan=0, freq=200, sustain=1;\r\n\tvar sig;\r\n\tvar env = EnvGen.ar(\\env.kr(Env([ 0,1,1,0 ],[1,1,1])),gate,doneAction:2);\r\n\tsig = SinOsc.ar(freq * ( env + 1 ));\r\n\tsig = sig * env;\r\n\tsig = Pan2.ar(sig, pan, amp);\r\n\tOut.ar(out, sig);\r\n}).add;\r\n);\r\n\r\n(\r\nPdef(\\plop, Pbind(\r\n\t\\instrument, \\plop,\r\n\t\\degree, Pseq([0],inf),\r\n\t\\dur, Pseq([8,2],inf),\r\n\t\\legato, 0.8,\r\n\t\\env, Pfunc({ arg ev; // Pfunc because rrand should be executed for each event\r\n\t\tvar times = [ rrand(0.01,1), rrand(0.01,1), rrand(0.01,1) ];\r\n\t\t// sustain is already available in the default event and is computed from dur and legato\r\n\t\tvar sustain = ev.use({ ~sustain.value }) / thisThread.clock.tempo; // ~sustain is in beats but env should be in seconds\r\n\t\tvar env;\r\n\t\tsustain.debug(\"sustain\");\r\n\t\tenv = Env([0,1,1,0], times.normalizeSum * sustain);\r\n\t\tenv.asCompileString.debug(\"env\");\r\n\t\t[env]; // array arguments need to be enclosed to avoid multichannel expansion in pbind\r\n\t}),\r\n\t\\amp, 0.1\r\n)).play;\r\n);\r\n\r\n\r\n//////////////\r\n//another way is to use timeScale of EnvGen\r\n\r\n(\r\nSynthDef(\\plop2, { arg out=0, amp=0.1, gate=1, pan=0, freq=200, tsustain=1;\r\n\tvar sig;\r\n\tvar env = EnvGen.ar(\\env.kr(Env([ 0,1,1,0 ],[1,1,1])),gate, timeScale:tsustain, doneAction:2);\r\n\tsig = SinOsc.ar(freq * ( env + 1 ));\r\n\tsig = sig * env;\r\n\tsig = Pan2.ar(sig, pan, amp);\r\n\tOut.ar(out, sig);\r\n}).add;\r\n);\r\n\r\n\r\n(\r\nPdef(\\plop, Pbind(\r\n\t\\instrument, \\plop2,\r\n\t\\degree, Pseq([0],inf),\r\n\t\\dur, Pseq([8,2],inf),\r\n\t\\legato, 0.8,\r\n\t\\tsustain, Pkey(\\sustain) / Pfunc({ thisThread.clock.tempo }), // sustain is in beats, tsustain in seconds\r\n\t\\env, Pfunc({ arg ev; \r\n\t\t// this method is not really shorter, i just removed all explanations\r\n\t\t[ Env([0,1,1,0], [ rrand(0.01,1), rrand(0.01,1), rrand(0.01,1) ].normalizeSum) ];\r\n\t}),\r\n\t\\amp, 0.1\r\n)).play;\r\n);"
}
