MainFrm.cpp
资源名称:语音程序.rar [点击查看]
上传用户:lczygg
上传日期:2007-07-03
资源大小:2947k
文件大小:15k
源码类别:
语音合成与识别
开发平台:
Visual C++
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "Bedlamite.h"
- #include "MainFrm.h"
- #include "direct.h"
- #include "CommandEngine.h"
- #include "BedlamiteView.h"
- #include "staticview.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- #define WAITTIMER 10 // 等待用户提问、回答的定时器
- #define OVERTIME 3 // 超时时间
- #define OVERTIMES 2 // 超时次数
- #define REJECT_NUM 5
- #define WRITEPORT 0x378
- #define READPORT 0x379
- int g_nQNum = ROBOT_ANSWER_COUNT;
- int g_nStatus = 0;
- extern int g_nDeviceID;
- extern BOOL g_bPlayEnd;
- extern int PlayFileAsync(int& wDeviceID, char *szWaveFileName, HWND hWnd);
- extern int StopPlayFileAsync(int& wDeviceID, HWND hWnd);
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_WM_TIMER()
- //}}AFX_MSG_MAP
- // Global help commands
- ON_COMMAND(ID_HELP_FINDER, CFrameWnd::OnHelpFinder)
- ON_COMMAND(ID_HELP, CFrameWnd::OnHelp)
- ON_COMMAND(ID_CONTEXT_HELP, CFrameWnd::OnContextHelp)
- ON_COMMAND(ID_DEFAULT_HELP, CFrameWnd::OnHelpFinder)
- ON_MESSAGE(WM_NEWUTTERANCE, OnBeginUtterance)
- ON_MESSAGE(WM_ADDWAVEFRAME, OnAddWaveFrame)
- ON_MESSAGE(WM_ENDUTTERANCE, OnEndUtterance)
- ON_MESSAGE(WM_DRAGONVOICE_FIRM, OnDragonFirm)
- ON_MESSAGE(WM_DRAGONVOICE_GETVOLUME, OnDragonGetVolume)
- ON_MESSAGE(MM_MCINOTIFY, OnMyPlayEnd)
- ON_MESSAGE(WM_PLAYTIME, OnPlayTime)
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- m_nOvertime = 0;
- m_nGetVol = 0;
- m_nLocalH = -1;
- m_nDegree = 0;
- m_bRun = FALSE;
- m_bSection = FALSE;
- m_bHave = FALSE;
- m_sScroll = "";
- m_aryList.RemoveAll();
- }
- CMainFrame::~CMainFrame()
- {
- theApp.m_bSetTiemr = FALSE;
- StopPlayFileAsync(g_nDeviceID, this->m_hWnd);
- g_bPlayEnd = TRUE;
- Sleep(100);
- FreeResource();
- theApp.m_LogTrace.WriteLine("释放资源");
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- {
- TRACE0("Failed to create toolbarn");
- return -1; // fail to create
- }
- //Insert Control of CMD
- CRect rc(5, 8, 50, 20);
- m_pEditCMD = new CEdit;
- m_pEditCMD = (CEdit *)m_wndToolBar.InsertControl(RUNTIME_CLASS(CEdit),
- _T(""), rc, IDC_STATIC_CMD, WS_TABSTOP | ES_READONLY | ES_RIGHT);
- m_pEditCMD->SetWindowText("命令集:");
- rc = CRect(0,0,100,120);
- m_pcmbCMD =(CComboBox *) m_wndToolBar.InsertControl(RUNTIME_CLASS(CComboBox),
- _T(""), rc, IDC_CMD, WS_VSCROLL | CBS_DROPDOWNLIST);
- m_pcmbCMD->AddString(_T("Default"));
- m_pcmbCMD->AddString(_T("CMDST_CMD1"));
- m_pcmbCMD->AddString(_T("CMDST_CMD2"));
- m_pcmbCMD->SetCurSel(0);
- //Insert Control of AM
- /*rc = CRect(5, 8, 65, 20);
- m_pEditAM = new CEdit;
- m_pEditAM = (CEdit *)m_wndToolBar.InsertControl(RUNTIME_CLASS(CEdit),
- _T(""), rc, ID_CONTEXT_HELP, WS_TABSTOP | ES_READONLY | ES_RIGHT);
- m_pEditAM->SetWindowText("00:00:00");
- /*rc = CRect(0,0,100,120);
- m_pcmbAM =(CComboBox *) m_wndToolBar.InsertControl(RUNTIME_CLASS(CComboBox),
- _T(""), rc, IDC_AM, WS_VSCROLL | CBS_DROPDOWNLIST);
- m_pcmbAM->AddString(_T("MALE"));
- m_pcmbAM->AddString(_T("FEMALE"));
- m_pcmbAM->SetCurSel(0);*/
- // 添加音量显示条
- rc = CRect(0, 0, 200, 20);
- m_pctrlVolume = (CProgressCtrl *)m_wndToolBar.InsertControl(RUNTIME_CLASS(CProgressCtrl),
- _T(""), rc, ID_CTRL_VOLUME, 0);
- m_pctrlVolume->SetRange(0, 100);
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- EnableDocking(CBRS_ALIGN_ANY);
- DockControlBar(&m_wndToolBar);
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- cs.style = WS_OVERLAPPED | WS_CAPTION | WS_THICKFRAME | WS_SYSMENU | WS_MINIMIZEBOX;
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- return TRUE;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- // ==============================================================
- // 自定义消息响应函数
- LRESULT CMainFrame::OnBeginUtterance(WPARAM wParam, LPARAM lParam)
- {
- return 0L;
- }
- LRESULT CMainFrame::OnAddWaveFrame(WPARAM wParam, LPARAM lParam)
- {
- return 0L;
- }
- LRESULT CMainFrame::OnEndUtterance(WPARAM wParam, LPARAM lParam)
- {
- return 0L;
- }
- LRESULT CMainFrame::OnDragonFirm(WPARAM wParam, LPARAM lParam)
- {
- theApp.m_LogTrace.WriteLine("调用OnDragonFirm");
- // 判断是否处理识别结果
- //if (!m_bDispose) return 0L;
- // 清空识别结果
- int nCount = 5;
- for (int i = 0; i < 5; i ++)
- {
- m_szResult[i][0] = ' ';
- }
- SrGetFirmWords(m_szResult, nCount);
- CView *pView = GetActiveView();
- if (strlen(m_szResult[0]) > 0)
- {
- // 有识别结果,关闭定时器,播放相应语音文件
- //KillTimer(WAITTIMER);
- pView->SendMessage(WM_DISPLAYRESULT, (WPARAM)(m_szResult[0]), 0);
- for (int j = 0; j < m_aryList.GetSize(); j ++)
- {
- LPQUESTIONINFO lpInfo = (LPQUESTIONINFO)m_aryList.GetAt(j);
- if (strcmp(m_szResult[0], lpInfo->cmdbuf) == 0)
- {
- // 判断有几部分组成
- PlayFileAsync(g_nDeviceID, lpInfo->filename, this->m_hWnd);
- if (lpInfo->nP == 3)
- {
- // 播放数字
- CString sPath;
- sPath.Format("./Data/Wav/%03d.wav", theApp.m_nCount);
- PlayFileAsync(g_nDeviceID, (LPTSTR)(LPCTSTR)sPath, this->m_hWnd);
- sPath = "./Data/Wav/19_end.wav";
- PlayFileAsync(g_nDeviceID, (LPTSTR)(LPCTSTR)sPath, this->m_hWnd);
- }
- break;
- }
- }
- theApp.m_LogTrace.WriteLine("处理识别结果");
- // 继续识别
- StartRecord();
- }
- else
- {
- theApp.m_LogTrace.WriteLine("无识别结果");
- pView->SendMessage(WM_DISPLAYRESULT, (WPARAM)("无识别结果"), 0);
- Sleep(200);
- StartRecord();
- }
- return 0L;
- }
- LRESULT CMainFrame::OnDragonGetVolume(WPARAM wParam, LPARAM lParam)
- {
- int nVol = SrGetAudioVolume();
- nVol *= 4;
- if (nVol > 100) nVol = 100;
- if (m_nGetVol == 1)
- {
- m_pctrlVolume->SetPos(nVol);
- m_nGetVol = 0;
- }
- else
- m_nGetVol ++;
- return 0L;
- }
- // =========================================================================
- BOOL CMainFrame::InitRecognition()
- {
- m_bDispose = FALSE;
- try
- {
- GenerateQuesion("Data.dll");
- for (int i = 0; i < 5; i ++)
- {
- m_szResult[i] = new char[MAX_PATH];
- m_szResult[i][0] = ' ';
- }
- memset(m_chCurSelCmd, ' ', sizeof(char));
- strcpy(m_chAM, "MALE");
- strcpy(m_chCMD, "CMD");
- char dataPathName[MAX_PATH];
- _getcwd(dataPathName, MAX_PATH);
- if (SrInitDragonVoice (dataPathName, m_chAM, GetSafeHwnd()) == 0)
- {
- SrLoadCommandMode("CMD");
- SrSetActiveCommand("CMD");
- SrSetParameter(SET_EP_CHECK, 3);
- }
- else
- {
- AfxMessageBox("Initialize Command Engine Failed, Exit");
- exit(1);
- }
- }
- catch(...)
- {
- AfxMessageBox("Initialize Command Engine Failed, Exit");
- exit(1);
- }
- theApp.m_LogTrace.WriteLine("初始化结束");
- SetTimer(ROBOT_STATUS_SYSTEM, 1000, NULL);
- return TRUE;
- }
- void CMainFrame::FreeResource()
- {
- SrExitDragonVoice();
- for (int i=0; i < 5; i++)
- {
- delete [] m_szResult[i];
- }
- for (i = 0; i < m_aryList.GetSize(); i ++)
- {
- LPQUESTIONINFO lpInfo = (LPQUESTIONINFO)m_aryList.GetAt(i);
- delete lpInfo;
- }
- m_aryList.RemoveAll();
- }
- void CMainFrame::OnTimer(UINT nIDEvent)
- {
- switch(nIDEvent)
- {
- /*case WAITTIMER :
- {
- // 判断当前时间(小时)是否改变,记录日志文件名改变
- CTime tm = CTime::GetCurrentTime();
- if (tm.GetHour() != m_nLocalH)
- {
- m_nLocalH = tm.GetHour();
- CString sFileName;
- sFileName.Format("%04d年%02d月%02d日%02d点.txt",
- tm.GetYear(), tm.GetMonth(), tm.GetDay(), tm.GetHour());
- theApp.m_LogTrace.SetFileName(sFileName);
- theApp.m_LogTrace.ResetFile();
- theApp.m_LogTrace.SetActive(TRUE);
- }
- m_nOvertime ++;
- // 端口监测
- CString sItem;
- int dwValue = (int)GetParallelStatus(sItem);
- switch (dwValue)
- {
- case 1 :
- {
- // 压力开关闭合
- if (m_bRun) SwitchStatus("./Data/Wav/welcome1.wav", "压力开关闭合");
- }
- break;
- case 2 :
- {
- // 有人靠近Mic
- if (m_bRun)
- {
- SwitchStatus("", "有人靠近Mic");
- StartRecord();
- }
- }
- break;
- case -1 :
- {
- // 压力开关断开
- if (m_bLeave)
- {
- m_bLeave = FALSE;
- SwitchStatus("./Data/Wav/leave1.wav", "压力开关断开");
- }
- }
- break;
- case -2 :
- {
- // 有人离开Mic
- if (g_nQNum != 0) SwitchStatus("./Data/Wav/close1.wav", "有人离开Mic");
- if (m_bClose && g_nQNum == 0)
- {
- m_bClose = FALSE;
- SwitchStatus("./Data/Wav/close1.wav", "有人离开Mic");
- }
- }
- break;
- default : // 状态未变
- // 是否进行超时处理
- m_bRun = SwitchNext();
- if (m_bRun)
- {
- if (m_nOvertime == OVERTIME)
- {
- // 超时,则不处理识别结果
- theApp.m_LogTrace.WriteLine("超时,则不处理识别结果");
- KillTimer(WAITTIMER);
- StopRecord();
- m_nOvertime = 0;
- m_nDegree ++;
- //超时处理
- Sleep(2000);
- CView *pView = GetActiveView();
- pView->SendMessage(WM_DISPLAYRESULT, (WPARAM)("超时处理"), 0);
- WritePort(0x00);
- PlayFileAsync(g_nDeviceID, "./Data/Wav/remind1.wav", this->m_hWnd);
- WritePort(0xff);
- if (m_nDegree == OVERTIMES)
- {
- g_nQNum --;
- m_nDegree = 0;
- m_bRun = SwitchNext();
- }
- if (m_bRun) StartRecord();
- if (theApp.m_bSetTiemr) SetTimer(WAITTIMER, 1000, NULL);
- }
- }
- break;
- }
- }
- break;*/
- case ROBOT_STATUS_SYSTEM :
- {
- KillTimer(ROBOT_STATUS_SYSTEM);
- // 系统初始化结束,进入自我介绍状态
- g_nStatus = ROBOT_STATUS_INTRO;
- // 播放欢迎词
- PlayFileAsync(g_nDeviceID, "./Data/Wav/welcome1.wav", this->m_hWnd);
- // 进入等待状态,开启端口监测定时器
- //g_nStatus = ROBOT_STATUS_WAIT;
- //if (theApp.m_bSetTiemr) SetTimer(WAITTIMER, 1000, NULL);
- StartRecord();
- }
- break;
- }
- }
- void CMainFrame::StartRecord()
- {
- theApp.m_LogTrace.WriteLine("开始录音");
- m_bDispose = TRUE;
- SrMicOn();
- }
- void CMainFrame::StopRecord()
- {
- theApp.m_LogTrace.WriteLine("关闭录音");
- m_bDispose = FALSE;
- SrMicOff();
- }
- // 返回值:
- // 0:状态未改变
- // 1:用户靠近机器人有效距离
- // -1:用户离开机器人有效距离
- // 2:用户靠近Mic有效距离
- // -2:用户离开Mic有效距离
- DWORD CMainFrame::GetParallelStatus(CString &sItem)
- {
- return 0;
- }
- void CMainFrame::GenerateQuesion(CString szFileName)
- {
- CStdioFile file;
- CFileException ex;
- CString sAnswer, sPathName, sS, sAct, sTmp;
- LPQUESTIONINFO pInfo;
- if(file.Open(szFileName, CFile::typeText | CFile::modeRead, &ex))
- {
- file.SeekToBegin();
- int nPosBgn = -1;
- int nPosEnd = 0;
- char pBuf[65535];
- memset(pBuf, 0, 65535);
- int nLen = file.GetLength();
- file.ReadHuge(pBuf, nLen);
- CString s = pBuf;
- nPosBgn = s.Find("{", nPosEnd);
- nPosEnd = s.Find("}", nPosBgn);
- while ((nPosEnd != -1) && (nPosBgn != -1))
- {
- sTmp = s.Mid(nPosBgn + 1, nPosEnd - nPosBgn - 1);
- //用于存储输入参数的PROPERTY和VALUE
- CMapStringToString msPropertys;
- //循环分析输入参数,将参数内容另存到变量msPropertys中
- int nPos;
- while(sTmp.GetLength() > 0)
- {
- nPos = sTmp.Find("[");
- if(nPos >= 0) sTmp = sTmp.Mid(nPos + 1);
- CString sProperty;
- nPos = sTmp.Find("]");
- if(nPos >= 0)
- {
- sProperty = sTmp.Left(nPos);
- sTmp = sTmp.Mid(nPos + 1);
- }
- CString sValue;
- nPos = sTmp.Find("[");
- if(nPos >= 0)
- {
- //还有下一个属性结点
- sValue = sTmp.Left(nPos);
- sValue = sValue.Mid(1); //去"="
- sTmp = sTmp.Mid(nPos);
- }
- else
- {
- //此为最后一个属性结点
- sValue = sTmp.Mid(1); //去"="
- sTmp.Empty();
- }
- //存储属性
- msPropertys.SetAt(sProperty, sValue);
- }
- msPropertys.Lookup("CMD", sAnswer);
- msPropertys.Lookup("FILENAME", sPathName);
- msPropertys.Lookup("S", sS);
- msPropertys.Lookup("ACT", sAct);
- m_sScroll +=sAnswer;
- m_sScroll += "|";
- pInfo = new QUESTIONINFO;
- strcpy(pInfo->cmdbuf, sAnswer);
- strcpy(pInfo->filename, sPathName);
- pInfo->nS = atoi(sS);
- pInfo->nAct = atoi(sAct);
- m_aryList.Add(pInfo);
- // 取下一个选项
- nPosBgn = s.Find("{", nPosEnd);
- nPosEnd = s.Find("}", nPosBgn);
- }
- file.Close();
- }
- }
- void CMainFrame::OnMyPlayEnd(WPARAM wParam, LPARAM lParam)
- {
- switch(wParam)
- {
- case MCI_NOTIFY_SUCCESSFUL:
- StopPlayFileAsync(g_nDeviceID, this->m_hWnd);
- g_bPlayEnd = TRUE;
- theApp.m_LogTrace.WriteLine("播放声音文件结束");
- break;
- default:
- break;
- }
- }
- BOOL CMainFrame::SwitchNext()
- {
- // 如果问题数为零,则不识别(返回FALSE,否则返回TRUE)。
- if (g_nQNum == 0)
- return FALSE;
- else
- return TRUE;
- }
- void CMainFrame::SwitchStatus(char *szFileName, char *szInfo)
- {
- theApp.m_LogTrace.WriteLine(szInfo);
- KillTimer(WAITTIMER);
- StopRecord();
- m_nOvertime = 0;
- CView *pView = GetActiveView();
- pView->SendMessage(WM_DISPLAYRESULT, (WPARAM)szInfo, 0);
- if (strlen(szFileName) > 0)
- {
- WritePort(0x00);
- PlayFileAsync(g_nDeviceID, szFileName, this->m_hWnd);
- WritePort(0xff);
- }
- if (theApp.m_bSetTiemr) SetTimer(WAITTIMER, 1000, NULL);
- }
- void CMainFrame::OnPlayTime(WPARAM wParam, LPARAM lParam)
- {
- DWORD dwLen = (DWORD)lParam;
- CString sLen;
- sLen.Format("%d", dwLen);
- int nLen = (int)dwLen / 1000;
- CString sMinute, sSecond;
- sMinute.Format("%d", nLen / 60);
- sSecond.Format("%d", nLen % 60);
- CString sMilli = sLen.Right(3);
- sMilli = sMilli.Left(2);
- sLen = sMinute + ":" + sSecond + ":" + sMilli;
- m_pEditAM->SetWindowText(sLen);
- }
- void CMainFrame::WritePort(DWORD dwValue)
- {
- //DP_OutpW(WRITEPORT, dwValue);
- }