如何在Python中创建一个动画(从左到右移动)的波形?

huangapple go评论101阅读模式
英文:

How do i create an animated (moving from left to right) waveform in Python?

问题

以下是您提供的代码的翻译部分:

  1. # 导入所需的库
  2. import numpy as np
  3. import pyaudio as pa
  4. import matplotlib.pyplot as plt
  5. # 定义参数
  6. CHUNK = 1024
  7. FORMAT = pa.paInt16
  8. CHANNELS = 1
  9. RATE = 44100
  10. # 初始化 PyAudio
  11. p = pa.PyAudio()
  12. # 打开音频输入流
  13. stream = p.open(
  14. format=FORMAT,
  15. channels=CHANNELS,
  16. rate=RATE,
  17. input=True,
  18. output=True,
  19. frames_per_buffer=CHUNK
  20. )
  21. # 创建一个用于存储数据的缓冲区
  22. buffer = [0] * CHUNK
  23. # 创建绘图窗口
  24. fig, ax = plt.subplots()
  25. x = np.arange(0, 2 * CHUNK, 2)
  26. line, = ax.plot(x, np.random.rand(CHUNK), 'r')
  27. ax.set_ylim(-32000, 32000)
  28. ax.set_xlim(0, CHUNK)
  29. fig.show()
  30. # 进入数据处理循环
  31. while True:
  32. # 从音频流中读取数据
  33. data = stream.read(CHUNK)
  34. dataInt = np.frombuffer(data, dtype=np.int16)
  35. # 更新缓冲区数据
  36. for i in range(len(dataInt)):
  37. buffer.insert(0, dataInt[i])
  38. del buffer[-1]
  39. line.set_ydata(buffer)
  40. fig.canvas.draw()
  41. fig.canvas.flush_events()

注意:这是您提供的 Python 代码的翻译部分,其中包含了用于创建移动波形的相关功能。

英文:

I need to create moving waveform using Python and pyaudio. I thought about creating a buffer and filling it with data from audio stream. It sorta works but it's too slow. I tried changing the size of chunk to something smaller, but i always get an "Input overflowed" error.

This is the code i came up with

  1. import numpy as np
  2. import pyaudio as pa
  3. import matplotlib.pyplot as plt
  4. CHUNK = 1024
  5. FORMAT = pa.paInt16
  6. CHANNELS = 1
  7. RATE = 44100
  8. p = pa.PyAudio()
  9. stream = p.open(
  10. format = FORMAT,
  11. channels = CHANNELS,
  12. rate = RATE,
  13. input=True,
  14. output=True,
  15. frames_per_buffer=CHUNK
  16. )
  17. buffer = [0]*CHUNK
  18. fig, ax = plt.subplots()
  19. x = np.arange(0,2*CHUNK,2)
  20. line, = ax.plot(x, np.random.rand(CHUNK),'r')
  21. ax.set_ylim(-32000,32000)
  22. ax.ser_xlim = (0,CHUNK)
  23. fig.show()
  24. while True:
  25. data = stream.read(CHUNK)
  26. dataInt = np.frombuffer(data, dtype=np.int16)
  27. for i in range(len(dataInt)):
  28. buffer.insert(0, dataInt[i])
  29. del buffer[-1]
  30. line.set_ydata(buffer)
  31. fig.canvas.draw()
  32. fig.canvas.flush_events()

答案1

得分: 0

主要问题在于for循环中的更新太频繁,我们知道数据的每次更新和重新绘制画布都是耗时的。我建议每处理几个数据点进行一次更新。您可以使用像 UPDATE_SIZE 这样的变量来控制更新的频率:

英文:

The main problem is that the update in the for loop is too frequent and we know each update in the data and redrawing the canvas is time-consuming. I suggest making the update once every few datapoints. You can control update frequency using a variable like UPDATE_SIZE:

  1. UPDATE_SIZE = 32
  2. while True:
  3. data = stream.read(CHUNK)
  4. dataInt = np.frombuffer(data, dtype=np.int16)
  5. for i in range(0, len(dataInt), UPDATE_SIZE):
  6. buffer = dataInt[i:i+UPDATE_SIZE].tolist()[::-1] + buffer[:-UPDATE_SIZE]
  7. line.set_ydata(buffer)
  8. fig.canvas.draw()
  9. fig.canvas.flush_events()

huangapple
  • 本文由 发表于 2023年2月19日 20:16:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定