Cabbage Logo
Back to Cabbage Site

How to handle a-rate audio channels in loops?

I’m making a plugin that routes audio at specific intervals or sequences. I could not figure out how to properly use a loop to handle the same function to a range of audio channels. Would anyone help me optimize this flow? I can provide the entire .CSD file if needed, but there isn’t much going on in the global variables used here.

Bloated block of code:

instr output
    ; get input channels
    a1, a2,
    a3, a4,
    a5, a6,
    a7, a8,
    a9, a10,
    a11, a12,
    a13, a14,
    a15, a16,
    a17, a18,
    a19, a20,
    a21, a22,
    a23, a24,
    a25, a26,
    a27, a28,
    a29, a30,
    a31, a32 inch 1, 2,
                  3, 4,
                  5, 6,
                  7, 8,
                  9, 10,
                  11, 12,
                  13, 14,
                  15, 16,
                  17, 18,
                  19, 20,
                  21, 22,
                  23, 24,
                  25, 26,
                  27, 28,
                  29, 30,
                  31, 32
    
    ; adjust gain & trigger output
    aOut1 = a1 * gkGain[0] * gkTriggers[0]
    aOut2 = a2 * gkGain[0] * gkTriggers[0]
    aOut3 = a3 * gkGain[1] * gkTriggers[1]
    aOut4 = a4 * gkGain[1] * gkTriggers[1]
    aOut5 = a5 * gkGain[2] * gkTriggers[2]
    aOut6 = a6 * gkGain[2] * gkTriggers[2]
    aOut7 = a7 * gkGain[3] * gkTriggers[3]
    aOut8 = a8 * gkGain[3] * gkTriggers[3]
    aOut9 = a9 * gkGain[4] * gkTriggers[4]
    aOut10 = a10 * gkGain[4] * gkTriggers[4]
    aOut11 = a11 * gkGain[5] * gkTriggers[5]
    aOut12 = a12 * gkGain[5] * gkTriggers[5]
    aOut13 = a13 * gkGain[6] * gkTriggers[6]
    aOut14 = a14 * gkGain[6] * gkTriggers[6]
    aOut15 = a15 * gkGain[7] * gkTriggers[7]
    aOut16 = a16 * gkGain[7] * gkTriggers[7]
    aOut17 = a17 * gkGain[8] * gkTriggers[8]
    aOut18 = a18 * gkGain[8] * gkTriggers[8]
    aOut19 = a19 * gkGain[9] * gkTriggers[9]
    aOut20 = a20 * gkGain[9] * gkTriggers[9]
    aOut21 = a21 * gkGain[10] * gkTriggers[10]
    aOut22 = a22 * gkGain[10] * gkTriggers[10]
    aOut23 = a23 * gkGain[11] * gkTriggers[11]
    aOut24 = a24 * gkGain[11] * gkTriggers[11]
    aOut25 = a25 * gkGain[12] * gkTriggers[12]
    aOut26 = a26 * gkGain[12] * gkTriggers[12]
    aOut27 = a27 * gkGain[13] * gkTriggers[13]
    aOut28 = a28 * gkGain[13] * gkTriggers[13]
    aOut29 = a29 * gkGain[14] * gkTriggers[14]
    aOut30 = a30 * gkGain[14] * gkTriggers[14]
    aOut31 = a31 * gkGain[15] * gkTriggers[15]
    aOut32 = a32 * gkGain[15] * gkTriggers[15]
    
    ; set output channels
    out aOut1, aOut2,
        aOut3, aOut4,
        aOut5, aOut6,
        aOut7, aOut8,
        aOut9, aOut10,
        aOut11, aOut12,
        aOut13, aOut14,
        aOut15, aOut16,
        aOut17, aOut18,
        aOut19, aOut20,
        aOut21, aOut22,
        aOut23, aOut24,
        aOut25, aOut26,
        aOut27, aOut28,
        aOut29, aOut30,
        aOut31, aOut32
endin

Untested, but maybe this can guide you:

instr output
    aIns[] init 32
    aOuts[] init 32
    aIns in ; throw all inputs into an array
    kIndex = 0
    while kIndex < 32 do
        aOuts[kIndex] = aIns[kIndex] * gkGain[kIndex] * gkTriggers[kIndex]
        kIndex += 1
    od
    out aOuts ; output all signal as an array
endin
1 Like

This is working great, thank you for showing the array versions I missed in the Csound docs.

To be able to work with stereo channels, I did it like this:

instr output    
    ; init channels
    aIns[] init 32
    aOuts[] init 32
    
    ; init indexes
    kI = 0 ; individual index, goes up to 32
    kSt = 0 ; stereo index, goes up to 16
    
    ; get inputs
    aIns in
    
    ; adjust for gain & trigger
    while kSt < 16 do
        ; adjust left stereo channel
        aOuts[kI] = aIns[kI] * gkGain[kSt] * gkTriggers[kSt]
        kI += 1
        ; adjust right stereo channel
        aOuts[kI] = aIns[kI] * gkGain[kSt] * gkTriggers[kSt]
        kI += 1
        kSt += 1
    od
    
    ; set outputs
    out aOuts
endin
1 Like