Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. def get_wav_info(wav_file):
  3. wav = wave.open(wav_file, 'r')
  4. frames = wav.readframes(-1)
  5. sound_info = pylab.frombuffer(frames, 'int16')
  6. frame_rate = wav.getframerate()
  7. wav.close()
  8. return sound_info, frame_rate
  9.  
  10. def get_audio_info(file):
  11. wav = AudioSegment.from_file(file)
  12. frames = wav.get_array_of_samples()
  13. sound_info = pylab.frombuffer(frames.tobytes())
  14. frame_rate = wav.frame_rate
  15. return sound_info, frame_rate
  16.  
  17. def graph_spectrogram(wav_file):
  18. if wav_file[-3:] == 'wav':
  19. sound_info, frame_rate = get_wav_info(wav_file)
  20. elif wav_file[-3:] == 'm4a':
  21. sound_info, frame_rate = get_audio_info(wav_file)
  22. print(type(sound_info))
  23. print(sound_info.shape)
  24. sound_info = sound_info[460000:920000]
  25. pylab.figure(num=None, figsize=(19, 12))
  26. pylab.subplot(111)
  27. pylab.title('spectrogram of %r' % wav_file)
  28. pylab.specgram(sound_info, Fs=frame_rate)
  29. # pylab.savefig('spectrogram.png')
  30. pylab.show()
  31.  
  32. if __name__ == '__main__':
  33. graph_spectrogram('rec_andrey.wav')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement