File: 04-ping-pong-delay.rst.txt

package info (click to toggle)
python-pyo 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,332 kB
  • sloc: python: 135,133; ansic: 127,822; javascript: 16,116; sh: 395; makefile: 388; cpp: 242
file content (70 lines) | stat: -rw-r--r-- 2,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
04-ping-pong-delay.py - Stereo ping-pong delay.
============================================================================================================================================


A stereo ping-pong delay is when you use two delays, one hard left and the
other hard right, and the delays alternate. 

This exemple illustrates how we can pass the signal of a first object to
the input of a second even if the first object does not exist yet when the
second is created.

.. code-block:: python

    from pyo import *
    import random
    
    s = Server(duplex=0).boot()
    
    # Compute the duration, in seconds, of one buffer size.
    buftime = s.getBufferSize() / s.getSamplingRate()
    
    # Delay parameters
    delay_time_l = Sig(0.125)  # Delay time for the left channel delay.
    delay_time_l.ctrl()
    delay_feed = Sig(0.75)  # Feedback value for both delays.
    delay_feed.ctrl()
    
    # Because the right delay gets its input sound from the left delay, while
    # it is computed before (to send its output sound to the left delay), it
    # will be one buffer size late. To compensate this additional delay on the
    # right, we substract one buffer size from the real delay time.
    delay_time_r = Sig(delay_time_l, add=-buftime)
    
    # Setup up a soundfile player.
    sf = SfPlayer("../snds/alum1.wav").stop()
    
    # Send the original sound to both speakers.
    sfout = sf.mix(2).out()
    
    # Initialize the right delay with zeros as input because the left delay
    # does not exist yet.
    right = Delay(Sig(0), delay=delay_time_r).out(1)
    
    # Initialize the left delay with the original mono source and the right
    # delay signal (multiplied by the feedback value) as input.
    left = Delay(sf + right * delay_feed, delay=delay_time_l).out()
    
    # One issue with recursive cross-delay is if we set the feedback to
    # 0, the right delay never gets any signal. To resolve this, we add a
    # non-recursive delay, with a gain that is the inverse of the feedback,
    # to the right delay input.
    original_delayed = Delay(sf, delay_time_l, mul=1 - delay_feed)
    
    # Change the right delay input (now that the left delay exists).
    right.setInput(original_delayed + left * delay_feed)
    
    
    def playit():
        "Assign a sound to the player and start playback."
        which = random.randint(1, 4)
        path = "../snds/alum%d.wav" % which
        sf.path = path
        sf.play()
    
    
    # Call the function "playit" every second.
    pat = Pattern(playit, 1).play()
    
    s.gui(locals())