wxpython多线程通信的应用-实现边录音边绘制音谱图

该博客介绍了如何利用wxpython在多线程环境下实现录音功能的同时,通过线程间通信实时绘制音谱图。通过一个简单实例展示了线程启动、消息传递和主线程接收及绘图的过程。
摘要由CSDN通过智能技术生成

wxpython多线程通信的应用-实现边录音边绘制音谱图

#!bin/bash/python
# -*- coding=utf-8 -*-
from __future__ import division

import threading
import wx
import wx.media
import os.path, os, gc, sys, pyaudio
import wave, numpy, codecs, time
import pylab as pl
import pyaudio
#import win32gui
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
import sys
from wx.lib.pubsub import pub


reload(sys)

wavCount = 1
input_filename = "input.wav"               # 麦克风采集的语音输入
input_filepath = "./"                      # 输入文件的path
in_path = input_filepath + input_filename
global_frames = []
xmajorLocator   = MultipleLocator(0.5)        # 将x主刻度标签设置为0.5的倍数
xmajorFormatter = FormatStrFormatter('%1.1f') # 设置x轴标签文本的格式
xminorLocator   = MultipleLocator(0.05)       # 将x轴次刻度标签设置为0.05的倍数

class MyFrame(wx.Frame):
    #初始化全部界面
    def __init__(self, wavlist, recList):
        self.wavTime = 0.000
        self.wavLength = 0.000
        self.labelcount = 0
        self.choosenItem = 0
        self.labelPosition = 0
        self.cutCount = 1
        self.wavList = []
        self.recList = []
        self.labellist = []
        self.labelPos = []
        self.tempwavlist = []
        self.wavList = wavlist
        self.recList = recList
        # self.wavfilepath = wavlist[0][1]
        # self.recTextStr = recList[0][1]
        self.wavfilepath = wavlist[0]
        self.recTextStr = recList[0]
        print ("wav:", self.wavfilepath)
        print ("rec:", self.recTextStr)
        wx.Frame.__init__(self,None,size=(1920,1080))

        self.sp=wx.SplitterWindow(self,style=wx.SP_3D)
        self.mainPanel=wx.Panel(self.sp)
        self.listPanel=wx.Panel(self.sp)
        self.sp2=wx.SplitterWindow(self.mainPanel)
        self.annoPanel=wx.Panel(self.sp2)
        self.subPanel=wx.Panel(self.sp2)
        self.box=wx.BoxSizer(wx.VERTICAL)
        self.box.Add(self.sp2,1,wx.EXPAND)
        self.sp3=wx.SplitterWindow(self.subPanel)
        self.cutPanel=wx.Panel(self.sp3)
        self.statisticPanel=wx.Panel(self.sp3)
        self.box2=wx.BoxSizer(wx.VERTICAL)
        self.box2.Add(self.sp3,1,wx.EXPAND)
        self.sp4=wx.SplitterWindow(self.statisticPanel)
        self.wavPanel=wx.Panel(self.sp4)
        # upgrade
        # self.upwavPanel=wx.Panel(self.sp4)
        self.infoPanel=wx.Panel(self.sp4)
        self.box3=wx.BoxSizer(wx.VERTICAL)
        self.box3.Add(self.sp4,1,wx.EXPAND)
        self.sp5=wx.SplitterWindow(self.infoPanel)
        self.labelPanel=wx.Panel(self.sp5)
        self.aaaPanel=wx.Panel(self.sp5)
        self.box4=wx.BoxSizer(wx.VERTICAL)
        self.box4.Add(self.sp5,1,wx.EXPAND)
        self.sp6=wx.SplitterWindow(self.aaaPanel)
        self.bbbPanel=wx.Panel(self.sp6)
        self.cccPanel=wx.Panel(self.sp6)
        self.box5=wx.BoxSizer(wx.VERTICAL)
        self.box5.Add(self.sp6,1,wx.EXPAND)
        self.mainPanel.SetSizer(self.box)
        self.subPanel.SetSizer(self.box2)
        self.statisticPanel.SetSizer(self.box3)
        self.infoPanel.SetSizer(self.box4)
        self.aaaPanel.SetSizer(self.box5)
        self.cutPanel.SetBackgroundColour("white")
        self.bbbPanel.SetBackgroundColour("white")
        self.cccPanel.SetBackgroundColour("white")
        print ("debug: wavfilepath:", self.wavfilepath)
        self.drawBackJpg(str(self.wavfilepath))

        # 创建布局
        self.CreateListUI(wavlist)
        self.CreateMainUI(recList)
        self.CreateWavUI()
        self.CreateLabelListUI()

        # 布局
        self.sp.SplitVertically(self.listPanel,self.mainPanel,150)
        self.sp2.SplitHorizontally(self.annoPanel,self.subPanel,300)
        self.sp3.SplitHorizontally(self.cutPanel,self.statisticPanel,110)
        self.sp4.SplitHorizontally(self.wavPanel,self.infoPanel,280)
        self.sp5.SplitHorizontally(self.labelPanel,self.aaaPanel,100)
        self.sp6.SplitVertically(self.bbbPanel,self.cccPanel,820)
        self.Bind(wx.EVT_LIST_ITEM_SELECTED,self.OnListBox,self.list)
        self.wavPanel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.wavPanel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnErasePass)
        self.wavPanel.Bind(wx.EVT_MOTION,self.OnMove)
        self.wavPanel.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
        self.wavPanel.Bind(wx.EVT_LEFT_UP,self.OnDrawRec)
        # self.upwavPanel.Bind(wx.EVT_PAINT, self.OnPaint)
        # self.upwavPanel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnErasePass)
        # self.upwavPanel.Bind(wx.EVT_MOTION,self.OnMove)
        # self.upwavPanel.Bind(wx.EVT_LEFT_DOWN,self.OnLeftDown)
        # self.upwavPanel.Bind(wx.EVT_LEFT_UP,self.OnDrawRec)
        self.labelPanel.Bind(wx.EVT_MOTION,self.OnLabelMove)
        self.labelPanel.Bind(wx.EVT_LEFT_UP,self.OnLabelDrawLine)
        self.labelPanel.Bind(wx.EVT_PAINT,self.OnLabelPaint)
        self.labelPanel.Bind(wx.EVT_ERASE_BACKGROUND,self.OnErasePass)

    #左侧列表的点击事件
    def OnListBox(self,event):
        # self.sp.Unsplit()
        if self.timer:
            self.timer.Stop()
            # del self.timer
        self.cutCount=1

        #获取点击对象
        index = event.GetIndex()
        self.choosenItem = index
        data = self.list.GetItem(index,0)
        tuple_temp = self.wavList[int(data.GetText())-1]
        # self.wavfilepath = tuple_temp[1]
        # self.recTextStr = self.recList[int(data.GetText())-1][1]
        self.wavfilepath = self.wavfilepath
        self.recTextStr = self.recTextStr
        print ("wav:", self.wavfilepath)
        print ("rec:", self.recTextStr)
        self.OnFreshUI()
        #刷新界面


    def OnFreshUI(self):
        self.InitMedia()
        self.rec_tc.SetValue(self.recTextStr)
        self.label_tc.SetValue("")
        self.anno_tc.SetValue("")
        self.wavpath_ST.SetLabel(u"当前操作文件 : "+ self.wavfilepath)

        self.drawBackJpg(str(self.wavfilepath))
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer)
        self.timer.Start(100)
        self.cutPanel.Refresh()
        self.annoPanel.Refresh()
        self.mainPanel.Refresh()

    #绘制音频波形的paint事件的回调函数
    def OnPaint(self, event):
        brush = wx.Brush("white")
        img = wx.Bitmap("mic4_output.png")
        dc = wx.BufferedPaintDC(self.wavPanel)
        dc.SetBackground(brush)
        # dc.Clear()
        dc.DrawBitmap(img,-172,0)

    #据说可以解决屏闪问题
    def OnErasePass(self, event):
        pass

    #--------------------------------------------------
    #列表界面的UI创建
    def CreateListUI(self,wavlist):
        self.ListBox = wx.BoxSizer(wx.HORIZONTAL)
        self.list = wx.ListCtrl(self.listPanel, -1, style=wx.LC_REPORT)
        self.list.InsertColumn(0, u'序号', width=50)
        self.list.InsertColumn(1, u'完成情况', width=150)
        for i in wavlist:
            index = self.list.InsertStringItem(sys.maxint, str(i[0]))
            self.list.SetStringItem(index, 1, u"未完成")
        self.ListBox.Add(self.list,1,wx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值