'''
Created on 2012-7-2
@author: Administrator
'''
import wx
import numpy as np
import wx.lib.plot as wxPyPlot # 导入绘图模块,并命名为wxPyPlot
import wave


# 需要把数据封装进入MyDataObject中
def MyDataObject():
    # 50 个点的sin函数,用蓝色圆点表示
    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函数,用红色表示
    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数据
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)
    # 读取波形数据
    str_data = file.readframes(nframes)
    # 文件使用完毕,关闭文件
    file.close()

    # 将波形数据装换成数组
    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)

        # # 创建菜单栏
        # self.mainmenu = wx.MenuBar()
        # # 创建菜单
        # 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)
        # # 创建状态栏,显示信息
        self.CreateStatusBar(2)
        self.pc = wxPyPlot.PlotCanvas(self)    # 此处导入绘图面板
        self.pc.Draw(MyWavData())

    def OnPlotDraw1(self, event):              # 绘图函数
        self.pc.Draw(MyDataObject())

    def OnPlotDraw_wav(self, event):
        self.pc.Draw(MyWavData())

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example,self).__init__(parent, title=title, size=(1000,600))
        self.InitUI()
        self.Centre()
        self.Show()

    def InitUI(self):
        panel = wx.Panel(self)
        sizer = wx.GridBagSizer(12, 12)

        # text = wx.StaticText(panel, label='Rename to')
        # sizer.Add(text, pos=(0, 0), flag=wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
        #
        # tc = wx.TextCtrl(panel)
        # sizer.Add(tc, pos=(0,1), span=(1,5), flag=wx.EXPAND, border=5)

        # 这里是原始语音
        tf = wx.Button(panel, label="原始语音")
        sizer.Add(tf, pos=(1, 1), span=(2, 12), flag = wx.EXPAND|wx.ALL)

        # 这里是增强
        up = wx.Button(panel, label="增强语音")
        sizer.Add(up, pos=(3, 1), span=(4, 12), flag = wx.EXPAND|wx.ALL)

        # 这里是识别文字
        cog = wx.Button(panel, label="识别文字")
        sizer.Add(cog, pos=(9, 1), span=(3, 12), flag = wx.EXPAND|wx.ALL)

        # 其他布局
        buttonLuyin = wx.Button(panel,label='录音',size=(90,28))
        buttonClose = wx.Button(panel,label='播放',size=(90,28))
        buttonRecog = wx.Button(panel,label='识别',size=(90,28))
        sizer.Add(buttonLuyin, pos=(12, 1))
        sizer.Add(buttonClose, pos=(12, 2))
        sizer.Add(buttonRecog, pos=(12, 3), flag=wx.RIGHT|wx.BOTTOM, border=5)

        sizer.AddGrowableRow(2)
        sizer.AddGrowableCol(1)
        panel.SetSizerAndFit(sizer)


if __name__ == '__main__':
    app = wx.App()
    Example(None, title="语音识别展示")
    app.MainLoop()

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