Guest User

Untitled

a guest
Jan 17th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import pyaudio
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import matplotlib.animation as animation
  5. import time
  6.  
  7. fig = plt.figure()
  8. ax = fig.add_subplot(1,1,1)
  9. ax.set_xlim((0,5000))
  10. ax.set_ylim((0,10000))
  11. line, = ax.plot([], [],c='k',lw=1)
  12.  
  13. def init():
  14. line.set_data([], [])
  15. return line,
  16.  
  17. def animate(i):
  18. data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
  19. n = len(data)
  20. x = np.linspace(0,44100/2,n/2)
  21. y = np.fft.fft(data)/n
  22. y = np.absolute(y)
  23. y = y[range(int(n/2))]
  24. line.set_data(x, y)
  25. return line,
  26.  
  27. CHUNK = 2000
  28. RATE = 44100
  29.  
  30. p=pyaudio.PyAudio()
  31. stream=p.open(format=pyaudio.paInt16,channels=1,rate=RATE,input=True,
  32. frames_per_buffer=CHUNK,input_device_index=2)
  33.  
  34. animation = animation.FuncAnimation(fig, animate, init_func=init,
  35. frames=200, interval=10, blit=True)
  36.  
  37.  
  38. plt.show()
Add Comment
Please, Sign In to add comment