Iteration picking successives elements of an Array

Hi, this is super nooby. I am sure this is quite simple but I don’t find the syntax online. In the following example I would like that the first iteration (.do) of the Task send the first elements of the freq array to the synth, the second iteration the second element of the freq array and so on.
Also, In this case what would happen if the number of iteration is smaller than the array size ? And if it is bigger ?

Thanks a lot

``````// Example of an array of freqs

c = [500, 507, 607, 637, 677, 734, 800, 850, 900, 910, 930];

(
Tdef(\glisse).set(\smin, 0.4, \smax, 0.4,  \emin, 0.1, \emax, 0.1, \h1, 7, \h2, 2.2, \fratio, 0.27, \fratio2, 0.35);

Tdef(\glisse, { |envir|

var f1, f2, h1, h2, temp, sus, sloped, envwide;
c.size.do {
sloped = exprand(envir.smin, envir.smax);
envwide = exprand(envir.emin, envir.emax);

freq = // ????
h1 = exprand(envir.h1, envir.h2);
h2 = h1 * (envir.fratio2 ** 1.0.rand2);

(	instrument: \glisson,
sloped: sloped,
envwide:envwide,
freq: freq,
h1:h1,
h2:h2,
pan: 0.5.rand2,
sustain: sloped + envwide / 0.9,
).play;
(sloped + envwide).wait;
};
}).play;
)

``````

Just iterating over an array is simple

``````c = [500, 507, 607, 637, 677, 734, 800, 850, 900, 910, 930];

c.size.do({|i|
var freq = c[i];
freq.postln;
})
``````

or

``````c.do({|freq|
freq.postln;
});
``````

But it sounds like you want to do something a bit different but that part isn’t clear to me

Thanks, that’s exactly your first solution that I was looking for because I need a number of iterations that is independant of the array size : I didn’t know the c[i] syntax ! But if the number of iteration is bigger than the array of course it returns nil. How to make it automatically start again at the begining of the Array ?

use the wrapAt method

``````a = [0,1,2,3];
a.wrapAt(9)
``````

see the help file for Array for other useful methods

I think the documentation you’re looking for is here…
https://doc.sccode.org/Reference/Control-Structures.html
and
https://doc.sccode.org/Classes/Array.html

Thanks both of you !

Perhaps you’ve already solved this, but here is an example. What I tend to do in these cases is wrap the finite iteration block within an infinite iteration block, so that the finite block repeats forever. You didn’t include your `\glisson` SynthDef, so I created a simple SynthDef as a placeholder.

Eli

``````c = [500, 507, 607, 637, 677, 734, 800, 850, 900, 910, 930];

(
SynthDef(\glisson, {
arg freq=440;
var sig, env;
sig = SinOsc.ar(freq);
sig = sig * Env.perc.kr(2);
sig = sig * 0.2!2;
Out.ar(0, sig);
)

(
Tdef(\glisse).set(\smin, 0.4, \smax, 0.4,  \emin, 0.1, \emax, 0.1, \h1, 7, \h2, 2.2, \fratio, 0.27, \fratio2, 0.35);

Tdef(\glisse, { |envir|

var f1, f2, h1, h2, temp, sus, sloped, envwide;
inf.do{
c.do { |freq| // <-- pass array values into iteration function
sloped = exprand(envir.smin, envir.smax);
envwide = exprand(envir.emin, envir.emax);

h1 = exprand(envir.h1, envir.h2);
h2 = h1 * (envir.fratio2 ** 1.0.rand2);

(	instrument: \glisson,
sloped: sloped,
envwide:envwide,
freq: freq,
h1:h1,
h2:h2,
pan: 0.5.rand2,
sustain: sloped + envwide / 0.9,
).play;
(sloped + envwide).wait;
};
};
}).play;
)
``````