Debian Codegraph @

pyo/examples/x09-callbacks/01-periodic-calls.py

raw symbols SCIP
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 # A small instrument to play the events emitted by the function call.
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 # Choose a duration for this event.
35 dur = random.choice([0.125, 0.125, 0.125, 0.25, 0.25, 0.5, 1])
36
37 # Assigns the new duration to the envelope.
38 amp.dur = dur
39 # Assigns the new duration to the caller, thus the next function call
40 # will be only after the current event has finished.
41 pat.time = dur
42
43 # Choose a new frequency.
44 freq = random.choice(midiToHz([60, 62, 63, 65, 67, 68, 71, 72]))
45
46 # Replace oscillator's frequencies.
47 osc.freq = [freq, freq * 1.003]
48
49 # Start the envelope.
50 amp.play()
51
52
53 # A Pattern object periodically call the referred function given as
54 # argument. The "time" argument is the delay between successive calls.
55 # The play() method must be explicitly called for a Pattern object
56 # to start its processing loop.
57 pat = Pattern(function=new_event, time=0.25).play()
58
59 s.gui(locals())