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