Cabbage Logo
Back to Cabbage Site

Summing a-rate variables in triggered instruments

Hello.

I’m trying to sum the output of each instance of instr 1, and output the sum in instr 99. This is because I want to apply opcodes such as filtering in instr 99 instead of 1 to reduce CPU usage.

Currently, this is causing feedback/loud noise the way I’ve implemented it (warning: loud sound)

Wondering if anyone has any tips on how to achieve this correctly. :slightly_smiling_face:

<Cabbage>
form caption("Stretch") size(600, 500), guiMode("queue"), pluginId("1287"), colour (0,100,0)
keyboard bounds(102, 380, 381, 95)
soundfiler bounds(110, 52, 300, 200), channel("Soundfiler1", "Soundfiler2"), colour(188, 188, 188), tableBackgroundColour(62, 71, 86), showScrubber(1), displayType("mono")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 --midi-key-cps=4 --midi-velocity-amp=5
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
ksmps = 32
nchnls = 2
0dbfs = 1

gaOutL init 0
gaOutR init 0

instr 98
    gSfile cabbageGet "LAST_FILE_DROPPED"

    if (changed(gSfile) == 1) then
        cabbageSet 1, "Soundfiler1", "file", gSfile
    endif
endin

instr 1
    Sfile cabbageGet "Soundfiler1", "file"
    
    if filevalid(Sfile) == 1 then
        if filenchnls(Sfile) == 1 then
            a1 diskin2 Sfile, 1, 0, 1
            gaOutL += a1
        else
            a1, a2 diskin2 Sfile, 1, 0, 1
            gaOutL += a1
            gaOutR += a2
        endif
    endif 
endin

instr 99 
    outs gaOutL, gaOutR
endin
</CsInstruments>
<CsScore>
f0 z
i98 0 z
i99 0 z
</CsScore>
</CsoundSynthesizer>

Because you are mixing into your global variables (e.g.: gaOutL += a1), not reassigning them on each k-pass, the value (or vector) each of them holds is retained and possibly accumulates as more audio is mixed in. The frequency of the whining tone produced is the equal to the control rate.
The fix is easy - just clear the global variable after you are finished with them:

instr 99 
    outs gaOutL, gaOutR
    clear gaOutL, gaOutR
endin
1 Like

Thank you, that did the trick :+1: