Debian Codegraph @

pyo/examples/x09-callbacks/03-delayed-calls.py

raw symbols SCIP
1 """
2 03-delayed-calls.py - Calling a function once, after a given delay.
3
4 If you want to setup a callback once in the future, the CallAfter
5 object is very easy to use. You just give it the function name, the
6 time to wait before making the call and an optional argument.
7
8 """
9 from pyo import *
10
11 s = Server().boot()
12
13 # A four-streams oscillator to produce a chord.
14 amp = Fader(fadein=0.005, fadeout=0.05, mul=0.2).play()
15 osc = SineLoop(freq=[0, 0, 0, 0], feedback=0.05, mul=amp)
16 rev = WGVerb(osc.mix(2), feedback=0.8, cutoff=4000, bal=0.2).out()
17
18 # A function to change the oscillator's frequencies and start the envelope.
19 def set_osc_freqs(notes):
20 print(notes)
21 osc.set(attr="freq", value=midiToHz(list(notes)), port=0.005)
22 amp.play()
23
24
25 # Initial chord.
26 set_osc_freqs([60, 64, 67, 72])
27
28 # We must be sure that our CallAfter object stays alive as long as
29 # it waits to call its function. If we don't keep a reference to it,
30 # it will be garbage-collected before doing its job.
31 call = None
32
33
34 def new_notes(notes):
35 global call # Use a global variable.
36 amp.stop() # Start the fadeout of the current notes...
37 # ... then, 50 ms later, call the function that change the frequencies.
38 call = CallAfter(set_osc_freqs, time=0.05, arg=notes)
39
40
41 # The sequence of events. We use a tuple for the list of frequencies
42 # because PyoObjects spread lists as argument over all their internal
43 # streams. This means that with a list of frequencies, only the first
44 # frequency would be passed to the callback of the first (and single)
45 # stream (a list of functions at first argument would create a
46 # multi-stream object). A tuple is treated as a single argument.
47 c1 = CallAfter(new_notes, time=0.95, arg=(60, 64, 67, 69))
48 c2 = CallAfter(new_notes, time=1.95, arg=(60, 65, 69, 76))
49 c3 = CallAfter(new_notes, time=2.95, arg=(62, 65, 69, 74))
50 c4 = CallAfter(new_notes, time=3.45, arg=(59, 65, 67, 74))
51 c5 = CallAfter(new_notes, time=3.95, arg=(60, 64, 67, 72))
52 # The last event activates the fadeout of the amplitude envelope.
53 c6 = CallAfter(amp.stop, time=5.95, arg=None)
54
55 s.gui(locals())