syncphasor

syncphasor — Produces a normalized moving phase value with sync input and output.

Description

Produces a moving phase value between zero and one and an extra impulse output ("sync out") whenever its phase value crosses or is reset to zero. The phase can be reset at any time by an impulse on the "sync in" parameter.

Syntax

aphase, asyncout syncphasor xcps, asyncin, [, iphs]

Initialization

iphs (optional) -- initial phase, expressed as a fraction of a cycle (0 to 1). A negative value will cause phase initialization to be skipped. The default value is zero.

Performance

aphase -- the output phase value; always between 0 and 1.

asyncout -- the sync output has a value of 1.0 for one sample whenever the phase value crosses zero or whenever the sync input is non-zero. It is zero at all other times.

asyncin -- the sync input causes the phase to reset to zero whenever asyncin is non-zero.

xcps -- frequency of the phasor in cycles-per-second. If xcps is negative, the phase value will decrease from 1 to 0 instead of increasing.

An internal phase is successively accumulated in accordance with the xcps frequency to produce a moving phase value, normalized to lie in the range 0 <= phs < 1. When used as the index to a table unit, this phase (multiplied by the desired function table length) will cause it to behave like an oscillator.

The phase of syncphasor though can be synced to another phasor (or other signal) using the asyncin parameter. Any time that asyncin is a non-zero value, the value of aphase will be reset to zero. syncphasor also outputs its own "sync" signal that consists of a one-sample impulse whenever its phase crosses zero or is reset. This makes it easy to chain together multiple syncphasor opcodes to create an oscillator "hard sync" effect.

Examples

Here is an example of the syncphasor opcode. It uses the file syncphasor.csd.

Example 892. Example of the syncphasor opcode.

See the sections Real-time Audio and Command Line Flags for more information on using command line flags.

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in
-odac           -iadc    ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o abs.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>

instr 1
    ; Use two syncphasors - one is the "master",
    ; the other the "slave"
    
    ; master's frequency determines pitch
    imastercps  =           cpspch(p4)
    imaxamp     =           10000

    ; the slave's frequency affects the timbre 
    kslavecps   line        imastercps, p3, imastercps * 3
    
    ; the master "oscillator"
    ; the master has no sync input 
    anosync     init        0.0
    am, async   syncphasor  imastercps, anosync
    
    ; the slave "oscillator"
    aout, as    syncphasor  kslavecps, async
    
    adeclick    linseg      0.0, 0.05, 1.0, p3 - 0.1, 1.0, 0.05, 0.0
    
    ; Output the slave's phase value which is a rising
    ; sawtooth wave.  This produces aliasing, but hey, this
    ; this is just an example ;)
    
              out         aout * adeclick * imaxamp
endin

</CsInstruments>
<CsScore>

i1 0 1    7.00
i1 + 0.5  7.02
i1 + .    7.05
i1 + .    7.07
i1 + .    7.09
i1 + 2    7.06

e

</CsScore>
</CsoundSynthesizer>


Here is another example of the syncphasor opcode. It uses the file syncphasor-CZresonance.csd.

Example 893. Another example of the syncphasor opcode.

<CsoundSynthesizer>
<CsOptions>
; Select audio/midi flags here according to platform
; Audio out   Audio in    No messages
-odac           -iadc     -d     ;;;RT audio I/O
; For Non-realtime ouput leave only the line below:
; -o syncphasor-CZresonance.wav -W ;;; for file output any platform
</CsOptions>
<CsInstruments>
; by Anthony Kozar. February 2008
; http://www.anthonykozar.net/

; Imitation of the Casio CZ-series synthesizer's "Resonance" waveforms
; using a synced phasor to read a sinusoid table.  The jumps at the sync 
; points are smoothed by multiplying with a windowing function controlled
; by the master phasor.

; Based on information from the Wikipedia article on phase distortion:
; http://en.wikipedia.org/wiki/Phase_distortion_synthesis

