问题
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]
回答1:
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.
来源:https://stackoverflow.com/questions/34710011/how-do-i-go-from-sound-to-spectrum-then-back-to-sound-in-python