sources

Sounds made from parameters alone: tones, noises, chirps and the LF glottal source, Klatt’s formant synthesizer, a voice rebuilt from a source-filter description, spectrotemporal ripples, random spectrograms with natural correlations, and trains of copies of one event.

sonore.sources.waveforms

Waveforms made from parameters: silence, tones, harmonic complexes, chirps, noises, and the LF glottal source.

Every generator returns a Sound normalized to RMS = 1. Anything random takes an rng argument (a seed or np.random.Generator) so stimuli can be regenerated exactly.

silence(duration: float, fs: float, *, n_channels: int = 1) → Sound[source]

Zeros, duration seconds long, in n_channels channels (RMS 0, the one generator not scaled to 1).

pure_tone(duration: float, fs: float, freq: float, *, phase: float = 0.0) → Sound[source]

cos(2*pi*freq*t + phase).

harmonic_complex(duration: float, fs: float, f0: float | tuple[ArrayLike, ArrayLike] | F0Contour, *, harmonics: TypeAliasForwardRef('ArrayLike') | None = None, amplitudes: ArrayLike | Callable[[ndarray, ndarray], ArrayLike] | Callable[[ndarray, ndarray, int], ArrayLike] | TimeFrequencyGrid | None = None, phases: TypeAliasForwardRef('ArrayLike') | Literal['cosine', 'sine', 'alternating', 'random', 'schroeder+', 'schroeder-'] = 'cosine', rng: int | Generator | None = None, f_max: float | None = None, ramp: float = 0.005, unvoiced: Literal['silence', 'noise'] = 'silence') → Sound[source]

Sum of harmonics a_n cos(n * Phi(t) + phi_n) of a fixed or moving F0.

f0 is either a number, for Phi = 2*pi*f0*t, or an F0 contour: a (times, values) pair or anything with .t and .f0, such as an F0Track, with values in Hz and 0 where unvoiced (one row per channel for a multichannel sound).

harmonics are the harmonic numbers; by default every one below Nyquist (fixed F0) or below f_max (contour). amplitudes is one value per harmonic, or a function amplitudes(t, f) giving the gain at times t and frequencies f (a spectral envelope, evaluated at every harmonic’s frequency). The function may also take the harmonic number as a third argument, amplitudes(t, f, n), and may return complex values: harmonic n is then Re(a_n exp(i(n Phi + phi_n))), so the angle of a_n adds to its phase and can change over time (an LF glottal pulse whose shape changes, as in glottal_source()). amplitudes may also be a spectral envelope, a SpectralEnvelope or any other TimeFrequencyGrid (power on a grid of times and frequencies), which is read point by point as amplitude, from its first channel. phases are starting phases: an array, one per harmonic, or one of "cosine", "sine", "alternating", "random", "schroeder+", "schroeder-".

A fixed F0 drops harmonics at or above Nyquist, with a warning. Given f_max, it also takes harmonics only below f_max by default and fades them out as a contour does (below); without it, nothing is faded.

A contour (docs/design/sources/harmonic-source.md) is filled across its unvoiced gaps and interpolated linearly to the sample rate, and the phase is its exact running integral, so every harmonic follows n times the contour with no jumps. Each harmonic fades out (cos^2) between 0.9 * f_max and f_max (default 0.45 * fs), so nothing aliases as the pitch rises. Harmonics are switched off where the contour is unvoiced, with Hann ramps ramp seconds long; unvoiced="noise" fills those stretches with white noise of the harmonics’ power instead of silence. Values are held beyond the first and last times. With a contour, phases has one value per harmonic number up to the highest that fits below f_max at the lowest voiced F0, unless harmonics is given.

Octave-numbered harmonics (harmonics=2 ** np.arange(n)) with a bell-shaped level over log frequency make a Shepard tone, and the same with a rising contour a Risset glide; the recipes are on the gallery page “Pitch class and height” (docs/gallery/music/chroma.py).

schroeder_complex(duration: float, fs: float, f0: float | tuple[ArrayLike, ArrayLike] | F0Contour, *, n_harmonics: int | None = None, sign: int = 1, **contour) → Sound[source]

Equal-amplitude harmonic complex with Schroeder phases sign * pi * n(n+1)/N. n_harmonics defaults to all below Nyquist (or below f_max for an F0 contour). f0 and the keyword arguments for a contour are as in harmonic_complex().

square_wave(duration: float, fs: float, f0: float | tuple[ArrayLike, ArrayLike] | F0Contour, *, phase: float = 0.0, bandlimited: bool = True, **contour) → Sound[source]

Square wave. Band-limited (odd harmonics below Nyquist) unless bandlimited=False, which aliases badly at high f0. A band-limited square wave also takes an F0 contour, as harmonic_complex() does.

sawtooth_wave(duration: float, fs: float, f0: float | tuple[ArrayLike, ArrayLike] | F0Contour, *, phase: float = 0.0, bandlimited: bool = True, **contour) → Sound[source]

Rising sawtooth. Band-limited unless bandlimited=False. A band-limited sawtooth also takes an F0 contour, as harmonic_complex() does.

pulse_train(duration: float, fs: float, f0: float | tuple[ArrayLike, ArrayLike] | F0Contour, *, phase: float = 0.0, bandlimited: bool = True, **contour) → Sound[source]

Periodic pulse (click) train.

Band-limited: equal-amplitude cosine-phase harmonics up to Nyquist; this also takes an F0 contour, as harmonic_complex() does. Otherwise single-sample impulses at the nearest sample to each period. phase (radians of the fundamental) shifts the pulses in time.

linear_chirp(duration: float, fs: float, f0: float, f1: float, *, phase: float = 0.0) → Sound[source]

Linear frequency sweep from f0 to f1.

exponential_chirp(duration: float, fs: float, f0: float, f1: float, *, phase: float = 0.0) → Sound[source]

Exponential (log-frequency) sweep from f0 to f1.

gaussian_noise(duration: float, fs: float, *, band: tuple[float, float] | None = None, tilt: float = 0.0, spectrum: Callable[[ndarray], ndarray] | tuple[ArrayLike, ArrayLike] | MeasuredSpectrum | None = None, n_channels: int = 1, rng: int | Generator | None = None) → Sound[source]

Gaussian noise, optionally spectrally shaped.

White Gaussian noise for the whole duration is shaped in the frequency domain: each bin of its FFT is multiplied by 10^(L(f)/20), the target level L(f) in dB at that bin from band, tilt and spectrum together, then transformed back, its mean removed, and scaled to RMS 1. The shape is the expected spectrum: the bins of any one draw scatter around it, as they do for any Gaussian noise.

Parameters:
  • band – (f_lo, f_hi) passband in Hz (brick-wall).

  • tilt – Spectral slope in dB/octave (-3 = pink, power 1/f; -6 = brown, power 1/f^2).

  • spectrum – Target spectrum level in dB (its shape only; the output is scaled to RMS 1): a Spectrum, a function f -> dB, or a (freqs, dB) pair, interpolated linearly in Hz. A Spectrum is silent outside its frequencies, since it was measured only there; a pair is held flat beyond its ends.

  • n_channels – Independent noise in each channel.

correlated_noise(duration: float, fs: float, *, corr: float = 1.0, rng: int | Generator | None = None, **noise_kwargs) → Sound[source]

Two-channel noise whose interaural correlation is exactly corr (in [-1, 1]), each channel at RMS 1. Extra keyword arguments go to gaussian_noise().

The symmetric-generator method, made exact as Hartmann and Cho (2011) describe: two independent noises x1 and x2 are made orthogonal (Gram-Schmidt) and equal in power, then mixed as a x1 + b x2 (left) and a x1 - b x2 (right) with a^2 - b^2 = corr. Without those two steps each draw would miss corr by chance, more so for few spectral components (narrow bands, short sounds). The correlation is the zero-lag one, over the whole sound.

iterated_ripple_noise(duration: float, fs: float, delay: float, *, gain: float = 1.0, iterations: int = 16, network: Literal['add-same', 'add-original'] = 'add-same', rng: int | Generator | None = None, **noise_kwargs) → Sound[source]

Iterated rippled noise (Yost, 1996). Pitch is at 1/delay.

network="add-same" (IRNS) iterates y <- y + g*delay(y); "add-original" (IRNO) iterates y <- x + g*delay(y). Implemented exactly in the frequency domain (fractional delays are fine), with a warm-up segment discarded so the output is stationary.

lf_harmonics(harmonics: ArrayLike, rd: float = 0.7, *, ra: float | None = None, rg: float | None = None, rk: float | None = None, flow: bool = False) → ndarray[source]

Complex Fourier coefficients of the LF pulse, one per harmonic number.

Harmonic k of a train of LF pulses is 2 |c_k| cos(k Phi + angle(c_k)) where Phi is the running phase of F0, so c_k depends only on k and the pulse’s shape, not on F0. The coefficients are those of the flow derivative scaled to -1 at the main excitation (Ee = 1); with flow=True, those of the flow itself, c_k / (2 pi i k), in units of Ee * T0. They are exact (a closed form), so a source built from them does not alias.

The shape is set by Fant’s (1995) rd (0.3-2.7, his main range, the one his predictions were fitted to; 0.7 is close to his typical adult male values), through his prediction of ra, rg and rk from it; or directly by ra = ta/T0, rg = T0/(2 tp) and rk = (te - tp)/tp (below 1), given together.

lf_pulse(x: ArrayLike, rd: float = 0.7, *, ra: float | None = None, rg: float | None = None, rk: float | None = None, flow: bool = False) → ndarray[source]

The LF flow derivative (or with flow=True, the flow) at times x in fractions of a period (wrapped into [0, 1)), scaled to -1 at the main excitation te (Ee = 1; for the laxest voices, near Rd 2.7, the open phase dips slightly below that just before it).

Evaluating the formula at sample times aliases, most for tense voices with an abrupt closure; use it to draw a period, and glottal_source() to make a sound. The shape is set as in lf_harmonics().

glottal_source(duration: float, fs: float, f0: float | tuple[ArrayLike, ArrayLike] | F0Contour, rd: float | tuple[TypeAliasForwardRef('ArrayLike'), TypeAliasForwardRef('ArrayLike')] = 0.7, *, flow: bool = False, f_max: float | None = None, ramp: float = 0.005) → Sound[source]

A voiced source of LF glottal pulses on a fixed F0 or an F0 contour.

The LF model (Fant, Liljencrants & Lin, 1985) describes one period of the glottal flow derivative: an exponentially growing sinusoid while the glottis opens, a sharp negative peak where it closes, and an exponential return phase. Fant’s (1995) single shape parameter Rd sets the whole pulse, from tense, pressed voice (small Rd: short open phase, abrupt closure, strong high harmonics) to lax, breathy voice (large Rd: long open phase, gradual closure, a dominant fundamental). The design, and the checks behind it, are in docs/design/sources/glottal-source.md.

The pulses are built from their harmonics (lf_harmonics()) by harmonic_complex(), so they do not alias, each period takes its own length on a moving F0, and f0, f_max and ramp work as there (unvoiced time windows are silent, and a contour that is never voiced gives silence). The result is the flow derivative, the source as it excites the vocal tract with radiation folded in, or with flow=True the flow itself; normalized to RMS 1.

rd is Fant’s (1995) shape parameter (0.3-2.7, default 0.7, close to his typical adult male values): a number, or a (times, values) track for a voice quality that changes, interpolated to every sample. LF models the periodic pulse only: breathy voice also needs aspiration noise, as in klatt_synthesize().

sonore.sources.klatt

A Klatt-style cascade/parallel formant synthesizer.

Speech from named acoustic parameters, after Klatt (1980): a voiced source and two noise sources, formant resonators in cascade (voicing and aspiration) and in parallel (frication), and radiation from the lips. Every control is something visible on a spectrogram, so the source-filter model can be taken apart one parameter at a time, and stimuli such as vowel and /ba/-/da/-/ga/ continua are set exactly. The design, and the checks behind it, are in docs/design/sources/klatt.md.

KLATT_DEFAULTS = mappingproxy({'F0': 100.0, 'AV': 60.0, 'SS': 1, 'RD': 0.7, 'AH': 0.0, 'AF': 0.0, 'AB': 0.0, 'F1': 500.0, 'B1': 60.0, 'F2': 1500.0, 'B2': 90.0, 'F3': 2500.0, 'B3': 150.0, 'F4': 3500.0, 'B4': 200.0, 'F5': 4500.0, 'B5': 250.0, 'F6': 4900.0, 'B6': 1000.0, 'A1': 0.0, 'A2': 0.0, 'A3': 0.0, 'A4': 0.0, 'A5': 0.0, 'A6': 0.0, 'FNP': 250.0, 'BNP': 100.0, 'FNZ': 250.0, 'BNZ': 100.0})

a neutral adult male vowel, voiced at 100 Hz, with no noise. Frequencies and bandwidths are in Hz, amplitudes in dB (60 = the source at unit level, 0 or less = off).

Type:

Every parameter klatt_synthesize() takes, with its default

klatt_synthesize(duration: float, fs: float, params: Mapping[str, float | tuple[ArrayLike, ArrayLike] | F0Contour] | None = None, *, rng: int | Generator | None = None, **kwargs: float | tuple[ArrayLike, ArrayLike] | F0Contour) → Sound[source]

Speech from Klatt’s (1980) parameters, as a cascade/parallel formant synthesizer.

Parameters are given by Klatt’s names, in params (a mapping, such as one made by klatt_continuum()) or as keyword arguments, which take precedence; anything not given takes its value in KLATT_DEFAULTS. Each is a number, or a (times, values) pair: times in seconds, interpolated linearly to every sample and held beyond the ends. A table of values every 5 ms, as Klatt used, is the pair (0.005 * np.arange(len(values)), values). F0 may also be an F0Track, to resynthesize a measured pitch contour.

  • Sources. Voicing: harmonics of F0 (harmonic_complex(), so the pitch can glide with no phase jumps or aliasing) at level AV, with the spectrum set by the source switch SS, numbered as in KLSYN88 (Klatt & Klatt, 1990). SS = 1 (the default): Klatt’s (1980) impulses through his glottal low-pass, falling about 12 dB per octave. SS = 3: Liljencrants-Fant glottal pulses (glottal_source()), their shape set by Fant’s RD, from tense (0.3, strong high harmonics) to lax (2.7, a dominant fundamental); at the default 0.7 its 3 kHz harmonic is about 12 dB weaker, relative to the first, than with SS = 1. KLSYN88’s SS = 2 (KLGLOTT88) is not included. Where F0 is 0 the harmonics are switched off. Aspiration (AH) and frication (AF, AB): white Gaussian noise. While voicing is on, both noises are amplitude modulated by a square wave at F0, 50% deep, as in Klatt’s synthesizer: full level in the half period that starts at each impulse of SS = 1 (periods counted from t = 0 with the harmonics’ own phase), half level in the other.

  • Cascade branch. Voicing and aspiration pass through the nasal pole and zero (FNP, BNP, FNZ, BNZ) and formants 1-5 (F1-F5, bandwidths B1-B5) in series: formant levels follow from the frequencies, as in a vowel. A formant default at or above Nyquist is left out (F5 at fs below 9 kHz).

  • Parallel branch. Frication passes through formants 1-6 side by side, each at its own level A1-A6 (dB at the formant’s peak), added with alternating signs so that they sum like the cascade between peaks; formants 2-6 get a differenced input, as in Klatt’s, to keep low frequencies out. AB adds the frication (at level AF) with no formants, so it is silent unless AF is on.

  • Radiation. The voiced source is differenced (+6 dB per octave), the radiation from the lips; the noises are white as they leave the lips.

Amplitudes are in dB: at 60 a source has RMS 1 as it leaves the lips (before the formants), so equal values mean equal source levels; each 20 dB is a factor of 10, and 0 or less is off. The result is normalized to RMS 1, like every generator; relative levels within one call are kept. Klatt’s quasi-sinusoidal voicing (AVS) and KLSYN88’s other voice-quality controls (open quotient, spectral tilt, flutter, double pulsing) are not included.

rng seeds the noise, so a call can be repeated exactly.

klatt_continuum(start: Mapping[str, float | tuple[ArrayLike, ArrayLike]], end: Mapping[str, float | tuple[ArrayLike, ArrayLike]], steps: int) → list[dict[str, float | tuple[ArrayLike, ArrayLike]]][source]

steps parameter sets for klatt_synthesize(), evenly spaced from start to end (both included), such as a /ba/-/da/ continuum.

A parameter given in only one of them takes its KLATT_DEFAULTS value in the other. The source switch SS is not interpolated: it must be the same at both ends. Numbers are interpolated directly; (times, values) pairs are interpolated value by value, so they must share their times (a number paired with a track is held at every one of its times).

sonore.sources.source_filter

A voice rebuilt from a source-filter description: an F0 track, a spectral envelope and, optionally, an aperiodicity, as the views measure them.

The sound is harmonics plus noise. The harmonics follow the F0 track and carry the part of the envelope that is not noise, S (1 - a), with S the envelope’s power and a the aperiodicity’s noise share; white noise, shaped over time and frequency, carries the rest, S a. Without an aperiodicity voicing is all or nothing, the classic source-filter resynthesis: a = 0 where the track is voiced and a = 1 where it is not. The checks behind it are in tools/check_voice_recipe_claims.py.

source_filter_synthesize(f0: tuple[ArrayLike, ArrayLike] | F0Contour, envelope: TimeFrequencyGrid | Callable[[ndarray, ndarray], ndarray], aperiodicity: TimeFrequencyGrid | None = None, *, fs: float | None = None, duration: float | None = None, rng: int | Generator | None = None, win_dur: float = 0.02) → Sound[source]

A voice from an F0 track, a spectral envelope and an aperiodicity.

On the aperiodicity’s grid of times and frequencies (without one, the envelope’s), with S the envelope’s power and a the noise share:

  1. Harmonics. harmonic_complex() on the F0 track, harmonic n carrying the power S (1 - a) over one harmonic spacing: an envelope reads unit white noise as 1, so a power density S with a harmonic every F0 is an amplitude a_n with a_n^2 / 2 = 2 F0 S / fs. harmonic_complex returns RMS 1, as every generator does, so its output is scaled back to that power.

  2. Noise. White noise of variance 1, which the envelope reads as 1, its STFT (win_dur long, on the grid’s hop) multiplied by sqrt(S a) and inverted. Unvoiced time windows have a = 1, so there the noise carries all of S.

  3. The two added.

The harmonics are switched on and off by the track alone: in the time windows it calls unvoiced their power is held at the nearest voiced window’s, and harmonic_complex() fades them over its 5 ms gate.

The harmonics start in cosine phase and keep their phases as the pitch moves, so each period’s waveform is a pulse, peakier than a voice’s.

Parameters:
  • f0 – An F0 track (an F0Track or a (times, f0) pair, one row per channel or one for all), usually the one the envelope and aperiodicity were measured with. It may be changed first (scale_f0(), a pitch change); the envelope stays, so the formants stay.

  • envelope – A SpectralEnvelope from any estimator, or any envelope read as envelope(t, f) (power, shape (n_channels, len(f), len(t))): another TimeFrequencyGrid, one moved by warp_frequency(), or a function. It is read at the aperiodicity’s times and frequencies. Without an aperiodicity it must be held on a grid (.t and .f, as every envelope view is).

  • aperiodicity – An Aperiodicity, or any TimeFrequencyGrid with an fs whose data is the amplitude ratio (its square the noise share). It sets the grid and the sampling rate. None (the default) is all-or-nothing voicing: no noise in the time windows the track calls voiced (the nearest given F0 above 0), all noise in the others.

  • fs – The sampling rate, needed only without an aperiodicity when the envelope has no fs (one built by hand).

  • duration – In seconds; by default one hop past the grid’s last time window.

  • rng – A seed or Generator for the noise.

  • win_dur – The length of the STFT that shapes the noise, in seconds.

sonore.sources.ripples

Spectrotemporal ripples: sounds defined by their modulation content.

A pattern is an envelope over time and log-frequency, E(t, x), where x is octaves above f_lo. ripple_sound() imposes a pattern on a carrier, which supplies the fine structure.

Patterns

Ripple

A moving ripple (Kowalski, Depireux & Shamma, 1996; Chi et al., 1999): 1 + depth * sin(2*pi*(rate*t + density*x) + phase). rate is in Hz and density in cycles/octave. With positive rate and density the ripple drifts downward in frequency; a negative rate drifts upward. Ripples add: Ripple(4, 1) + Ripple(-8, 2) is a RippleSum.

DynamicRipple

A dynamic moving ripple (Escabí & Schreiner, 2002) whose rate and density wander slowly and randomly within given ranges, for STRF estimation.

Any object with an envelope(t, x) method

An EnvelopePattern: it returns the envelope with shape (len(x), len(t)). The patterns above are such objects; a class of your own works without inheriting anything.

Any callable f(t, x)

Evaluated with t as a row and x as a column, so ordinary numpy broadcasting works, e.g. lambda t, x: 1 + 0.5 * np.sin(2*np.pi*(3*t + x**2)).

Carriers

"tones": log-spaced tones with random phases, the classic ripple carrier. "harmonic": harmonics of f0. "noise": narrowband Gaussian noise per channel. "low-noise": the fine structure of that noise with its envelope flattened, so it adds no envelope fluctuations of its own. Or any Sound, whose fine structure is used the same way.

All carriers are scaled to equal energy per octave, so switching carriers changes the fine structure but not the long-term spectrum.

class Ripple(rate: float, density: float, depth: float = 0.9, phase: float = 0.0, scale: Literal['linear', 'db'] = 'linear')[source]

Bases: _Pattern

A moving ripple 1 + depth*sin(2*pi*(rate*t + density*x) + phase).

Parameters:
  • rate (float) – Temporal modulation [Hz]. Positive: drifts down in frequency.

  • density (float) – Spectral modulation [cycles/octave].

  • depth (float) – scale="linear": modulation depth in [0, 1]. scale="db": peak-to-peak depth in dB (the envelope is 10**((depth/2)*sin(...)/20)).

  • phase (float) – Starting phase [radians].

rate: float
density: float
depth: float = 0.9
phase: float = 0.0
scale: Literal['linear', 'db'] = 'linear'
property direction: str[source]
envelope(t: ndarray, x: ndarray) → ndarray[source]
class RippleSum(components: tuple[Ripple, ...])[source]

Bases: _Pattern

A sum of ripples sharing one depth scale. Linear depths must total <= 1 so the envelope stays non-negative.

components: tuple[Ripple, ...]
property scale: str[source]
envelope(t: ndarray, x: ndarray) → ndarray[source]
class DynamicRipple(rate_range: tuple[float, float] = (-350.0, 350.0), density_range: tuple[float, float] = (0.0, 4.0), rate_change: float = 3.0, density_change: float = 6.0, depth: float = 45.0, seed: int | None = None, grid_fs: float = 1000.0)[source]

Bases: _Pattern

Dynamic moving ripple (Escabí & Schreiner, 2002).

The envelope is 10**((depth/2) * sin(2*pi*density(t)*x + Phi(t)) / 20) with Phi(t) = 2*pi * integral of rate(t). rate(t) and density(t) are independent, slowly varying random processes, uniformly distributed over their ranges, whose fastest changes are limited to rate_change and density_change Hz.

Note that rate(t) is the temporal modulation at x = 0 (f_lo). Elsewhere the local rate is rate(t) + x * d(density)/dt, because a changing density fans the ripple out across frequency, so some of the modulation power lies outside rate_range: about 7% for the defaults over 5 octaves (about 12% in the top octave), and about 1% with density_change=0.25. These are averages over 20 draws of 4 s; single draws range from 3 to 10% and from 0 to 6% (measured by tools/measure_docstring_numbers.py). Analyze with ModulationSpectrum.octave(..., scale="db"), since the pattern is defined in dB.