; Sawtooth Resonance waveform. Smoothing function is just the inverted
; master phasor.

; The Wikipedia article shows an inverted cosine as the stored waveform,
; which implies that it must be unipolar for the smoothing to work.
; I have substituted a sine wave in the first phrase to keep the output
; bipolar.  The second phrase demonstrates the much "rezzier" sound of the
; bipolar cosine due to discontinuities.

instr 1
  ifreq      =           cpspch(p4)
  initReson  =           p5
  itable     =           p6
  imaxamp    =           10000
  anosync    init        0.0

  kslavecps       line        ifreq * initReson, p3, ifreq
  amaster, async  syncphasor  ifreq, anosync		; pair of phasors
  aslave, async2  syncphasor  kslavecps, async		; slave synced to master
  aosc            tablei      aslave, itable, 1		; use slave phasor to read a (co)sine table
  aout            =           aosc * (1.0 - amaster)	; inverted master smoothes jumps 
  adeclick        linseg      0.0, 0.05, 1.0, p3 - 0.1, 1.0, 0.05, 0.0

                    out         aout * adeclick * imaxamp
endin

; Triangle or Trapezoidal Resonance waveform. Uses a second table to change
; the shape of the smoothing function.  (This is my best guess so far as to
; how these worked). The cosine table works fine with the triangular smoothing
; but we once again need to use a sine table with the trapezoidal smoothing.

; (It might be interesting to be able to vary the "width" of the trapezoid.
; This could be done with the pdhalf opcode).

instr 2
  ifreq      =           cpspch(p4)
  initReson  =           p5
  itable     =           p6
  ismoothtbl =           p7
  imaxamp    =           10000
  anosync    init        0.0

  kslavecps       line        ifreq * initReson, p3, ifreq
  amaster, async  syncphasor  ifreq, anosync		; pair of phasors
  aslave, async2  syncphasor  kslavecps, async		; slave synced to master
  aosc            tablei      aslave, itable, 1		; use slave phasor to read a (co)sine table
  asmooth         tablei      amaster, ismoothtbl, 1	; use master phasor to read smoothing table
  aout            =           aosc * asmooth
  adeclick        linseg      0.0, 0.05, 1.0, p3 - 0.1, 1.0, 0.05, 0.0

  out         aout * adeclick * imaxamp
endin

</CsInstruments>
<CsScore>
f1 0 16385 10  1
f3 0 16385  9  1 1 270			; inverted cosine
f5 0 4097   7  0.0 2048 1.0 2049 0.0	; unipolar triangle
f6 0 4097   7  1.0 2048 1.0 2049 0.0    ; "trapezoid"

; Sawtooth resonance with a sine table
i1 0 1    7.00  5.0  1
i. + 0.5  7.02  4.0
i. + .    7.05  3.0
i. + .    7.07  2.0
i. + .    7.09  1.0
i. + 2    7.06  12.0
f0 6
s

; Sawtooth resonance with a cosine table
i1 0 1    7.00  5.0  3
i. + 0.5  7.02  4.0
i. + .    7.05  3.0
i. + .    7.07  2.0
i. + .    7.09  1.0
i. + 2    7.06  12.0
f0 6
s

; Triangle resonance with a cosine table
i2 0 1    7.00  5.0  3  5
i. + 0.5  7.02  4.0
i. + .    7.05  3.0
i. + .    7.07  2.0
i. + .    7.09  1.0
i. + 2    7.06  12.0
f0 6
s

; Trapezoidal resonance with a sine table
i2 0 1    7.00  5.0  1  6
i. + 0.5  7.02  4.0
i. + .    7.05  3.0
i. + .    7.07  2.0
i. + .    7.09  1.0
i. + 2    7.06  12.0

e

</CsScore>
</CsoundSynthesizer>


See also

phasor.

And the Table Access opcodes like: table, tablei, table3 and tab.

Credits

Adapted from the phasor opcode by Anthony Kozar
January 2008

New in Csound version 5.08