librosa.onset.onset_strength_multi#

librosa.onset.onset_strength_multi(*, y=None, sr=22050, S=None, n_fft=2048, hop_length=512, lag=1, max_size=1, ref=None, detrend=False, center=True, feature=None, aggregate=None, channels=None, top_db=80.0, mode=None, **kwargs)[source]#

Compute a spectral flux onset strength envelope across multiple channels.

Onset strength for channel i at time t is determined by:

mean_{f in channels[i]} max(0, S[f, t+1] - S[f, t])
Parameters:
ynp.ndarray [shape=(…, n,)]

audio time-series. Multi-channel is supported.

srnumber > 0 [scalar]

sampling rate of y

Snp.ndarray [shape=(…, d, m)]

pre-computed spectrogram. Can be real-valued (magnitude/power) or complex-valued.

n_fftint > 0 [scalar]

length of the FFT frame for use in feature() if S is not provided.

hop_lengthint > 0 [scalar]

hop length for use in feature() if S is not provided.

lagint > 0

time lag for computing differences

max_sizeint > 0

size (in frequency bins) of the local max filter. set to 1 to disable filtering. Must be 1 when mode="complex".

refNone or np.ndarray [shape=(d, m)]

An optional pre-computed reference spectrum, of the same shape as S. If not provided, it will be computed from S. If provided, it will override any local max filtering governed by max_size. Must be None when mode="complex".

detrendbool [scalar]

Filter the onset strength to remove the DC component

centerbool [scalar]

Shift the onset function by n_fft // (2 * hop_length) frames. This corresponds to using a centered frame analysis in the short-time Fourier transform.

featurefunction, optional

Function for computing time-series features (spectrograms). By default:

  • In "flux" mode: uses librosa.feature.melspectrogram with fmax=sr/2 and log-amplitude scaling (power_to_db).

  • In "complex" mode: uses librosa.core.stft to preserve complex phase.

If a custom callable is provided, arguments are filtered to match its signature, allowing functions like librosa.stft, librosa.cqt, librosa.vqt, or custom callables to be passed directly.

aggregatefunction or False

Aggregation function to use when combining onsets at different frequency bins.

If False, then no aggregation is performed.

Default: np.mean

channelslist or None

Array of channel boundaries or slice objects. If None, then a single channel is generated to span all bands.

top_dbfloat or None

Threshold the spectrogram at top_db below its peak before the dB conversion in "flux" mode, as in power_to_db. Pass None to disable thresholding. Ignored when mode="complex".

mode{“flux”, “complex”} or None, default=None

Computation mode for onset strength:

  • None: infer automatically from S if provided ("complex" if numpy.iscomplexobj(S) else "flux"); defaults to "flux" if (y, sr) is provided.

  • "flux": standard spectral flux (rectified spectral difference).

  • "complex": complex-domain spectral flux.

**kwargsadditional keyword arguments

Additional parameters to feature(), if S is not provided.

Returns:
onset_envelopenp.ndarray [shape=(…, n_channels, m)]

array containing the onset strength envelope for each specified channel

Raises:
ParameterValueError

if neither (y, sr) nor S are provided

or if lag or max_size are not positive integers

or if mode is not one of None, "flux", or "complex"

or if mode="complex" and max_size > 1

or if mode="complex" and ref is not None

or if mode="complex" and S (or feature(...)) is real-valued

See also

onset_strength

Notes

This function caches at level 30.

Examples

First, load some audio and plot the spectrogram

>>> import matplotlib.pyplot as plt
>>> y, sr = librosa.loadx("choice", duration=5)
>>> D = np.abs(librosa.stft(y))
>>> fig, ax = plt.subplots(nrows=2, sharex=True)
>>> img1 = librosa.display.specshow(
...     D, vscale="dBFS", y_axis="log", x_axis="time", ax=ax[0]
... )
>>> ax[0].set(title="Power spectrogram")
>>> ax[0].label_outer()
>>> librosa.display.colorbar_db(img1, ax=ax[0])

Construct a standard onset function over four sub-bands

>>> onset_subbands = librosa.onset.onset_strength_multi(
...     y=y, sr=sr, channels=[0, 32, 64, 96, 128]
... )
>>> img2 = librosa.display.specshow(onset_subbands, x_axis="time", ax=ax[1])
>>> ax[1].set(ylabel="Sub-bands", title="Sub-band onset strength")
>>> fig.colorbar(img2, ax=[ax[1]])
../../_images/librosa-onset-onset_strength_multi-1.png