Cabbage Logo
Back to Cabbage Site

Calling a instrument - if .. then

Hello everybody.

I start again feeling like a csound noob, so i post my problem here…

I want to call a instrument depending on the values of two buttons but it’s not working and i don’t know why. Maybe someone can check this out and help me. I just can’t see it. I already tried out several options.

Here is the example:

instr 1 ; widget controlls
gkAmp cabbageGetValue "amp"
gkDelTime chnget "delTime"
;gkTrig init 0
gkTrig chnget "trig"
printk2 gkTrig
gkTrig port gkTrig, 0.001
gkPanState chnget "pan"
gkPanState init 0
;gkRndDelTime init 0
gkRndDelTime chnget "rndDelTime"
printk2 gkRndDelTime, 50


;    if gkRndDelTime == 1 && gkTrig == 1 then
;            event "i", 2, 0, 1  
;    endif

if gkRndDelTime == 1 then
        if gkTrig == 1 then
            event "i", 2, 0, 1
        endif
endif
endin

instr 2
prints "HELLO\n"
turnoff 
endin

Here the .csd file
glitch-delay.csd (2.8 KB)

You want instrument 2 to start if gkRndDelTime and gkTrig are 1? You will need to use the changed opcode, or for convenience, just use the trigger output from cabbageGetValue. Something like this does the trick:

instr 1 ; widget controlls
    kTrigButton, kTrigTrig  cabbageGetValue "trig"
    kRndDelTime, kRndDelTrig cabbageGetValue "rndDelTime"

    if kRndDelTrig+kTrigTrig > 0 then
        //instr 2 will only play if both buttons are in the on position
        if kRndDelTime == 1 && kTrigButton == 1 then
            event "i", 2, 0, 1  
        endif
    endif 
endin

instr 2
    prints "HELLO\n"
    turnoff 
endin

Ah, thank you again!

And another problem i don’t get:

i want something to happen when two parameters are on the same value, but it happens immediately and i don’t know why

instr 3 ; envelope for delay time
p3 = random:i(1, 5)
gkDelTime expseg i(gkEnvStart), p3, i(gkEnvAim)
cabbageSetValue "delTime", gkDelTime
if gkDelTime == gkEnvAim then
        prints "HELLO 3\n"
        ; cabbageSetValue "trig", 0
endif

endin

What 2 parameters? The only thing you test in this code is gkDelTime…

I want something to happen if gkDelTime is equal to the gkEnvAim value.
So basically i want trigger an event when the expseg opcode reached his final value.

Do i miss here something?

Right, sorry. It may well be working but you’re trying to print something at k-time with an i-time opcode:

if gkDelTime == gkEnvAim then
        prints "HELLO 3\n"
endif 

prints is i-rate only. Try a k-rate print opcode instead…

but this is also the same when i want to use this line

cabbageSetValue "trig", 0

i basically want to set the widget and it’s value to zero after the if condition is true. But this also happens immediately.

The same problem as before, you’re trying to calling a i-rate opcode at perf time. Check out cabbageSetValue here

Thank you!
There was also the problem that i used an expseg and in the nature of exponentiell functions it will never reach the final value…So the if statement didn’t also work with that.

Yes it’s better to test if the variable goes above a certain value. Using == with floating point numbers can be a bit hit and miss.