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

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

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

问题

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

# 导入所需的库
import numpy as np
import pyaudio as pa
import matplotlib.pyplot as plt

# 定义参数
CHUNK = 1024
FORMAT = pa.paInt16
CHANNELS = 1
RATE = 44100

# 初始化 PyAudio
p = pa.PyAudio()

# 打开音频输入流
stream = p.open(
    format=FORMAT,
    channels=CHANNELS,
    rate=RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

# 创建一个用于存储数据的缓冲区
buffer = [0] * CHUNK

# 创建绘图窗口
fig, ax = plt.subplots()
x = np.arange(0, 2 * CHUNK, 2)
line, = ax.plot(x, np.random.rand(CHUNK), 'r')
ax.set_ylim(-32000, 32000)
ax.set_xlim(0, CHUNK)
fig.show()

# 进入数据处理循环
while True:
    # 从音频流中读取数据
    data = stream.read(CHUNK)
    dataInt = np.frombuffer(data, dtype=np.int16)
    
    # 更新缓冲区数据
    for i in range(len(dataInt)):
        buffer.insert(0, dataInt[i])
        del buffer[-1]
        line.set_ydata(buffer)
        fig.canvas.draw()
        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

import numpy as np
import pyaudio as pa
import matplotlib.pyplot as plt 

CHUNK = 1024
FORMAT = pa.paInt16
CHANNELS = 1
RATE = 44100

p = pa.PyAudio()

stream = p.open(
    format = FORMAT,
    channels = CHANNELS,
    rate = RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

buffer = [0]*CHUNK

fig, ax = plt.subplots()
x = np.arange(0,2*CHUNK,2)
line, = ax.plot(x, np.random.rand(CHUNK),'r')
ax.set_ylim(-32000,32000)
ax.ser_xlim = (0,CHUNK)
fig.show()
while True:
    data = stream.read(CHUNK)
    dataInt = np.frombuffer(data, dtype=np.int16)
    for i in range(len(dataInt)):
        buffer.insert(0, dataInt[i])
        del buffer[-1]
        line.set_ydata(buffer)
        fig.canvas.draw()
        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:

UPDATE_SIZE = 32
while True:
    data = stream.read(CHUNK)
    dataInt = np.frombuffer(data, dtype=np.int16)
    for i in range(0, len(dataInt), UPDATE_SIZE):
        buffer = dataInt[i:i+UPDATE_SIZE].tolist()[::-1] + buffer[:-UPDATE_SIZE]
        line.set_ydata(buffer)
        fig.canvas.draw()
        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:

确定