Cabbage Logo
Back to Cabbage Site

Counters / timers and initialization in UDO

Let’s say there is an opcode which runs after a k-rate conditional testing.
Inside the opcode there is a time based k-rate parameter that should renew each time running the opcode, but doesn’t.

From the floss manual (and trying) I understood:

k init 0 (only 0 from the start and lost afterward)
k = 0 (k is 0 all the time, so nothing really happens)

Really, how do you renew a continuously changing variable like a counter (inside UDO) ?

I’m curious.

You can just put k1 = 0 inside a block of code that will only get executed under the right conditions, i.e:

if k1 == 100 then
    k1 = 0
endif

This is it in a nutshell :

opcode setDecay k, k
k1 xin

kCount = 0

while k1 == 100 do
kCount += .001
od

ky = exp(-kCount)

xout ky
endop


if k1 == 100 then

ky setDecay k1

endif

Now, it crashes half of the time and the other time it doesn’t react.

Can you post the full code, and format it using the </> button in the editor. I’m not sure what you’re trying to do here, but in the example you post, if k1 is 100 you will probably enter an infinite loop, which should bring about a crash…

Well, the entire code is about 3000 lines, but I think you’ll get the giste in this shortened example.
The problem remains the same.

<Cabbage>

form caption(“JEIZZMEISTER”) size(1280, 820), guiMode(“queue”), pluginID(“jm12”), colour(127,127,127)
button bounds(560, 634, 58, 11), channel(“string1fret1”), text(“string1fret1-0”, “string1fret1-1”), latched(0)
image bounds(0, 500, 1042, 14) channel(“normalStringPickFretBoardE”), file(“string highlight green.png”), visible(0)



; Select audio/midi flags here according to platform
; Audio out Audio in No messages
-odac -iadc -d ;;;RT audio I/O

sr=44100
kr=441
ksmps=100
nchnls=2

opcode setStringFBBody, 0, kkk
kS, kSTrig, kTrigS xin

kCount init 0

while kS > 0 || kSTrig > 0 do
kCount += .001
od

kTrig metro 27.5

cabbageSet kTrigS, “normalStringPickFretBoardE”, sprintfk(“visible(”%.02f")", 1)
cabbageSet kTrig, “normalStringPickFretBoardE”, “alpha”, exp(-kCount)
cabbageSet kTrig, “normalStringPickFretBoardE”, “alpha”, exp(-kCount)

endop

instr 1

kS1F1 cabbageGetValue “string1fret1”

idur = 1
kS1 = kS1F1

kTrigS1 changed kS1F1

kSTrig1 trigger kS1, 1, 1
kSTrig1 trighold kSTrig1, idur
kSTrig12 trigger kSTrig1, 1, 1

if kS1 > 0 || kSTrig1 > 0 then
setStringFBBody kS1, kSTrig1, kTrigS1
endif

if kS1 == 0 then
cabbageSet kSTrig12, “normalStringPickFretBoardE”, sprintfk(“visible(”%.02f")", 0)
endif

endin

f0 z i1 0 z [aaaa.csd|attachment](upload://kg3TzhN8YjwQQQmzjUP6PcyGPvM.csd) (1.5 KB) ![string highlight green|690x388](upload://9HvS4zogzngH9MnRshDicNJIjPl.png)

Can you format the code?

aaaa.csd (1.5 KB)

If you use while you will enter into an infinite loop whenever the condition is true. Should you not be using if?

It works both ways, but the counter doesn’t reinitialize whatsoever with either = 0 or init 0.
With init, it continues where it left off last time; and with eaquals, it doesn’t do the incrementing at k-rate.

I can’t really follow your code here, but if all you wish to do is reset the counter on each click, you can just check if one of the input vars has changed value. I stripped down your code to remove the bits I didn’t get:

<Cabbage>
form caption("JEIZZMEISTER") size(1280, 820),  guiMode("queue"), pluginID("jm12"), colour(127,127,127)
button bounds(560, 634, 58, 11), channel("string1fret1"), text("string1fret1-0", "string1fret1-1"), latched(0)
image bounds(0, 500, 1042, 14) channel("normalStringPickFretBoardE"), file("string highlight green.png"), visible(0)
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in    No messages
-odac           -iadc     -d     ;;;RT audio I/O
</CsOptions>
<CsInstruments>

sr=44100
kr=441
ksmps=100
nchnls=2

opcode setStringFBBody, k, k
    kSTrig xin 
    kCount init 0
    kOut init 0
   
    if kSTrig > 0 then
        kCount += .001
        kOut = kCount
    endif
   
    if changed(kSTrig) == 1 && kSTrig == 0 then
        kCount = 0
    endif
    
    xout kOut
      
endop

instr 1
	
    kS1F1 cabbageGetValue "string1fret1"
    kSTrig1 trighold kS1F1, 1
    printk2 setStringFBBody(kSTrig1)
    

endin

</CsInstruments>
<CsScore>
f0 z
i1 0 z

</CsScore>
</CsoundSynthesizer>

p.s. when you paste in Csound code, select it and hit the </> but in the text editor controls to format it…

1 Like

Sussed it.
Finally.
Strange though it doesn’t do the incrementing when including kCount = 0 beforehand.

No, because if you do kCount = 0 it will set kCount to 0 on every k-cycle.

I see