Advertisement
paster442

Untitled

Aug 30th, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. import numpy as np
  2. from pydub import AudioSegment
  3. from os import listdir, chdir
  4. from os.path import isfile, join
  5. from scipy.fft import *
  6. from scipy.io import wavfile
  7. from termcolor import colored
  8.  
  9. path = "C:\\Users\\HP\\Downloads\\TMF\\TMF-audio\\"
  10. chdir(path)
  11.  
  12. def amplitude(file): #.wav files only
  13. return AudioSegment.from_wav(path + file).max
  14.  
  15. def duration(file):
  16. return AudioSegment.from_wav(path + file).duration_seconds*1000
  17.  
  18. def freq(file):
  19. start_time = 0
  20. end_time = duration(file)
  21.  
  22. sr, data = wavfile.read(file)
  23. if data.ndim > 1:
  24. data = data[:, 0]
  25. else:
  26. pass
  27.  
  28. dataToRead = data[int(start_time * sr / 1000) : int(end_time * sr / 1000) + 1]
  29.  
  30. N = len(dataToRead)
  31. yf = rfft(dataToRead)
  32. xf = rfftfreq(N, 1 / sr)
  33.  
  34. idx = np.argmax(np.abs(yf))
  35. freq = xf[idx]
  36.  
  37. return freq
  38.  
  39.  
  40. amplitudes = list()
  41. for audio in list(f for f in listdir(path) if isfile(join(path, f))):
  42. try:
  43. amplitudes.append(amplitude(audio))
  44. print(colored((audio + " - " + str(amplitude(audio)) + " dbFS, " + str(freq(audio)) + " Hz"), "green"))
  45. except:
  46. pass
  47. amplitudes.sort()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement