@lazzarello you can use arrays and array arguments in synthdefs, though there are some limitations. see the help files for SynthDef, Mulltichannel Expansion, Mix, &c.
here’s an example with 3 sinewaves in a phase-modulation loop. (feedback provided by LocalIn/LocalOut.)
demonstrates:
- array arguments (for freq, mod index, and delay time)
- building and using arrays of ugens programatically in the synthdef
note the use of the array literal notation (#[])
(warning: gets noisy)
Routine {
SynthDef.new(\fm3, {
arg out=0, amp=0.05,
freqs = #[110, 220, 330],
mods = #[0.1, 0,1, 0.1],
delays = #[0.001, 0.001, 0.001],
freqlag = 0.1, modlag=0.1, delaylag=0.1;
var mod_in;
var mod_out;
var sins;
mod_in = LocalIn.ar(3);
sins = freqs.collect({ arg f, i;
SinOsc.ar(Lag.kr(freqs[i], freqlag), mod_in[(i+1)%3] * Lag.kr(mods[i], modlag))
});
mod_out = sins.collect({ arg sin, i;
DelayC.ar(sin, 0.2, Lag.kr(delays[i], delaylag))
});
LocalOut.ar(mod_out);
Out.ar(out, amp * Mix.new(sins));
}).send(s);
s.sync;
x = Synth.new(\fm3);
// command-period to stop!
inf.do ({
(1 + 1.0.rand).wait;
x.set(\freqs, Array.fill(3, {200 + 200.rand}).postln);
x.set(\mods, Array.fill(3, { 2.0.rand}).postln);
x.set(\delays, Array.fill(3, {0.1.rand}).postln);
})
}.play;