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 a Sound instance s, 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)
Parameters:
  • func – A function that takes three arguments: a scalar sampling frequency, an m x 1 array of sample indices, and a 1 x n array of channel indices. func should return an m x 1 or m x n array of sample values.
  • 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.
static func(fs, samples, channels)

Demo function. Replace this, e.g. by saying y.func = something_else, where y is your Synth instance. Or use the @y.Use decorator when defining a new function. Or use @Synth as the decorator in the first place.