1
votes

I am working on speech synthesis and I have constructed spectrograms using librosa. When I want to convert the spectrogram into audio to save as wav file, it creates problem. I looked for help and found that liborsa have a function mel_to_audio but that isn't working.

I used this function to get spectrogram of audio file.

librosa.feature.melspectrogram

Here is the function I am using to convert spectrogram to audio.

librosa.feature.inverse.mel_to_audio

But I am getting this error.

ModuleNotFoundError: No module named 'librosa.feature.inverse'

This is how I am reading file using librosa.

def read_audio_from_filename(filename):
    audio, sr = librosa.load(filename)
    D = np.abs(librosa.stft(audio))**2
    audio= librosa.feature.melspectrogram(y=audio, sr=sr, S=D)
    return audio

Is there any otherway to convert mel to audio and save it as wav file?

Minimal example:

import librosa
import librosa.display
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def read_audio_from_filename(filename):
    audio, sr = librosa.load(filename)
    D = np.abs(librosa.stft(audio))**2
    audio= librosa.feature.melspectrogram(y=audio, sr=sr, S=D)
    return audio
def convert_data():
    wav_filename = "Audio/Audio1.wav"
    audio = read_audio_from_filename(wav_filename)
    return audio
specto = convert_data()
res = librosa.feature.inverse.mel_to_audio(specto)

This is the error:

AttributeError: module 'librosa.feature' has no attribute 'inverse'
2
Can you post a minimal reproducible example along with the complete error message/stack? That would make it much easier to help.Hendrik
@hendrik I have added the exampleshahid hamdam
I'm sorry, but that's definitely not a [mvce]. Line 3 contains a typo mel), librosa.display_to_audio does not seem to exist. DATA_AUDIO_DIR is not defined. The imports for join and iglob are missing. Also, the full error stack is missing.Hendrik
please check it now. I just want to generate audio from spectrogram. Using librosa is not mandatory.shahid hamdam
Checked. I'm sorry, but the best I can come up with is my answer below.Hendrik

2 Answers

1
votes

The module librosa.feature.inverse was introduced in version 0.7. If you install librosa through conda and you don't have the latest version of conda then the version 0.6 will be installed.

A quick fix is installing librosa through pip.

0
votes

Your code works for me without error. I recommend reinstalling the latest version of librosa using a clean miniconda environment:

conda install -c conda-forge librosa

See also librosa installation instructions.