我将.wav文件转换为频谱图,然后将其另存为图像。我收到以下错误

时间:2019-03-17 06:36:01

标签: python png wav spectrogram librosa

我遇到的错误

NoBackendError                            Traceback (most recent call last)
<ipython-input-17-c5b68785d1e2> in <module>
      1 direct = 'C:\\Users\\Patrick\\Desktop\\Filtered Instrument'
----> 2 get_spectrograms(direct)

<ipython-input-16-3e5dbf8ac09b> in get_spectrograms(directory)
     28             os.makedirs(os.path.join(data_path,audio_spectrogram,folder))
     29         for file in os.listdir(os.path.join(directory,folder)):
---> 30             wave,sr = librosa.load(os.path.join(directory,folder,file))
     31             #mfcc = librosa.feature.mfcc(y=wave, sr=sr, n_mfcc=18)
     32             #Using short time fourier with tow of 512

~\Anaconda3\lib\site-packages\librosa\core\audio.py in load(path, sr, mono, offset, duration, dtype, res_type)
    117 
    118     y = []
--> 119     with audioread.audio_open(os.path.realpath(path)) as input_file:
    120         sr_native = input_file.samplerate
    121         n_channels = input_file.channels

~\Anaconda3\lib\site-packages\audioread\__init__.py in audio_open(path)
    114 
    115     # All backends failed!
--> 116     raise NoBackendError()

NoBackendError: 

这很奇怪,因为转换并保存为.png很好,但是在对几个文件进行转换后,却出现了该错误。

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
# Change Directory

window_size = 1024
window = np.hanning(window_size)

path = 'C:\\Users\\Patrick\\Desktop\\Filtered Instrument'
audio_spectrogram = "audio_spectrogram"

def get_spectrograms(directory):
    #file system encoding is required to avoid errors 

    #Check if directory exit, Else create directory
    if(not os.path.exists(os.path.join(directory,audio_spectrogram))):
        os.makedirs(os.path.join(data_path,audio_spectrogram))

    #Get spectrogram of all files
    for folder in os.listdir(directory):
        #Check to see if the directory for the specific audio spectrogram exist. Else create it.
        if(folder == audio_spectrogram):
            continue
        elif(not os.path.exists(os.path.join(directory,audio_spectrogram,folder))):
            os.makedirs(os.path.join(data_path,audio_spectrogram,folder))
        for file in os.listdir(os.path.join(directory,folder)):
            wave,sr = librosa.load(os.path.join(directory,folder,file))
            #mfcc = librosa.feature.mfcc(y=wave, sr=sr, n_mfcc=18)
            #Using short time fourier with tow of 512
            stft  = librosa.core.spectrum.stft(wave, n_fft=window_size, hop_length=512, window=window)
            #sampling times two divided by sum of 1024 hannings
            out = 2 * np.abs(stft) / np.sum(window)
            ##Storing the displayed spectogram
            fig = plt.Figure()
            canvas = FigureCanvas(fig)
            ax = fig.add_subplot(111)
            p = librosa.display.specshow(librosa.amplitude_to_db(out, ref=np.max), ax=ax, y_axis='log', x_axis='time')
            ##Removing the .wav extension
            base = os.path.basename(os.path.join(directory,folder,file));
            fig.savefig(os.path.join(directory,audio_spectrogram,folder,os.path.splitext(base)[0]))

direct = 'C:\\Desktop\\Filtered Instrument'
get_spectrograms(direct)

有人说我安装了ffmpeg,但没有解决。 这是数据链接,https://github.com/Patrick0x00/Audio-Files 感谢您的帮助。

0 个答案:

没有答案