music.noise

music.noise(noise_type='brown', duration=2, min_freq=15, max_freq=15000, number_of_samples=None, sample_rate=None, seed=None)[source]

Return a colored or user-refined noise.

Parameters:
noise_typestr or scalar

Specifies the decibels gain or attenuation per octave. It can be specified numerically (e.g. ntype=3.5 is 3.5 decibels gain per octave) or by strings: - “brown” is -6dB/octave - “pink” is -3dB/octave - “white” is 0dB/octave - “blue” is 3dB/octave - “violet” is 6dB/octave - “black” is -12/dB/octave but, in theory, is any < -6dB/octave See [1] for more information.

durationscalar

The duration of the noise in seconds.

min_freqscalar in [0, fs/2]

The lowest frequency allowed. A component exactly at it is kept.

max_freqscalar in [0, fs/2]

The highest frequency allowed, kept too if a component falls exactly on it. It should be > fmin.

number_of_samplesinteger or None

The number of samples of the resulting sonic vector. If given, duration is ignored, and zero is no samples.

sample_rateinteger or None

The sample rate to use. None, the default, is the rate music.get_sample_rate() returns.

seedinteger or None

Optional deterministic seed for local random phases. Default None preserves legacy use of NumPy’s global random state.

Returns:
ndarray

A mono sequence of PCM samples, normalized to [-1, 1].

Raises:
ValueError

If noise_type is neither a finite number of decibels per octave nor one of the named colours, if sample_rate is not positive, or if the band is empty: min_freq above max_freq or above the Nyquist frequency. A misspelt colour would otherwise render as silence or as an unrelated slope, a slope of NaN as NaN, and an empty band as silence.

Parameters:
Return type:

NDArray[float64]

Notes

Every colour is the article’s [1]: the inverse transform of a spectrum whose components, at \(f_i = i f_s / \Lambda\) (eq:branco), have a random phase \(x_i\) and a magnitude that changes by \(\beta\) decibels an octave,

\[c_i = e^{j x_i} \left(10^{\beta / 20}\right)^{ \log_2 (f_i / f_{\min})},\]

from \(f_{\min}\), min_freq, to max_freq, and zero outside them; conjugate above the Nyquist frequency, so the noise is real, and normalized to a peak of 1. \(\beta\) is -6 for brown (eq:marrom), -3 for pink (eq:rosa), 0 for white, 3 for blue (eq:azul), 6 for violet (eq:violeta) and -12 for black, which eq:preto asks only to fall faster than brown.

The components are the multiples of \(f_s / \Lambda\), which is \(1 / \Delta\) Hz for a noise of \(\Delta\) seconds, from min_freq to max_freq, both included. The band used to be rounded down at both ends, which let in a component below min_freq whenever it fell between two, and always left out one at max_freq. A noise too short to hold any component in the band is silence.

Cite the following article whenever you use this function.

References

[1]

Fabbri, Renato, et al. “Musical elements in the discrete-time representation of sound.” arXiv preprint arXiv:abs/1412.6853 (2017)

Examples

>>> hiss = noise("white", duration=0.5)     # flat, 0 dB per octave
>>> len(hiss)
22050
>>> darker = noise("brown", duration=0.5)   # -6 dB per octave
>>> tilted = noise(-9, duration=0.5)        # or any slope you name