Parameterizing Env for buffer initialization

Dear Supercollider community,

I’m struggling with creating an Env from a SynthDef's args. I can’t imagine this wouldn’t be possible in terms of code reuse. I would preferably like to encapsulate the wavetable, buffer, and Shaper from my previous post inside the SynthDef function. However, when I use simple arithmetic with the args I pass to the Synthdef I keep getting the following error:

ERROR: Primitive '_ArrayEnvAt' failed.
Wrong type.

Here is the code in which I try to do this:

(
SynthDef.new(\parameterized_buffer, {
  |
  envAmp=0.8,
  envCurve=8,
  out=0
  |
  var env, sig, envBuffer;
  env = Env(
    levels: [0 - envAmp, 0.0, envAmp],
    times: [1, 1],
    curve: [envCurve, 0 - envCurve],
  ).asSignal(1025).asWavetableNoWrap;

  envBuffer = Buffer.loadCollection(s, env);

  sig = SinOsc.ar;

  Out.ar(out, sig);
}).add;
)

Thank you in advance!

loadCollection is a language-side operation and can’t be used in a SynthDef.

You’ll have to prepare the buffers from the language side before calling Synth().

hjh