Fix Python Librosa NoBackendError When Reading an Audio – Librosa Tutorial

By | October 7, 2023

When we are using python librosa to read an audio, we may get this error: NoBackendError. In this tutorial, we will introduce you how to fix it.

This error looks like:

Fix Python Librosa NoBackendError When Reading an Audio - Librosa Tutorial

How to fix this error?

We can read this audio using soundfile or scipy.io.wavfile.read().

Here are two tutorials:

The Difference Between scipy.io.wavfile.read() and librosa.load() in Python – Python Tutorial

Understand librosa.load() is Between -1.0 and 1.0 – Librosa Tutorial

Then, we can save audio data to pcm and read it by librosa again.

Here is an solution.

  1. import librosa
  2. import soundfile as sf
  3. def save_file(audio_data, wave_file, sr = 8000):
  4. sf.write(wave_file, audio_data, sr, 'PCM_16')
  5. file = "test.mp3"
  6. try:
  7. data, sr = librosa.load(file, sr = 8000, mono=True)
  8. except Exception as e:
  9. audio_data, samplerate = sf.read(file, dtype="float32")
  10. save_file(audio_data, file, sr = samplerate )
import librosa
import soundfile as sf

def save_file(audio_data, wave_file, sr = 8000):
    sf.write(wave_file, audio_data, sr, 'PCM_16')

file = "test.mp3"

try:
    data, sr = librosa.load(file, sr = 8000, mono=True)
except Exception as e:
    audio_data, samplerate = sf.read(file, dtype="float32")
    save_file(audio_data, file, sr = samplerate )

In this solution, we read test.mp3 file using soundfile, then save it to new format.

Finally, we can use librosa to read test.mp3 successfully.