Search code examples
supercollider

SuperCollider -- Synthdef with mix of variable amount of SinOsc's


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;
)

Solution

  • 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