User-Defined Opcode Database

Counter

Step counter

Download UDO File

Description

Counts steps upwards or downwards, whenever a trigger signal has been received. This is meant to be used in live interaction, and is simliar to counter objects in realtime programs like Max or Pd. The example shows how the basic function can be extended to repeat sequences in a certain range.

Syntax

kcount Counter kup, kdown [, istep [, istart]]

Initialization

istep - step size (default = 1)
istart - starting value (default = 0)

Performance

kup - counts upwards when 1
kdown - counts downwards when 1
kcount - current count as output

Code

  opcode Counter, k, kkio
kup, kdown, istep, istart xin
kcount    init      istart
kchange   changed   kup, kdown
if kchange == 1 then
 if kup == 1 then
kcount    =         kcount+istep
 elseif kdown == 1 then	
kcount    =         kcount-istep
 endif
endif
          xout      kcount
  endop

Examples

<CsoundSynthesizer>
<CsOptions>
-odac 
</CsOptions>
<CsInstruments>

  opcode Counter, k, kkio
;adds (subtracts) istep to the current value (beginning at istart) whenever kup (kdown) is 1
kup, kdown, istep, istart xin
kcount    init      istart
kchange   changed   kup, kdown
if kchange == 1 then
 if kup == 1 then
kcount    =         kcount+istep
 elseif kdown == 1 then	
kcount    =         kcount-istep
 endif
endif
          xout      kcount
  endop
  
  opcode KeyOnce, kk, kkk
;returns '1' just in the k-cycle a certain key has been pressed (kdown) or released (kup)
key, kd, kascii    xin ;sensekey output and ascii code of the key (e.g. 32 for space)
knew      changed   key
kdown     =         (key == kascii && knew == 1 && kd == 1 ? 1 : 0)
kup       =         (key == kascii && knew == 1 && kd == 0 ? 1 : 0)
          xout      kdown, kup
  endop
  

instr 1 ;counts up or down for the "+" or "-" key
;can be used either endless (kcount) or in a step sequencer (kseq)
iseq      =         7 ;how many steps to use in a sequencer
key,kd    sensekey
kup,k0    KeyOnce   key, kd, 43
kdown,k0  KeyOnce   key, kd, 45
kcount    Counter   kup, kdown, 1, 0
kseq      =         kcount % iseq ;sequencer 1-7
kseq      =         (kseq > 0 ? kseq : iseq+kseq)
knew      changed   kcount
 if knew == 1 then
          printks   "kcount = %.0f, kseq = %.0f%n", 0, kcount, kseq
 endif
endin

</CsInstruments>
<CsScore>
i 1 0 1000
</CsScore>
</CsoundSynthesizer>

Credits

joachim heintz 2011


Previous Home Next
trackerSplice   Y_x4p_i