Debian Codegraph @

docs/_sources/examples/22-events/04-custom-params.rst.txt

raw
1 04-custom-params.py - Passing custom parameters to the instrument.
2 ============================================================================================================================================
3
4
5 The built-in arguments of the Events object can't cover all possible controls
6 an instrument could need to process its sounds. Because of that, the user
7 should be able to create its own custom parameters and pass them to the
8 instrument's instances generated by the events. To do so, it's very simple.
9 One can give any argument to the Events object, it will be automatically part
10 of the parameters passed to the instrument's instance. In the instrument,
11 these arguments become instance variables (self.param_name).
12
13 .. code-block:: python
14
15 from pyo import *
16
17 s = Server().boot()
18
19
20 class MyInstrument(EventInstrument):
21 def __init__(self, **args):
22 EventInstrument.__init__(self, **args)
23
24 # Here, we use two custom parameters, self.duty and self.cutoff. These
25 # variables must exist, and therefore should be given as arguments to
26 # the Events object that produce the events.
27
28 self.phase = Phasor([self.freq, self.freq * 1.003])
29 self.osc = Compare(self.phase, self.duty, mode="<", mul=1, add=-0.5)
30 self.filt = ButLP(self.osc, freq=self.cutoff, mul=self.env).out()
31
32
33 e = Events(
34 instr=MyInstrument,
35 degree=EventSeq([5.00, 5.04, 5.07, 6.00]),
36 duty=EventSeq([0.02, 0.1, 0.25, 0.5]), # self.duty in the instrument
37 cutoff=EventSeq([5000, 4000, 3000, 2000]), # self.cutoff in the instrument
38 beat=1 / 2.0,
39 db=[-6, -9, -9, -12],
40 attack=0.001,
41 decay=0.05,
42 sustain=0.5,
43 release=0.005,
44 ).play()
45
46 s.gui(locals())
47