User-Defined Opcode Database

Stayed

Reports if a control signal has stayed at the same value for a certain time.

Download UDO File

Description

Outputs 1 if kin has not changed since at least ksec seconds, otherwise outputs 0.

Syntax

kout Stayed kin, ksec

Performance

kin - input signal
ksec - time in seconds for which kin is asked to have the same value or not
kout - 1 if the value has not changed since at least ksec seconds, otherwise 0

Code

  opcode Stayed, k, kk
kin, ksec	xin
kout		init		0
knumk		=		ksec * kr ;number of control cycles for ksec
kinit		init		1
kcount		init		0
 if kinit == 1 then		;just once, at the beginning
kprevious	=		kin
kinit		=		0
 endif
 if kin == kprevious then
kcount		=		kcount + 1
 else
kcount		=		0
kprevious	=		kin
 endif
 if kcount > knumk then
kout		=		1
 else
kout		=		0
 endif
		xout		kout
  endop

Examples

<CsoundSynthesizer>
<CsInstruments>
ksmps = 4410 ;10 control cycles per second

  opcode Stayed, k, kk
;outputs 1 if kin has not changed since ksec seconds, otherwise outputs 0
kin, ksec	xin
kout		init		0
knumk		=		ksec * kr ;number of control cycles for ksec
kinit		init		1
kcount		init		0
 if kinit == 1 then		;just once, at the beginning
kprevious	=		kin
kinit		=		0
 endif
 if kin == kprevious then
kcount		=		kcount + 1
 else
kcount		=		0
kprevious	=		kin
 endif
 if kcount > knumk then
kout		=		1
 else
kout		=		0
 endif
		xout		kout
  endop

  
  instr 1
kval		randh		10, 1, 2 ;new value once per second
kout		Stayed		kval, .75 ;outputs 1 if kval has not changed since 0.75 seconds
 		printk 	0, kval
 		printk 	0, kout, 10
  endin
</CsInstruments>
<CsScore>
i 1 0 5
</CsScore>
</CsoundSynthesizer>

Credits

joachim heintz 2010


Previous Home Next
Scala   ADSD