1

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'
halfer
  • 19,824
  • 17
  • 99
  • 186
shahid hamdam
  • 751
  • 1
  • 10
  • 24
  • 1
    Can you post a [mcve] along with the complete error message/stack? That would make it much easier to help. – Hendrik Sep 17 '19 at 07:16
  • @hendrik I have added the example – shahid hamdam Sep 17 '19 at 07:43
  • 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 Sep 17 '19 at 07:51
  • please check it now. I just want to generate audio from spectrogram. Using librosa is not mandatory. – shahid hamdam Sep 17 '19 at 08:05
  • Checked. I'm sorry, but the best I can come up with is my [answer below](https://stackoverflow.com/a/57970048/942774). – Hendrik Sep 17 '19 at 13:01

2 Answers2

1

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.

Christian Pao.
  • 484
  • 4
  • 13
0

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.

Hendrik
  • 5,085
  • 24
  • 56