Debian Codegraph @

pyo/examples/x22-events/03-instruments.py

raw symbols SCIP
1 """
2 03-instruments.py - Using custom instrument with events.
3
4 The default instrument ( DefaultInstrument ) is a basic stereo RC oscillator
5 passing through reverberation unit. For the events framework to be really
6 useful, it has to give the user the opportunity to use his own instruments.
7 Composing an instrument is very simple.
8
9 An Events's instrument must be derived from the EventInstrument class. Its
10 signature must be::
11
12 class InstrumentName(EventInstrument):
13 def __init__(self, **args):
14 EventInstrument.__init__(self, **args)
15
16 The EventInstrument is responsible for the creation of the envelope, accessible
17 through the variable self.env, and also for clearing its resources when it's done
18 playing.
19
20 All arguments given to the Events object can be retrieved in our instrument with
21 the syntax self.argument_name (ex.: self.freq).
22
23 """
24 from pyo import *
25
26 s = Server().boot()
27
28
29 class MyInstrument(EventInstrument):
30 def __init__(self, **args):
31 EventInstrument.__init__(self, **args)
32
33 # self.freq is derived from the 'degree' argument.
34 self.phase = Phasor([self.freq, self.freq * 1.003])
35
36 # self.dur is derived from the 'beat' argument.
37 self.duty = Expseg([(0, 0.05), (self.dur, 0.5)], exp=4).play()
38
39 self.osc = Compare(self.phase, self.duty, mode="<", mul=1, add=-0.5)
40
41 # EventInstrument created the amplitude envelope as self.env.
42 self.filt = ButLP(self.osc, freq=5000, mul=self.env).out()
43
44
45 # We tell the Events object which instrument to use with the 'instr' argument.
46 e = Events(
47 instr=MyInstrument,
48 degree=EventSeq([5.00, 5.04, 5.07, 6.00]),
49 beat=1 / 2.0,
50 db=-12,
51 attack=0.001,
52 decay=0.05,
53 sustain=0.5,
54 release=0.005,
55 ).play()
56
57 s.gui(locals())