我正在运行一个python程序来实时绘制声音。我基本上是在遵循一般教程,但是没有正确绘制它。信封:python2.7,pyside + pyqtgraph,windows10,anaconda venv
我的代码如下:
# coding:utf-8
import numpy as np
import sys
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import pyaudio
sample_rate = 16000
frame_length = 1024
frame_shift = 80
class PlotWindow:
def __init__(self):
self.win = pg.GraphicsWindow()
self.win.setWindowTitle("realtime plotting")
self.win.resize(1100, 400)
self.plt = self.win.addPlot(title="updating_plot")
pg.setConfigOptions(antialias=True)
#self.ymin = -1000
#self.ymax = 1000
self.plt.setYRange(-1.0, 1.0)
self.curve = self.plt.plot(pen='y')
self.CHUNK = frame_length
self.RATE = sample_rate
self.audio = pyaudio.PyAudio()
self.stream = self.audio.open(format=pyaudio.paInt16,
channels=1,
rate=self.RATE,
input=True,
output=True,
frames_per_buffer=self.CHUNK)
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(5)
self.data = np.zeros(self.CHUNK)
def update(self):
self.data = self.AudioInput()
self.curve.setData(self.data)
def AudioInput(self):
ret = self.stream.read(self.CHUNK)
ret = np.frombuffer(ret, dtype="int16") / 32768
numpydata = self.stream.read(self.CHUNK)
numpydata = np.fromstring(numpydata, dtype=np.int16)
#print("RET", ret)
#print("NUMPYDATA", numpydata)
return numpydata
if __name__ == "__main__":
plotwin = PlotWindow()
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
QtGui.QApplication.instance().exec_()
并且绘制的图像是这样的。似乎顶部已以某种方式修剪,并且无法显示全部数据。
绘图图像
即使使用超级简单的代码版本,其行为也相同。
app = QApplication(sys.argv)
data = [10,20,30,40,50,60,70,80,90,100]
pg.plot(data)
sys.exit(app.exec_())
绘图图像