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