www.pudn.com > ruier_TTS_SR(vc6).rar > SpeechRecognition.cpp
// SpeechRecognition.cpp: implementation of the CSpeechRecognition class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TTS_SR.h"
#include "SpeechRecognition.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSpeechRecognition::CSpeechRecognition()
{
}
CSpeechRecognition::~CSpeechRecognition()
{
}
//初始化函数Initialize设定了语音识别引擎的基本工作环境,包括引擎、识别上下文、语法、音频和事件等的初始化
BOOL CSpeechRecognition::Initialize(HWND hWnd,int nIDDlgItem, BOOL bIsShared)
{
// 初始化com库
if (FAILED(CoInitialize(NULL)))
{
m_sError=_T("Error intialization COM");
return FALSE;
}
// 初始化识别引擎
HRESULT hr = S_OK;
if (bIsShared)
{
// 设置是否为共享语音识别
hr = m_cpRecoEngine.CoCreateInstance( CLSID_SpSharedRecognizer );
}
else
{
hr = m_cpRecoEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
}
// RecoContext接口
if( SUCCEEDED( hr ) )
{
hr = m_cpRecoEngine->CreateRecoContext( &m_cpRecoCtxt );
}
// 设置消息通知
if (SUCCEEDED(hr))
{
hr = m_cpRecoCtxt->SetNotifyWindowMessage( hWnd, WM_SREVENT, 0, 0 );
}
if (SUCCEEDED(hr))
{
// 设置感兴趣的消息
hr = m_cpRecoCtxt->SetInterest(SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION));
}
// 设置输入为默认输入
CComPtr cpAudio;
hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &cpAudio);
// 设置输入
hr = m_cpRecoEngine->SetInput(cpAudio, TRUE);
hr = m_cpRecoEngine->SetRecoState( SPRST_ACTIVE );
// grammar
if (SUCCEEDED(hr))
{
// 设置听写时想用的语法
// 初始化语法接口
hr = m_cpRecoCtxt->CreateGrammar( 0, &m_cpDictationGrammar );
}
if (SUCCEEDED(hr))
{
hr = m_cpDictationGrammar->LoadDictation(NULL, SPLO_STATIC);
}
if (SUCCEEDED(hr))
{
hr = m_cpDictationGrammar->SetDictationState( SPRS_ACTIVE );
}
if (FAILED(hr))
{
m_cpDictationGrammar.Release();
}
InitTokenList(hWnd,nIDDlgItem);
return (hr == S_OK);
}
void CSpeechRecognition::Destroy()
{
if (m_cpDictationGrammar)
m_cpDictationGrammar.Release();
if (m_cpRecoCtxt)
m_cpRecoCtxt.Release();
if (m_cpRecoEngine)
m_cpRecoEngine.Release();
CoUninitialize();
}
//函数Start和Stop用来控制开始和停止接受及识别语音,它们通过调用引擎接口的SetRecoState方法来实现
BOOL CSpeechRecognition::Start()
{
if (m_bOnDictation)
return TRUE;
HRESULT hr = m_cpRecoEngine->SetRecoState( SPRST_ACTIVE );
if (FAILED(hr))
return FALSE;
m_bOnDictation = TRUE;
return TRUE;
}
BOOL CSpeechRecognition::Stop()
{
if (! m_bOnDictation)
return TRUE;
HRESULT hr = m_cpRecoEngine->SetRecoState( SPRST_INACTIVE );
if (FAILED(hr))
return FALSE;
m_bOnDictation = FALSE;
return TRUE;
}
//函数GetText是获取从语音中已识别出的文字的关键,应该在响应事件消息的函数中调用,其代码如下所示。
void CSpeechRecognition::GetText(WCHAR **ppszCoMemText, ULONG ulStart, ULONG nlCount)
{
USES_CONVERSION;//USES_CONVERSION是专门为字符串类型转换宏如A2OLE()定义变量的宏。
//如果要使用这些宏,就必须在函数的开始加入USES_CONVERSION宏。
CSpEvent event;
// 处理所有的声音识别的事件
while (event.GetFrom(m_cpRecoCtxt) == S_OK)//消息队列中还有消息
{
switch (event.eEventId)
{
case SPEI_RECOGNITION:
{
HRESULT hr = S_OK;
//两种得到短语的方法,没看懂,默认用第一个就行了
if (nlCount == -1)
event.RecoResult()->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, ppszCoMemText, NULL);
else
{
ASSERT(nlCount > 0);//调试用,不满足条件则中断
event.RecoResult()->GetText(ulStart, nlCount, FALSE, ppszCoMemText, NULL);
}
}
break;
}
}
}
//函数InitTokenList调用SpInitTokenComboBox和SpInitTokenListBox函数来实现语音语言在列表或组合列表中的列表显示和选择
HRESULT CSpeechRecognition::InitTokenList(HWND hWnd,int nIDDlgItem )
{
return SpInitTokenComboBox(GetDlgItem(hWnd,nIDDlgItem), SPCAT_RECOGNIZERS);
}
HRESULT CSpeechRecognition::SetVoice(HWND hWnd,int nIDDlgItem)
{
HRESULT hr = S_OK;
CComPtr g_cpVoice;
hr = m_cpRecoCtxt->GetVoice(&g_cpVoice);
g_cpVoice->SetVoice(SpGetCurSelComboBoxToken(GetDlgItem(hWnd,nIDDlgItem)));
hr = m_cpRecoCtxt->SetVoice(g_cpVoice,TRUE);
if(FAILED(hr))
{
m_sError = _T("Error to set voice");
return hr;
}
return hr;
}
//显示设置话筒的向导
HRESULT CSpeechRecognition::MicrophoneSetup(HWND hWndParent)
{
return m_cpRecoEngine->DisplayUI(hWndParent, NULL, SPDUI_MicTraining, NULL, 0);
}
//显示语音训练向导
HRESULT CSpeechRecognition::VoiceTraining(HWND hWndParent)
{
return m_cpRecoEngine->DisplayUI(hWndParent, NULL, SPDUI_UserTraining, NULL, 0);
}
CString CSpeechRecognition::GetErrorString()
{
return m_sError;
}