User-Defined Opcode Database

PhsEdge

Reports whether the upper or lower border of a phasor has been reached.

Download UDO File

Description

Reports whether a phasor has reached its upper border 1 when running with positive frequency (forwards), or its lower border 0 when running with negative frequency (backwards)

Syntax

kend PhsEdge kphs, kfq, ktrig [, iphs]

Initialization

iphs - starting phase of the phasor (default=0)

Performance

kphs - phasor output (range 0-1)
kfq - phasor frequency
ktrig - 1 for start, any other number bypasses
kend - returns '1' just once in a k-cycle whenever the upper or lower border has been reached (see examples)

Code

  opcode PhsEdge, k, kkko
kphs, kfq, ktrig, iphs xin 
kfirst		init		1 ;don't check in first k-cycle
if ktrig == 1 then ;don't do anything if not turned on
 if kfirst == 1 then ;first k-cycle
;set previous phase value to 1 if negative frequency and phasor starting from the beginning (for avoiding kend=1 immediately after start), otherwise previous phase is set to 0
kprevphs	=		(kfq < 0 && iphs == 0 ? 1 : iphs)
kfirst		=		0 ;end of first cycle 
 else ;after first k-cycle
  ;for positive speed, check if this index is lower than the previous one
  if kfq > 0 && kphs < kprevphs then
kend		=		1
 ;for negative speed, check if this index is higher than the previous one
  elseif kfq < 0 && kphs > kprevphs then
kend		=		1
  else ;no end reached
kend		=		0
  endif
kprevphs	=		kphs ;next previous is this phase
 endif
else ;bypass if no trigger
kend		=		0
endif
		xout		kend
  endop

Examples

<CsoundSynthesizer>
<CsOptions>
-n 
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 32
nchnls = 1
0dbfs = 1

seed 0

  opcode PhsEdge, k, kkko
;reports whether a phasor has reached one of its edges  
kphs, kfq, ktrig, iphs xin ;phasor output, its frequency, trigger for start and optional starting phase
kfirst		init		1 ;don't check in first k-cycle
if ktrig == 1 then ;don't do anything if not turned on
 if kfirst == 1 then ;first k-cycle
;set previous phase value to 1 if negative frequency and phasor starting from the beginning (for avoiding kend=1 immediately after start), otherwise previous phase is set to 0
kprevphs	=		(kfq < 0 && iphs == 0 ? 1 : iphs)
kfirst		=		0 ;end of first cycle 
 else ;after first k-cycle
  ;for positive speed, check if this index is lower than the previous one
  if kfq > 0 && kphs < kprevphs then
kend		=		1
 ;for negative speed, check if this index is higher than the previous one
  elseif kfq < 0 && kphs > kprevphs then
kend		=		1
  else ;no end reached
kend		=		0
  endif
kprevphs	=		kphs ;next previous is this phase
 endif
else ;bypass if no trigger
kend		=		0
endif
		xout		kend
  endop

instr 1 ;basic tests
kfq		=		p4
iphs		=		p5
kphas		phasor		kfq, iphs
kend	 	PhsEdge	kphas, kfq, 1, iphs
 if kend == 1 then
printk 0, kend
 endif
endin

instr 2 ;stops after soundfile has been played once either direction and with variable speed
ktrig		init		0 ;necessary for resetting the trigger at second call
ift		ftgen		0, 0, -121569, 1, "fox.wav", 0, 0, 1
kfq		randomi	p4, p5, 2
kfq		=		kfq/(ftlen(ift)/sr)
kphas		phasor		kfq
aphas		interp		kphas
asound		table3		aphas, ift, 1, 0, 1
		out		asound
kend		PhsEdge	kphas, kfq, ktrig
 if kend == 1 then
		printks	"Edge Reached!\n", 0
		turnoff
 endif
ktrig		=		1
endin
</CsInstruments>
<CsScore>
i 1 0 5 1 0
i . + . -1 0
i . + . 1 .5
i . + . -1 .5
i . + . -.25 0
i 2 25 10 -1 -.25
i . + . .25 2
</CsScore>
</CsoundSynthesizer>

Credits

joachim heintz 2010


Previous Home Next
BufRec2   stereoWidth