Cabbage Logo
Back to Cabbage Site

Passing variable into instrument using event defaults to 0

Hello, have an issue here. When triggering an instrument with the event opcode, the value from a variable seems to reset to 0 inside the instrument? Hope someone can take a look at it :+1:

<Cabbage>
form caption("Test") size(650, 550), guiMode("queue"), pluginId("Test"), opcodeDir("."), presetIgnore(1)
keyboard bounds(201, 385, 400, 100) channel("keyboard10049")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-dm0 -n -+rtmidi=null -M0 -Q0 --midi-key=4 --midi-velocity=5
</CsOptions>
<CsInstruments>
ksmps = 128
nchnls = 2
0dbfs = 1

// Listens to all MIDI channels
instr 1
    cabbageMidiSender // Send midi events to cabbageMidiListener opcode..
endin

// Midi instrument
instr 2
    kNotes[], kVels[], kChns[], kCnt cabbageMidiListener 1
    
    kTrig = metro(1)
    
    kNote = kNotes[0]
    
    printk2 kNote
        
    if (kNote != 0 && kTrig > 0) then
        // Trigger test-tones
        event "i", 100, 0, 1, i(kNote), 1
    endif
endin

// Oscillator
instr 100
    print p4
    
    iGain = p5/127
    kEnv expon iGain, p3, 0.001
    aOut oscili kEnv, cpsmidinn(p4)
    outs aOut, aOut  
endin

</CsInstruments>
<CsScore>
i2 0 z
</CsScore>
</CsoundSynthesizer>

all i variables gets value on init time. so i(kNote) will not change on kNote change dirong performance, it keeps init kNote value which is 0.
to change i use reinit
like:
if kTrig == 1 then
reinit NOT
endif
NOT:
iNote = i(kNote)
rireturn

Apart from what @Kzz has pointed out, I have to ask, why the cast to i() at all? There is no need for it in this context as it’s a r-tate conditional, coupled with a k-rate opcode. Just drop the cast.

1 Like

@Kzz @rorywalsh

Thanks to both, removing the type casting fixed it :+1: