# How to loop an array to give values to my SynthDefh?

Hi Everyone!!
I´m a beginner in SC wordl.
I’m building a SynthDef from the data in an array and I don’t know how to loop through that array within the SynthDef.

For example:

´´´
m = Array.fillND([3, 3, 3]).debug(“start m”);
(
z = {
var sig, f1, f2, f3, p1, p2, p3, a1, a2, a3, s1, s2, s3
f1 =m[0][0][0];
f2 = m[0][1][0];
f3 = m[0][2][0];
p1 = m[0][0][1];
p2 = m[0][1][1];
p3 = m[0][2][1];
a1 = m[0][0][2];
a2 = m[0][1][2];
a3 = m[0][2][2];
s1 = Pan2.ar((SinOsc.ar(f1)),p1,a1);
s2 = Pan2.ar((SinOsc.ar(f2)),p2,a2);
s3 = Pan2.ar((SinOsc.ar(f3)),p3,a3);
sig = s1 + s2 + s3;
}.play;
)
´´´

Hello,

First of all I can’t see any SynthDefs, but you are using a function.

Here you can find a nice intro to SC
https://doc.sccode.org/Tutorials/Getting-Started/00-Getting-Started-With-SC.html

Sequencing
you couldn check patterns too

If I can Ask, do you really need a N dimensional Array to do what you are trying to do or you just need to change the freqs of 3 SinOsc, the pan position and the amplitude in a simple way?

PS: I suggest you to use the Preformatted functionality that is offered by the website for the readability .

@soundgold you just used ticks (´) instead of backticks (`)

This approach would work for static values: if you use an array like that you won’t be able to change values when the synth is running (as they are read once, when the SynthDef is created).

If you are ok with this, a few notes:

• Array.fillND is getting you an array filled with nils. If you want, for example, a random value in each slot, consider:
``````m = Array.fillND([3, 3, 3]){1.0.rand}
``````
• I thought you might like a little syntax simplification when you read values:
``````(
// this does the same as your z above
z = {
var freqs, pans, amps;
// flopping m gets you three arrays, swapping "rows" and "columns". Then you can get:
# freqs, pans, amps = m[0].flop;
// # is a multiple assignment: m[0].flop returns an array of three elements, and # assigns each element to a different variable

// multichannel-expansion: when an UGen receives an array as argument, it creates an array of UGens, one for each argument.
Pan2.ar(SinOsc.ar(freqs), pans, amps).sum;
}.play;
)
``````
2 Likes