The Synth
class
- class audiomath.Synth(func=None, fs=44100, duration='inf', nChannels=1, dtype='float32')
Bases:
object
This class is a duck-typed substitute for the numpy array
s.y
at the heart of aSound
instances
, but instead of containing static data it allows on-the-fly functional computation of the sound signal.Example:
import numpy as np, audiomath as am TWO_PI = 2.0 * np.pi carrierFreq = 440 modFreq = 0.5 @am.Synth def wobble(fs, samples, channels): t = samples / fs return np.sin(TWO_PI * carrierFreq * t) * (0.5 - 0.5 * np.cos(TWO_PI * modFreq * t)) p = am.Player(wobble) # could also say am.Sound(wobble) print(p.sound) p.Play()
You can also use a pre-configured instance of
Synth
as the decorator, with the same effect:@am.Synth(fs=22050) def y(fs, samples, channels): t = samples / fs return np.sin(2 * np.pi * 440 * t) p = am.Player(y) # could also say am.Sound(y)
…or the
Use()
method of an instance:y = am.Synth() @y.Use def func(fs, samples, channels): # this way, `func` will be unchanged t = samples / fs return np.sin(2 * np.pi * 440 * t) p = am.Player(y) # could also say am.Sound(y)
…or you can subclass
am.Synth
withfunc
as an overshadowed method:class Tone(am.Synth): def func(self, fs, samples, channels): return np.sin(2 * np.pi * self.freq * samples / fs ) t = Tone() t.freq = 440 p = am.Player(t)
- Parameters:
func – A function that takes three arguments: a scalar sampling frequency, an
m x 1
array of sample indices, and a1 x n
array of channel indices.func
should return anm x 1
orm x n
array of sample values.Alternatively,
func
may be defined like a method (four arguments, the first beingself
).fs (float) – Scalar numeric value specifying the sampling rate.
duration (float) – The nominal duration of the sound in seconds. May be infinite.
nChannels (int) – Nominal number of channels.
dtype (str) – numpy data type for the generated sample data.