Storing all the values from a MIDI controller in a 2-dimensional array

Hey,

I’m trying to build an instrument for getting all the values from MIDI controller on every channel. I tried using a nested loop, something like this:
loop_i:
loop_j:

gk_every_knob[ii][ij] ctrl7 ii, ij, 0, 127

loop_lt ijndx, 1, ginum_of_knobs, loop_j
loop_lt iindx, 1, ginum_of_channels, loop_i

If i put the code in an instrument the loop only runs once. I would need the instrument to keep upgrading the array based on the current values of the controller. What am I missing?

hi andras -

one reason is that you use a loop at i-rate.
this means that the loop is only executed once, at initialization of the
instrument.
what you might try is to call this instrument at every k-cycle, like

gkArr[][] init 2,3

instr CallAndPrint
schedulek(“Write”,0,1)
printarray(gkArr)
endin
schedule(“CallAndPrint”,0,0.01)

instr Write
k_i = 0
while (k_i < 2) do
k_j = 0
while (k_j < 3) do
gkArr[k_i][k_j] = rnd:k(100)
k_j += 1
od
k_i += 1
od
turnoff
endin

note that i used the index for gkArr as k-variable, too.
but i guess this should be possible with your ctrl7 example, too:

gk_every_knob[ii][ij] = ctrl7:k(ii, ij, 0, 127)

joachim

1 Like

Thank you for the answer.
So far I haven’t managed to make a nested loop work, so now my goal is to get all the controller values from a single channel. But I ran into problems with that as well.
The problem is with the ctrl7 opcode, because it works only with i-rate variables as the channel number. I tried using the i() opcode in the loop for getting the current index value as an i-rate variable, but that doesn’t work . I also tried the version you have provided, but I couldn’t make it work.
Is there an opcode that listens to MIDI controllers and takes k-rate variables as channel number arguments?
What is the usual solution to the original problem (storing and updating all the MIDI controller values dynamically in a global array)? I think it would be convenient to store the controller values like this, , because getting the controller values one-by one takes a lot of redundant code. So I guess there must be a usual solution for this in Csound that I don’t know of.