【问题标题】:Return blit-able numpy array from figure matplotlib从图 matplotlib 返回 blit-able numpy 数组
【发布时间】: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 窗口。

【问题讨论】:

标签: python matplotlib pygame


【解决方案1】:

您可以使用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()

这将显示如下内容:

重新采样捕获的块以匹配窗口的宽度。根据this answersamplerate 是重新采样音频的最佳方式,而不是基本的线性插值。

然后将信号的幅度缩放到窗口高度。 将屏幕尺寸更改为 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))

然后你会看到类似的东西:

【讨论】:

  • 谢谢!这很有效 - 我将不得不再玩一些!
  • 不客气,如果有什么需要澄清的,请告诉我
猜你喜欢
  • 1970-01-01
  • 2021-08-22
  • 1970-01-01
  • 2016-05-23
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-28
相关资源
最近更新 更多