Debian Codegraph @

pyo/examples/x02-controls/08-handling-channels.py

raw symbols SCIP
1 """
2 08-handling-channels.py - Managing object's internal audio streams.
3
4 Because audio objects expand their number of streams according to lists
5 given to their arguments and the fact that an audio object is considered
6 as a list, if a multi-streams object is given as an argument to another
7 audio object, the later will also be expanded in order to process all
8 given streams. This is really powerful to create polyphonic processes
9 without copying long chunks of code but it can be very CPU expensive.
10
11 In this example, we create a square from a sum of sine waves. After that,
12 a chorus is applied to the resulting waveform. If we don't mix down the
13 square wave, we got tens of Chorus objects in the processing chain (one
14 per sine). This can easily overrun the CPU. The exact same result can
15 be obtained with only one Chorus applied to the sum of the sine waves.
16 The `mix(voices)` method of the PyoObject helps the handling of channels
17 in order to save CPU cycles. Here, we down mix all streams to only two
18 streams (to maintain the stereo) before processing the Chorus arguments.
19
20 """
21 from pyo import *
22
23 s = Server().boot()
24
25 # Sets fundamental frequency and highest harmonic.
26 freq = 100
27 high = 20
28
29 # Generates lists for frequencies and amplitudes
30 harms = [freq * i for i in range(1, high) if i % 2 == 1]
31 amps = [0.33 / i for i in range(1, high) if i % 2 == 1]
32
33 # Creates a square wave by additive synthesis.
34 a = Sine(freq=harms, mul=amps)
35 print("Number of Sine streams: %d" % len(a))
36
37 # Mix down the number of streams of "a" before computing the Chorus.
38 b = Chorus(a.mix(2), feedback=0.5).out()
39 print("Number of Chorus streams: %d" % len(b))
40
41 s.gui(locals())