Using Select in Synth

My following synth is throwing an error, because I am trying to choose my envelope conditionally:

(
SynthDef(\x, {
	var env_shape = Select.kr(
		\envshape.kr(1),
		[
			Env([0, 1], [\duration.kr], \lin),
			Env([0, 1, 0], [0.01, \duration.kr - 0.01], \lin),
			Env([1, 0], [\duration.kr], \lin),
		]
	);
	Out.ar(0, SinOsc.ar(mul: EnvGen.kr(env_shape)))
}).add
)

Can any one help me understand why sc doesn’t like this synth?
Thank you!

You forgot to specify the out bus :

Out.ar(SinOsc.ar(mul: EnvGen.kr(env_shape)))

should be

Out.ar(0, SinOsc.ar(mul: EnvGen.kr(env_shape)))

But this doesn’t fix it. I tried to ‘flop’ the array because apparently, SuperCollider doesn’t really understand what to do with it :

SynthDef(\x, {
	var env_shape = Select.kr(
		\envshape.kr(1),
		[
			Env([0, 1], [\duration.kr], \lin),
			Env([0, 1, 0], [0.01, \duration.kr - 0.01], \lin),
			Env([1, 0], [\duration.kr], \lin),
		].flop
	);
	Out.ar(SinOsc.ar(mul: EnvGen.kr(env_shape)))
}).add
)

Apparently that ‘works’, but Select is complaining that he doesn’t like Envs as inputs.

Instead of declaring an array of Env, I tried to wrap them directly inside EnvGens.

Env format is not ‘audio format data’. EnvGen converts an Env to ‘audio data format’ so you can make it interact with other synth, for example by multiplying it, controlling a parameter, or, in this case, use it as a parameter for Select. If you have an Env, most of the time, the first thing you’d like to do is to convert it with EnvGen before using it for anything.

(
SynthDef(\x, {
	var env_shape = Select.kr(
		\envshape.kr(1),
		[
			EnvGen.kr(Env([0, 1], [\duration.kr], \lin)),
			EnvGen.kr(Env([0, 1, 0], [0.01, \duration.kr - 0.01], \lin)),
			EnvGen.kr(Env([1, 0], [\duration.kr], \lin)),
		]
	);
	Out.ar(0, SinOsc.ar(mul: env_shape))
}).add
)

No more error messages, so I suppose it works ? Didn’t test. No need to ‘flop’ any more. I have an insight on why, but couldn’t explain it correctly, so this will stay a mystery for now.

1 Like

From what I imagine you tried to do, Select is not what you want. It cycles through an Array of audio signals.

If the envelopes are short, like your example, why not just write three SynthDefs? Is there a reason not to do it?

Then you can choose which one you want. It will make more sense if you’re trying to use a lot of short sounds, like granular synthesis. Cleaner code and is much more efficient. Select needs all audio channels on its Array to be running, and it will be more costly performance-wise.

1 Like