5

How do I take a wav file, transform it into an array of frequency intensities every couple ms, do something with that array then transform that new array back into a wav file.

Is there a library that looks something like this

wav_data = library.read_wav('aoeu.wav') # [0, 3, 201, ... etc]

spectrum = library.get_spectrum(wav_data)  
# [[0, 0, 0, .2, 0, .7, ... etc], 
#  [0, 0, 0, .3, 0, .8, ... etc],
#  ... etc] 

spectrum[:, 0] = 0 # kill the lowest frequency (assuming spectrum is a numpy array)

library.spectrum_to_wav(spectrum) # [0, 3, 201, ... etc]
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103

1 Answers1

8

Use librosa.stft and librosa.istft and read the audio file with librosa.load

import librosa

audio, sample_rate = librosa.load('song.wav')

spectrum = librosa.stft(audio)
reconstructed_audio = librosa.istft(spectrum)

sum(audio[:len(reconstructed_audio)] - reconstructed_audio)  # very close to 0

I'm using audio[:len(reconstructed_audio)] because information is lost in the transform. istft(stft(foo)) can return an array slightly shorter than foo and with slightly different values.

Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103