wxpython繪制音頻效果
本文實(shí)例為大家分享了wxpython繪制音頻的具體代碼,供大家參考,具體內(nèi)容如下
#-*- coding: utf-8 -*- ################################################################################ ## 使用wxPython的繪圖模塊wxPyPlot,需要數(shù)據(jù)可視化的時候,無需再借用其他的庫或模塊了 ################################################################################ import numpy as np import wx import wx.lib.plot as wxPyPlot # 導(dǎo)入繪圖模塊,并命名為wxPyPlot import wave import pylab as pl # 需要把數(shù)據(jù)封裝進(jìn)入MyDataObject中 def MyDataObject(): # 50 個點(diǎn)的sin函數(shù),用藍(lán)色圓點(diǎn)表示 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個點(diǎn)的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) # 文件使用完畢,關(guān)閉文件 file.close() # 將波形數(shù)據(jù)裝換成數(shù)組 wave_data = np.fromstring(str_data, dtype=np.short) wave_data.shape = (-1, 2) wave_data = wave_data.T # 矩陣轉(zhuǎn)置 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') # 設(shè)置菜單Bar self.SetMenuBar(self.mainmenu) # 創(chuàng)建狀態(tài)欄,顯示信息 self.CreateStatusBar(2) self.pc = wxPyPlot.PlotCanvas(self) # 此處導(dǎo)入繪圖面板 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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python中similarity函數(shù)實(shí)例用法
在本篇文章里小編給大家整理的是一篇關(guān)于python中similarity函數(shù)實(shí)例用法,有興趣的朋友們可以跟著學(xué)習(xí)下。2021-10-10淺談Python_Openpyxl使用(最全總結(jié))
這篇文章主要介紹了淺談Python_Openpyxl使用(最全總結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09打印tensorflow恢復(fù)模型中所有變量與操作節(jié)點(diǎn)方式
這篇文章主要介紹了打印tensorflow恢復(fù)模型中所有變量與操作節(jié)點(diǎn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Python如何使用print()函數(shù)輸出格式化字符串
Python中內(nèi)置的%操作符和format函數(shù),都可以用于格式化字符串,下面這篇文章主要給大家介紹了關(guān)于Python如何使用print()函數(shù)輸出格式化字符串的相關(guān)資料,需要的朋友可以參考下2021-08-08Python標(biāo)準(zhǔn)庫之sqlite3使用實(shí)例
這篇文章主要介紹了Python標(biāo)準(zhǔn)庫之sqlite3使用實(shí)例,本文講解了創(chuàng)建數(shù)據(jù)庫、插入數(shù)據(jù)、查詢數(shù)據(jù)、更新與刪除數(shù)據(jù)操作實(shí)例,需要的朋友可以參考下2014-11-11Django?使用VScode?創(chuàng)建工程的詳細(xì)步驟
這篇文章主要介紹了Django?使用VScode?創(chuàng)建工程,創(chuàng)建Django 項(xiàng)目,可以和虛擬環(huán)境放在同一目錄,也可以放在虛擬環(huán)境的文件夾里,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09python 實(shí)現(xiàn)圖片旋轉(zhuǎn) 上下左右 180度旋轉(zhuǎn)的示例
今天小編就為大家分享一篇python 實(shí)現(xiàn)圖片旋轉(zhuǎn) 上下左右 180度旋轉(zhuǎn)的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01