Debian Codegraph @

pyo/examples/x10-tables/04-granulation.py

raw symbols SCIP
1 """
2 04-granulation.py - Full control granular synthesis.
3
4 The output of a Granular Synthesis is composed of many individual
5 grains of sound. A grain is a short chunk of sound, typically between
6 10 and 100 ms (but can also vary outside this range), with an amplitude
7 envelope in the shape of a bell curve. The sonic quality of a granular
8 texture is a result of the distribution of grains in time and of the
9 parameters selected for the synthesis of each grain.
10
11 This example shows the usage of the most featured granulation object
12 of the library!
13
14 Available granulation objects, in order of complexity, are:
15
16 - Granulator
17 - Granule
18 - Particle
19 - Particle2
20
21 """
22 from pyo import *
23
24 s = Server().boot()
25
26 snd = SndTable("../snds/baseballmajeur_m.aif")
27 snd.view()
28
29 # Remove sr/4 samples to the size of the table, just to be sure
30 # that the reading pointer never exceeds the end of the table.
31 end = snd.getSize() - s.getSamplingRate() * 0.25
32
33 # A Tuckey envelope for the grains (also known as flat-top envelope).
34 env = WinTable(7)
35 env.view(title="Grain envelope")
36
37 # The grain pitch has a default value of 1, to which we can add a
38 # randomness factor by raising the "mul" value of the Noise.
39 pit = Noise(0, add=1)
40 pit.ctrl([SLMap(0, 1, "lin", "mul", 0)], title="Pitch Randomness")
41
42 # The grain position oscillates slowly between the beginning and
43 # the end of the table. We add a little jitter to the position to
44 # attenuate phasing artifacts when overlapping the grains.
45 pososc = Sine(0.05).range(0, end)
46 posrnd = Noise(mul=0.01, add=1)
47 pos = pososc * posrnd
48
49 # The grain panoramisation is completely random.
50 pan = Noise(mul=0.5, add=0.5)
51
52 # The grain filter center frequency choices are the first 40
53 # harmonics of a base frequency oscillating between 75 and 125 Hz.
54 cf = Sine(freq=0.07).range(75, 125)
55 fcf = Choice(list(range(1, 40)), freq=150, mul=cf)
56
57 grn = Particle2(
58 table=snd, # The table to read.
59 env=env, # The grain envelope.
60 dens=128, # The density of grains per second.
61 # The next arguments are sampled at the beginning of the grain
62 # and hold their until the end of the grain.
63 pitch=pit, # The pitch of the grain.
64 pos=pos, # The position of the grain in the table.
65 dur=0.2, # The duration of the grain in seconds.
66 dev=0.005, # The maximum deviation of the start time,
67 # synchronous versus asynchronous granulation.
68 pan=pan, # The pan value of the grain.
69 filterfreq=fcf, # The filter frequency of the grain.
70 filterq=20, # The filter Q of the grain.
71 filtertype=2, # The filter type of the grain.
72 # End of sampled arguments.
73 chnls=2, # The output number of streams of the granulator.
74 mul=0.2,
75 )
76 grn.ctrl()
77
78 # Some grains can be surprisingly loud so we compress the output of the granulator.
79 comp = Compress(grn, thresh=-20, ratio=4, risetime=0.005, falltime=0.10, knee=0.5, mul=2).out()
80
81 s.gui(locals())