M
M
Maruto2021-07-19 22:53:01
Python
Maruto, 2021-07-19 22:53:01

Why is the original and restored sound different?

Good afternoon. I wrote this code.

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, sr = None)
    print(sr)
    D = np.abs(librosa.stft(audio))**2
    audio= librosa.feature.melspectrogram(y=audio, sr=sr, S=D)
    return audio

def convert_data():
    wav_filename = "path"
    audio = read_audio_from_filename(wav_filename)
    return audio

specto = convert_data()
res = librosa.feature.inverse.mel_to_audio(specto)

sf.write('path-1', res, 44100, 'PCM_24')

ipd.display(ipd.Audio('path'))
ipd.display(ipd.Audio('path-1'))


Also I noticed that len(res) and len(audio) are different. This is obviously distorting the audio, maybe the whole problem and only this. How to fix it?

In the first function, I output the sr that the library offers me, with the same sr I restore the audio at the end, but the sound is different. How to fix? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-07-20
@phaggi

I think because the default settings greatly spoil the audio recording. It is necessary to fine-tune the library when loading audio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question