shahid hamdam
shahid hamdam

Reputation: 821

Convert spectrogram to audio using librosa functions

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'

Upvotes: 2

Views: 12151

Answers (2)

Christian Pao.
Christian Pao.

Reputation: 543

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.

Upvotes: 1

Hendrik
Hendrik

Reputation: 5310

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.

Upvotes: 0

Related Questions