How to convert a .wav file to a spectrogram in python3

前端 未结 4 1802
-上瘾入骨i
-上瘾入骨i 2020-11-29 03:18

I am trying to create a spectrogram from a .wav file in python3.

I want the final saved image to look similar to this image:

I have tried the foll

4条回答
  •  星月不相逢
    2020-11-29 03:27

    import os
    import wave
    
    import pylab
    def graph_spectrogram(wav_file):
        sound_info, frame_rate = get_wav_info(wav_file)
        pylab.figure(num=None, figsize=(19, 12))
        pylab.subplot(111)
        pylab.title('spectrogram of %r' % wav_file)
        pylab.specgram(sound_info, Fs=frame_rate)
        pylab.savefig('spectrogram.png')
    def get_wav_info(wav_file):
        wav = wave.open(wav_file, 'r')
        frames = wav.readframes(-1)
        sound_info = pylab.fromstring(frames, 'int16')
        frame_rate = wav.getframerate()
        wav.close()
        return sound_info, frame_rate
    

    for A Capella Science - Bohemian Gravity! this gives:

    Use graph_spectrogram(path_to_your_wav_file). I don't remember the blog from where I took this snippet. I will add the link whenever I see it again.

提交回复
热议问题