audio_visualize_3.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. def audio_visualize_3d(self):
  2. t = np.linspace(0, 2. * np.pi, self.window * 2)
  3. xf = np.cos(t)
  4. yf = np.sin(t)
  5. fig = plt.figure()
  6. ax = fig.gca(projection='3d')
  7. lf, = ax.plot(xf, yf, np.zeros(self.window * 2), lw=1, color='lightblue')
  8. ax.set_zlim(-0.2, 1.2)
  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. z_detrend = detrend(data_int)
  16. zf = np.abs(np.fft.fft(z_detrend))
  17. z_vals = zf / (64 * self.window)
  18. ind = np.where(z_vals > (np.max(z_vals) + np.min(z_vals)) / 2)
  19. z_vals[ind[0]] *= 4
  20. lf.set_xdata(xf)
  21. lf.set_ydata(yf)
  22. lf.set_3d_properties(z_vals)
  23. try:
  24. ax.figure.canvas.draw()
  25. ax.figure.canvas.flush_events()
  26. except TclError:
  27. self.stream.stop_stream()
  28. self.stream.close()
  29. break