| 1 |
10-arithmetic-ops.py - Using arithmetic with event generators. |
| 2 |
============================================================================================================================================ |
| 3 |
|
| 4 |
|
| 5 |
Arithmetic operators can be used with EventGenerator objects to control |
| 6 |
their behaviour (and change the pattern of the sequence of values). |
| 7 |
|
| 8 |
Available operators are: |
| 9 |
|
| 10 |
`+` : addition |
| 11 |
|
| 12 |
`-` : substraction |
| 13 |
|
| 14 |
`*` : multiplication |
| 15 |
|
| 16 |
`/` : division |
| 17 |
|
| 18 |
`**`: exponent |
| 19 |
|
| 20 |
`%` : modulo (remaining of the division) |
| 21 |
|
| 22 |
`//`: quantizer (returns te nearest multiple of its argument) |
| 23 |
|
| 24 |
Arithmetic can be done with event generators as both operand and operator. |
| 25 |
|
| 26 |
.. code-block:: python |
| 27 |
|
| 28 |
from pyo import * |
| 29 |
|
| 30 |
s = Server().boot() |
| 31 |
|
| 32 |
# Half-semitone scale (the octave is divided in 24 equally-spaced steps). |
| 33 |
# The integer (a semitone in midi note) is divided by 2, which mean that |
| 34 |
# there are two values inside a single integer. |
| 35 |
e = Events( |
| 36 |
midinote=EventDrunk(list(range(48)), maxStep=-2) / 2.0 + 72, |
| 37 |
beat=1 / 4.0, |
| 38 |
db=-12, |
| 39 |
attack=0.001, |
| 40 |
decay=0.05, |
| 41 |
sustain=0.5, |
| 42 |
release=0.005, |
| 43 |
).play() |
| 44 |
|
| 45 |
# Quantize to multiples of a minor third. |
| 46 |
e2 = Events( |
| 47 |
midinote=EventCall(random.uniform, 60, 72) // 3, |
| 48 |
beat=1 / 4.0, |
| 49 |
db=-12, |
| 50 |
durmul=1, |
| 51 |
attack=0.001, |
| 52 |
decay=0.05, |
| 53 |
sustain=0.5, |
| 54 |
release=0.005, |
| 55 |
).play() |
| 56 |
|
| 57 |
# EventGenerator as arithmetic operator. Kind of ostinato. |
| 58 |
e3 = Events( |
| 59 |
midinote=EventSeq([48, 50, 52]) - EventSeq([0, 2, 4, 6]), |
| 60 |
beat=1 / 4.0, |
| 61 |
db=-12, |
| 62 |
durmul=1, |
| 63 |
attack=0.001, |
| 64 |
decay=0.05, |
| 65 |
sustain=0.5, |
| 66 |
release=0.005, |
| 67 |
).play() |
| 68 |
|
| 69 |
s.gui(locals()) |
| 70 |
|