从图形 matplotlib 返回 blit-able numpy 数组 [英] Return blit-able numpy array from figure matplotlib

查看:70
本文介绍了从图形 matplotlib 返回 blit-able numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法来返回一个可以被 blitted 到 pygame 屏幕上的 numpy 数组.这是到目前为止的代码:


导入pyaudio导入结构将numpy导入为np导入matplotlib.pyplot作为plt导入时间CHUNK = 4000格式 = pyaudio.paInt16频道 = 1汇率= 42000p = pyaudio.PyAudio()selected_device_index = -1流 = p.open(格式=格式,频道= CHANNELS,费率=费率,input_device_index = chosen_device_index,输入=正确,输出=真,frame_per_buffer=CHUNK)plt.ion()无花果,ax = plt.subplots()x = np.arange(0, CHUNK)数据= stream.read(CHUNK)data_int16 = struct.unpack(str(CHUNK) + 'h', 数据)线,= ax.plot(x,data_int16)#ax.set_xlim([xmin,xmax])ax.set_ylim([-2 ** 15,(2 ** 15)-1])为真:数据= struct.unpack(str(CHUNK)+'h',stream.read(CHUNK))line.set_ydata(数据)fig.canvas.draw()fig.canvas.flush_events()


这是图表的示例图像:

我希望能够用这样的图表不断更新 PyGame 窗口.

解决方案

您可以使用

捕获的块被重新采样以匹配窗口的宽度.根据

您可能希望通过修改垂直缩放来调整增益.

编辑:要消除差距,请使用 pygame.draw.aaline(...)在连续点之间绘制抗锯齿线.例如.用黑色填充屏幕后:

 #在每个点之间绘制线对于范围x中的x(1,len(samples)):y0 = int(rescaled[x-1])y1 = int(重新缩放[x])pygame.draw.aaline(screen,pygame.Color("turquoise"),(x-1,y0),(x,y1))

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

I am trying to find a way to return a numpy array that can be blitted onto a pygame screen. Here is the code so far:


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()


This is an example image of what the graph looks like:

I would like to be able to constantly update a PyGame window with such a graph.

解决方案

You can use a pixel array to manipulate the individual pixels on your pygame surface.

Here's a minimal example based on your pyaudio input capturing:

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 will show something like this:

The captured chunk is resampled to match the width of the window. According to this answer samplerate is the best way to resample audio rather than a basic linear interpolation.

The magnitude of the signal is then scaled to the window height. Changing the screen dimensions to 800x400 looks like this:

You may wish to adjust the gain by tinkering with the vertical scaling.

EDIT: To eliminate the gaps, use pygame.draw.aaline(…) to draw an anti-aliased line between successive points. E.g. after filling the screen with black:

# 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))

Then you'll see something like:

这篇关于从图形 matplotlib 返回 blit-able numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