📄 dlgspeech.cpp
字号:
// DlgSpeech.cpp : implementation file
//
#include "stdafx.h"
#include "VcDemo_pro.h"
#include "DlgSpeech.h"
#include "VoiceAngel.h"
#include "VcDemo_proDlg.h"
#include "VcDemo_pro.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CVcDemo_proApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CDlgSpeech dialog
CDlgSpeech::CDlgSpeech(CWnd* pParent /*=NULL*/)
: CDialog(CDlgSpeech::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgSpeech)
//}}AFX_DATA_INIT
}
void CDlgSpeech::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgSpeech)
DDX_Control(pDX, IDC_BTN_START_RECOGNIZE, m_btnStartRecognize);
DDX_Control(pDX, IDC_LST_NAMES, m_lstNames);
DDX_Control(pDX, IDC_EDT_TEXT_TO_SPEEK, m_edtTextToSpeak);
DDX_Control(pDX, IDC_EDT_NAME, m_edtName);
DDX_Control(pDX, IDC_EDT_MESSAGES, m_edtMsgs);
DDX_Control(pDX, IDC_CK_SPELLING, m_ckSpelling);
DDX_Control(pDX, IDC_CK_SPEAK_ASYNCHRONOUSLY, m_ckSpeakAsyn);
DDX_Control(pDX, IDC_CK_DICTATION, m_ckDictation);
DDX_Control(pDX, IDC_CK_CC, m_ckCC);
DDX_Control(pDX, IDC_BTN_SPEAK, m_btnSpeak);
DDX_Control(pDX, IDC_BTN_ADD_NAME, m_btnAddName);
DDX_Control(pDX, IDC_CMB_SR_ENGINES, m_cmbSrEngines);
DDX_Control(pDX, IDC_CMB_TTS_ENGINES, m_cmbTtsEngines);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgSpeech, CDialog)
//{{AFX_MSG_MAP(CDlgSpeech)
ON_BN_CLICKED(IDC_BTN_START_RECOGNIZE, OnBtnStartRecognize)
ON_BN_CLICKED(IDC_BTN_ADD_NAME, OnBtnAddName)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_BTN_SPEAK, OnBtnSpeak)
ON_CBN_EDITCHANGE(IDC_CMB_SR_ENGINES, OnEditchangeCmbSrEngines)
ON_BN_CLICKED(IDC_CK_DICTATION, OnCkDictation)
ON_BN_CLICKED(IDC_CK_CC, OnCkCc)
ON_BN_CLICKED(IDC_CK_SPELLING, OnCkSpelling)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgSpeech message handlers
void CDlgSpeech::UpdateGrammar()
{ long EngineIndex;
if((EngineIndex=m_cmbSrEngines.GetCurSel())==-1) return;
CVcDemo_proDlg * pMainDlg=( CVcDemo_proDlg * )theApp.m_pMainWnd;
CVoiceAngel * pva=&pMainDlg->m_Va ;
pva->SrSetCurEngine(EngineIndex);
if(m_ckDictation.GetCheck())
{ pva->SrLoadGrammar(1,"");
if(pva->GetErrorNumber())
ShowMsg(" Error occured while loading dictation grammar, Error Description:"+pva->GetErrorDescribe());
else
{ ShowMsg (" Dictation grammar loaded, you can say anything .");
}
}
else
pva->SrUnloadGrammar(1);
CString ss=pva->GetSrEngineName();
if(m_ckCC.GetCheck())
{ if(pva->GetSrEngineName().Find("English")!=-1)
{ pva->SrLoadGrammar(2,"test.xml");
if(pva->GetErrorNumber())
ShowMsg(" Error occured while loading grammar file test.xml, Error Description:"+pva->GetErrorDescribe());
else
{ShowMsg (" Grammar file text.xml has been loaded. After starting, You can say: My name is (Mike, Jack, Li Ping), or I'm (...). For details about the grammar, please open the unicode format test.xml with notepad.");
ShowMsg( " To recognize more names, please click 'Add a Name To The List' button.");
}
}
else if(pva->GetSrEngineName().Find("Chinese")!=-1)
{ pva->SrLoadGrammar(2,"test_chs.xml");
if(pva->GetErrorNumber())
ShowMsg(" Error occured while loading grammar file test_chs.xml, Error Description:"+pva->GetErrorDescribe());
else
{ ShowMsg (" 规则定义文件test_chs.xml已加载,开始识别后,你可以说: 我叫(李华,孙平,刘飞),或者我的名子叫(..), 要看详细的语法,请用写字板打开unicode格式的test_chs.xml");
ShowMsg (" 要想识别更多的名字,请点AddNameToList按钮加入更多的名字");
}
}
else
ShowMsg("Sorry, we've not prepared the grammar file for this engine. Command and control grammar not loaded.");
}
else
pva->SrUnloadGrammar(2);
if(m_ckSpelling.GetCheck())
{ pva->SrLoadGrammar(4,"");
if(pva->GetErrorNumber())
ShowMsg(" Error occured while loading spelling grammar, Error Description:"+pva->GetErrorDescribe());
else
{ ShowMsg (" Spelling grammar loaded, you can say A B C D...");
}
}
else
pva->SrUnloadGrammar(4);
}
void CDlgSpeech::ShowMsg(CString msg)
{
CString s1,s2,s3;
s2=13;s3=10;
s2=s2+s3;
m_edtMsgs.GetWindowText(s1);
if (strlen(s1)!=0)
s1=s1+s2;
s1=s1+msg;
m_edtMsgs.SetWindowText(s1);
m_edtMsgs.SetSel(s1.GetLength(),-1);
}
void CDlgSpeech::OnBtnStartRecognize()
{
CVcDemo_proDlg * pMainDlg=( CVcDemo_proDlg * )theApp.m_pMainWnd;
CVoiceAngel * pva=&pMainDlg->m_Va ;
CString Caption;
m_btnStartRecognize.GetWindowText(Caption);
if(Caption=="Stop")
{ pva->SrStopRecognize();
if(pva->GetErrorNumber()) ShowMsg(pva->GetErrorDescribe ());
else m_btnStartRecognize.SetWindowText("Start Recognize");
}
else
{ pva->SrStartRecognize();
if(pva->GetErrorNumber()) ShowMsg(pva->GetErrorDescribe ());
else
{ m_btnStartRecognize.SetWindowText("Stop");
ShowMsg("Started. Please speak now.");
}
}
}
void CDlgSpeech::OnBtnAddName()
{
CVcDemo_proDlg * pMainDlg=( CVcDemo_proDlg * )theApp.m_pMainWnd;
CVoiceAngel * pva=&pMainDlg->m_Va ;
CString sName;
m_edtName.GetWindowText(sName);
sName.TrimLeft();
if(sName=="")
{ MessageBox("Please enter a name first.","Notice",MB_OK);
return;
}
pva->SrAddWordTransition("name","",sName.GetBuffer(20));
if(pva->GetErrorNumber()) ShowMsg(pva->GetErrorDescribe());
else
{ m_lstNames.AddString(sName.GetBuffer(20));
ShowMsg("Name "+ sName +" has been added to the list successfully.");
}
}
void CDlgSpeech::OnClose()
{
// TODO: Add your message handler code here and/or call default
CDialog::OnClose();
}
void CDlgSpeech::OnBtnSpeak()
{
// TODO: Add your control notification handler code here
long EngineIndex;
if((EngineIndex=m_cmbTtsEngines.GetCurSel())==-1)
{ ShowMsg( "Please select an TTS engine first.");
return;
}
CVcDemo_proDlg * pMainDlg=( CVcDemo_proDlg * )theApp.m_pMainWnd;
CVoiceAngel * pva=&pMainDlg->m_Va ;
CString Caption;
m_btnSpeak.GetWindowText(Caption);
pva->TtsSetCurEngine(EngineIndex);
if(Caption=="Stop")
{ pva->TtsStopSpeak();
if(pva->GetErrorNumber()) ShowMsg(pva->GetErrorDescribe ());
else m_btnSpeak.SetWindowText("Speak");
}
else
{ CString text;
m_edtTextToSpeak.GetWindowText(text);
pva->TtsSpeak(text.GetBuffer(50), m_ckSpeakAsyn.GetCheck() );
if(pva->GetErrorNumber()) ShowMsg(pva->GetErrorDescribe ());
else
{ m_btnSpeak.SetWindowText("Stop");
}
}
}
BOOL CDlgSpeech::OnInitDialog()
{
CDialog::OnInitDialog();
LoadSpeech();
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDlgSpeech::OnEditchangeCmbSrEngines()
{
UpdateGrammar();
}
void CDlgSpeech::OnCkDictation()
{
UpdateGrammar();
}
void CDlgSpeech::OnCkCc()
{
UpdateGrammar();
}
void CDlgSpeech::OnCkSpelling()
{
UpdateGrammar();
}
void CDlgSpeech::OnDestroy()
{
CVcDemo_proDlg * pMainDlg=( CVcDemo_proDlg * )theApp.m_pMainWnd;
CVoiceAngel * pva=&pMainDlg->m_Va ;
pva->SrShutDown();
CDialog::OnDestroy();
}
void CDlgSpeech::LoadSpeech()
{CVcDemo_proDlg * pMainDlg=( CVcDemo_proDlg * )theApp.m_pMainWnd;
CVoiceAngel * pva=&pMainDlg->m_Va ;
pva->TtsInitialize();
if(pva->GetTtsEngineCount())
{ for(int i=0; i<pva->GetTtsEngineCount();i++)
{ pva->TtsSetCurEngine(i);
m_cmbTtsEngines.AddString( pva->GetTtsEngineName());
}
m_cmbTtsEngines.SetCurSel(0);
}
pva->SrInitialize();
if(pva->GetSrEngineCount())
{ for( int i=0; i<pva->GetSrEngineCount();i++)
{ pva->SrSetCurEngine(i);
m_cmbSrEngines.AddString( pva->GetSrEngineName());
}
m_cmbSrEngines.SetCurSel(0);
}
m_ckCC.SetCheck(true);
m_ckSpeakAsyn.SetCheck(true);
m_edtTextToSpeak.SetWindowText("Hello word.");
UpdateGrammar();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -