| 1 |
Prefix expression evaluators |
| 2 |
============================= |
| 3 |
|
| 4 |
.. currentmodule:: pyo |
| 5 |
|
| 6 |
.. highlight:: none |
| 7 |
|
| 8 |
Prefix audio expression evaluator. |
| 9 |
|
| 10 |
This family implements a tiny functional programming language that |
| 11 |
can be used to write synthesis or signal processing algorithms. |
| 12 |
|
| 13 |
Objects in this category |
| 14 |
------------------------------ |
| 15 |
|
| 16 |
- :py:class:`Expr` : Prefix audio expression evaluator. |
| 17 |
|
| 18 |
**API documentation** |
| 19 |
--------------------- |
| 20 |
|
| 21 |
This API is in alpha stage and subject to future changes! |
| 22 |
|
| 23 |
Builtin functions |
| 24 |
----------------- |
| 25 |
|
| 26 |
**Arithmetic operators** |
| 27 |
|
| 28 |
- (+ x y) : returns the sum of two values. |
| 29 |
- (- x y) : substracts the second value to the first and returns the result. |
| 30 |
- (* x y) : returns the multiplication of two values. |
| 31 |
- (/ x y) : returns the quotient of x/y. |
| 32 |
- (^ x y) : returns x to the power y. |
| 33 |
- (% x y) : returns the floating-point remainder of x/y. |
| 34 |
- (neg x) : returns the negative of x. |
| 35 |
|
| 36 |
**Moving phase operators** |
| 37 |
|
| 38 |
- (++ x y) : increments its internal state by x and wrap around 0.0 and y. |
| 39 |
- (-- x y) : decrements its internal state by x and wrap around 0.0 and y. |
| 40 |
- (~ x y) : generates a periodic ramp from 0 to 1 with frequency x and phase y. |
| 41 |
|
| 42 |
**Conditional operators** |
| 43 |
|
| 44 |
- (< x y) : returns 1 if x is less than y, otherwise returns 0. |
| 45 |
- (<= x y) : returns 1 if x is less than or equal to y, otherwise returns 0. |
| 46 |
- (> x y) : returns 1 if x is greater than y, otherwise returns 0. |
| 47 |
- (>= x y) : returns 1 if x is greater than or equal to y, otherwise returns 0. |
| 48 |
- (== x y) : returns 1 if x is equal to y, otherwise returns 0. |
| 49 |
- (!= x y) : returns 1 if x is not equal to y, otherwise returns 0. |
| 50 |
- (if (cond) (then) (else)) : returns then for any non-zero value of cond, otherwise returns else. |
| 51 |
- (and x y) : returns 1 if both x and y are not 0, otherwise returns 0. |
| 52 |
- (or x y) : returns 1 if one of x or y are not 0, otherwise returns 0. |
| 53 |
|
| 54 |
**Trigonometric functions** |
| 55 |
|
| 56 |
- (sin x) : returns the sine of an angle of x radians. |
| 57 |
- (cos x) : returns the cosine of an angle of x radians. |
| 58 |
- (tan x) : returns the tangent of x radians. |
| 59 |
- (tanh x) : returns the hyperbolic tangent of x radians. |
| 60 |
- (atan x) : returns the principal value of the arc tangent of x, expressed in radians. |
| 61 |
- (atan2 x y) : returns the principal value of the arc tangent of y/x, expressed in radians. |
| 62 |
|
| 63 |
**Power and logarithmic functions** |
| 64 |
|
| 65 |
- (sqrt x) : returns the square root of x. |
| 66 |
- (log x) : returns the natural logarithm of x. |
| 67 |
- (log2 x) : returns the binary (base-2) logarithm of x. |
| 68 |
- (log10 x) : returns the common (base-10) logarithm of x. |
| 69 |
- (pow x y) : returns x to the power y. |
| 70 |
|
| 71 |
**Clipping functions** |
| 72 |
|
| 73 |
- (abs x) : returns the absolute value of x. |
| 74 |
- (floor x) : rounds x downward, returning the largest integral value that is not greater than x. |
| 75 |
- (ceil x) : rounds x upward, returning the smallest integral value that is not less than x. |
| 76 |
- (exp x) : returns the constant e to the power x. |
| 77 |
- (round x) : returns the integral value that is nearest to x. |
| 78 |
- (min x y) : returns the smaller of its arguments: either x or y. |
| 79 |
- (max x y) : returns the larger of its arguments: either x or y. |
| 80 |
- (wrap x) : wraps x between 0 and 1. |
| 81 |
|
| 82 |
**Random fuctions** |
| 83 |
|
| 84 |
- (randf x y) : returns a pseudo-random floating-point number in the range between x and y. |
| 85 |
- (randi x y) : returns a pseudo-random integral number in the range between x and y. |
| 86 |
|
| 87 |
**Complex numbers** |
| 88 |
|
| 89 |
- (complex x y) : returns a complex number where x is the real part and y the imaginary part. |
| 90 |
- (real x) : returns the real part of the complex number x. |
| 91 |
- (imag x) : returns the imaginary part of the complex number x. |
| 92 |
|
| 93 |
**Filter functions** |
| 94 |
|
| 95 |
- (delay x) : one sample delay. |
| 96 |
- (sah x y) : samples and holds x value whenever y is smaller than its previous state. |
| 97 |
- (rpole x y) : real one-pole recursive filter. returns x + last_out * y. |
| 98 |
- (rzero x y) : real one-zero non-recursive filter. returns x - last_x * y. |
| 99 |
- (cpole x y) : complex one-pole recursive filter. x is the complex signal to filter, y is a complex coefficient, it returns a complex signal. |
| 100 |
- (czero x y) : complex one-zero non-recursive filter. x is the complex signal to filter, y is a complex coefficient, it returns a complex signal. |
| 101 |
|
| 102 |
**Multi-output function** |
| 103 |
|
| 104 |
- (out x y) : If using multiple outputs, creates an audio stream where x is the output channel and y the signal to write to the channel. |
| 105 |
|
| 106 |
**Constants** |
| 107 |
|
| 108 |
- (const x) : returns x. |
| 109 |
- pi : returns an approximated value of pi. |
| 110 |
- twopi : returns a constant with value pi*2. |
| 111 |
- e : returns an approximated value of e. |
| 112 |
- sr : returns the current sampling rate. |
| 113 |
|
| 114 |
Comments |
| 115 |
-------- |
| 116 |
|
| 117 |
A comment starts with two slashs ( // ) and ends at the end of the line:: |
| 118 |
|
| 119 |
// This is a comment! |
| 120 |
|
| 121 |
Input and Output signals |
| 122 |
------------------------ |
| 123 |
|
| 124 |
User has access to the last buffer size of input and output samples. |
| 125 |
|
| 126 |
To use samples from past inputs, use $x0[n] notation, where 0 is the first |
| 127 |
audio stream ($x1[0] would retrieve the second audio stream) and n is the |
| 128 |
position from the current time. $x0[0] is the current input sample, $x0[-1] |
| 129 |
is the previous one and $x0[-buffersize] is the last available input sample. |
| 130 |
|
| 131 |
The first audio stream can also simply be named $x[0] ($x[0] is the same as |
| 132 |
$x0[0]). |
| 133 |
|
| 134 |
To use samples from past output, use $y0[n] notation, where 0 is the first |
| 135 |
output audio stream ($y1[0] would retrieve the second output audio stream) |
| 136 |
and n is the position from the current time. $y0[-1] is the previous output |
| 137 |
and $y0[-buffersize] is the last available output sample of the stream. |
| 138 |
|
| 139 |
The first output audio stream can also simply be named $y[-1] ($y[-1] is the |
| 140 |
same as $y0[-1]). |
| 141 |
|
| 142 |
If the object generates only one channel output (the default), the last |
| 143 |
expression in the script is the output signal. Otherwise, output signals must |
| 144 |
be created with the out function. |
| 145 |
|
| 146 |
Here an example of a first-order IIR lowpass filter expression:: |
| 147 |
|
| 148 |
// A first-order IIR lowpass filter |
| 149 |
+ $x[0] (* (- $y[-1] $x[0]) 0.99) |
| 150 |
|
| 151 |
A simple ring-modulation expression:: |
| 152 |
|
| 153 |
// ring-modulation between the first two input signals |
| 154 |
* $x[0] $x1[0] |
| 155 |
|
| 156 |
Two output channels:: |
| 157 |
|
| 158 |
// First channel |
| 159 |
(out 0 (sin (* (twopi) (~ 400)))) |
| 160 |
// Second channel |
| 161 |
(out 1 (sin (* (twopi) (~ 500)))) |
| 162 |
|
| 163 |
Defining custom functions |
| 164 |
------------------------- |
| 165 |
|
| 166 |
The `define` keyword starts the definition of a custom function:: |
| 167 |
|
| 168 |
(define funcname (body)) |
| 169 |
|
| 170 |
funcname is the name used to call the function in the expression and body |
| 171 |
is the sequence of functions to execute. Arguments of the function are |
| 172 |
extracted directly from the body. They must be named $1, $2, $3, ..., $9. |
| 173 |
|
| 174 |
Example of a sine wave function:: |
| 175 |
|
| 176 |
(define osc ( |
| 177 |
sin (* (twopi) (~ $1)) |
| 178 |
) |
| 179 |
) |
| 180 |
// play a sine wave |
| 181 |
* (osc 440) 0.3 |
| 182 |
|
| 183 |
|
| 184 |
State variables |
| 185 |
--------------- |
| 186 |
|
| 187 |
User can create state variable with the keyword `let`. This is useful |
| 188 |
to set an intermediate state to be used in multiple places in the |
| 189 |
processing chain. The syntax is:: |
| 190 |
|
| 191 |
(let #var (body)) |
| 192 |
|
| 193 |
The variable name must begin with a `#`:: |
| 194 |
|
| 195 |
(let #sr 44100) |
| 196 |
(let #freq 1000) |
| 197 |
(let #coeff ( |
| 198 |
^ (e) (/ (* (* -2 (pi)) #freq) #sr) |
| 199 |
) |
| 200 |
) |
| 201 |
+ $x[0] (* (- $y[-1] $x[0]) #coeff) |
| 202 |
|
| 203 |
The variable is private to a function if created inside a custom function:: |
| 204 |
|
| 205 |
(let #freq 250) // global #freq variable |
| 206 |
(define osc ( |
| 207 |
(let #freq (* $1 $2)) // local #freq variable |
| 208 |
sin (* (twopi) (~ #freq)) |
| 209 |
) |
| 210 |
) |
| 211 |
* (+ (osc 1 #freq) (osc 2 #freq)) 0.2 |
| 212 |
|
| 213 |
State variables can be used to do 1 sample feedback if used before created. |
| 214 |
Undefined variables are initialized to 0:: |
| 215 |
|
| 216 |
(define oscloop ( |
| 217 |
(let #xsin |
| 218 |
(sin (+ (* (~ $1) (twopi)) (* #xsin $2))) // #xsin used before... |
| 219 |
) // ... "let" statement finished! |
| 220 |
#xsin // oscloop function outputs #xsin variable |
| 221 |
) |
| 222 |
) |
| 223 |
* (oscloop 200 0.7) 0.3 |
| 224 |
|
| 225 |
An optional third argument, as a float, allow to set the initial value of the variable. |
| 226 |
|
| 227 |
(let #x (+ #x (* #dx #delta)), 1.0) // #x is used before defined, initial value = 1.0. |
| 228 |
|
| 229 |
A state variable can also contain a complex number:: |
| 230 |
|
| 231 |
(let #v (complex 0.2 0.7)) // #v = (0.2 0.7) |
| 232 |
|
| 233 |
User variables |
| 234 |
-------------- |
| 235 |
|
| 236 |
User variables are created with the keyword `var`:: |
| 237 |
|
| 238 |
(var #var (init)) |
| 239 |
|
| 240 |
The variable name must begin with a `#`. |
| 241 |
|
| 242 |
They are computed only at initialization, but can be changed from the python |
| 243 |
script with method calls (varname is a string and value is a float):: |
| 244 |
|
| 245 |
obj.setVar(varname, value) |
| 246 |
|
| 247 |
The following example shows how to control the cutoff fequency of a lowpass |
| 248 |
filter with a user variable:: |
| 249 |
|
| 250 |
expression = ''' |
| 251 |
(var #freq 1000) |
| 252 |
(let #coeff ( |
| 253 |
^ e (/ (* (neg twopi) #freq) 44100) |
| 254 |
) |
| 255 |
) |
| 256 |
+ $x[0] (* (- $y[-1] $x[0]) #coeff) |
| 257 |
''' |
| 258 |
ex = Expr(Noise(0.5), expression).out() |
| 259 |
freq = Sine(0.25).range(250, 10000) |
| 260 |
|
| 261 |
def update(): |
| 262 |
ex.setVar("#freq", freq.get()) |
| 263 |
|
| 264 |
pat = Pattern(update, 0.02).play() |
| 265 |
|
| 266 |
Library importation |
| 267 |
------------------- |
| 268 |
|
| 269 |
Custom functions can be defined in an external file and imported with the |
| 270 |
`load` function:: |
| 271 |
|
| 272 |
(load path/to/the/file) |
| 273 |
|
| 274 |
The content of the file will be inserted where the load function is called |
| 275 |
and all functions defined inside the file will then be accessible. The path |
| 276 |
can be absolute or relative to the current working directory. |
| 277 |
|
| 278 |
Complex numbers |
| 279 |
--------------- |
| 280 |
|
| 281 |
A complex number is created with the `complex` function:: |
| 282 |
|
| 283 |
(complex x y) |
| 284 |
|
| 285 |
We can retrieve one part of a complex number with `real` and `imag` functions:: |
| 286 |
|
| 287 |
// get the real part (x) of a number |
| 288 |
(real (complex x y)) |
| 289 |
|
| 290 |
If a complex number is used somewhere not waiting for a complex, real value |
| 291 |
will be used. |
| 292 |
|
| 293 |
If a real number is used somewhere waiting for a complex, the imaginary part |
| 294 |
is set to 0.0. |
| 295 |
|
| 296 |
Examples |
| 297 |
-------- |
| 298 |
|
| 299 |
Here is some expression examples. |
| 300 |
|
| 301 |
A first-order IIR lowpass filter:: |
| 302 |
|
| 303 |
(var #sr 44100) |
| 304 |
(var #cutoff 1000) |
| 305 |
(let #coeff (exp (/ (* (* -2 (pi)) #cutoff) #sr))) |
| 306 |
+ $x[0] (* (- $y[-1] $x[0]) #coeff) |
| 307 |
|
| 308 |
A LFO'ed hyperbolic tangent distortion:: |
| 309 |
|
| 310 |
// $1 = lfo frequency, $2 = lfo depth |
| 311 |
(define lfo ( |
| 312 |
(+ (* (sin (* (twopi) (~ $1))) (- $2 1)) $2) |
| 313 |
) |
| 314 |
) |
| 315 |
tanh (* $x[0] (lfo .25 10)) |
| 316 |
|
| 317 |
A triangle waveform generator (use Sig(0) as input argument to bypass input):: |
| 318 |
|
| 319 |
(var #freq 440) |
| 320 |
// $1 = oscillator frequency |
| 321 |
(define triangle ( |
| 322 |
(let #ph (~ $1)) |
| 323 |
(- (* (min #ph (- 1 #ph)) 4) 1) |
| 324 |
) |
| 325 |
) |
| 326 |
triangle #freq |
| 327 |
|
| 328 |
.. highlight:: python |
| 329 |
|
| 330 |
**Objects** |
| 331 |
----------- |
| 332 |
|
| 333 |
*Expr* |
| 334 |
---------- |
| 335 |
|
| 336 |
.. autoclass:: Expr |
| 337 |
:members: |
| 338 |
|
| 339 |
.. autoclasstoc:: |