question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(1, n_samples) is not accepted as a mono waveform

See original GitHub issue

Description Librosa typically operates on one-dimensional, one-channel signals whose shape is (n_samples,). In some cases, stereo signals are accepted in “time-major” format, e.g. of shape (2, n_samples). But the analogous single-channel signal, whose shape is (1, n_samples), is rejected – at least by numpy.resample()

To Reproduce

import librosa
import numpy as np

# Conventional mono
mono_signal = np.random.randn(1000)
resampled_mono = librosa.resample(mono_signal, 1000, 2000)

# Conventional stereo
stereo_signal = np.random.randn(2, 1000)
resampled_stereo = librosa.resample(stereo_signal, 1000, 2000)

# Stereo-like single channel
onechan_signal = np.random.randn(1, 1000)
resampled_onechan = librosa.resample(onechan_signal, 1000, 2000)

Expected behavior resampled_mono == resampled_onechan[0]

Actual behavior

---------------------------------------------------------------------------
ParameterError                            Traceback (most recent call last)
<ipython-input-7-3776517cab8b> in <module>()
      1 onechan_signal = np.random.randn(1, 1000)
----> 2 resampled_onechan = librosa.resample(onechan_signal, 1000, 2000)
      3 print(resampled_onechan.shape)

1 frames
/usr/local/lib/python3.7/dist-packages/librosa/util/utils.py in valid_audio(y, mono)
    304     elif y.ndim == 2 and y.shape[0] < 2:
    305         raise ParameterError(
--> 306             "Mono data must have shape (samples,). " "Received shape={}".format(y.shape)
    307         )
    308 

ParameterError: Mono data must have shape (samples,). Received shape=(1, 1000)

Software versions

Linux-5.4.104+-x86_64-with-Ubuntu-18.04-bionic
Python 3.7.10 (default, May  3 2021, 02:48:31) 
[GCC 7.5.0]
NumPy 1.19.5
SciPy 1.4.1
librosa 0.8.1
INSTALLED VERSIONS
------------------
python: 3.7.10 (default, May  3 2021, 02:48:31) 
[GCC 7.5.0]

librosa: 0.8.1

audioread: 2.1.9
numpy: 1.19.5
scipy: 1.4.1
sklearn: 0.22.2.post1
joblib: 1.0.1
decorator: 4.4.2
soundfile: 0.10.3
resampy: 0.2.2
numba: 0.51.2

numpydoc: None
sphinx: 1.8.5
sphinx_rtd_theme: None
sphinxcontrib.versioning: None
sphinx-gallery: None
pytest: 3.6.4
pytest-mpl: None
pytest-cov: None
matplotlib: 3.2.2
presets: None

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
bmcfeecommented, Jul 20, 2021

Interesting. I wasn’t suggesting that we’d validate (via valid_audio) empty arrays with no time samples, like (1, 2, 3, 4, 0). Only those with nonzero time samples and zero channels (whatever non-last dimension you assign to channel): like, (1, 2, 3, 0, 256).

So an empty dimension is okay as long as it’s not in the time position? I guess the shape check would then be y.shape[-1] != 0?

I also agree on the padding problem, which would work in zero mode but not in reflect mode. So in my opinion, np.array([]) should not pass valid_audio because we can’t guarantee that a downstream STFT with pad_mode=reflect (the default!) will also pass NumPy’s np.pad.

Or, we could switch the default padding behavior to constant/zeros. It’s going into a major revision, after all. 😁

Back to my original question, i think it would be nice if the following assertions could hold:

x = librosa.mfcc(np.zeros((2, 1024)))
y = librosa.mfcc(np.zeros((0, 1024)))

assert x.dtype == y.dtype
assert x.shape[1:] == y.shape[1:]

Yeah, I think I agree with that. The lingering question for me is whether there are legitimate use cases for a length-0 signal. I can’t think of one right now, but there might be some weird edge cases that pop up, eg, in the cqt module where it might be nice to have a graceful fallback when the signal is sliced too thin.

1reaction
lostanlencommented, Jul 15, 2021

Valid audio should check finitude and dtype Shape can be anything If the trailing dimension is not the largest, throw a warning

sounds good to me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bpod Wiki - AudioPlayer - Google Sites
Note: Both speakers play mono waveforms. Bytes 3 - 6: The number of samples in the audio waveform expressed as a 32-bit integer...
Read more >
Use Quick Sampler to edit and play single samples in Logic Pro
Load any audio file into Quick Sampler, or create samples by recording directly ... Click one of these options above the waveform display:....
Read more >
Waveform User Guide 2021 - Tracktion
Chapter 1 - Introduction. Welcome to the Waveform User Guide. The goal of this guide is to help you learn Waveform. If.
Read more >
sound: The Waveform Matrix of a Sample Object in sound: A Sound ...
Here, filenames are not accepted for codes. The matrix can have one (for mono samples) or two rows (for stereo samples), where in...
Read more >
Using the WAVEFORMATEX Structure - Win32 apps
For PCM audio data on no more than two channels and with 8-bit or 16-bit samples, use the WAVEFORMATEX structure to specify the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found