music.CanonicalSynth

class music.CanonicalSynth(**statevars)[source]

Bases: object

Simple synthesizer for sound synthesis with vibrato, tremolo, and ADSR.

All functions but absorbState return a sonic array. You can parametrize the synth in any function call. If you want to keep some set of states for specific calls, clone your CanonicalSynth or create a new instance. You can also pass arbitrary variables to use later on.

Parameters:
**statevars

Any attribute, set before the synth is set up. The ones it reads are those of synthSetup() and adsrSetup(), and tables and samplerate: fundamental_frequency, duration, vibrato_frequency, vibrato_depth, tremolo_frequency, tremolo_depth and the three tables. Every render method takes them too, and keeps them.

Examples

>>> synth = CanonicalSynth()
>>> sound = synth.render(duration=0.2)
>>> sound.shape
(8820,)
>>> lower = synth.render(duration=0.2, fundamental_frequency=110)
>>> len(lower) == len(sound)
True
__init__(**statevars)[source]

Initializes the synthesizer with given state variables.

Parameters:
**statevarsdict

Arbitrary keyword arguments for state variables.

Methods

__init__(**statevars)

Initializes the synthesizer with given state variables.

absorbState(**statevars)

Absorb state variables.

adsrApply(audio_vec)

Apply ADSR envelope to the audio vector.

adsrSetup([A, D, S, R, render_note, adsr_method])

Setup ADSR parameters.

rawRender(**statevars)

Render the sound without applying ADSR.

render(**statevars)

Render a note with given parameters.

render2(**statevars)

Render the sound and apply ADSR.

synthSetup([table, vibrato_table, ...])

Setup synth engine.

tremoloEnvelope([sonic_vector])

Calculate the tremolo envelope.

Attributes

table: ndarray
vibrato_table: ndarray
tremolo_table: ndarray
vibrato_depth: float
vibrato_frequency: float
tremolo_depth: float
tremolo_frequency: float
duration: float
fundamental_frequency: float
vibrato: bool
tremolo: bool
A: float
S: float
D: ndarray
R: ndarray
A_: ndarray
A_i: ndarray
D_i: ndarray
R_i: ndarray
ii: ndarray
Lambda_A: int
Lambda_D: int
Lambda_R: int
a_S: float
render_note: bool
adsr_method: str
tables: Any
samplerate: int
synthSetup(table=None, vibrato_table=None, tremolo_table=None, vibrato_depth=0.1, vibrato_frequency=2.0, tremolo_depth=3.0, tremolo_frequency=0.2, duration=2, fundamental_frequency=220)[source]

Setup synth engine. ADSR is configured separately.

Parameters:
tablearray_like, optional

The waveform table for sound synthesis, by default None.

vibrato_tablearray_like, optional

The waveform table for vibrato oscillatory pattern, by default None.

tremolo_tablearray_like, optional

The waveform table for tremolo oscillatory pattern, by default None.

vibrato_depthfloat, optional

The depth of vibrato in semitones, by default 0.1.

vibrato_frequencyfloat, optional

The frequency of the vibrato oscillations in Hertz, by default 2.0.

tremolo_depthfloat, optional

The depth of tremolo in decibels, by default 3.0.

tremolo_frequencyfloat, optional

The frequency of the tremolo oscillations in Hertz, by default 0.2.

durationfloat, optional

The duration of the note in seconds, by default 2.

fundamental_frequencyfloat, optional

The fundamental frequency of the note in Hertz, by default 220.

adsrSetup(A=100.0, D=40, S=-5.0, R=50, render_note=False, adsr_method='absolute')[source]

Setup ADSR parameters.

Parameters:
Afloat, optional

Attack time in milliseconds, by default 100.

Dint, optional

Decay time in milliseconds, by default 40.

Sfloat, optional

Sustain level in decibels, by default -5.

Rint, optional

Release time in milliseconds, by default 50.

render_notebool, optional

Stored as an attribute and not read: rendering the note here was never implemented. By default False.

adsr_methodstr, optional

The ADSR method, by default “absolute”. Stored and not read either: “absolute” is the only one there is.

adsrApply(audio_vec)[source]

Apply ADSR envelope to the audio vector.

Parameters:
audio_vecarray_like

Input audio vector.

Returns:
array_like

Audio vector with applied ADSR envelope.

render(**statevars)[source]

Render a note with given parameters.

Parameters:
**statevarsdict

Arbitrary keyword arguments for state variables.

Returns:
array_like

Rendered audio vector.

tremoloEnvelope(sonic_vector=None, **statevars)[source]

Calculate the tremolo envelope.

Parameters:
sonic_vectorarray_like, optional

Input sonic vector, by default None.

**statevarsdict

Arbitrary keyword arguments for state variables.

Returns:
array_like

Tremolo envelope.

absorbState(**statevars)[source]

Absorb state variables.

Parameters:
**statevarsdict

Arbitrary keyword arguments for state variables.

rawRender(**statevars)[source]

Render the sound without applying ADSR.

Parameters:
**statevarsdict

Arbitrary keyword arguments for state variables.

Returns:
array_like

Rendered audio vector.

render2(**statevars)[source]

Render the sound and apply ADSR.

Parameters:
**statevarsdict

Arbitrary keyword arguments for state variables.

Returns:
array_like

Rendered audio vector with applied ADSR envelope.

Notes

This is rawRender() with the envelope, and no tremolo. It used to repeat that routine but for the table’s length, which it took from tables.size rather than from table: a table of any other length played at the wrong rate and read only part of itself.