music.CanonicalSynth¶
- class music.CanonicalSynth(**statevars)[source]¶
Bases:
objectSimple 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()andadsrSetup(), andtablesandsamplerate:fundamental_frequency,duration,vibrato_frequency,vibrato_depth,tremolo_frequency,tremolo_depthand 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:
- **statevars
dict Arbitrary keyword arguments for state variables.
- **statevars
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
- 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_depth
float,optional The depth of vibrato in semitones, by default 0.1.
- vibrato_frequency
float,optional The frequency of the vibrato oscillations in Hertz, by default 2.0.
- tremolo_depth
float,optional The depth of tremolo in decibels, by default 3.0.
- tremolo_frequency
float,optional The frequency of the tremolo oscillations in Hertz, by default 0.2.
- duration
float,optional The duration of the note in seconds, by default 2.
- fundamental_frequency
float,optional The fundamental frequency of the note in Hertz, by default 220.
- tablearray_like,
- adsrSetup(A=100.0, D=40, S=-5.0, R=50, render_note=False, adsr_method='absolute')[source]¶
Setup ADSR parameters.
- Parameters:
- A
float,optional Attack time in milliseconds, by default 100.
- D
int,optional Decay time in milliseconds, by default 40.
- S
float,optional Sustain level in decibels, by default -5.
- R
int,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_method
str,optional The ADSR method, by default “absolute”. Stored and not read either: “absolute” is the only one there is.
- A
- 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:
- **statevars
dict Arbitrary keyword arguments for state variables.
- **statevars
- 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.
- **statevars
dict Arbitrary keyword arguments for state variables.
- sonic_vectorarray_like,
- Returns:
- array_like
Tremolo envelope.
- absorbState(**statevars)[source]¶
Absorb state variables.
- Parameters:
- **statevars
dict Arbitrary keyword arguments for state variables.
- **statevars
- rawRender(**statevars)[source]¶
Render the sound without applying ADSR.
- Parameters:
- **statevars
dict Arbitrary keyword arguments for state variables.
- **statevars
- Returns:
- array_like
Rendered audio vector.
- render2(**statevars)[source]¶
Render the sound and apply ADSR.
- Parameters:
- **statevars
dict Arbitrary keyword arguments for state variables.
- **statevars
- 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 fromtables.sizerather than fromtable: a table of any other length played at the wrong rate and read only part of itself.