Caution
You're reading the documentation for a development version. For the latest released version, please have a look at 0.11.0.
librosa.to_mono
- librosa.to_mono(*signals, pad=True, norm=True, out=None)[source]
Convert an audio signal to mono by averaging samples across channels.
- Parameters:
- *signalsnp.ndarray [shape=(…, n)]
One or more signals to convert to mono. Each input signal can be mono or multi-channel, and they need not all have identical lengths.
- padbool
If True, pad the output to match the length of the longest input: n_out = max(y.shape[-1] for y in signals).
If False, trim the output to match the length of the shortest input: n_out = min(y.shape[-1] for y in signals).
- normbool
If True (default), signals are combined by averaging.
If False, signals are combined by summing.
- outnp.ndarray [shape=(n_out,)] or None
A pre-allocated output buffer. If provided, it must match the shape
(n_out,), and its dtype must be at least as precise as the result dtype; it is zeroed and filled in place and returned, avoiding a new allocation. IfNone(default), a new array is allocated.
- Returns:
- y_mononp.ndarray [shape=(n_out,)]
All signals combined together into a single mono signal. If
outwas provided, this is the same object asout.
See also
Notes
The dtype of the output signal will be the most general type that can accommodate all input signals. If the input signals have different dtypes, the output will be promoted to the most general type.
Warning
Any input signal with more than one channel will be downmixed to mono prior to being combined with other signals. This means that the following are generally not equivalent for multi-channel inputs y1 and y2 when norm=True:
>>> y_mono = librosa.to_mono(y1, y2) >>> y_mono = librosa.to_mono(np.vstack((y1, y2)))
Examples
Downmix a stereo input
>>> y, sr = librosa.loadx('trumpet', mono=False) >>> y.shape (2, 117601) >>> y_mono = librosa.to_mono(y) >>> y_mono.shape (117601,)
Mix three independent mono inputs with different lengths
>>> y_440 = librosa.tone(440.0, sr=22050, duration=1.0) >>> y_550 = librosa.tone(550.0, sr=22050, duration=1.5) >>> y_660 = librosa.tone(660.0, sr=22050, duration=2.0) >>> y_mono = librosa.to_mono(y_440, y_550, y_660, pad=True) >>> y_mono.shape (44100,)