accum

Abstract

Simple accumulator of scalar values

Description

accum can be used together with changed, changed2, metro, etc, to convert a binary trigger to an incremental one. Incremental triggers are used by many opcodes (printf, for example), so by doing accum(changed(kvar)) it is possible to use binary triggers wherever an incremental trigger is expected. accum outputs its current value and increments it afterwords.

Syntax

kout accum kstep, initial=0, kreset=0
aout accum kstep, initial=0, kreset=0

Arguments

  • kstep: the step to add. This value will be added at each iteration (at each k-cycle for accum:k and at each sample for accum:a)
  • initial: initial value of the accumulator
  • kreset: if 1, the accummulator is reset to the initial value

Output

  • kout: accumulated value

Execution Time

  • Init

Examples

kout accum 1, 0    ; outputs 0, 1, 2, 3, 4...

; Play a sample with variable speed, stop the event when finished
aindex accum 1
kspeed = linseg:k(0.5, ilen, 2)
ilen = ftlen(ift)
aindex *= kspeed
asig table3 aindex, ift
if aindex[0] >= ilen - (ksmps*kspeed) then
    turnoff
endif
ifade = 1/ksmps
out asig * linsegr(0, ifade, 1, ifade, 0)
<CsoundSynthesizer>
<CsOptions>
--nosound
</CsOptions>

<CsInstruments>

instr 1
  kx linseg 0, p3, 1
  printf "kx=%f \n", accum(changed(kx)), kx
  ; the same without accum would only print the first time,
  ; since changed would return always 1 but printf expects an ever
  ; increasing trigger
endin

</CsInstruments>
<CsScore>

i1 0 0.1

</CsScore>
</CsoundSynthesizer>

See also

Credits

Eduardo Moguillansky, 2019