wxpython繪制音頻效果
更新時間:2019年11月18日 11:16:32 作者:douzujun
這篇文章主要為大家詳細介紹了wxpython繪制音頻效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了wxpython繪制音頻的具體代碼,供大家參考,具體內容如下
#-*- coding: utf-8 -*-
################################################################################
## 使用wxPython的繪圖模塊wxPyPlot,需要數(shù)據(jù)可視化的時候,無需再借用其他的庫或模塊了
################################################################################
import numpy as np
import wx
import wx.lib.plot as wxPyPlot # 導入繪圖模塊,并命名為wxPyPlot
import wave
import pylab as pl
# 需要把數(shù)據(jù)封裝進入MyDataObject中
def MyDataObject():
# 50 個點的sin函數(shù),用藍色圓點表示
data1 = 2.*np.pi*np.arange(100)/100.
data1.shape = (50, 2)
data1[:, 1] = np.sin(data1[:, 0])
print ("debug:", data1.shape)
markers = wxPyPlot.PolyMarker(data1, legend='Green Markers', colour='blue', marker='circle', size=1)
# 50個點的cos函數(shù),用紅色表示
data2 = 2.*np.pi*np.arange(100)/100.
data2.shape = (50, 2)
print ("debug: data2", len(data2))
data2[:, 1] = np.cos(data2[:, 0])
lines = wxPyPlot.PolySpline(data2, legend='Red Line', colour='red')
GraphTitle = "Plot Data(Sin and Cos)"
return wxPyPlot.PlotGraphics([markers, lines], GraphTitle, "X Axis", "Y Axis")
# 解析wav數(shù)據(jù)
def MyWavData(wav_filename=""):
print('working')
# 打開wav文檔
file = wave.open("mic4.wav", "r")
# 讀取格式信息
# (nchannels, sampwidth,framerate, nframes, comptype, compname)
params = file.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
print (nchannels, sampwidth, framerate, nframes)
# 讀取波形數(shù)據(jù)
str_data = file.readframes(nframes)
# 文件使用完畢,關閉文件
file.close()
# 將波形數(shù)據(jù)裝換成數(shù)組
wave_data = np.fromstring(str_data, dtype=np.short)
wave_data.shape = (-1, 2)
wave_data = wave_data.T # 矩陣轉置
time = np.arange(0, nframes) * (1.0 / framerate)
# print ("debug: time:", len(time))
# print ("debug: wave_data:", len(wave_data[0][0:len(time)]))
# print ("debug: time:", time)
# print ("debug: wave:", wave_data)
time_and_wav = np.asarray([time, wave_data[0][0:len(time)]]).T
print ("debug: len of time and wav: ", len(time_and_wav))
print ("debug: time and wav: ", time_and_wav.shape)
lines = wxPyPlot.PolySpline(time_and_wav, legend='Blue Line', colour='blue')
GraphTitle = "the freq of wav file"
return wxPyPlot.PlotGraphics([lines, ], GraphTitle, "time/s", "fre/Hz")
class TestFrame1(wx.Frame):
def __init__(self, parent=None, id=wx.ID_ANY, title="Using wxPyPlot"):
wx.Frame.__init__(self, parent, id, title, size=(800, 600))
# 創(chuàng)建菜單欄
self.mainmenu = wx.MenuBar()
# 創(chuàng)建菜單
menu = wx.Menu()
menu.Append(100, 'Draw1', 'Draw plots1')
self.Bind(wx.EVT_MENU, self.OnPlotDraw1, id=100)
menu.Append(200, 'Draw_wav', 'Draw wav')
self.Bind(wx.EVT_MENU, self.OnPlotDraw_wav, id=200)
# 添加菜單到菜單欄
self.mainmenu.Append(menu, '&Plot')
# 設置菜單Bar
self.SetMenuBar(self.mainmenu)
# 創(chuàng)建狀態(tài)欄,顯示信息
self.CreateStatusBar(2)
self.pc = wxPyPlot.PlotCanvas(self) # 此處導入繪圖面板
def OnPlotDraw1(self, event): # 繪圖函數(shù)
self.pc.Draw(MyDataObject())
def OnPlotDraw_wav(self, event):
self.pc.Draw(MyWavData())
def main():
app = wx.App()
# MyWavData()
tf = TestFrame1()
tf.Show()
app.MainLoop()
# 測試wxPyPlot的代碼
if __name__ == '__main__':
main()

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
解決Vscode中jupyter出現(xiàn)kernel dead問題
遇到VSCode中Jupyter Kernel Dead時,可通過Anaconda Prompt安裝ipykernel解決,首先使用jupyter kernelspec list命令查看內核,若發(fā)現(xiàn)缺少ipykernel,激活相應虛擬環(huán)境,使用conda install ipykernel命令安裝,操作后,VSCode中Jupyter應能正常運行2024-09-09
python 中Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格
這篇文章主要介紹了python Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-10-10
Python urllib request模塊發(fā)送請求實現(xiàn)過程解析
這篇文章主要介紹了Python urllib request模塊發(fā)送請求實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-12-12
Python實現(xiàn)將絕對URL替換成相對URL的方法
這篇文章主要介紹了Python實現(xiàn)將絕對URL替換成相對URL的方法,涉及Python字符串操作及正則匹配的相關技巧,需要的朋友可以參考下2015-06-06
Python數(shù)學建模StatsModels統(tǒng)計回歸可視化示例詳解
圖形總是比數(shù)據(jù)更加醒目、直觀。解決統(tǒng)計回歸問題,無論在分析問題的過程中,還是在結果的呈現(xiàn)和發(fā)表時,都需要可視化工具的幫助和支持2021-10-10
使用Python向DataFrame中指定位置添加一列或多列的方法
今天小編就為大家分享一篇使用Python向DataFrame中指定位置添加一列或多列的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01

