Debian Codegraph @

docs/_sources/examples/02-controls/08-handling-channels.rst.txt

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