Guest User

Untitled

a guest
Apr 15th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. import pyaudio
  2. import wave
  3. import struct
  4. import numpy as np
  5. import time
  6. import itertools
  7.  
  8. CHUNK = 512
  9. FORMAT = pyaudio.paInt16
  10. CHANNELS = 1
  11. RATE = 44100
  12. num_chunks=1
  13. freqs=np.fft.fftfreq(CHUNK*num_chunks)
  14. print(len(freqs))
  15. p = pyaudio.PyAudio()
  16.  
  17. stream = p.open(format=FORMAT,
  18. channels=CHANNELS,
  19. rate=RATE,
  20. input=True,
  21. frames_per_buffer=CHUNK)
  22.  
  23.  
  24. while True:
  25. data = stream.read(CHUNK)
  26. data=struct.unpack('%dh'%CHUNK, data)
  27. w = np.fft.fft(data)
  28. amp=np.array([np.absolute(x) for x in w])
  29. idx=np.argmax(amp)
  30. max_freq=freqs[idx]
  31. freq_in_hertz=abs(max_freq*RATE)
  32. print(freq_in_hertz)
Advertisement
Add Comment
Please, Sign In to add comment