【发布时间】:2021-07-13 04:34:54
【问题描述】:
我正在尝试找到一种方法来返回一个可以被 blitted 到 pygame 屏幕上的 numpy 数组。到目前为止的代码如下:
import pyaudio
import struct
import numpy as np
import matplotlib.pyplot as plt
import time
CHUNK = 4000
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 42000
p = pyaudio.PyAudio()
chosen_device_index = -1
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input_device_index=chosen_device_index,
input=True,
output=True,
frames_per_buffer=CHUNK
)
plt.ion()
fig, ax = plt.subplots()
x = np.arange(0, CHUNK)
data = stream.read(CHUNK)
data_int16 = struct.unpack(str(CHUNK) + 'h', data)
line, = ax.plot(x, data_int16)
#ax.set_xlim([xmin,xmax])
ax.set_ylim([-2**15,(2**15)-1])
while True:
data = struct.unpack(str(CHUNK) + 'h', stream.read(CHUNK))
line.set_ydata(data)
fig.canvas.draw()
fig.canvas.flush_events()
这是图表外观的示例图像:
我希望能够使用这样的图表不断更新 PyGame 窗口。
【问题讨论】:
-
这能回答你的问题吗? Using matplotlib in pygame
标签: python matplotlib pygame