lazy_synth Module

Simple audio/stream synthesis module

Summary of module contents:

Name Description
modulo_counter Creates a lazy endless counter stream with the given modulo, i.e., its values ranges from 0. to the given “modulo”, somewhat equivalent to:
line Finite Stream with a straight line, could be used as fade in/out effects.
fadein Linear fading in.
fadeout Linear fading out. Multiply by this one at end to finish and avoid clicks.
attack Linear ADS fading attack stream generator, useful to be multiplied with a given stream.
ones Ones stream generator. You may multiply your endless stream by this to enforce an end to it.
zeros Zeros/zeroes stream generator. You may sum your endless stream by this to enforce an end to it.
zeroes Zeros/zeroes stream generator. You may sum your endless stream by this to enforce an end to it.
adsr Linear ADSR envelope.
white_noise White noise stream generator.
gauss_noise Gaussian (normal) noise stream generator.
TableLookupMeta Table lookup metaclass. This class overloads all operators to the TableLookup class, applying them to the table contents, elementwise. Table length and number of cycles should be equal for this to work.
TableLookup Table lookup synthesis class, also allowing multi-cycle tables as input.
DEFAULT_TABLE_SIZE int(x=0) -> integer int(x, base=10) -> integer
sin_table Table lookup synthesis class, also allowing multi-cycle tables as input.
saw_table Table lookup synthesis class, also allowing multi-cycle tables as input.
sinusoid Sinusoid based on the optimized math.sin
impulse Impulse stream generator.
karplus_strong Karplus-Strong “digitar” synthesis algorithm.
modulo_counter(start=0.0, modulo=256.0, step=1.0)[source]

Creates a lazy endless counter stream with the given modulo, i.e., its values ranges from 0. to the given “modulo”, somewhat equivalent to:

Stream(itertools.count(start, step)) % modulo

Yet the given step can be an iterable, and doen’t create unneeded big ints. All inputs can be float. Input order remembers slice/range inputs. All inputs can also be iterables. If any of them is an iterable, the end of this counter happen when there’s no more data in one of those inputs. to continue iteration.

line(dur, begin=0.0, end=1.0, finish=False)[source]

Finite Stream with a straight line, could be used as fade in/out effects.

Parameters:
  • dur – Duration, given in number of samples. Use the sHz function to help with durations in seconds.
  • [begin – ...
  • end] – First and last (or stop) values to be yielded. Defaults to [0., 1.], respectively.
  • finish – Choose if end it the last to be yielded or it shouldn’t be yield at all. Defauts to False, which means that end won’t be yield. The last sample won’t have “end” amplitude unless finish is True, i.e., without explicitly saying “finish=True”, the “end” input works like a “stop” range parameter, although it can [should] be a float. This is so to help concatenating several lines.
Returns:

A finite Stream with the linearly spaced data.

Examples:

With finish = True, it works just like NumPy np.linspace, besides argument order and lazyness:

>>> import numpy as np # This test needs Numpy
>>> np.linspace(.2, .7, 6)
array([ 0.2,  0.3,  0.4,  0.5,  0.6,  0.7])
>>> line(6, .1, .7, finish=True)
<audiolazy.lazy_stream.Stream object at 0x...>
>>> list(line(6, .2, .7, finish=True))
[0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
>>> list(line(6, 1, 4)) # With finish = False (default)
[1.0, 1.5, 2.0, 2.5, 3.0, 3.5]

Line also works with Numpy arrays and matrices

>>> a = np.mat([[1, 2], [3, 4]])
>>> b = np.mat([[3, 2], [2, 1]])
>>> for el in line(4, a, b):
...   print(el)
[[ 1.  2.]
 [ 3.  4.]]
[[ 1.5   2.  ]
 [ 2.75  3.25]]
[[ 2.   2. ]
 [ 2.5  2.5]]
[[ 2.5   2.  ]
 [ 2.25  1.75]]

And also with ZFilter instances:

>>> from audiolazy import z
>>> for el in line(4, z ** 2 - 5, z + 2):
...   print(el)
z^2 - 5
0.75 * z^2 + 0.25 * z - 3.25
0.5 * z^2 + 0.5 * z - 1.5
0.25 * z^2 + 0.75 * z + 0.25

Note

Amplitudes commonly should be float numbers between -1 and 1. Using line(<inputs>).append([end]) you can finish the line with one extra sample without worrying with the “finish” input.

See also

sHz
Second and hertz constants from samples/second rate.
fadein(dur)[source]

Linear fading in.

Parameters:dur – Duration, in number of samples.
Returns:Stream instance yielding a line from zero to one.
fadeout(dur)[source]

Linear fading out. Multiply by this one at end to finish and avoid clicks.

Parameters:dur – Duration, in number of samples.
Returns:Stream instance yielding the line. The starting amplitude is is 1.0.
attack(a, d, s)[source]

Linear ADS fading attack stream generator, useful to be multiplied with a given stream.

Parameters:
  • a – “Attack” time, in number of samples.
  • d – “Decay” time, in number of samples.
  • s – “Sustain” amplitude level (should be based on attack amplitude). The sustain can be a Stream, if desired.
Returns:

Stream instance yielding an endless envelope, or a finite envelope if the sustain input is a finite Stream. The attack amplitude is is 1.0.

ones(dur=None)[source]

Ones stream generator. You may multiply your endless stream by this to enforce an end to it.

Parameters:dur – Duration, in number of samples; endless if not given.
Returns:Stream that repeats “1.0” during a given time duration (if any) or endlessly.
zeros(dur=None)[source]

Zeros/zeroes stream generator. You may sum your endless stream by this to enforce an end to it.

Parameters:dur – Duration, in number of samples; endless if not given.
Returns:Stream that repeats “0.0” during a given time duration (if any) or endlessly.
zeroes(dur=None)

Zeros/zeroes stream generator. You may sum your endless stream by this to enforce an end to it.

Parameters:dur – Duration, in number of samples; endless if not given.
Returns:Stream that repeats “0.0” during a given time duration (if any) or endlessly.
adsr(dur, a, d, s, r)[source]

Linear ADSR envelope.

Parameters:
  • dur – Duration, in number of samples, including the release time.
  • a – “Attack” time, in number of samples.
  • d – “Decay” time, in number of samples.
  • s – “Sustain” amplitude level (should be based on attack amplitude).
  • r – “Release” time, in number of samples.
Returns:

Stream instance yielding a finite ADSR envelope, starting and finishing with 0.0, having peak value of 1.0.

white_noise(dur=None, low=-1.0, high=1.0)[source]

White noise stream generator.

Parameters:
  • dur – Duration, in number of samples; endless if not given (or None).
  • [low – ...
  • high] – Lower and higher limits. Defaults to the [-1; 1] range.
Returns:

Stream yielding random numbers between -1 and 1.

gauss_noise(dur=None, mu=0.0, sigma=1.0)[source]

Gaussian (normal) noise stream generator.

Parameters:
  • dur – Duration, in number of samples; endless if not given (or None).
  • mu – Distribution mean. Defaults to zero.
  • sigma – Distribution standard deviation. Defaults to one.
Returns:

Stream yielding Gaussian-distributed random numbers.

Warning

This function can yield values outside the [-1; 1] range, and you might need to clip its results.

See also

clip
Clips the signal up to both a lower and a higher limit.
class TableLookupMeta[source]

Bases: audiolazy.lazy_core.AbstractOperatorOverloaderMeta

Table lookup metaclass. This class overloads all operators to the TableLookup class, applying them to the table contents, elementwise. Table length and number of cycles should be equal for this to work.

__binary__(op)[source]
__operators__ = '+ - * / // % ** << >> & | ^ ~'
__rbinary__(op)[source]
__unary__(op)[source]
class TableLookup(table, cycles=1)[source]

Bases: object

Table lookup synthesis class, also allowing multi-cycle tables as input.

__add__(other)
__and__(other)
__call__(freq, phase=0.0)[source]

Returns a wavetable lookup synthesis endless stream. Play it with the given frequency and starting phase. Phase is given in rads, and frequency in rad/sample. Accepts streams of numbers, as well as numbers, for both frequency and phase inputs.

__eq__(other)[source]
__floordiv__(other)
__getitem__(idx)[source]

Gets an item from the table from its index, which can possibly be a float. The data is linearly interpolated.

__hash__ = None
__init__(table, cycles=1)[source]

Inits a table lookup. The given table should be a sequence, like a list. The cycles input should have the number of cycles in table for frequency calculation afterwards.

__invert__()
__len__()[source]
__lshift__(other)
__mod__(other)
__mul__(other)
__ne__(other)[source]
__neg__()
__or__(other)
__pos__()
__pow__(other)
__radd__(other)
__rand__(other)
__rfloordiv__(other)
__rlshift__(other)
__rmod__(other)
__rmul__(other)
__ror__(other)
__rpow__(other)
__rrshift__(other)
__rshift__(other)
__rsub__(other)
__rtruediv__(other)
__rxor__(other)
__sub__(other)
__truediv__(other)
__xor__(other)
harmonize(harmonics_dict)[source]

Returns a “harmonized” table lookup instance by using a “harmonics” dictionary with {partial: amplitude} terms, where all “partial” keys have to be integers.

normalize()[source]

Returns a new table with values ranging from -1 to 1, reaching at least one of these, unless there’s no data.

table
sinusoid(freq, phase=0.0)[source]

Sinusoid based on the optimized math.sin

impulse(dur=None, one=1.0, zero=0.0)[source]

Impulse stream generator.

Parameters:dur – Duration, in number of samples; endless if not given.
Returns:Stream that repeats “0.0” during a given time duration (if any) or endlessly, but starts with one (and only one) “1.0”.
karplus_strong(freq, tau=20000.0, memory=<function white_noise>)[source]

Karplus-Strong “digitar” synthesis algorithm.

Parameters:
  • freq – Frequency, in rad/sample.
  • tau – Time decay (up to 1/e, or -8.686 dB), in number of samples. Defaults to 2e4. Be careful: using the default value will make duration different on each sample rate value. Use sHz if you need that independent from the sample rate and in seconds unit.
  • memory – Memory data for the comb filter (delayed “output” data in memory). Defaults to the white_noise function.
Returns:

Stream instance with the synthesized data.

Note

The fractional delays are solved by exponent linearization.

See also

sHz
Second and hertz constants from samples/second rate.
white_noise
White noise stream generator.