【发布时间】:2018-12-03 18:33:11
【问题描述】:
我的论文项目需要处理音频波形。而且我真的很难用 PyQtGraph 绘制我的音频波形并且找不到任何解决方案。
我试过用 matplotlib 绘制它,效果很好。
这是通过在 tkinter 窗口上嵌入 matplotlib 进行绘图的代码(不会发布 tkinter 画布嵌入的完整代码,因为我认为这不是我的问题的重点):
def waveform(self, audio):
self.waveFile = wave.open(audio, 'rb') # reading wave file
format = p.get_format_from_width(self.waveFile.getsampwidth())
channel = self.waveFile.getnchannels()
rate = self.waveFile.getframerate()
self.frame = self.waveFile.getnframes()
self.stream = p.open(format=format, # DATA needed for streaming
channels=channel,
rate=rate,
output=True)
durationF = self.frame / float(rate)
plt.title('Audio Waveform')
self.data_int = self.waveFile.readframes(self.frame)
self.data_plot = np.fromstring(self.data_int, dtype=np.short)
self.data_plot.shape = -1, 2
self.data_plot = self.data_plot.T
self.time = np.arange(0, self.frame) * (1.0 / rate)
self.fig = Figure(figsize=(30, 6), dpi=30)
self.fig.set_size_inches(34, 5.3)
self.fig.subplots_adjust(left=0.05, bottom=0.09, right=1.05, top=1.00, wspace=0, hspace=0)
self.fig.tight_layout()
self.xticks = np.arange(0, self.timeDuration, 15)
self.wavePlot = self.fig.add_subplot(111)
self.wavePlot.set_xticks(self.xticks)
if (channel == 1):
print 'Just mono files only'
self.wavePlot.plot(self.time, self.data_plot[0])
elif (channel == 2):
print 'Just stereo files only'
self.wavePlot.plot(self.time, self.data_plot[1], c="b")
self.wavePlot.tick_params(axis='both', which="major", labelsize=20)
self.wavePlot.tick_params(axis='both', which="minor", labelsize=30)
self.canvas = FigureCanvasTkAgg(self.fig, self.waveFrame)
self.canvas.get_tk_widget().grid(row=0, column=0, sticky="nsew")
当我发现 PyQtGraph 时,我转而使用它来进行更具交互性和快速的绘图。 现在,当我在 PyQt/PyQtGraph 中使用几乎完全相同的东西执行此操作时,出现了错误,我无法再绘制它了。
这是我在 PyQT4 中使用 PyQtGraph 进行绘图的代码:
def waveform(self, audio):
self.waveFile = wave.open(audio,'rb')
self.format = p.get_format_from_width(self.waveFile.getsampwidth())
channel = self.waveFile.getnchannels()
self.rate = self.waveFile.getframerate()
self.frame = self.waveFile.getnframes()
self.stream = p.open(format=self.format, # DATA needed for streaming
channels=channel,
rate=self.rate,
output=True)
durationF = self.frame / float(self.rate)
#Setting up waveform plot
self.data_int = self.waveFile.readframes(self.frame)
self.data_plot = np.fromstring(self.data_int, 'Int16')
self.time = np.arange(0, self.frame) * (1.0 / self.rate)
self.win = pg.GraphicsWindow(title='Spectrum Analyzer')
self.win.setWindowTitle('Spectrum Analyzer')
self.win.setGeometry(5, 115, 1910, 1070)
wf_xlabels = [(0, '0'), (2048, '2048'), (4096, '4096')]
wf_xaxis = pg.AxisItem(orientation='bottom')
wf_xaxis.setTicks([wf_xlabels])
wf_ylabels = [(0, '0'), (127, '128'), (255, '255')]
wf_yaxis = pg.AxisItem(orientation='left')
wf_yaxis.setTicks([wf_ylabels])
self.waveform = self.win.addPlot(
title='WAVEFORM', row=1, col=1, axisItems={'bottom': wf_xaxis,
'left': wf_yaxis},
)
self.waveform.plot(pen='c', width=3)
self.waveform.setYRange(0,255, padding=0)
self.waveform.setXRange(0,2 * self.chunk, padding = 0.005)
self.waveform.plot(self.time, self.data_plot)
当我运行上面的代码时,一个错误提示 ---in updateData raise Exception("X 和 Y 数组的形状必须相同——得到 %s 和 %s。" % (self.xData.shape, self.yData.shape)) 例外:X 和 Y 数组的形状必须相同——得到 (1535696L,) 和 (767848L,)。
我尝试了不同的绘图方式,但我还没有想出任何解决方案来绘制它。我想知道为什么 matplotlib 代码可以正常工作而 PyQtGraph 不能? 任何回应都会有很大帮助。
【问题讨论】:
标签: python matplotlib audio plot pyqtgraph