File: 06-table-stutter.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,388 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
06-table-stutter.py - Variable length table reading.
============================================================================================================================================


This little program use a function and a time-variable function caller
to stutter a sound loaded in a table. The starting point of the table
playback is set initially to near the end of the table, and move toward
the beginning each time a new playback is called.

For the playback, we use a Pointer object, which is a table reader with
control on the pointer position (normalized between 0 and 1).

.. code-block:: python

    from pyo import *
    import random
    
    s = Server().boot()
    
    STUTTER = 0.025  # Delta time added each time the playback restart.
    FADETIME = 0.01  # Fadein-fadeout time to avoid clicks.
    
    # Load a sound in the table and get its duration.
    table = SndTable(SNDS_PATH + "/transparent.aif")
    tabdur = table.getDur()
    
    # Intialize the line used to read the table.
    line = Linseg([(0, 0), (1, 1)])
    
    # Amplitude envelope, to avoid clicks.
    amp = Fader(FADETIME, FADETIME, dur=STUTTER, mul=0.5)
    amp.setExp(2)
    
    # Read the sound and mix it to stereo.
    read = Pointer(table=table, index=line, mul=amp).mix(2).out()
    
    # Global variables (start position and playback duration).
    start = tabdur - STUTTER
    dur = STUTTER
    
    
    def go():
        "Read a segment of a sound table and set the duration before the next iteration."
        global start, dur
    
        # Create the pointer segment (from normalized start position to the end of the table)
        line.list = [(0, start / tabdur), (dur, 1)]
    
        # Assign duration to the envelope and the function caller (Pattern).
        amp.dur = pat.time = dur
    
        # Decrement start position and increment duration by STUTTER seconds.
        start -= STUTTER
        dur += STUTTER
    
        # Reset start and dur variables when reaching the beginning of the sound.
        if start < 0:
            start = tabdur - STUTTER
            dur = STUTTER
    
        # Activate the pointer's index line and the amplitude envelope.
        line.play()
        amp.play()
    
    
    # Call go() each time the playback reaches the end of the file
    pat = Pattern(function=go, time=STUTTER).play()
    
    s.gui(locals())