wxpython繪制音頻效果
本文實(shí)例為大家分享了wxpython繪制音頻的具體代碼,供大家參考,具體內(nèi)容如下
#-*- coding: utf-8 -*-
################################################################################
## 使用wxPython的繪圖模塊wxPyPlot,需要數(shù)據(jù)可視化的時(shí)候,無(wú)需再借用其他的庫(kù)或模塊了
################################################################################
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 個(gè)點(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個(gè)點(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')
# 打開(kāi)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()
# 測(cè)試wxPyPlot的代碼
if __name__ == '__main__':
main()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Vscode中jupyter出現(xiàn)kernel dead問(wèn)題
遇到VSCode中Jupyter Kernel Dead時(shí),可通過(guò)Anaconda Prompt安裝ipykernel解決,首先使用jupyter kernelspec list命令查看內(nèi)核,若發(fā)現(xiàn)缺少ipykernel,激活相應(yīng)虛擬環(huán)境,使用conda install ipykernel命令安裝,操作后,VSCode中Jupyter應(yīng)能正常運(yùn)行2024-09-09
python 中Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格
這篇文章主要介紹了python Arduino串口傳輸數(shù)據(jù)到電腦并保存至excel表格,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
python密碼學(xué)簡(jiǎn)單替代密碼解密及測(cè)試教程
這篇文章主要介紹了python密碼學(xué)簡(jiǎn)單替代密碼解密及測(cè)試教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Python urllib request模塊發(fā)送請(qǐng)求實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Python urllib request模塊發(fā)送請(qǐng)求實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
Python實(shí)現(xiàn)將絕對(duì)URL替換成相對(duì)URL的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將絕對(duì)URL替換成相對(duì)URL的方法,涉及Python字符串操作及正則匹配的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Python實(shí)現(xiàn)轉(zhuǎn)換圖片背景顏色代碼
這篇文章主要介紹了Python實(shí)現(xiàn)轉(zhuǎn)換圖片背景顏色代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Python數(shù)學(xué)建模StatsModels統(tǒng)計(jì)回歸可視化示例詳解
圖形總是比數(shù)據(jù)更加醒目、直觀。解決統(tǒng)計(jì)回歸問(wèn)題,無(wú)論在分析問(wèn)題的過(guò)程中,還是在結(jié)果的呈現(xiàn)和發(fā)表時(shí),都需要可視化工具的幫助和支持2021-10-10
python 數(shù)字類型和字符串類型的相互轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇python 數(shù)字類型和字符串類型的相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
使用Python向DataFrame中指定位置添加一列或多列的方法
今天小編就為大家分享一篇使用Python向DataFrame中指定位置添加一列或多列的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
構(gòu)建?Python?命令行參數(shù)的?4?種常見(jiàn)方式
這篇文章主要介紹了構(gòu)建?Python?命令行參數(shù)的?4?種常見(jiàn)方式,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06

