我正在尝试找到一种方法来返回可以 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 窗口。
最佳答案
您可以使用 pixel array操纵 pygame 表面上的各个像素。
这是一个基于 pyaudio
输入捕获的最小示例:
import pygame
import pyaudio
import struct
import numpy as np
import samplerate
# initialise audio capture
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=False,
frames_per_buffer=CHUNK,
)
pygame.init()
width = 320
height = 240
clock = pygame.time.Clock()
screen = pygame.display.set_mode([width, height])
pygame.display.set_caption("Audio Input")
pixels = pygame.PixelArray(screen)
done = False
while not done:
# Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# capture data
data = struct.unpack(str(CHUNK) + "h", stream.read(CHUNK))
samples = samplerate.resample(data, width / len(data))
# need to rescale from 0 (top) to height (bottom)
norm = ((samples / 2 ** 15) + 1) / 2 # normalise to 0 - 1
rescaled = (1 - norm) * height
screen.fill(pygame.Color("black"))
for x in range(len(samples)):
y = int(rescaled[x])
pixels[x][y] = pygame.Color("green")
pygame.display.update()
clock.tick(60)
pygame.quit()
p.terminate()
这将显示如下内容:
对捕获的 block 进行重新采样以匹配窗口的宽度。根据this answer samplerate是重新采样音频的最佳方法,而不是基本的线性插值。
然后将信号的幅度缩放到窗口高度。 将屏幕尺寸更改为 800x400 如下所示:
您可能希望通过调整垂直缩放来调整增益。
编辑:要消除间隙,请使用pygame.draw.aaline(…)
在连续点之间绘制一条抗锯齿线。例如。用黑色填充屏幕后:
# draw lines between each point
for x in range(1, len(samples)):
y0 = int(rescaled[x-1])
y1 = int(rescaled[x])
pygame.draw.aaline(screen, pygame.Color("turquoise"), (x-1, y0), (x, y1))
然后你会看到类似的内容:
关于python - 从图 matplotlib 返回可 blit 的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67154213/