Iteration in supercollider

523 Views Asked by At

I need a help with syntax of iteration using .do ;

I have this function:

(
{var freq;
    freq = [[660, 880], [440, 660], [1320, 880],[220,660]].choose;
    SinOsc.ar(freq, 0, 0.1);
}.play;
)

I try to iterate over it so in each iteration I will get a different array of frequencies and also will hear is output.

I try something like this without a success:

(
10.do({arg i;
    [[660, 880], [440, 660], [1320, 880],[220,660]].choose;
    {SinOsc.ar(i,0,0.1);        
    }.play;
};
) 
)

Thank you for any help!

2

There are 2 best solutions below

0
On
(
10.do({
    var hz;
    hz = [[660, 880], [440, 660], [1320, 880],[220,660]].choose;
    {
        SinOsc.ar(hz, 0, 0.05)
    }.play;
});
)
0
On

The code structure:

{
\\...
}.play;

is a shortcut to create and play a SynthDef. You can only use UGens in a SynthDef. This means the the freq = [/*...*/].choose line cannot go in a SynthDef.

If you do want to choose within a SynthDef, you can use a Demand rate Ugen like Drand. See the Demand helpfile for more https://doc.sccode.org/Classes/Demand.html