Advertisement
wandrake

Untitled

May 12th, 2012
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import struct
  4. import bisect
  5. import numpy
  6. import wave
  7. import sys
  8. import math
  9.  
  10. w = wave.open(sys.argv[1])
  11.  
  12. buf = w.readframes(65536)
  13. res = [0, 0, 0, 0, 0, 0, 0, 0]
  14. i = 0
  15.  
  16. blackman = numpy.blackman(65536)
  17. wss = sum(map(lambda x: x*x, blackman)) * 65536
  18.  
  19. while 65536*i < w.getnframes():
  20.     w.setpos(65536*i)
  21.     def valFromPCM(l):
  22.         f = lambda(x, y): ord(y)*256+ord(x)
  23.         t = map(f, zip(l[0::2], l[1::2]))
  24.         return map (lambda r: -(r ^ 65535) + 1 if r > 32768 else r, t)
  25.  
  26.     values = map(lambda(x, y): x*y, zip(valFromPCM(buf), blackman))
  27.  
  28.     i += 1
  29.     t = numpy.fft.fft(values)
  30.  
  31.     k = -1
  32.     deltaf = 44100./65536.
  33.    
  34.     for j in range (0, 65536):
  35.         k = bisect.bisect_left([20, 40, 160, 315, 2500, 5000, 10000, 20000], j*deltaf)
  36.         if k >= 0 and k <= 7:
  37.             res[k] += (abs(t[j]) ** 2.) / wss
  38.  
  39. res = map(lambda(x, y): x/y, zip(res, [20., 20., 120., 315.-160., 2500.-315., 2500., 5000., 10000.]))
  40. m = max(res)
  41. res = map(lambda(x, y): x/y, zip(res, 8*[m]))
  42.  
  43. for n in range(0, 8):
  44.     if res[n] != 0.:
  45.         print 10*math.log(res[n]),
  46.     else:
  47.         print "-infty",
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement