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_type
stror scalar The gain or attenuation per octave, in decibels: a number, such as
noise_type=3.5for 3.5 dB more each octave up, or a colour [1]:"brown"is -6 dB per octave"pink"is -3 dB per octave"white"is 0 dB per octave"blue"is 3 dB per octave"violet"is 6 dB per octave"black"is -12 dB per octave, though in theory any slope steeper than brown’s
- 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_samples
integerorNone The number of samples of the resulting sonic vector. If given, duration is ignored, and zero is no samples.
- sample_rate
integerorNone The sample rate to use. None, the default, is the rate
music.get_sample_rate()returns.- seed
integerorNone A seed for the random phases: the same seed draws the same noise again, and leaves NumPy’s global random state alone. None, the default, draws from that state, as this always did.
- noise_type
- Returns:
ndarrayA mono sequence of PCM samples, normalized to [-1, 1].
- Raises:
ValueErrorIf
noise_typeis neither a finite number of decibels per octave nor one of the named colours, ifsample_rateis not positive, or if the band is empty:min_freqabovemax_freqor 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, whicheq:pretoasks 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_freqtomax_freq, both included. The band used to be rounded down at both ends, which let in a component belowmin_freqwhenever it fell between two, and always left out one atmax_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