API reference¶
Every name below is re-exported from the top level: music.note(...) works
regardless of which submodule defines it.
Sample rate¶
Every routine that takes a sample_rate defaults it to None, which means
the rate get_sample_rate returns: 44,100 Hz unless set_sample_rate
has changed it, for the rest of a script or, in a with statement, for
one block. A rate given to a routine always wins.
The sample rate routines use when they are not given one. |
|
Set the sample rate routines use when they are not given one. |
Synthesis¶
Notes¶
Each returns a numpy array of PCM samples. note is the plain wavetable
lookup, as MASS specifies it; the rest layer vibrato, pitch transitions,
frequency modulation and movement onto it.
Every routine here, and every envelope and generator below, takes its
length as a duration in seconds or as a number_of_samples, which
wins when given. A count is a whole number, 100 or 100.0, and a
fraction of one is refused rather than rounded; a negative duration or
count is no samples, except to the band-limited routines, which refuse
both. A frequency, a depth or a position that is NaN or infinite is
refused too, since there is no point of the table to read at it.
Synthesize a basic musical note. |
|
Synthesize a basic musical note with a phase. |
|
Synthesize a musical note with a vibrato. |
|
A note with a vibrato that also has a secondary oscillatory pattern. |
|
A note with a pitch transition: a glissando. |
|
A note with a pitch transition (a glissando) and a vibrato. |
|
A note with a glissando and a vibrato that also has a secondary oscillatory pattern. |
|
A note with an arbitrary sequence of pitch transition and a meta-vibrato. |
|
A sound with arbitrary meta-vibratos, transitions of frequency and localization. |
|
Synthesize a musical note with FM synthesis. |
|
A simple note with a transition of localization and resulting Doppler effect. |
|
A trill: notes alternating fast, one after another. |
Band-limited rendering¶
bandlimited_note renders a fixed pitch without the partials above the
Nyquist frequency that note folds back into the band, and
render_oversampled renders any generator at a multiple of the rate and
filters it, for the folding a table cannot prevent.
Band-limited synthesis describes both, with the oscillators for a
pitch that moves.
Render a static-pitch periodic note with prefiltered harmonics. |
|
Render at a higher sampling rate, low-pass, then downsample. |
Sensory stimulation¶
Stimuli for sensory-stimulation work, each rendering one technique catalogued in SSTIM, the Sensory Stimulation Vocabulary. Every docstring names the SSTIM term it implements and states whether the modulation is physically present in the signal or constructed by the listener.
Synthesize a binaural beat: |
|
Synthesize a monaural beat: |
|
Synthesize an isochronic tone train: |
|
Sinusoidally modulate a carrier: |
|
Sweep a carrier's pitch: |
|
Modulate a band of noise: |
|
Orbit a source around the listener: |
A protocol is a sequence of those rather than one of them.
StimulationSession holds that sequence and renders it as
one sound, joining the phases with crossfades centred on their boundaries
so that the session lasts exactly the sum of the durations it was given.
A sequence of stimuli rendered as one sound. |
|
One phase of a session: a stimulus, for a while, ramped into. |
Envelopes and noise¶
Synthesize an AM envelope or apply it to a sound. |
|
Synthesize a tremolo envelope or apply it to a sound. |
|
An envelope with multiple tremolos. |
|
Return a colored or user-refined noise. |
|
Noise in one band of frequencies, Gaussian in its samples. |
|
Generate a silence of specified length. |
Filters¶
Amplitude¶
Synthesize an ADSR envelope. |
|
A shorthand to make an ADSR envelope for a stereo sound. |
|
Creates a note with a vibrato and an ADSR envelope. |
|
A fade in or out. |
|
Cross fade two sounds over duration_ms milliseconds. |
|
An envelope for a linear or exponential transition of amplitude. |
|
An envelope with linear or exponential transitions of amplitude. |
Spectral and spatial¶
Apply a FIR filter to a sonic_array. |
|
Apply an IIR filter to a signal. |
|
Apply an artificial reverberation or return the impulse response. |
|
Make a mono sound stereo and localize it by a very naive method. |
|
Make a mono sound stereo and localize it by experimental methods. |
|
Localize a sound along a straight path between two angles. |
|
Place a mono sound with a measured pair of impulse responses. |
|
Makes a sequence of squeezes of the fragment in x. |
Filter design¶
iir applies coefficients; these compute them, from the four designs the
MASS article specifies. Cutoff, centre and bandwidth are fractions of the
sample rate, which fraction_of converts a frequency in Hertz into.
A one-pole low pass, from the article's equation |
|
A one-pole high pass, from the article's equation |
|
A two-pole band pass, from the article's |
|
A two-pole notch, from the article's |
|
A frequency in Hertz, as the fraction of the sample rate a design wants. |
Input and output¶
Read an audio file and return an array of its values. |
|
Write a sound to a file, mono or stereo, WAV or FLAC. |
|
Write a mono sound to a WAV or FLAC file. |
|
Write a stereo sound to a WAV or FLAC file. |
|
Play a sonic vector using the |
|
Normalize a mono sonic vector. |
|
Normalize a stereo sonic vector. |
Sequencing¶
Schedules notes and renders them as audio. |
Musical structures¶
Permutations, algebraic groups and change-ringing peals.
Get permutations of n elements in meaningful sequences. |
|
Shifts a permutation up or down by step positions. |
|
Computes the cyclic distance between the two elements of a permutation. |
|
A collection of named peals that can be acted on a domain. |
|
Uses permutations to make peals and represents peals as permutations. |
|
Presents plain changes as swaps and acts in domains to make peals. |
|
Prints a peal with colored numbers. |
Music theory¶
Scales, chords and the harmonic series, counted in semitones from a tonic or
root of zero – which is what pitch_to_freq takes, so any of these
becomes frequencies and then sound in two steps.
The semitones of a named scale, counted from tonic. |
|
A diatonic mode, built by rotating the steps as |
|
The first partials harmonics, in semitones above the fundamental. |
|
The semitones of a named chord, counted from root. |
|
One more third on top of a triad. |
|
Move one note of a chord by whole octaves. |
|
The number of semitones in an interval, from its traditional name. |
|
Every traditional name for an interval of this many semitones. |
|
The interval in semitones between two frequencies, rounded. |
|
How the article classifies an interval. |
The tables these read from are exported too: the scales, the chords, the article’s own table of partials, and the intervals.
Every scale this module knows, by name. |
|
The seven modes of |
|
The three minor scales of |
|
The step pattern every diatonic mode is a rotation of, from |
|
Every chord this module knows, by name. |
|
The four triads of |
|
The tetrads the article reaches by adding one more third: 10 semitones for a minor seventh, 11 for a major one. |
|
The first twenty partials as |
|
The simple intervals -- an octave wide at most -- by traditional notation, from the table of |
|
How the article classifies each simple interval. |
Bonds¶
Relations tying a note’s vibrato and tremolo to its frequency, so that a piece decides once how its notes behave rather than note by note.
A set of relations tying a note's characteristics to its frequency. |
|
A bond that rises with the frequency: |
|
A bond that falls with the frequency: |
|
A bond that changes in steps rather than continuously. |
Utilities¶
Conversions¶
Converts a frequency in Hertz to a MIDI note number. |
|
Converts a MIDI note number to the corresponding frequency in Hertz. |
|
Converts an interval in semitones to the ratio of its frequencies. |
|
Generates a list of frequencies based on a list of semitones and a starting frequency. |
|
Converts a difference in decibels to a difference in amplitude. |
|
Converts a difference in amplitude to a difference in decibels. |
|
Returns durations from rhythmic patterns. |
Combining sonic vectors¶
Join sounds end to end, mono or stereo. |
|
Mixes two sonic vectors. |
|
Mix sonic vectors of arbitrary lengths. |
|
Mix any number of sonic vectors, each at its own offset. |
|
Mixes two stereo sonic vectors. |
|
Mix two sonic vector by placing the beginning of the second one a specified number of seconds after the first one. |
|
Converts a sound vector to stereo format. |
|
Resolve stereo arguments for a function. |
|
Applies pan transitions to a sonic vector. |
|
Summarize a namespace of variables. |
Wavetables¶
Provides primary tables for waveform lookup. |
|
One period of a primary waveform, as a lookup table. |
|
The waveforms |
|
A period of a sine, 16,384 samples long and starting at zero: the table vibratos, tremolos and modulations read when given none. |
|
A period of a triangle wave, rising from -1 to 1 and falling back: the table |
|
A period of a square wave, at -1 for its first half and 1 for its second. |
|
A period of a sawtooth, rising from -1 to just below 1. |
The four tables are the ones used by default throughout the package, and
the names that appear in the synthesis signatures above, so
music.note(waveform_table=music.WAVEFORM_SINE) works. They are
read-only, since every routine given no table reads one of them: a table
to change is a copy, music.WAVEFORM_SINE * 0.5, or a new one from
waveform_table.
Singing¶
sing() sings a lyric to a melody, one syllable a note, with either of
two backends that sing the same score at the same pitches for the same
lengths, so that the two can be compared.
The default, backend="psola", is the package’s own singer. espeak-ng
says each syllable, and Praat’s pitch-synchronous overlap-add, through
praat-parselmouth, holds it at its note’s pitch and makes it the note’s
length. A syllable said in less time than its note is said again, slower,
down to 80 words a minute, and only the middle of its vowel is lengthened,
so that its consonants keep the length they were said with. A long note is
sung with a vibrato, 0.35 semitones each way at 5.5 Hz, setting in a
quarter of a second into it, and the notes on the meter’s strong beats are
sung up to 2.3 dB louder, as abc2midi accents them for eCantorix. It sings
eCantorix’s effects, "tremolo", "melt" and "flite", with the
package’s own tremolo, reverberation and resampling. It needs
espeak-ng and pip install 'music[singing]', and sing() says
which is missing.
backend="ecantorix" sings the same score with the external
eCantorix engine, the default until
the package had its own singer, kept as the reference. Run
setup_engine() once to clone it; it also needs git, make,
perl, espeak, abc2midi (the abcmidi package) and sox
on the system, and the Perl modules MIDI, Math::FFT,
URI::Escape and Digest::SHA. setup_engine() and sing()
say which are missing.
The examples singing_backends.py and singing_effects.py sing the
same songs, and the effects, with both. tools/compare_singing.py
measures both backends’ pitch, length and render time against the score;
tools/compare_singing_timing.py renders timings and syllable shapes for
a level-matched listening review; and tools/score_singing_asr.py has
Whisper, with pip install faster-whisper, count the words it hears in
each.
Sing a line of text to a melody. |
|
Clone the eCantorix repository into the user's cache directory. |
|
Return the path to the local eCantorix engine. |
|
Sing a short phrase with eCantorix, to check the engine works. |
Legacy¶
Synthesizer classes kept for backwards compatibility, and as material for making more music.
A configurable sequence walker that renders notes. |
|
Simple synthesizer for sound synthesis with vibrato, tremolo, and ADSR. |
|
A synthesizer that iterates through arbitrary lists of variables. |
Deprecated names¶
Second names for routines that have better ones. Each still works, warns
with a DeprecationWarning naming its replacement, and will be removed
in a major release. Renamed parameters still take their old names the
same way: those renamed for their unit – attack_ms and the rest of
adsr’s and adsr_stereo’s, cross_fade’s duration_ms and the
writers’ fades_ms – and those renamed to the names their siblings
give them – adsr’s duration, note_with_two_vibratos’
max_pitch_dev, secondary_max_pitch_dev and
secondary_vibrato_waveform_table, note_with_two_vibratos_glissando’s
two vibrato tables, and note_with_vibrato_seq_localization’s
vibratos_max_pitch_devs.
The name this had while it was the second mixer rather than the general one. |
|
The name this had before it was renamed for saying, in the name, how it differs from |
|
The name |