Additive Synth, dynamic array size

Hi,
I´m trying to sequence a Synth with different Array Sizes and I´m not sure how to archive that. Somehow I need the SynthDef to create a new Array every time it is played. I couldn´t find any helpfull threads. Anybody has an Solution or an idea?
This is example code to show the problem:

(
SynthDef(\test,
{
arg freq = 220, atk = 0.01, rel = 1, overtone = 5, undertone = 5;
var sig, env;
env = EnvGen.ar(Env.new([0.01,1,0.01], [atk, rel]), doneAction: 2);
sig = Array.fill(overtone,{
|i| SinOsc.ar(freq * (i+1), mul: 1/((i+1).sqrt));})
+ Array.fill(undertone, {
|i| SinOsc.ar(freq / (i+1), mul: 1/((i+1).sqrt));});
sig = sig * env * 0.1;
Out.ar([0,1], sig!2);
}).play;
)

Help would be much appreciated, thanks :slight_smile:

This is a recurrent issue with SynthDef. After it depends on your goal. Personally, I would manage it as a routine for instance with environmental variables like so:

(
~freq = 220;
~atk = 0.01;
~rel = 10;
~overtone = 5;
~undertone = 5;
{
	Array.fill(~overtone,{|i| SinOsc.ar(~freq * (i+1), mul: 1/((i+1).sqrt))})
	+ Array.fill(~undertone, {|i| SinOsc.ar(~freq / (i+1), mul: 1/((i+1).sqrt))}) 
	* 0.1!2
	* EnvGen.ar(Env.new([0.01,1,0.01], [~atk, ~rel]), doneAction: 2);
}.play;
)

Y.

this thread from last week may be useful! Is it possible to make a synthdef to deal with a variable number of oscillators?

If you are not using patterns I like to use a function like so:

~someOvertones = {|arraySize fundamental|
   { arraySize.collect{ |i| SinOsc.ar( fundamental * (x +1) 0, 0.05) }}.play
}

Yes I was planning to use Patterns to control the array size. That thread seems to have a solution. I will try to implement it like this. Thanks a lot