1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
"""
State variable inside an expression
**04-let-function.py**
User can create state variable with the keyword `let`. This is
useful to set an intermediate state to be used in multiple places
in the processing chain. The syntax is:
.. code-block:: scheme
(let #var (body))
The variable name must begin with a `#`.
The prefix expression `documentation`_ gives common use cases where we
may want to use state variables.
.. _documentation: http://ajaxsoundstudio.com/pyodoc/api/classes/expression.html
The following example implements a phase-aligned formant (PAF) generator, as
described by Miller Puckette in "The Theory and Technique of Electronic
Music", p158.
"""
from pyo import *
s = Server().boot()
expression = """// PAF generator (single formant) //
// User variables
(let #fund 100) // fundamental frequency
(let #band 600) // formant bandwidth
(let #form 600) // formant center frequency
// Intermediate variables
(let #bw (/ #band #fund))
(let #cf (/ #form #fund))
(let #ph (* (- (~ #fund) 0.5) twopi))
(let #car ( // Carrier
(cos (max (min (* #ph #bw) pi) (- 0 pi)))
)
)
(let #mod ( // Modulator
(let #oct (floor #cf))
(let #frac (% #cf 1))
(+ (* (cos (* #ph #oct)) (- 1 #frac))
(* (cos (* #ph (+ #oct 1))) #frac)
)
)
)
* (* #car #mod) 0.5
"""
expr = Expr(Sig(0), expression, mul=0.5)
expr.editor()
sp = Spectrum(expr)
pan = Pan(expr).out()
s.gui(locals())
|