Advertisement
mojomojomojo

Plot Audio dB Level

Jul 2nd, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. # https://stackoverflow.com/questions/13243690/decibel-values-at-specific-points-in-wav-file/13244011#13244011
  4.  
  5. import scipy.io.wavfile
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. import sys, argparse, os, os.path
  9.  
  10. parser = argparse.ArgumentParser('plot the dB level of a .WAV file')
  11. parser.add_argument('wav_file')
  12. cmdline = parser.parse_args()
  13.  
  14. samprate, wavdata = scipy.io.wavfile.read(cmdline.wav_file)
  15. numchunks = len(wavdata)/10
  16. chunks = np.array_split(wavdata.astype(np.int64), numchunks)
  17.  
  18. ref_level = (1 << 15)-1 # 16-bit (signed) PCM
  19. dbs = [20*np.log10( max(np.sqrt(np.mean(chunk**2)),.0001) / ref_level ) for chunk in chunks]
  20.  
  21. total_time_sec = len(wavdata) / samprate
  22. t = np.linspace(0,total_time_sec,len(wavdata)/10)
  23. plt.plot(t,dbs,'k-')
  24. plt.title(os.path.basename(cmdline.wav_file))
  25. plt.grid(True)
  26. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement