查看源码: 语音程序.rar_WordRecPDADlg.cpp - VerySource

WordRecPDADlg.cpp

上传用户:lczygg
上传日期:2007-07-03
资源大小:2947k
文件大小:11k
源码类别:语音合成与识别
开发平台:Visual C++
  1. // WordRecPDADlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "WordRecPDA.h"
  5. #include "WordRecPDADlg.h"
  6. #include "direct.h"
  7. #include "Utility.h"
  8. #include "..\CommandEngine.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16. class CAboutDlg : public CDialog
  17. {
  18. public:
  19. CAboutDlg();
  20. // Dialog Data
  21. //{{AFX_DATA(CAboutDlg)
  22. enum { IDD = IDD_ABOUTBOX };
  23. //}}AFX_DATA
  24. // ClassWizard generated virtual function overrides
  25. //{{AFX_VIRTUAL(CAboutDlg)
  26. protected:
  27. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  28. //}}AFX_VIRTUAL
  29. // Implementation
  30. protected:
  31. //{{AFX_MSG(CAboutDlg)
  32. //}}AFX_MSG
  33. DECLARE_MESSAGE_MAP()
  34. };
  35. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  36. {
  37. //{{AFX_DATA_INIT(CAboutDlg)
  38. //}}AFX_DATA_INIT
  39. }
  40. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CDialog::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CAboutDlg)
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  47. //{{AFX_MSG_MAP(CAboutDlg)
  48. // No message handlers
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CWordRecPDADlg dialog
  53. CWordRecPDADlg::CWordRecPDADlg(CWnd* pParent /*=NULL*/)
  54. : CDialog(CWordRecPDADlg::IDD, pParent)
  55. {
  56. //{{AFX_DATA_INIT(CWordRecPDADlg)
  57. m_strInf = _T("");
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. }
  62. void CWordRecPDADlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CWordRecPDADlg)
  66. DDX_Control(pDX, IDC_LIST_ALL, m_ctrAll);
  67. DDX_Control(pDX, IDC_PROGRESS_VOLUME, m_ctrVol);
  68. DDX_Control(pDX, IDC_COMBO_CMD, m_ctrCmd);
  69. DDX_Control(pDX, IDC_COMBO_AM, m_ctrAM);
  70. DDX_Control(pDX, IDC_LIST_WORD, m_ctrWrd);
  71. DDX_Control(pDX, IDC_BUTTON_TEST, m_ctrTest);
  72. DDX_Control(pDX, IDC_BUTTON_STOP, m_ctrStop);
  73. DDX_Control(pDX, IDC_BUTTON_START, m_ctrStart);
  74. DDX_Text(pDX, IDC_EDIT_INF, m_strInf);
  75. DDV_MaxChars(pDX, m_strInf, 256);
  76. //}}AFX_DATA_MAP
  77. }
  78. BEGIN_MESSAGE_MAP(CWordRecPDADlg, CDialog)
  79. //{{AFX_MSG_MAP(CWordRecPDADlg)
  80. ON_WM_SYSCOMMAND()
  81. ON_WM_PAINT()
  82. ON_WM_QUERYDRAGICON()
  83. ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
  84. ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
  85. ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
  86. ON_CBN_SELCHANGE(IDC_COMBO_AM, OnSelchangeComboAm)
  87. ON_CBN_SELCHANGE(IDC_COMBO_CMD, OnSelchangeComboCmd)
  88. ON_LBN_SELCHANGE(IDC_LIST_WORD, OnSelchangeListWord)
  89. ON_BN_CLICKED(IDC_BUTTON_EDIT, OnButtonEdit)
  90. ON_LBN_SELCHANGE(IDC_LIST_ALL, OnSelchangeListAll)
  91. ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
  92. ON_BN_CLICKED(IDC_TEST1, OnTest1)
  93. ON_WM_TIMER()
  94. ON_WM_DESTROY()
  95. //}}AFX_MSG_MAP
  96. ON_WM_DESTROY()
  97. ON_MESSAGE(WM_NEWUTTERANCE, OnBeginUtterance)
  98. ON_MESSAGE(WM_ADDWAVEFRAME, OnAddWaveFrame)
  99. ON_MESSAGE(WM_ENDUTTERANCE, OnEndUtterance)
  100. ON_MESSAGE(WM_DRAGONVOICE_FIRM, OnDragonFirm)
  101. ON_MESSAGE(WM_DRAGONVOICE_GETVOLUME, OnDragonGetVolume)
  102. //
  103. END_MESSAGE_MAP()
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CWordRecPDADlg message handlers
  106. BOOL CWordRecPDADlg::OnInitDialog()
  107. {
  108. CDialog::OnInitDialog();
  109. // Add "About..." menu item to system menu.
  110. // IDM_ABOUTBOX must be in the system command range.
  111. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  112. ASSERT(IDM_ABOUTBOX < 0xF000);
  113. CMenu* pSysMenu = GetSystemMenu(FALSE);
  114. if (pSysMenu != NULL)
  115. {
  116. CString strAboutMenu;
  117. strAboutMenu.LoadString(IDS_ABOUTBOX);
  118. if (!strAboutMenu.IsEmpty())
  119. {
  120. pSysMenu->AppendMenu(MF_SEPARATOR);
  121. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  122. }
  123. }
  124. // Set the icon for this dialog.  The framework does this automatically
  125. //  when the application's main window is not a dialog
  126. SetIcon(m_hIcon, TRUE); // Set big icon
  127. SetIcon(m_hIcon, FALSE); // Set small icon
  128. // TODO: Add extra initialization here
  129. ResetLOG();
  130. m_nTmCount = 0;
  131. for (int i=0; i < 5; i++){
  132. m_strResult[i] = new char[MAX_PATH];
  133. m_strInfo[i] = new char[MAX_PATH];
  134. m_strResult[i][0] = '';
  135. m_strInfo[i][0] = '';
  136. }
  137. memset(m_strCurSelWrd, '', sizeof(char));
  138. strcpy(m_strAM, "MALE");
  139. strcpy(m_strCmd, "CMDST_DEFAULT");
  140. m_ctrAM.SelectString(-1, _T(m_strAM));
  141. m_ctrCmd.SelectString(-1, _T(m_strCmd));
  142. m_ctrVol.SetPos(0);
  143. char dataPathName[MAX_PATH];
  144. //strcpy(dataPathName, "D:\MyProject\ASR_WORKSPACE\PC_CmdEngineTest");
  145. _getcwd(dataPathName, MAX_PATH);
  146. if (SrInitDragonVoice (dataPathName, m_strAM, GetSafeHwnd()) == 0)
  147. {
  148. SrLoadCommandMode("CMDST_DEFAULT");
  149. SrLoadCommandMode("COMMANDSET_1");
  150. SrLoadCommandMode("COMMANDSET_2");
  151. SrSetActiveCommand(m_strCmd);
  152. DemoAllCmd();
  153. SrSetParameter(SET_EP_CHECK, 3);
  154. SrMicOn();
  155. SrMicOff();
  156. }
  157. else
  158. {
  159. AfxMessageBox("Initialize Command Engine Failed, Exit");
  160. exit(1);
  161. }
  162. return TRUE;  // return TRUE  unless you set the focus to a control
  163. }
  164. void CWordRecPDADlg::OnSysCommand(UINT nID, LPARAM lParam)
  165. {
  166. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  167. {
  168. CAboutDlg dlgAbout;
  169. dlgAbout.DoModal();
  170. }
  171. else
  172. {
  173. CDialog::OnSysCommand(nID, lParam);
  174. }
  175. }
  176. // If you add a minimize button to your dialog, you will need the code below
  177. //  to draw the icon.  For MFC applications using the document/view model,
  178. //  this is automatically done for you by the framework.
  179. void CWordRecPDADlg::OnPaint() 
  180. {
  181. if (IsIconic())
  182. {
  183. CPaintDC dc(this); // device context for painting
  184. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  185. // Center icon in client rectangle
  186. int cxIcon = GetSystemMetrics(SM_CXICON);
  187. int cyIcon = GetSystemMetrics(SM_CYICON);
  188. CRect rect;
  189. GetClientRect(&rect);
  190. int x = (rect.Width() - cxIcon + 1) / 2;
  191. int y = (rect.Height() - cyIcon + 1) / 2;
  192. // Draw the icon
  193. dc.DrawIcon(x, y, m_hIcon);
  194. }
  195. else
  196. {
  197. CDialog::OnPaint();
  198. }
  199. }
  200. // The system calls this to obtain the cursor to display while the user drags
  201. //  the minimized window.
  202. HCURSOR CWordRecPDADlg::OnQueryDragIcon()
  203. {
  204. return (HCURSOR) m_hIcon;
  205. }
  206. void CWordRecPDADlg::DemoAllCmd()
  207. {
  208. int nCmd = SrGetAllCmd(NULL);
  209. char ** strCmd = new char*[nCmd];
  210. for (int i=0; i < nCmd; i++)
  211. strCmd[i] = new char[MAX_WORD_LENGTH*2+2];
  212. nCmd = SrGetAllCmd(strCmd);
  213. m_ctrAll.ResetContent();
  214. for (i=0; i < nCmd; i++)
  215. m_ctrAll.AddString(strCmd[i]);
  216. for (i=0; i < nCmd; i++)
  217. delete [] strCmd[i];
  218. delete [] strCmd;
  219. }
  220. void CWordRecPDADlg::OnButtonStart() 
  221. {
  222. if (SrMicOn() == 0)
  223. {
  224. m_ctrStart.EnableWindow(FALSE);
  225. m_ctrStop.EnableWindow(TRUE);
  226. }
  227. SetTimer(1, 1000, NULL);
  228. }
  229. void CWordRecPDADlg::OnButtonStop() 
  230. {
  231. SrMicOff();
  232. }
  233. void CWordRecPDADlg::OnButtonTest() 
  234. {
  235. SrBatchWavTest();
  236. }
  237. void CWordRecPDADlg::OnDestroy() 
  238. {
  239. SrExitDragonVoice();
  240. for (int i=0; i < 5; i++)
  241. {
  242. delete [] m_strResult[i];
  243. delete [] m_strInfo[i];
  244. }
  245. CDialog::OnDestroy();
  246. }
  247. void CWordRecPDADlg::OnSelchangeComboAm() 
  248. {
  249. // TODO: Add your control notification handler code here
  250. // int nIndex = m_ctrAM.GetCurSel();
  251. // m_ctrAM.GetLBText(nIndex, m_strAM);
  252. // if (SrReplaceAcModel(m_strAM) != 0)
  253. // AfxMessageBox("声学模型切换失败");
  254. }
  255. void CWordRecPDADlg::OnSelchangeComboCmd() 
  256. {
  257. int nIndex = m_ctrCmd.GetCurSel();
  258. m_ctrCmd.GetLBText(nIndex, m_strCmd);
  259. if (SrSetActiveCommand(m_strCmd) != 0)
  260. AfxMessageBox("命令集切换失败");
  261. else
  262. DemoAllCmd();
  263. }
  264. void CWordRecPDADlg::OnSelchangeListWord() 
  265. {
  266. int nIndex = m_ctrWrd.GetCurSel();
  267. if (nIndex < 0) return;
  268. CString csTemp;
  269. m_ctrWrd.GetText(nIndex, csTemp);
  270. strcpy(m_strCurSelWrd, csTemp.GetBuffer(MAX_WORD_LENGTH*2));
  271. csTemp.ReleaseBuffer(MAX_WORD_LENGTH*2);
  272. }
  273. void CWordRecPDADlg::OnSelchangeListAll() 
  274. {
  275. int nIndex = m_ctrAll.GetCurSel();
  276. if (nIndex < 0) return;
  277. CString csTemp;
  278. m_ctrAll.GetText(nIndex, csTemp);
  279. strcpy(m_strCurSelWrd, csTemp.GetBuffer(MAX_WORD_LENGTH*2));
  280. csTemp.ReleaseBuffer(MAX_WORD_LENGTH*2);
  281. }
  282. void CWordRecPDADlg::OnButtonEdit() 
  283. {
  284. oCardEdit.DoModal();
  285. if (oCardEdit.m_bUpdateBook)
  286. {
  287. char strCommandSetName[MAX_PATH];
  288. SrGetActiveCommand(strCommandSetName);
  289. SrLoadCommandMode(strCommandSetName);
  290. }
  291. if (oCardEdit.m_bTestBatch)
  292. {
  293. char strCommandSetName[] = "PPP";
  294. SrUnLoadCommandMode(strCommandSetName);
  295. SrLoadCommandMode(strCommandSetName);
  296. }
  297. DemoAllCmd();
  298. memset(m_strCurSelWrd, '', MAX_WORD_LENGTH*2*sizeof(char));
  299. }
  300. void CWordRecPDADlg::OnButtonDel() 
  301. if (AfxMessageBox("您确认要删除吗?", MB_ICONEXCLAMATION  || MB_ICONWARNING)
  302. == IDCANCEL)
  303. return;
  304. if (SrConvertInit() == 0)
  305. {
  306. if (SrDeleteWord(m_strCurSelWrd) == 0)
  307. AfxMessageBox("OK");
  308. else
  309. AfxMessageBox("Failed");
  310. SrConvertExit();
  311. char strCommandSetName[MAX_PATH];
  312. SrGetActiveCommand(strCommandSetName);
  313. SrLoadCommandMode(strCommandSetName);
  314. DemoAllCmd();
  315. memset(m_strCurSelWrd, '', MAX_WORD_LENGTH*2*sizeof(char));
  316. }
  317. else
  318. AfxMessageBox("Failed");
  319. }
  320. // ==============================================================
  321. // 自定义消息响应函数
  322. LRESULT CWordRecPDADlg::OnBeginUtterance(WPARAM wParam, LPARAM lParam)
  323. {
  324. return 0L;
  325. }
  326. LRESULT CWordRecPDADlg::OnAddWaveFrame(WPARAM wParam, LPARAM lParam)
  327. {
  328. return 0L;
  329. }
  330. LRESULT CWordRecPDADlg::OnEndUtterance(WPARAM wParam, LPARAM lParam)
  331. {
  332. TRACE1("Total Frames is %dnn", lParam);
  333. // 调试录音和端点检测线程时
  334. #ifdef DEBUG_1
  335. char strResult[260];
  336. sprintf(strResult, "%d", lParam);
  337. if (m_ctrWrd.GetCount() >= 5)
  338. m_ctrWrd.ResetContent();
  339. m_ctrWrd.AddString(strResult);
  340. m_ctrStart.EnableWindow(TRUE);
  341. m_ctrStop.EnableWindow(FALSE);
  342. #endif
  343. return 0L;
  344. }
  345. LRESULT CWordRecPDADlg::OnDragonFirm(WPARAM wParam, LPARAM lParam)
  346. {
  347. int i, nWrd = 5;
  348. SrGetFirmWords(m_strResult, nWrd);
  349. if (nWrd > 0)
  350. {
  351. m_ctrWrd.ResetContent();
  352. for (i=0; i < nWrd; i++)
  353. m_ctrWrd.AddString(m_strResult[i]);
  354. m_ctrStart.EnableWindow(TRUE);
  355. m_ctrStop.EnableWindow(FALSE);
  356. //////////////////////////////
  357. Sleep(200);
  358. OnButtonStart();
  359. //////////////////////////////
  360. }
  361. return 0L;
  362. }
  363. LRESULT CWordRecPDADlg::OnDragonGetVolume(WPARAM wParam, LPARAM lParam)
  364. {
  365. int nVol = SrGetAudioVolume();
  366. nVol *= 4;
  367. nVol = nVol <= 100 ? nVol : 100;
  368. m_ctrVol.SetPos(nVol);
  369. return 0L;
  370. }
  371. // ========================================================================= 
  372. void CWordRecPDADlg::OnTest1() 
  373. {
  374. CFileDialog oFileDlg(TRUE, "wav", "*.wav");
  375. if (oFileDlg.DoModal() != IDOK) return;
  376. CString strFilePath = oFileDlg.GetPathName();
  377. int  nWrd = 5;
  378. char * strWrd[260];
  379. for (int i=0; i < nWrd; i++)
  380. strWrd[i] = new char[260];
  381. SrSingleWavTest(strFilePath.GetBuffer(MAX_PATH), strWrd, nWrd);
  382. strFilePath.ReleaseBuffer();
  383. while (m_ctrWrd.GetCount() != 0)
  384. m_ctrWrd.DeleteString(0);
  385. for (i=0; i < nWrd; i++)
  386. m_ctrWrd.AddString(strWrd[i]);
  387. for (i=0; i < 5; i++)
  388. delete [] strWrd[i];
  389. }
  390. void CWordRecPDADlg::OnTimer(UINT nIDEvent) 
  391. {
  392. if (nIDEvent == 1)
  393. {
  394. //
  395. m_nTmCount ++;
  396. if (m_nTmCount == 10)
  397. {
  398. m_nTmCount = 0;
  399. KillTimer(1);
  400. SrMicOff();
  401. Sleep(1000);
  402. OnButtonStart();
  403. }
  404. }
  405. CDialog::OnTimer(nIDEvent);
  406. }