The defaults follow the ranges commonly used after Escabí & Schreiner (2002); check them against the study you are matching.

The pattern is fully determined by seed (drawn at random if not given, and stored). It depends on neither the sampling rate nor the duration: a shorter sound gets the start of a longer one’s pattern.

rate_range: tuple[float, float] = (-350.0, 350.0)
density_range: tuple[float, float] = (0.0, 4.0)
rate_change: float = 3.0
density_change: float = 6.0
depth: float = 45.0
seed: int | None = None
grid_fs: float = 1000.0
property scale: str[source]
trajectories(t: ndarray) → tuple[ndarray, ndarray][source]

rate(t) [Hz] and density(t) [cycles/octave] at times t.

envelope(t: ndarray, x: ndarray) → ndarray[source]
ripple_sound(pattern: EnvelopePattern | Callable[[ndarray, ndarray], ndarray], duration: float, fs: float, *, f_lo: float = 250.0, f_hi: float = 8000.0, carrier: Literal['tones', 'harmonic', 'noise', 'low-noise'] | Sound = 'tones', tones_per_octave: float = 20.0, f0: float = 100.0, bands_per_octave: float = 24.0, rng: int | Generator | None = None, chunk: int = 32) → Sound[source]

Synthesize a sound whose spectrotemporal envelope is pattern.

Parameters:
  • pattern – A Ripple, RippleSum, DynamicRipple, or a function f(t, x) of time [s] and octaves above f_lo.

  • f_lo – Frequency range [Hz]; x runs from 0 to log2(f_hi/f_lo).

  • f_hi – Frequency range [Hz]; x runs from 0 to log2(f_hi/f_lo).

  • carrier – "tones", "harmonic", "noise", "low-noise", or a Sound.

  • tones_per_octave – Density of the tone carrier. Spectral modulation up to half this (cycles/octave) is representable.

  • f0 – Fundamental of the harmonic carrier. Harmonic spacing in octaves is coarse at low harmonic numbers, which limits the representable ripple density there; a warning is issued when it’s exceeded.

  • bands_per_octave – Channel density for noise and Sound carriers.

  • (tones (The result has RMS = 1. Phases)

  • from (harmonics) and noise are drawn)

  • rng.

render(pattern: EnvelopePattern | Callable[[ndarray, ndarray], ndarray], filterbank: Filterbank, duration: float, fs: float) → TemporalEnvelope[source]

Evaluate any pattern (including a plain f(t, x)) on the band centers of an octave filterbank, giving a TemporalEnvelope. Compare it with a sound’s measured envelopes on the same filterbank.

sonore.sources.gaussian_spectrogram

Random spectrograms with natural correlations (McDermott, Wrobleski & Oxenham, 2011).

A spectrogram of log amplitudes, one cell per ERB band and time window, is drawn from a Gaussian whose correlations fall off exponentially in time and in frequency, as those of spoken words and animal calls roughly do. The result is a TemporalEnvelope; put it on a carrier with to_sound() to hear it. The sounds share coarse statistics with natural sources without being any recognizable one (docs/design/sources/gaussian-spectrogram.md).

gaussian_spectrogram(duration: float, fs: float, *, band_correlation_erb: float = 8.78, time_correlation: float = 0.154, sd_db: float = 14.1, n_bands: int = 39, f_lo: float = 20.0, f_hi: float = 4000.0, window: float = 0.02, rng: int | Generator | None = None) → TemporalEnvelope[source]

A random spectrogram after McDermott, Wrobleski & Oxenham (2011).

Each cell, one ERB band by one time window, holds a level in dB drawn from a multivariate Gaussian. Two cells correlate by exp(-band distance [ERB] / band_correlation_erb) times exp(-time distance [s] / time_correlation). The defaults are the paper’s: 39 half-cosine ERB filters from 20 to 4000 Hz, 20 ms raised-cosine windows overlapping by half, and decay constants of 0.075 per filter and 0.065 per window, which are about 8.8 ERB and 154 ms. The paper gives no variance; sd_db (the standard deviation of each cell) defaults to 14.1 dB, a variance of 0.5 in log10 amplitude, as in the author’s 2017 implementation.

