Debian Codegraph @

pyo/examples/x08-dynamics/02-ducking.py

raw symbols SCIP
1 """
2 02-ducking.py - Adjust the gain of a signal based on the presence of another one.
3
4 Ducking is an audio effect commonly used in radio. In ducking, the
5 level of one audio signal is reduced by the presence of another signal.
6
7 Here we use a Follower object to track the RMS envelope of the voice signal. Then
8 we use an audio conditional to create a switch, whose value is 1 when the voice is
9 talking and 0 when it is silent. This signal is finally used to change the amplitude
10 of the music whenever the voice is talking.
11
12 """
13 from pyo import *
14
15 s = Server().boot()
16
17 # Alternate voice and silence.
18 table = SndTable(SNDS_PATH + "/transparent.aif")
19 metro = SDelay(Metro(3).play(), 1)
20 voice = TrigEnv(metro, table, dur=table.getDur(), mul=0.7)
21 stvoice = voice.mix(2).out()
22
23 # Play some music-box style tune!
24 freqs = midiToHz([60, 62, 64, 65, 67, 69, 71, 72])
25 choice = Choice(choice=freqs, freq=[1, 2, 3, 4])
26 port = Port(choice, risetime=0.001, falltime=0.001)
27 sines = SineLoop(port, feedback=0.05)
28 music = SPan(sines, pan=[0, 1, 0.2, 0.8, 0.5], mul=0.1).mix(2)
29
30 # Follow voice RMS amplitude.
31 follow = Follower(voice, freq=10)
32 # talk = 1 if voice is playing and 0 if not.
33 talk = follow > 0.005
34
35 # Smooth the on/off signal (rising is faster than falling)...
36 amp = Port(talk, risetime=0.05, falltime=0.1)
37 # ... then rescale it (1 when no voice and 0.1 when voice is playing).
38 ampscl = Scale(amp, outmin=1, outmax=0.1)
39
40 # Display the gain factor.
41 sc = Scope(ampscl)
42
43 # Apply gain factor and output music.
44 outsynth = (music * ampscl).out()
45
46 s.gui(locals())