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.



