0

This code returns an error, but it works if I remove "arg" from line 4. What can I do to make n an argument and not get an error?

(
SynthDef("test",
    {
        arg n=8;

        f=Mix.fill(n, {
            arg index;
            var freq, amp;
            freq=440*((7/6)**index);
            //freq.postln;
            amp=(1-(index / n)) / (n*(n+1) / (2*n));
            SinOsc.ar(freq,0,0.2*amp)
        });
        //f=SinOsc.ar(440,0,0.2);
        Out.ar(0,f)
    }).add;
)
Joshua Meyers
  • 619
  • 1
  • 8
  • 17

2 Answers2

0

SynthDefs always have fixed "wiring", so you cannot vary the number of SinOscs. That is a hard constraint which you cannot avoid.

What you can do is procedurally generate synthdefs for each cardinality:

(
(2..10).do{|num|

    SynthDef("wiggler%".format(num), {|freq=440, amp=0.1|
        var oscs;
        oscs = Mix.fill(num, {|index|
            SinOsc.ar(freq * index)
        });
        Out.ar(0, oscs * amp);
    }).add;

}
)

x = Synth("wiggler2")

x.free

x = Synth("wiggler10")

x.free
Dan Stowell
  • 4,618
  • 2
  • 20
  • 30
0

In case you have a an upper bound for n (let's say n<=16), then you can create a continuous cutoff array with which you multiply the harmonics.

(
SynthDef("test",
    {
        arg n=8;

        var cutoff = tanh( (1..16)-n-0.5 *100 ) * -1 / 2 + 0.5; // this

        f=Mix.fill(16, { // run it through the upper bound
            arg index;
            var freq, amp;
            freq=440*((7/6)**index);
            //freq.postln;
            amp=(1-(index / n)) / (n*(n+1) / (2*n));
            cutoff[index] * SinOsc.ar(freq,0,0.2*amp) // multiply with cutoff
        });
        //f=SinOsc.ar(440,0,0.2);
        Out.ar(0,f)
    }).add;
)

The cutoff array has values 1 if index<n, and zeros after that. Lets say n=3, then cutoff==[1,1,1,0,0,0,...].

grault
  • 55
  • 5