Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

librosa.load() takes too long to load(sample) mp3 files

I am trying to sample (convert analog to digital) mp3 files via the following Python code using the librosa library, but it takes too much time (around 4 seconds for one file). I suspect this is because librosa doesn't support mp3 and hence uses the slower audioread to sample mp3

Code:

import time
import librosa

s = time.time()
for i in mp3_list[:10]: # list of mp3 file paths, doing for 10 files
    y, sr = librosa.load(i)

print('time taken =', time.time() - s)

time taken = 36.55561399459839

I also get this warning:

UserWarning: "PySoundFile failed. Trying audioread instead."

Obviously, this is too much time for any practical application. I want to know if there are better alternatives to this?

For comparison, it only took around 1.2 seconds total time to sample 10 same-sized wav conversions

like image 994
john doe Avatar asked Jan 22 '20 07:01

john doe


People also ask

Does librosa work with MP3 files?

Read specific formats librosa uses soundfile and audioread for reading audio. As of v0. 7, librosa uses soundfile by default, and falls back on audioread only when dealing with codecs unsupported by soundfile (notably, MP3, and some variants of WAV).

What is sampling rate in librosa?

The sampling rate is nothing but samples taken per second, and by default, librosa samples the file at a sampling rate of 22050; you can override it by your desired sampling rate. Take the product of sampling rate and length of the file you will get the total number of samples.

What does librosa load do?

load. Load an audio file as a floating point time series. Audio will be automatically resampled to the given rate (default sr=22050 ).

What is librosa in Python?

Librosa is a Python package for music and audio analysis. Librosa is basically used when we work with audio data like in music generation(using LSTM's), Automatic Speech Recognition. It provides the building blocks necessary to create the music information retrieval systems.


Video Answer


1 Answers

So the warning kind of hints it. The Librosa developers addressed a similar question in this GitHub question:

This warning will always occur when loading mp3 because libsndfile does not (yet/currently) support the mp3 format. Librosa tries to use libsndfile first, and if that fails, it will fall back on the audioread package, which is a bit slower and more brittle, but supports more formats.

This is confirmed in the Librosa-code: try ... except RuntimeError ...

So what you can do in this case is either implement your own load() that directly uses audioread to avoid the time wasted in the first block of librosa.load(), or you can use a different library such as pydub. Alternatively, you can use ffmpeg to convert your mp3 to wave before loading them.

like image 137
SuperKogito Avatar answered Sep 22 '22 11:09

SuperKogito