Debian Codegraph @

pyo/examples/x10-tables/06-table-stutter.py

raw symbols SCIP
1 """
2 06-table-stutter.py - Variable length table reading.
3
4 This little program use a function and a time-variable function caller
5 to stutter a sound loaded in a table. The starting point of the table
6 playback is set initially to near the end of the table, and move toward
7 the beginning each time a new playback is called.
8
9 For the playback, we use a Pointer object, which is a table reader with
10 control on the pointer position (normalized between 0 and 1).
11
12 """
13 from pyo import *
14 import random
15
16 s = Server().boot()
17
18 STUTTER = 0.025 # Delta time added each time the playback restart.
19 FADETIME = 0.01 # Fadein-fadeout time to avoid clicks.
20
21 # Load a sound in the table and get its duration.
22 table = SndTable(SNDS_PATH + "/transparent.aif")
23 tabdur = table.getDur()
24
25 # Intialize the line used to read the table.
26 line = Linseg([(0, 0), (1, 1)])
27
28 # Amplitude envelope, to avoid clicks.
29 amp = Fader(FADETIME, FADETIME, dur=STUTTER, mul=0.5)
30 amp.setExp(2)
31
32 # Read the sound and mix it to stereo.
33 read = Pointer(table=table, index=line, mul=amp).mix(2).out()
34
35 # Global variables (start position and playback duration).
36 start = tabdur - STUTTER
37 dur = STUTTER
38
39
40 def go():
41 "Read a segment of a sound table and set the duration before the next iteration."
42 global start, dur
43
44 # Create the pointer segment (from normalized start position to the end of the table)
45 line.list = [(0, start / tabdur), (dur, 1)]
46
47 # Assign duration to the envelope and the function caller (Pattern).
48 amp.dur = pat.time = dur
49
50 # Decrement start position and increment duration by STUTTER seconds.
51 start -= STUTTER
52 dur += STUTTER
53
54 # Reset start and dur variables when reaching the beginning of the sound.
55 if start < 0:
56 start = tabdur - STUTTER
57 dur = STUTTER
58
59 # Activate the pointer's index line and the amplitude envelope.
60 line.play()
61 amp.play()
62
63
64 # Call go() each time the playback reaches the end of the file
65 pat = Pattern(function=go, time=STUTTER).play()
66
67 s.gui(locals())