SuperCollider - Synthdef混合了可变数量的SinOsc

时间:2015-12-13 11:22:22

标签: supercollider

此代码返回错误,但如果我从第4行删除“arg”,它会起作用。我该怎样做才能使n成为参数而不会出错?

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

2 个答案:

答案 0 :(得分:0)

SynthDefs始终固定"接线",因此您无法改变SinOsc的数量。这是一个你无法避免的严格限制。

你可以做的是在程序上为每个基数生成合成器:

(
(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

答案 1 :(得分:0)

如果您有n的上限(假设n<=16),则可以创建一个连续的截止数组,并用它乘以谐波。

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

如果index<n,截止数组的值为1,此后为零。让我们说n=3,然后说cutoff==[1,1,1,0,0,0,...]