[Python] Audio to spectrogram image and back to audio

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am able to convert a wav file to spectrogram and then back again with an acceptable level of quality. I can plot and save that spectrogram as jpg file, but I have been able to import the jpg and convert it back to audio.

    I can convert the audio to a db scaled spectrogram

    import librosa
    x, sr = librosa.load(librosa.ex('trumpet'))
    X = librosa.stft(x)
    Xdb = librosa.amplitude_to_db(abs(X))



    And I am able to convert the db scaled spectrogram back to audio

    X2 = librosa.db_to_amplitude(Xdb)
    audio = librosa.griffinlim(X2)
    import soundfile as sf
    sf.write("test1.wav", audio, sr)


    I can save the array as a 32bit Tiff, and recreate the audio from that tiff file.

    from PIL import Image
    import numpy as np

    im =Image.fromarray(Xdb).convert('F')
    im.save("test.tiff")
    img = Image.open("test.tiff")
    recspec = np.array(img)

    X2 = librosa.db_to_amplitude(recspec)
    audio = librosa.griffinlim(X2)
    import soundfile as sf
    sf.write("test1.wav", audio, sr)


    I can plot the db scaled spectrogram and save it as a jpg

    from matplotlib import pyplot as plt
    import librosa.display
    fig = plt.figure(figsize=(10, 10), dpi=1000, frameon=False)
    ax = fig.add_axes([0, 0, 1, 1], frameon=False)
    ax.axis('off')
    librosa.display.specshow(Xdb, sr=sr, cmap='gray', x_axis='time', y_axis='hz')
    plt.savefig("test.jpg", bbox_inches=0, pad_inches=0)


    But I have been completely unable to figure out how to reimport the jpg in such a way as to recreate the audio from it. I realise is not as simple as just importing the jpg in the same way as the tiff and in saving it as a lossy format like jpg is going to result in some significant loss of quality, but I would be ok with that if the resulting audio at least slightly resembled what went in.I have looked into code to do similar things but their approach has been much more complicated such as using the colour channels to encode phase etc, I have been happy with the quality of the griffinlim reconstruction so am happy to skip that. If someone could point me in the right direction that would be great.

    Continue reading...

Compartilhe esta Página