如何在 PyQtGraph 中绘制音频波形
How to plot audio waveform in PyQtGraph
我正在研究需要处理音频波形的论文项目。我真的很难用 PyQtGraph 绘制我的音频波形,找不到任何解决方案。
我试过用 matplotlib 绘制它并且效果很好。
这是一个代码,通过在 tkinter window 上嵌入 matplotlib 绘制(不会 post tkinter canvas 嵌入的完整代码,因为我不认为它不是我的问题的重点):
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
引发异常 ("X and Y arrays must be the same shape--got %s and %s." % (self.xData.shape, self.yData.shape))
例外:X 和 Y 数组的形状必须相同——得到 (1535696L,) 和 (767848L,)。
我尝试了不同的绘图方式,但我还没有想出任何解决方案来绘制它。我想知道为什么 matplotlib 代码可以正常工作而 PyQtGraph 不能?
任何回应都将是一个很大的帮助。
X 和 Y 的长度必须相同。在您的错误消息中,一个是另一个的两倍。
在您的 MatPlotLib 代码示例中,有一些代码似乎将 self.data_plot
重塑为两行。
self.data_plot.shape = -1, 2
self.data_plot = self.data_plot.T
PyQtGraph 代码中缺少此功能。
我正在研究需要处理音频波形的论文项目。我真的很难用 PyQtGraph 绘制我的音频波形,找不到任何解决方案。
我试过用 matplotlib 绘制它并且效果很好。
这是一个代码,通过在 tkinter window 上嵌入 matplotlib 绘制(不会 post tkinter canvas 嵌入的完整代码,因为我不认为它不是我的问题的重点):
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 引发异常 ("X and Y arrays must be the same shape--got %s and %s." % (self.xData.shape, self.yData.shape)) 例外:X 和 Y 数组的形状必须相同——得到 (1535696L,) 和 (767848L,)。
我尝试了不同的绘图方式,但我还没有想出任何解决方案来绘制它。我想知道为什么 matplotlib 代码可以正常工作而 PyQtGraph 不能? 任何回应都将是一个很大的帮助。
X 和 Y 的长度必须相同。在您的错误消息中,一个是另一个的两倍。
在您的 MatPlotLib 代码示例中,有一些代码似乎将 self.data_plot
重塑为两行。
self.data_plot.shape = -1, 2
self.data_plot = self.data_plot.T
PyQtGraph 代码中缺少此功能。