Debian Codegraph @

pyo/examples/x22-events/04-custom-params.py

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