ContextsΒΆ

In this example, we see how to use the with keyword to open the PdPy patch. When exiting the context, the file is automatically written to disk. So, there is no need to call PdPy.write() at the end.

The patch creates a spectrum of 16 harmonics with phased-out cosine amplitude envelopes

 1from pdpy_lib import PdPy, Obj, Msg
 2
 3fund = 110
 4npartials = 16
 5partials = [i for i in range(1, npartials)]
 6
 7# create the python context
 8with PdPy(name="contexts", root=True) as pd:
 9
10  # create a dac object
11  dac = Obj('dac~')
12  pd.create(dac)
13
14  for n in range(1,npartials):
15    # create an envelope
16    phasor = Obj('phasor~').addargs(0.25)
17    envelope = [
18      phasor,
19      Obj('-~').addargs(3.14159 / 2.0),
20      Obj('cos~'),
21      Obj('+~').addargs(1),
22      Obj('/~').addargs(2)
23    ]
24    pd.create(*envelope)
25    pd.connect(*envelope)
26
27    # initialize the phasor's phase
28    loadbang = Obj('loadbang')
29    message = Msg(str(n/npartials))
30    pd.create(loadbang, message)
31    pd.connect(loadbang, message)
32    pd.connect(message, [phasor, 1])
33
34    # create a sinusoid
35    oscillator = Obj('osc~').addargs(fund * n)
36    multiplier = Obj('*~').addargs(1.0 / npartials)
37    mult = Obj('*~')
38    pd.create(oscillator, multiplier, mult)
39    pd.connect(oscillator, multiplier, mult)
40
41    # connect the envelope to the sinusoid's last multiplier
42    pd.connect(envelope[-1], [mult, 1])
43
44    # connect the last multiplier to this partials' dac
45    pd.connect(mult, [dac, n%2])