1

I read some Audio file, labeled them, and together with their path, save the path and emotion of each Audioo file in a csv file. Now I want to read their path from the file and open them but I get this Error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
File ~\.conda\envs\nhashemi\lib\site-packages\librosa\core\audio.py:155, in load(path, sr, mono, offset, duration, dtype, res_type)
    153 else:
    154     # Otherwise, create the soundfile object
--> 155     context = sf.SoundFile(path)
    157 with context as sf_desc:

File ~\.conda\envs\nhashemi\lib\site-packages\soundfile.py:629, in SoundFile.__init__(self, file, mode, samplerate, channels, subtype, endian, format, closefd)
    627 self._info = _create_info_struct(file, mode, samplerate, channels,
    628                                  format, subtype, endian)
--> 629 self._file = self._open(file, mode_int, closefd)
    630 if set(mode).issuperset('r+') and self.seekable():
    631     # Move write position to 0 (like in Python file objects)

File ~\.conda\envs\nhashemi\lib\site-packages\soundfile.py:1183, in SoundFile._open(self, file, mode_int, closefd)
   1182     raise TypeError("Invalid file: {0!r}".format(self.name))
-> 1183 _error_check(_snd.sf_error(file_ptr),
   1184              "Error opening {0!r}: ".format(self.name))
   1185 if mode_int == _snd.SFM_WRITE:
   1186     # Due to a bug in libsndfile version <= 1.0.25, frames != 0
   1187     # when opening a named pipe in SFM_WRITE mode.
   1188     # See http://github.com/erikd/libsndfile/issues/77.

File ~\.conda\envs\nhashemi\lib\site-packages\soundfile.py:1357, in _error_check(err, prefix)
   1356 err_str = _snd.sf_error_number(err)
-> 1357 raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace'))

RuntimeError: Error opening 'C:/Users/external_dipf/Documents/Dataset/CREMA/AudioWAV/1001_IEO_FEA_HI.wav': File contains data in an unknown format.

During handling of the above exception, another exception occurred:

NoBackendError                            Traceback (most recent call last)
Input In [553], in <cell line: 3>()
      1 emotion='fear'
      2 path = np.array(data_path.Path[data_path.Emotions==emotion])[1]
----> 3 data, sampling_rate = librosa.load(path)
      4 create_waveplot(data, sampling_rate, emotion)
      5 create_spectrogram(data, sampling_rate, emotion)

File ~\.conda\envs\nhashemi\lib\site-packages\librosa\util\decorators.py:88, in deprecate_positional_args.<locals>._inner_deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
     86 extra_args = len(args) - len(all_args)
     87 if extra_args <= 0:
---> 88     return f(*args, **kwargs)
     90 # extra_args > 0
     91 args_msg = [
     92     "{}={}".format(name, arg)
     93     for name, arg in zip(kwonly_args[:extra_args], args[-extra_args:])
     94 ]

File ~\.conda\envs\nhashemi\lib\site-packages\librosa\core\audio.py:174, in load(path, sr, mono, offset, duration, dtype, res_type)
    172 if isinstance(path, (str, pathlib.PurePath)):
    173     warnings.warn("PySoundFile failed. Trying audioread instead.", stacklevel=2)
--> 174     y, sr_native = __audioread_load(path, offset, duration, dtype)
    175 else:
    176     raise (exc)

File ~\.conda\envs\nhashemi\lib\site-packages\librosa\core\audio.py:198, in __audioread_load(path, offset, duration, dtype)
    192 """Load an audio buffer using audioread.
    193 
    194 This loads one block at a time, and then concatenates the results.
    195 """
    197 y = []
--> 198 with audioread.audio_open(path) as input_file:
    199     sr_native = input_file.samplerate
    200     n_channels = input_file.channels

File ~\.conda\envs\nhashemi\lib\site-packages\audioread\__init__.py:116, in audio_open(path, backends)
    113         pass
    115 # All backends failed!
--> 116 raise NoBackendError()

NoBackendError: 

Here is my code to label and specify the label (emotion) of each file

CREMA ="C:/Users/external_dipf/Documents/Dataset/CREMA/AudioWAV/"
crema_directory_list = os.listdir(CREMA)

file_emotion = []
file_path = []

for file in crema_directory_list:
    # storing file paths
    file_path.append(CREMA + file)
    # storing file emotions
    part=file.split('_')
    if part[2] == 'SAD':
        file_emotion.append('sad')
    elif part[2] == 'ANG':
        file_emotion.append('angry')
    elif part[2] == 'DIS':
        file_emotion.append('disgust')
    elif part[2] == 'FEA':
        file_emotion.append('fear')
    elif part[2] == 'HAP':
        file_emotion.append('happy')
    elif part[2] == 'NEU':
        file_emotion.append('neutral')
    else:
        file_emotion.append('Unknown')
        
# dataframe for emotion of files
emotion_df = pd.DataFrame(file_emotion, columns=['Emotions'])

# dataframe for path of files.
path_df = pd.DataFrame(file_path, columns=['Path'])
CREMA_df = pd.concat([emotion_df, path_df], axis=1)
CREMA_df.head()

Here is were I save them in a CSV file

data_path = pd.concat([CREMA_df, RAVDESS_df, TESS_df, SAVEE_df], axis = 0)
data_path.to_csv("data_path.csv",index=False)
data_path.head()

and here I am trying to read the file. The error is related to the CREMA dataset.

emotion='fear'
path = np.array(data_path.Path[data_path.Emotions==emotion])[1]
data, sampling_rate = librosa.load(path)
create_waveplot(data, sampling_rate, emotion)
create_spectrogram(data, sampling_rate, emotion)
Audio(path)

I checked the path file, everything was correct. I can open other wav files. My librosa version is 0.9.1

Niboo
  • 11
  • 5
  • 3
    Could you please simplify the example? What happens if you just pass the file path directly to `librosa.load()`? Are you sure that interface expects a file path? – Iguananaut Jun 14 '22 at 10:38
  • yes I am sure and if I give it directly I get the same error. – Niboo Jun 14 '22 at 10:41
  • 1
    @Niboo it will be helpful if you can remove all the content about building a dataframe from your question as the problem is `librosa.load` not recognizing your file format. There are previous questions similar to this one, but unfortunately no clear accepted answer https://stackoverflow.com/questions/58821546/error-opening-wav-file-contains-data-in-an-unknown-format https://stackoverflow.com/questions/58810320/no-backend-error-while-reading-files-in-python https://stackoverflow.com/questions/62774301/can%C2%B4t-use-librosa-with-python-3 – Stuart Jun 14 '22 at 10:47
  • 1
    Ideally, post full traceback you get. Also - is this error with opening a single/specific file (i.e. are you able to open some other wav file)? Also which version of `librosa` do you use? – buran Jun 14 '22 at 10:49
  • 1
    Looking at the other questions, have you tried (1) making sure your [.wav file is not corrupted](https://stackoverflow.com/a/63480393/567595) especially if it comes from a zipped folder (2) ensuring [ffmpeg is installed](https://stackoverflow.com/a/62774334/567595)? – Stuart Jun 14 '22 at 11:12
  • problem solved :) Thank you, it was because of the corrupted file – Niboo Jun 16 '22 at 12:29
  • in my case the file is not even corrupt, and still it wouldn't work – Sadaf Shafi Dec 20 '22 at 07:59

0 Answers0