5

i'm trying to use librosa to generate some data by cutting 1s pieces from some .wav file with a duration of 60s.

This part works, i create all my files and i can also listen to them via any player, but if i try to open them with librosa.load i receive this error:

>>> librosa.load('.\\train\\audio\\silence\\0doing_the_dishes.wav', sr=None)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "C:\Users\gionata\AppData\Local\Programs\Python\Python36\lib\site\packages\librosa\core\audio.py", line 107, in load
with audioread.audio_open(os.path.realpath(path)) as input_file: File "C:\Users\gionata\AppData\Local\Programs\Python\Python36\lib\site-packages\audioread\__init__.py", line 116, in audio_open 
raise NoBackendError()
audioread.NoBackendError

Do you have any suggestion? I create the file.wav with this function:

def create_silence():
    path=DB+"_background_noise_/"
    sounds = [x[len(DB):] for x in glob.glob(path+ '*.wav')]
    for elem in enumerate(sounds):
       sound=elem.split('\\')[1]
       print(sound)
       for j,i in enumerate(np.arange(0.0, 59.0, 0.3)):
           y, sr=librosa.load(DB+elem, sr=None, offset=i, duration=1.0)
           librosa.output.write_wav(DB+'silence/'+str(j)+sound, y, sr=sr, norm=False)

The problem only presents itself with file created by librosa, librosa.load has worked with other files with no problems at all.

Gionata Benelli
  • 357
  • 1
  • 3
  • 20

5 Answers5

5

It's about ffmpeg, if you use windows, you can solve this according to here and if you use linux, if can try:

sudo apt-get install libav-tools
renewu
  • 51
  • 1
  • 3
2

libav-tools is deprecated in ubuntu, so

sudo apt-get install ffmpeg 

did the trick

2
import librosa
audio_path='C:/Users/hp/name.wav' #location 
(xf, sr) = librosa.load(audio_path)

It have worked for me xf=array of sound file,sr=frequency

1

I solvede this, Librosa outputs the values as they are, in my case the np.array where float32 but the standard is 16 bit for each value, so changing the type does the trick:

def create_silence():
path=DB+"_background_noise_/"
maxv = np.iinfo(np.int16).max
sounds = [x[len(DB):] for x in glob.glob(path+ '*.wav')]
for elem in sounds:
    sound=elem.split('\\')[1]
    print(sound)
    for j,i in enumerate(np.arange(0.0, 59.0, 0.3)):
        y, fs=librosa.load(DB+elem, sr=None, offset=i, duration=1.0, mono=False)
        librosa.output.write_wav(DB+'silence/'+str(j)+sound, y=(y*maxv).astype(np.int16), sr=fs, norm=False)
Gionata Benelli
  • 357
  • 1
  • 3
  • 20
  • I was wondering, did sr change too? In your first code snippet, you get sr from "y, sr=librosa.load(DB+elem, sr=None, offset=i, duration=1.0)", but in the fixed code, you use fs=sr, where fs comes from "y, fs=librosa.load(DB+elem, sr=None, offset=i, duration=1.0, mono=False)". Did you have to change sr too for the code to work? – J.D Feb 05 '19 at 09:31
1

I couldn't get 吴连伟's or Gionata's solution to work, however this did the trick:

from scipy.io import wavfile
import scipy
maxv = np.iinfo(np.int16).max
scipy.io.wavfile.write(path, sr, (y*maxv).astype(np.int16))

(Where path is the path and file name, y is the first output from librosa.load, and sr the second output from librosa.load)

This wav-file I could load with librosa in a later stage, so it solved the problem!

J.D
  • 425
  • 4
  • 8
  • 19