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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
Creating a custom PyoObject - Flanger
=============================================================
There are few steps we need to take care of in order to create a class with all
of the PyoObject behaviors.
Things to consider:
- The parent class must be PyoObject, that means the PyoObject's __init__ method must be called inside the object's __init__ method to properly initialize PyoObject's basic attributes.
- When a PyoObject receives another PyoObject, it looks for a list of objects called "self._base_objs". This list must contain the C implementation of the audio objects generating the output sound of the process.
- Adding "mul" and "add" arguments (they act on objects in self._base_objs).
- All PyoObjects support "list expansion".
- All PyoObjects with sound in input support cross-fading between old and new sources.
- We will probably want to override the play(), out() and stop() methods.
- There is an attribute for any function that modify a parameter.
- We should override the ctrl() method to allow a GUI to control parameters.
In this tutorial, we will define a Flanger object with this definition:
.. code-block:: python
Flanger(input, depth=0.75, lfofreq=0.2, feedback=0.25, mul=1, add=0)
First of all, we need to import the pyo module
.. code-block:: python
from pyo import *
Step 1 - Declaring the class
------------------------------
We will create a new class called Flanger with PyoObject as its parent class.
Another good habit is to put a __doc__ string at the beginning of our classes.
Doing so will allow other users to retrieve the object's documentation with the
standard python help() function.
.. code-block:: python
class Flanger(PyoObject):
"""
Flanging effect.
A flanging is an audio effect produced by mixing two identical signals together,
with one signal delayed by a small and gradually changing period, usually smaller
than 20 milliseconds. This produces a swept comb filter effect: peaks and notches
are produced in the resultant frequency spectrum, related to each other in a linear
harmonic series. Varying the time delay causes these to sweep up and down the
frequency spectrum.
:Parent: :py:class:`PyoObject`
:Args:
input : PyoObject
Input signal to process.
depth : float or PyoObject, optional
Amplitude of the delay line modulation, between 0 and 1.
Defaults to 0.75.
lfofreq : float or PyoObject, optional
Frequency of the delay line modulation, in Hertz.
Defaults to 0.2.
feedback : float or PyoObject, optional
Amount of output signal reinjected into the delay line.
Defaults to 0.25.
>>> s = Server().boot()
>>> s.start()
>>> inp = SfPlayer(SNDS_PATH + "/transparent.aif", loop=True)
>>> lf = Sine(0.005, mul=0.25, add=0.5)
>>> flg = Flanger(input=inp, depth=0.9, lfofreq=0.1, feedback=lf).out()
"""
Step 2 - The __init__ method
-------------------------------
This is the place where we have to take care of some of pyo's generic behaviours.
The most important thing to remember is that when a PyoObject receives another
PyoObject in input, it looks for an attribute called self._base_objs. This attribute
is a list of the object's base classes and is considered the audio output signal
of the object (the Sine object uses internally an object called Sine_base). The
getBaseObjects() method returns the list of base classes for a given PyoObject. We
will call the getBaseObjects() method on the objects generating the output signal of
our process. .play(), .out(), .stop() and .mix() methods act on this list.
We also need to add two arguments to the definition of the object: "mul" and "add".
The attributes "self._mul" and "self._add" are handled by the parent class and are
automatically applied to the objects stored in the list "self._base_objs".
Finally, we have to consider the "multi-channel expansion" feature, allowing lists given as
arguments to create multiple instances of our object and managing multiple audio streams.
Two functions help us to accomplish this:
`convertArgsToLists(*args)` : Return arguments converted to lists and the maximum list size.
wrap(list,i) : Return value at position "i" in "list" with wrap around len(list).
.. code-block:: python
def __init__(self, input, depth=0.75, lfofreq=0.2, feedback=0.5, mul=1, add=0):
# Properly initialize PyoObject's basic attributes
PyoObject.__init__(self)
# Keep references of all raw arguments
self._input = input
self._depth = depth
self._lfofreq = lfofreq
self._feedback = feedback
# Using InputFader to manage input sound allows cross-fade when changing sources
self._in_fader = InputFader(input)
# Convert all arguments to lists for "multi-channel expansion"
in_fader, depth, lfofreq, feedback, mul, add, lmax = convertArgsToLists(
self._in_fader, depth, lfofreq, feedback, mul, add)
# Apply processing
self._modamp = Sig(depth, mul=0.005)
self._mod = Sine(freq=lfofreq, mul=self._modamp, add=0.005)
self._dls = Delay(in_fader, delay=self._mod, feedback=feedback)
self._flange = Interp(in_fader, self._dls, mul=mul, add=add)
# self._base_objs is the audio output seen by the outside world!
self._base_objs = self._flange.getBaseObjects()
Step 3 - setXXX methods and attributes
------------------------------------------
Now, we will add methods and attributes getter and setter for all controllable
parameters. It should be noted that we use the setInput() method of the
InputFader object to change an input source. This object implements a cross-fade
between the old source and the new one with a cross-fade duration argument.
Here, we need to keep references of raw argument in order to get the
real current state when we call the dump() method.
.. code-block:: python
def setInput(self, x, fadetime=0.05):
"""
Replace the `input` attribute.
:Args:
x : PyoObject
New signal to process.
fadetime : float, optional
Crossfade time between old and new input. Defaults to 0.05.
"""
self._input = x
self._in_fader.setInput(x, fadetime)
def setDepth(self, x):
"""
Replace the `depth` attribute.
:Args:
x : float or PyoObject
New `depth` attribute.
"""
self._depth = x
self._modamp.value = x
def setLfoFreq(self, x):
"""
Replace the `lfofreq` attribute.
:Args:
x : float or PyoObject
New `lfofreq` attribute.
"""
self._lfofreq = x
self._mod.freq = x
def setFeedback(self, x):
"""
Replace the `feedback` attribute.
:Args:
x : float or PyoObject
New `feedback` attribute.
"""
self._feedback = x
self._dls.feedback = x
@property
def input(self):
"""PyoObject. Input signal to process."""
return self._input
@input.setter
def input(self, x):
self.setInput(x)
@property
def depth(self):
"""float or PyoObject. Amplitude of the delay line modulation."""
return self._depth
@depth.setter
def depth(self, x):
self.setDepth(x)
@property
def lfofreq(self):
"""float or PyoObject. Frequency of the delay line modulation."""
return self._lfofreq
@lfofreq.setter
def lfofreq(self, x):
self.setLfoFreq(x)
@property
def feedback(self):
"""float or PyoObject. Amount of out sig sent back in delay line."""
return self._feedback
@feedback.setter
def feedback(self, x):
self.setFeedback(x)
Step 4 - The ctrl() method
-----------------------------
The ctrl() method of a PyoObject is used to pop-up a GUI to control the parameters
of the object. The initialization of sliders is done with a list of SLMap objects
where we can set the range of the slider, the type of scaling, the name of the
attribute linked to the slider and the initial value. We will define a default
"self._map_list" that will be used if the user doesn't provide one to the parameter
"map_list". If the object doesn't have any parameter to control with a GUI, this
.. code-block:: python
def ctrl(self, map_list=None, title=None, wxnoserver=False):
self._map_list = [SLMap(0., 1., "lin", "depth", self._depth),
SLMap(0.001, 20., "log", "lfofreq", self._lfofreq),
SLMap(0., 1., "lin", "feedback", self._feedback),
SLMapMul(self._mul)]
PyoObject.ctrl(self, map_list, title, wxnoserver)
Step 5 - Overriding the .play(), .stop() and .out() methods
-------------------------------------------------------------
Finally, we might want to override .play(), .stop() and .out() methods to be sure all
our internal PyoObjects are consequently managed instead of only objects in self._base_obj,
as it is in built-in objects. To handle properly the process for self._base_objs, we still
need to call the method that belongs to PyoObject. We return the returned value (self) of
these methods in order to possibly append the method to the object's creation. See the
definition of these methods in the PyoObject man page to understand the meaning of arguments.
.. code-block:: python
def play(self, dur=0, delay=0):
self._modamp.play(dur, delay)
self._mod.play(dur, delay)
self._dls.play(dur, delay)
return PyoObject.play(self, dur, delay)
def stop(self, wait=0):
self._modamp.stop(wait)
self._mod.stop(wait)
self._dls.stop(wait)
return PyoObject.stop(self, wait)
def out(self, chnl=0, inc=1, dur=0, delay=0):
self._modamp.play(dur, delay)
self._mod.play(dur, delay)
self._dls.play(dur, delay)
return PyoObject.out(self, chnl, inc, dur, delay)
Here we are, we've just created our second custom pyo object!
Complete class definition and test
----------------------------------------
.. code-block:: python
from pyo import *
class Flanger(PyoObject):
"""
Flanging effect.
A flanging is an audio effect produced by mixing two identical signals together,
with one signal delayed by a small and gradually changing period, usually smaller
than 20 milliseconds. This produces a swept comb filter effect: peaks and notches
are produced in the resultant frequency spectrum, related to each other in a linear
harmonic series. Varying the time delay causes these to sweep up and down the
frequency spectrum.
:Parent: :py:class:`PyoObject`
:Args:
input : PyoObject
Input signal to process.
depth : float or PyoObject, optional
Amplitude of the delay line modulation, between 0 and 1.
Defaults to 0.75.
lfofreq : float or PyoObject, optional
Frequency of the delay line modulation, in Hertz.
Defaults to 0.2.
feedback : float or PyoObject, optional
Amount of output signal reinjected into the delay line.
Defaults to 0.25.
>>> s = Server().boot()
>>> s.start()
>>> inp = SfPlayer(SNDS_PATH + "/transparent.aif", loop=True)
>>> lf = Sine(0.005, mul=0.25, add=0.5)
>>> flg = Flanger(input=inp, depth=0.9, lfofreq=0.1, feedback=lf).out()
"""
def __init__(self, input, depth=0.75, lfofreq=0.2, feedback=0.5, mul=1, add=0):
PyoObject.__init__(self)
self._input = input
self._depth = depth
self._lfofreq = lfofreq
self._feedback = feedback
self._in_fader = InputFader(input)
in_fader, depth, lfofreq, feedback, mul, add, lmax = convertArgsToLists(
self._in_fader, depth, lfofreq, feedback, mul, add)
self._modamp = Sig(depth, mul=0.005)
self._mod = Sine(freq=lfofreq, mul=self._modamp, add=0.005)
self._dls = Delay(in_fader, delay=self._mod, feedback=feedback)
self._flange = Interp(in_fader, self._dls, mul=mul, add=add)
self._base_objs = self._flange.getBaseObjects()
def setInput(self, x, fadetime=0.05):
"""
Replace the `input` attribute.
:Args:
x : PyoObject
New signal to process.
fadetime : float, optional
Crossfade time between old and new input. Defaults to 0.05.
"""
self._input = x
self._in_fader.setInput(x, fadetime)
def setDepth(self, x):
"""
Replace the `depth` attribute.
:Args:
x : float or PyoObject
New `depth` attribute.
"""
self._depth = x
self._modamp.value = x
def setLfoFreq(self, x):
"""
Replace the `lfofreq` attribute.
:Args:
x : float or PyoObject
New `lfofreq` attribute.
"""
self._lfofreq = x
self._mod.freq = x
def setFeedback(self, x):
"""
Replace the `feedback` attribute.
:Args:
x : float or PyoObject
New `feedback` attribute.
"""
self._feedback = x
self._dls.feedback = x
def play(self, dur=0, delay=0):
self._modamp.play(dur, delay)
self._mod.play(dur, delay)
self._dls.play(dur, delay)
return PyoObject.play(self, dur, delay)
def stop(self, wait=0):
self._modamp.stop(wait)
self._mod.stop(wait)
self._dls.stop(wait)
return PyoObject.stop(self, wait)
def out(self, chnl=0, inc=1, dur=0, delay=0):
self._modamp.play(dur, delay)
self._mod.play(dur, delay)
self._dls.play(dur, delay)
return PyoObject.out(self, chnl, inc, dur, delay)
def ctrl(self, map_list=None, title=None, wxnoserver=False):
self._map_list = [SLMap(0., 1., "lin", "depth", self._depth),
SLMap(0.001, 20., "log", "lfofreq", self._lfofreq),
SLMap(0., 1., "lin", "feedback", self._feedback),
SLMapMul(self._mul)]
PyoObject.ctrl(self, map_list, title, wxnoserver)
@property
def input(self):
"""PyoObject. Input signal to process."""
return self._input
@input.setter
def input(self, x):
self.setInput(x)
@property
def depth(self):
"""float or PyoObject. Amplitude of the delay line modulation."""
return self._depth
@depth.setter
def depth(self, x):
self.setDepth(x)
@property
def lfofreq(self):
"""float or PyoObject. Frequency of the delay line modulation."""
return self._lfofreq
@lfofreq.setter
def lfofreq(self, x):
self.setLfoFreq(x)
@property
def feedback(self):
"""float or PyoObject. Amount of out sig sent back in delay line."""
return self._feedback
@feedback.setter
def feedback(self, x):
self.setFeedback(x)
# Run the script to test the Flanger object.
if __name__ == "__main__":
s = Server().boot()
src = BrownNoise([.2,.2]).out()
fl = Flanger(src, depth=.9, lfofreq=.1, feedback=.5, mul=.5).out()
s.gui(locals())
|