Cabbage Logo
Back to Cabbage Site

How is this not an infinite loop?

Alright, I got two widgets locked together, and for the life of me I can’t understand why this isn’t an infinite loop, where one widget changes, causing the other widget to change, which in turn causes the other widget…

<Cabbage>
form caption("Untitled") size(400, 300), guiMode("queue"), pluginId("def1")

rslider bounds(20, 20, 50, 50) range(0, 10, 1, 1, 0.1) channel("mySlider")
nslider bounds(100, 20, 100, 24) range(0, 10, 1, 1, 0.1) channel("myBox")

keyboard bounds(8, 158, 381, 95)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d -+rtmidi=NULL -M0 -m0d --midi-key-cps=4 --midi-velocity-amp=5
</CsOptions>
<CsInstruments>
; Initialize the global variables. 
ksmps = 32
nchnls = 2
0dbfs = 1

;instrument will be triggered by keyboard widget
instr 1
kEnv madsr .1, .2, .6, .4
aOut vco2 p5, p4
outs aOut*kEnv, aOut*kEnv
endin

instr 2
    kMySlider, kMySliderTrig cabbageGetValue "mySlider"
    kMyBox, kMyBoxTrig cabbageGetValue "myBox"
    
    if kMySliderTrig == 1 then
        cabbageSetValue "myBox", kMySlider
        printsk "slider %f\n", kMySlider
    endif
    if kMyBoxTrig == 1 then
        cabbageSetValue "mySlider", kMyBox
        printsk "box %f\n", kMySlider
    endif
endin

</CsInstruments>
<CsScore>
;causes Csound to run for about 7000 years...
i 2 0 -1
f0 z
</CsScore>
</CsoundSynthesizer>

Does this have something to do with the value not actually changing once it is set in the other widget?

Im at my phone, but I think you need to add the trigger variable to the cabbageSetValue opcodes. If theres no trigger variable used, it will run at i-time instead of k-time

The cabbageSetValue opcodes can work without a trigger, it’s the cabbageSet opcodes that require this to work at k-rate.

As for why it’s not an infinite loop, is it not because you are setting the two widgets to have the same value, box get slide’s value, and slider the value the of the box. So as soon as you stop moving the slider no more changes will be registered? If you really want an infinite loop, do this:

instr 2
    kMySlider, kMySliderTrig cabbageGetValue "mySlider"
    kMyBox, kMyBoxTrig cabbageGetValue "myBox"
    
    if kMySliderTrig == 1 then
        cabbageSetValue "myBox", kMySlider
        printsk "slider %f\n", kMySlider
    endif
    if kMyBoxTrig == 1 then
        cabbageSetValue "mySlider", kMyBox+1
        printsk "box %f\n", kMySlider
    endif
endin

It also would appear that if these two widgets don’t actually share the same value, but different representations, them you will get an infinite loop! So we long as there are no calculations made on the data, it should be fine.