Debian Codegraph @

pyo/examples/x05-envelopes/04-simple-envelopes.py

raw symbols SCIP
1 """
2 04-simple-envelopes.py - ASR and ADSR envelopes.
3
4 The Fader object is a simple way to setup an Attack/Sustain/Release envelope.
5 This envelope allows to apply fadein and fadeout on audio streams.
6
7 If the `dur` argument of the Fader object is set to 0 (the default), the
8 object waits for a stop() command before activating the release part of
9 the envelope. Otherwise, the sum of `fadein` and `fadeout` must be less
10 than or egal to `dur` and the envelope runs to the end on a play() command.
11
12 The Adsr object (Attack/Decay/Sustain/Release) acts exactly like the Fader
13 object, with a more flexible (and so common) kind of envelope.
14
15 """
16 from pyo import *
17 import random
18
19 s = Server().boot()
20
21 # Infinite sustain for the global envelope.
22 globalamp = Fader(fadein=2, fadeout=2, dur=0).play()
23
24 # Envelope for discrete events, sharp attack, long release.
25 env = Adsr(attack=0.01, decay=0.1, sustain=0.5, release=1.5, dur=2, mul=0.5)
26 # setExp method can be used to create exponential or logarithmic envelope.
27 env.setExp(0.75)
28
29 # Initialize a simple wave player and apply both envelopes.
30 sig = SuperSaw(freq=[100, 101], detune=0.6, bal=0.8, mul=globalamp * env).out()
31
32
33 def play_note():
34 "Play a new note with random frequency and jitterized envelope."
35 freq = random.choice(midiToHz([36, 38, 41, 43, 45]))
36 sig.freq = [freq, freq * 1.005]
37 env.attack = random.uniform(0.002, 0.01)
38 env.decay = random.uniform(0.1, 0.5)
39 env.sustain = random.uniform(0.3, 0.6)
40 env.release = random.uniform(0.8, 1.4)
41 # Start the envelope for the event.
42 env.play()
43
44
45 # Periodically call a function.
46 pat = Pattern(play_note, time=2).play()
47
48 s.gui(locals())