I have over 1000 audio files (it's just a initial development, in the future, there will be even more audio files), and would like to convert them to melspectrogram.
Since my workstation has a Intel® Xeon® Processor E5-2698 v3, which has 32 threads, I would like to use multithread to do my job.
My code
import os
import librosa
from librosa.display import specshow
from natsort import natsorted
import numpy as np
import sys
# Libraries for multi thread
from multiprocessing.dummy import Pool as ThreadPool
import subprocess
pool = ThreadPool(20)
songlist = os.listdir('../opensmile/devset_2015/')
songlist= natsorted(songlist)
def get_spectrogram(song):
print("start")
y, sr = librosa.load('../opensmile/devset_2015/' + song)
## Add some function to cut y
y_list = y
##
for i, y_i in enumerate([y_list]): # can remove for loop if no audio is cut
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128,fmax=8000)
try:
np.save('./Test/' + song + '/' + str(i), S)
except:
os.makedirs('./Test/' + song)
np.save('./Test/' + song + '/' + str(i), S)
print("done saving")
pool.map(get_spectrogram, songlist)
My Problem
However, my script freezes after finished the first conversion.
To debug what's going on, I commented out S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128,fmax=8000)
and replace it by S=0
.
Then the multi-thread code works fine.
What's wrong with the librosa.feature.melspectrogram
function? Does it not support multi-thread? Or is it a problem of ffmpeg? (When using librosa, it asks me to install ffmpeg before.)