librosa.util.valid_audio

librosa.util.valid_audio(y, mono=True)[source]

Determine whether a variable contains valid audio data.

If mono=True, then y is only considered valid if it has shape (N,) (number of samples).

If mono=False, then y may be either monophonic, or have shape (2, N) (stereo) or (K, N) for K>=2 for general multi-channel.

Parameters
ynp.ndarray

The input data to validate

monobool

Whether or not to require monophonic audio

Returns
validbool

True if all tests pass

Raises
ParameterError
In any of these cases:
  • type(y) is not np.ndarray

  • y.dtype is not floating-point

  • mono == True and y.ndim is not 1

  • mono == False and y.ndim is not 1 or 2

  • mono == False and y.ndim == 2 but y.shape[0] == 1

  • np.isfinite(y).all() is False

See also

numpy.float32

Notes

This function caches at level 20.

Examples

>>> # By default, valid_audio allows only mono signals
>>> filepath = librosa.ex('trumpet', hq=True)
>>> y_mono, sr = librosa.load(filepath, mono=True)
>>> y_stereo, _ = librosa.load(filepath, mono=False)
>>> librosa.util.valid_audio(y_mono), librosa.util.valid_audio(y_stereo)
True, False
>>> # To allow stereo signals, set mono=False
>>> librosa.util.valid_audio(y_stereo, mono=False)
True