How to get the proper duration of an env in the Pattern context?

Hi everyone,
I’m trying to wrap my mind around on how could I pass the right amount of time to the Env
considering that I want the env long as \dur from the Pbindef?

  SynthDef(\DcOuts,{|amp=1,minAmp=0,delta=1.0,bus|
        var env = EnvGen.kr(
    	    \env.kr(Env.newClear(8)),
    	    levelBias: minAmp,
    	    timeScale:delta,
    	    doneAction:2
    	); 
    	
    	Out.kr(bus,amp*env);
    	
    }).add;

x = Pbindef(\test,     
    \instrument, \DcOuts,
    \env,[[Env.perc]],
    \dur,Pseq([1,2,1,3],inf),
).play

TempoClock.default.tempo = 2

Thank you very much for your attention.

While we wait for some more expert voices about Patterns, here are my discoveries:

  • Only the clock knows durations in seconds? Duration values are in beats up until EventStreamPlayer plays the event on the clock
  • However, synths get a \dur in beats from patterns. And you can pass the tempo along as well, so that you can do your conversion inside the synth:
// receives \dur in beats, \tempo in bps.
SynthDef(\beatEnv){ |out=0|
  // convert dur in seconds
  var dur = \dur.kr / \tempo.kr(1); 
  var env = EnvGen.kr(Env.perc,timeScale:dur);
  dur.poll; // let's see some numbers in the post window
  Out.kr(out, env);
}.add;

Pbindef(\p,\instrument,\beatEnv,
  \tempo,Pfunc{thisThread.clock.tempo},
  \dur,Pseq([1,2,3])
).trace.play;

// when changing tempo, synths get updated on the next event
TempoClock.default.tempo = 2;
TempoClock.default.tempo = 1;
2 Likes

I posted a slightly more advanced example of how to use envelope passing in a real workflow here:

Among other things, I’ve got a post-processing phase in \finish where I ensure all my Env parameters are actually Envs, and set their durations to 1…0. Then later, in the synth, I rescale them back to \sustain… this is a bit of a simplification, but I think it’s roughly the right approach for scaling env arguments, depending on use-case of course.

2 Likes