Two-dimensional a-rate array

Hi there,

In one Csound project I was able to create a phasor by looping through the indices of an a-rate variable like this:

kphs init 0
kndx = 0
while (kndx < ksmps) do
    aphs[kndx] = kphs
    kphs += 1
    kndx += 1 
od

In my current Csound project, I’d like to create an array of 10 phasors using a similar method, but Csound won’t let me do it. Here’s example code:

; instr 0
gatrack_phase[] init 10

;instr 1
kphs init 0
kndx = 0
while (kndx < ksmps) do
    gatrack_phase[0][kndx] = kphs
    kphs += 1
    kndx += 1
od

Csound gives this error: PERF ERROR in instr 1 (opcode ##array_set.x) line 0: Array dimension 2 does not match for dimensions 1.

Does Csound not let you use a-rate arrays like in the second example? I know I can use the phasor opcode but I was hoping to do it with a ksmps loop for the flexibility it offers.

Thanks,
Jason

While I think you can do something like aSig[0] to access the first element of an audio vector, what you are trying to do won’t work. Csound see gatrack_phase[0][kndx] and thinks you are trying to access values from a 2d array. There may well be a way to do this, but I’m not sure what’s the best one.

On the other hand, if you really want to do this with a ksmps loop, then put it in a UDO and set ksmps to 1 with setksmps. Btw, using an opcode is always much faster than coding it by hand in Csound. 3 times faster, or thereabouts.

Thanks Rory. I think I’ve figured out a workaround. Looks like this and seems to be working:

kphs init 0
kindex = 0
while (kindex < ksmps) do
    atemp_phase[kindex] = kphs
    ; gatrack_phase[0][kindex] = kphs
    kphs += 1
    kindex += 1
od
gatrack_phase[0] = atemp_phase

Ah, thanks for the tip about the performance gains of opcodes. I was wondering about that. Just to be clear, you’re saying that if I were to use an opcode like phasor it’ll be about 3x faster than hand coding a ksmps loop?

EDIT: Does the 3x performance gain also apply to UDOs or only to native Csound opcodes due to them being written in C/C++?

Thanks!
Jason

It only applies to opcodes, for reasons you already figured out. The 3x faster thing is an approximation of course. I’m pretty sure I could write an opcode so bad that it would be much slower than a UDO :rofl:

I believe UDOs might also suffer from a slight performance hit too. Someone with more knowledge of the inner workings of Csound can comment better on this. One thing is clear, they can really help to modularise one’s code. And that’s worth a lot in my book.

Perfect, thanks for the answer!