Creating synthdefs by evaluating a function

I want to have a function that sort of acts as a template for a synthdef, so that i can just evaluate it and then a synthdef will be created.
But why does this not work?

(
//synthdef is not yet created
f = {|uGen|
	SynthDef(\test,{
		Out.ar(0, uGen)
	}).add;
};
//now the synthdef is being instantiated
f.value(SinOsc.ar());

//silence
x = Synth(\test)
)

I understand that at the creation of a synthdef the ugen graph is set in stone. But in this case the synthdef is being created at the moment f is evaluated, so what im doing should be within the rules, shouldnt it?

This IS annoying.

The reason why it does not work is because the ugen has to be created inside the braces of the synthdef. Currently it is created on line 9, outside of synthdef’s scope.

If you passed a function that returned a ugen, and evaluated that inside the synthdef it will work.

Yes you can do this.
However, it only really makes sense when you are passing around large things.
Actually this is much better than passing around the Ugen directly as you can have controls passed in too.

~revTemplate = {|name, inputFunc|
	SynthDef(name, {
		var input = inputFunc.();
		// do something with input. Perhaps reverb?
	})
};

~revTemplate.(\test, { SinOsc.ar(\freq.kr(440)) }).add;

x = Synth(\test);
x.set(\freq, 220)
3 Likes