Set synth argument with lag

I would like to have a lag of N seconds for some arguments of my synth, so that when I do synth.set(\arg, newValue) the value is changed over N seconds, and not immediately. How do I do it?

I think there are at least two ways to do it:


If you use the built-in rates argument for SynthDef:

(
SynthDef(\test1, { |freq=440, amp=0.1, panPos=0, out=0|
	var sig = SinOsc.ar(freq);
	sig = Pan2.ar(sig, panPos, amp);
	Out.ar(out, sig)
}, [2, 4, 10, 0]).add
)

x = Synth(\test1)
x.set(\freq, 880)
x.set(\panPos, -0.9)
x.set(\panPos, 0.9)
x.set(\out, 1)

x.free

This is documented in the SynthDef help document.


If you use the .lag(lagTime) method:

(
SynthDef(\test2, { |freq=440, amp=0.1, panPos=0, out=0, lag=2|
	var sig = SinOsc.ar(freq.lag(lag));
	sig = Pan2.ar(sig, panPos.lag(lag), amp);
	Out.ar(out, sig)
}).add
)

x = Synth(\test2)
x.set(\freq, 880)
x.set(\freq, 440, \lag, 0)
x.set(\freq, 880, \lag, 10)
x.set(\panPos, -0.9, \lag, 0)
x.set(\panPos, 0.9, \lag, 10)

x.free

You might also use the lag2 and lag3 methods to get a smoother transition.