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.

`librosa.load()` silently ignores the `duration` parameter if it is a string

See original GitHub issue

Description

When using soundfile, librosa.load() silently ignores the duration parameter if it is a string. I haven’t tested other input types, but passing "4" as the duration should either work as expected or raise an exception rather than silently being ignored,

Steps/Code to Reproduce

  1. Ensure you’re loading a format supported by soundfile (eg. WAV or FLAC)
  2. Pass a string as the duration parameter of librosa.load():
audio, sr = librosa.load('file.flac', duration="4")  # Note that `duration` is a string
print((len(audio) / sr) == 4)

This generally comes out False since the duration parameter is ignored and the whole file is loaded.

Expected Results

The duration of the loaded audio should be 4 seconds (if the string is castable to an int), OR a TypeError (or some other exception) should be raised.

Versions

>>> import platform; print(platform.platform())
Windows-10-10.0.18362-SP0
>>> import sys; print("Python", sys.version)
Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)]
>>> import numpy; print("NumPy", numpy.__version__)
NumPy 1.16.4
>>> import scipy; print("SciPy", scipy.__version__)
SciPy 1.3.1
>>> import librosa; print("librosa", librosa.__version__)
librosa 0.7.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
konoikoncommented, Sep 21, 2019

I do not believe that it is being ignored. In fact, there is something more interesting going on. Here’s a part of load():

if duration is not None:
	frame_duration = int(duration * sr_native)

When duration="4" is passed, the string is multiplied and results in a frame duration of a sr_native number of 4s. Does that seem right?

1reaction
lostanlencommented, Sep 22, 2019

@konoikon i’m not a big fan of try-catch statements unless absolutely necessary. They are difficult to specify, difficult to test, and difficult to maintain. I am also wary of implicit type conversions at runtime. I would prefer a simple if statement on the type and value and duration with a clear error message, thereby deferring the responsibility of supplying the correct type to the user.

Read more comments on GitHub >

github_iconTop Results From Across the Web

librosa.load — librosa 0.10.0.dev0 documentation
Load an audio file as a floating point time series. Audio will be automatically resampled to the given rate (default sr=22050 ). To...
Read more >
Source code for librosa.core.audio
def __audioread_load(path, offset, duration, dtype): """Load an audio buffer using audioread. This loads one block at a time, and then concatenates the ...
Read more >
Source code for librosa.core.audio
def __soundfile_load(path, offset, duration, dtype): """Load an audio ... path, or file-like If provided, all other parameters are ignored, and the duration ......
Read more >
Changelog — librosa 0.9.0 documentation
#1418 librosa.load and librosa.stream can now operate directly on open soundfile ... a bug which ignored weights when step_sizes_sigma did not match length....
Read more >
librosa.get_duration — librosa 0.10.0.dev0 documentation
If provided, all other parameters are ignored, and the duration is calculated directly from the audio file. Note that this avoids loading 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