audio_visualize_2.py 1013 B

1234567891011121314151617181920212223242526272829303132
  1. def audio_visualize_2d(self):
  2. t = np.linspace(0, 2. * np.pi, self.window * 2)
  3. xf = np.cos(t)
  4. yf = np.sin(t)
  5. fig, ax = plt.subplots(figsize=(7, 7))
  6. lf, = ax.plot(xf, yf, lw=1, color='lightblue')
  7. ax.set_xlim(-3, 3)
  8. ax.set_ylim(-3, 3)
  9. ax.set_axis_off()
  10. plt.show(block=False)
  11. plt.pause(self.record_delay)
  12. while self.stream.is_active():
  13. data = self.stream.read(self.window)
  14. data_int = struct.unpack(str(self.window * 2) + 'B', data)
  15. y_detrend = detrend(data_int)
  16. yft = np.abs(np.fft.fft(y_detrend))
  17. y_vals = yft / (64 * self.window)
  18. ind = np.where(y_vals > (np.max(y_vals) + np.min(y_vals)) / 2)
  19. y_vals[ind[0]] *= 4
  20. lf.set_xdata(xf + y_vals * np.cos(t))
  21. lf.set_ydata(yf + y_vals * np.sin(t))
  22. try:
  23. ax.figure.canvas.draw()
  24. ax.figure.canvas.flush_events()
  25. except TclError:
  26. self.stream.stop_stream()
  27. self.stream.close()
  28. break