librosa is not working for multiple files in looping, only for single files

742 views
Skip to first unread message

Daniel Yudi Miyahara Nakamura

unread,
Apr 23, 2023, 4:12:11 AM4/23/23
to librosa
Hello, 

librosa.load is perfectly running when I load a single audio file:

audio_dir = '/Users/labanfibios/Desktop/IC_MAX/teste3calls'
audio_files = []
for file_name in os.listdir(audio_dir): 
    file_path = os.path.join(audio_dir, file_name) 
    audio_files.append(file_path)
y, sr = librosa.load(audio_files[0]) # run just the first file

However, librosa.load is not running for multiple files. How can I fix it?
for file in audio_files:
    y, sr = librosa.load(file)


Output error:
/var/folders/wp/74vl7hq962n0vqdx3h3pfrd80000gn/T/ipykernel_80199/3925862108.py:3: UserWarning: PySoundFile failed. Trying audioread instead.
  y, sr = librosa.load(file)
/Users/labanfibios/anaconda3/lib/python3.10/site-packages/librosa/core/audio.py:184: FutureWarning: librosa.core.audio.__audioread_load
Deprecated as of librosa version 0.10.0.
It will be removed in librosa version 1.0.
  y, sr_native = __audioread_load(path, offset, duration, dtype)
---------------------------------------------------------------------------
LibsndfileError                           Traceback (most recent call last)
File ~/anaconda3/lib/python3.10/site-packages/librosa/core/audio.py:176, in load(path, sr, mono, offset, duration, dtype, res_type)
    175 try:
--> 176     y, sr_native = __soundfile_load(path, offset, duration, dtype)
    178 except sf.SoundFileRuntimeError as exc:
    179     # If soundfile failed, try audioread instead

File ~/anaconda3/lib/python3.10/site-packages/librosa/core/audio.py:209, in __soundfile_load(path, offset, duration, dtype)
    207 else:
    208     # Otherwise, create the soundfile object
--> 209     context = sf.SoundFile(path)
    211 with context as sf_desc:

File ~/anaconda3/lib/python3.10/site-packages/soundfile.py:658, in SoundFile.__init__(self, file, mode, samplerate, channels, subtype, endian, format, closefd)
    656 self._info = _create_info_struct(file, mode, samplerate, channels,
    657                                  format, subtype, endian)
--> 658 self._file = self._open(file, mode_int, closefd)
    659 if set(mode).issuperset('r+') and self.seekable():
    660     # Move write position to 0 (like in Python file objects)

File ~/anaconda3/lib/python3.10/site-packages/soundfile.py:1216, in SoundFile._open(self, file, mode_int, closefd)
   1215     err = _snd.sf_error(file_ptr)
-> 1216     raise LibsndfileError(err, prefix="Error opening {0!r}: ".format(self.name))
   1217 if mode_int == _snd.SFM_WRITE:
   1218     # Due to a bug in libsndfile version <= 1.0.25, frames != 0
   1219     # when opening a named pipe in SFM_WRITE mode.
   1220     # See http://github.com/erikd/libsndfile/issues/77.

LibsndfileError: Error opening '/Users/labanfibios/Desktop/IC_MAX/teste3calls/.DS_Store': Format not recognised.

During handling of the above exception, another exception occurred:

NoBackendError                            Traceback (most recent call last)
Cell In[92], line 3
      1 # Load calls
      2 for file in audio_files:
----> 3     y, sr = librosa.load(file)

File ~/anaconda3/lib/python3.10/site-packages/librosa/core/audio.py:184, in load(path, sr, mono, offset, duration, dtype, res_type)
    180 if isinstance(path, (str, pathlib.PurePath)):
    181     warnings.warn(
    182         "PySoundFile failed. Trying audioread instead.", stacklevel=2
    183     )
--> 184     y, sr_native = __audioread_load(path, offset, duration, dtype)
    185 else:
    186     raise exc

File ~/anaconda3/lib/python3.10/site-packages/decorator.py:232, in decorate.<locals>.fun(*args, **kw)
    230 if not kwsyntax:
    231     args, kw = fix(args, kw, sig)
--> 232 return caller(func, *(extras + args), **kw)

File ~/anaconda3/lib/python3.10/site-packages/librosa/util/decorators.py:60, in deprecated.<locals>.__wrapper(func, *args, **kwargs)
     51 """Warn the user, and then proceed."""
     52 warnings.warn(
     53     "{:s}.{:s}\n\tDeprecated as of librosa version {:s}."
     54     "\n\tIt will be removed in librosa version {:s}.".format(
   (...)
     58     stacklevel=3,  # Would be 2, but the decorator adds a level
     59 )
---> 60 return func(*args, **kwargs)

File ~/anaconda3/lib/python3.10/site-packages/librosa/core/audio.py:241, in __audioread_load(path, offset, duration, dtype)
    238     reader = path
    239 else:
    240     # If the input was not an audioread object, try to open it
--> 241     reader = audioread.audio_open(path)
    243 with reader as input_file:
    244     sr_native = input_file.samplerate

File ~/anaconda3/lib/python3.10/site-packages/audioread/__init__.py:132, in audio_open(path, backends)
    129         pass
    131 # All backends failed!
--> 132 raise NoBackendError()

NoBackendError:

Vincent Lostanlen

unread,
Apr 23, 2023, 6:13:20 AM4/23/23
to Daniel Yudi Miyahara Nakamura, librosa
Hello,

Pay attention to this line

Error opening '/Users/labanfibios/Desktop/IC_MAX/teste3calls/.DS_Store': Format not recognised.


In case you didn’t know, “.DS_Store” is a hidden file which is produced by OS X.

It seems like not all strings in your “audio_files” list actually point to audio files.

The solution is to filter your audio files list before calling librosa.load in a loop


I hope this helps


Vincent.



--
You received this message because you are subscribed to the Google Groups "librosa" group.
To unsubscribe from this group and stop receiving emails from it, send an email to librosa+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/librosa/6019de6c-373a-4981-b14e-eac11b0e2b10n%40googlegroups.com.

Brian McFee

unread,
Apr 24, 2023, 6:47:33 AM4/24/23
to librosa
Even better, you can use librosa's built in find_files function to search a directory for audio content instead of writing your own filter.  https://librosa.org/doc/latest/generated/librosa.util.find_files.html
Reply all
Reply to author
Forward
0 new messages