Using argument as do receiver?

I quickly wrote this SynthDef a few mins ago:

(
SynthDef.new(\saw, {
|freq=10, atk=0.5, sus=1, rel=2, t_trig=1, boost=1.25, pan=0, amp=0.5|
var sig, env;
env = EnvGen.kr(Env([0,1,1,0],[atk,sus,rel],[1,0,-1]), gate:t_trig, doneAction:2);
sig = SawDPW.ar(freq);
5.do {sig = (sig * boost).fold(-1,1)}; // folding cycle
sig = sig * env;
sig = Pan2.ar(sig, pan, amp);
Out.ar(0, sig);
}).add;
)

I want to be able to change the number of times the do cycle runs (its receiver, 5 in this case) when creating synths but it just doesn’t work if I assign it as a variable in the SynthDef like this:

arg foldingcycles = 5;
[…]
foldingcycles.do {…}

seems like it doesn’t fold at all. How can I do this?

Thanks!

foldingcycles is fixed with SynthDef evaluation, you cant use a SynthDef argument to change it afterwards.
You can for example predefine a maximum value you want to use and create different SynthDefs up to this value for later use.

(
var maxFoldingCycles = 5;

maxFoldingCycles.collect { |foldingCycle|
	SynthDef(("saw" ++ foldingCycle).asSymbol, {
		|freq=10, atk=0.5, sus=1, rel=2, t_trig=1, boost=1.25, pan=0, amp=0.5|
		var sig, env;
		env = EnvGen.kr(Env([0,1,1,0],[atk,sus,rel],[1,0,-1]), gate:t_trig, doneAction:2);
		sig = SawDPW.ar(freq);
		foldingCycle.do {sig = (sig * boost).fold(-1,1)}; // folding cycle
		sig = sig * env;
		sig = Pan2.ar(sig, pan, amp);
		Out.ar(0, sig);
	}).add;
};
)

Synth(\saw5, [\freq, 100]);

you could also check out miSCellaneous_lib there are some folding Ugens in there with a bit more flexibility. maybe they are of interested to you.

1 Like