The mean level of each band rises with its bandwidth in Hz, so the long-term spectrum is flat on average, as in the paper. Each band’s envelope is the sum over windows of the cell amplitude times that window’s raised cosine; the windows sum to 1, so this is the paper’s “scale each window to its cell”, written as an envelope. The edge bands of the filterbank get zero envelopes.

The sound comes from TemporalEnvelope.to_sound(). A noise carrier’s fine structure alone adds no envelope of its own:

env = so.gaussian_spectrogram(0.4, fs, rng=1)
sound = env.to_sound(carrier=so.gaussian_noise(0.4, fs, rng=2)).normalize().ramp(0.01)

(the paper used 10 ms ramps). Re-filtering through overlapping bands and windows makes the sound’s own spectrogram differ somewhat from the one drawn, as the paper notes.

The levels are in dB relative to the band of mean width, so the sound comes out loud, and its level varies from draw to draw with the spread of the cells. With the defaults, 1 s on a noise’s fine structure has an RMS 29 dB above 1 at the median, and 21 to 43 dB above it for 90% of draws (measured by tools/measure_docstring_numbers.py). Set the level afterwards, as normalize does above.

sonore.sources.events

Copies of one event scattered in time, from single events to a texture.

One short sound, the event, is copied many times. The time between onsets, each copy’s level and each copy’s pitch are drawn from distributions the caller gives, and a rate track can speed the onsets up or slow them down over time. At a low rate the events stand apart; at a high one they overlap many deep and the sum approaches Gaussian noise with the event’s spectrum (docs/design/sources/event-texture.md).

event_train(event: Sound | Callable[[float], Sound], duration: float, intervals: float | Distribution, *, rate_scale: float | tuple[ArrayLike, ArrayLike] = 1.0, level_db: float | Distribution = 0.0, pitch_cents: float | Distribution = 0.0, lead: float = 0.0, rng: int | Generator | None = None) → Sound[source]

Copies of event at random times, levels and pitches.

The intervals between onsets are drawn independently from intervals (seconds), so scipy.stats.expon(scale=1/30) gives Poisson timing at 30 events per second, the number 1/30 gives regular timing at the same rate, and scipy.stats.gamma(16, scale=1/(30*16)) lies between. Each copy’s level (dB, relative to the event as given) and pitch shift (cents) are drawn from level_db and pitch_cents. Each of the three takes a number, which every event shares, or anything with SciPy’s rvs(size=, random_state=).

rate_scale (a number, or (times, values) interpolated linearly) multiplies the rate over time by warping time: onsets are drawn on a clock that runs rate_scale times as fast as real time, so where it is 2 events come twice as often. With exponential intervals this is a Poisson process with that rate at each moment. It must not be negative.

With every pitch the same, the result is the event convolved with a train of impulses, one per onset with the copy’s amplitude (shot noise, for Poisson timing); otherwise the copies are added one by one. Pitch shifts are rounded to the nearest cent, and each distinct one is made once: a Sound event is read every ratio samples with the windowed sinc that move_sound() reads between samples with, widened by the ratio going up so nothing folds back (so a higher copy is also shorter), while an event given as a function event(ratio) -> Sound makes its own copy at that pitch ratio, so a synthetic event can keep its length.

Onsets are also drawn over lead seconds before the start, so that with lead at the event’s length an event already under way at time 0 is heard; the result is duration long and copies running past the end are cut. Each onset falls on the nearest sample. The overall level is left to the caller: for an event with no steady (DC) part, the expected power grows in proportion to the rate (a steady part adds a constant that grows with the square of the rate).

Intervals of zero are allowed (events at one onset), but a distribution that gives nothing else is refused, since the onsets would never move on.

docs/design/sources/event-texture.md measures what each distribution does to the sound’s statistics (tools/check_event_texture_claims.py).