| 1 |
""" |
| 2 |
01-periodic-calls.py - Periodic event generation. |
| 3 |
|
| 4 |
When an audio thread is running, the user must take care of |
| 5 |
not freezing the interpreter lock by using blocking function |
| 6 |
like sleep(). It is better to use objects from the library |
| 7 |
that are designed to call a function without blocking the |
| 8 |
interpreter. Because these objects are aware of the server's |
| 9 |
timeline, they are well suited for generating rhythmic |
| 10 |
sequences of functions calls. |
| 11 |
|
| 12 |
Pyo offers three objects for calling function: |
| 13 |
|
| 14 |
- Pattern: Periodically calls a Python function. |
| 15 |
- Score: Calls functions by incrementation of a preformatted name. |
| 16 |
- CallAfter: Calls a Python function after a given time. |
| 17 |
|
| 18 |
This example shows the usage of the Pattern object to create a |
| 19 |
sequence of audio events. |
| 20 |
|
| 21 |
""" |
| 22 |
from pyo import * |
| 23 |
import random |
| 24 |
|
| 25 |
s = Server().boot() |
| 26 |
|
| 27 |
|
| 28 |
amp = Fader(fadein=0.005, fadeout=0.05, mul=0.15) |
| 29 |
osc = RCOsc(freq=[100, 100], mul=amp).out() |
| 30 |
dly = Delay(osc, delay=1.0, feedback=0.5).out() |
| 31 |
|
| 32 |
|
| 33 |
def new_event(): |
| 34 |
|
| 35 |
dur = random.choice([0.125, 0.125, 0.125, 0.25, 0.25, 0.5, 1]) |
| 36 |
|
| 37 |
|
| 38 |
amp.dur = dur |
| 39 |
|
| 40 |
|
| 41 |
pat.time = dur |
| 42 |
|
| 43 |
|
| 44 |
freq = random.choice(midiToHz([60, 62, 63, 65, 67, 68, 71, 72])) |
| 45 |
|
| 46 |
|
| 47 |
osc.freq = [freq, freq * 1.003] |
| 48 |
|
| 49 |
|
| 50 |
amp.play() |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
pat = Pattern(function=new_event, time=0.25).play() |
| 58 |
|
| 59 |
s.gui(locals()) |