1

I already found a way to make a spectrogram from an audio file using code from this medium article:

'''
Function for converting all the waves files to image files.
https://medium.com/@keur.plkar/from-sound-to-image-to-building-an-image-classifier-for-sound-with-fast-ai-3294909b3885
'''

import matplotlib.pyplot as plt
def wavesToSpecs(audio_path:str, spec_path:str):
  
  fname = str(audio_path).split('/')[-1].split('.')[0]

  samples, sample_rate = librosa.load(audio_path,sr=None)
  # sample_rate, samples = wavfile.read(audio_path)

  fig, ax = plt.subplots(figsize=(5,5))
  ax.set_axis_off()
  ax.specgram(samples,Fs=2);
  fig.savefig(f'{fname}.png',bbox_inches="tight",pad_inches=0)
  plt.close(fig)
  del sample_rate, samples, fig, ax

wavesToSpecs("/content/wav/test.wav", "test.jpg")

Now how would I go about converting it back to audio from the image?

EDIT:

This is what I have now:

import librosa
from scipy import signal 
import scipy.io.wavfile as sf    

file = r"C:\desktop\file.wav"
# samples, sample_rate = sf.read(file)
sample_rate, samples = sf.read(file)

nperseg = int(sample_rate * 0.001 * 20)
frequencies, times, spectrogram = signal.spectrogram(samples, 
                                                     sample_rate, 
                                                     nperseg=nperseg, 
                                                     window=signal.hann(nperseg))

audio_signal = librosa.griffinlim(spectrogram)
print(audio_signal, audio_signal.shape)

sf.write('test.wav', audio_signal, sample_rate)

I'm getting the error: Exception has occurred: ValueError window is longer than input signal from line 14:

frequencies, times, spectrogram = signal.spectrogram(samples, 
                                                     sample_rate, 
                                                     nperseg=nperseg, 
                                                     window=signal.hann(nperseg))
jangles
  • 303
  • 1
  • 6
  • 22
  • 1
    Check this similar question in this link : https://stackoverflow.com/questions/60377585/how-can-i-reverse-a-scipy-signal-spectrogram-to-audio-with-python – Mohamed El Amine BELLEBNA Sep 16 '22 at 17:36
  • @MohamedElAmineBELLEBNA Thanks that is what I'm looking for. I'm having trouble using their code though. I'll edit my post with what I have now. – jangles Sep 21 '22 at 00:18

0 Answers0