www.pudn.com > HMRspeech.rar > SpeechRecognition.cpp, change:2009-11-04,size:4371b


// SpeechRecognition.cpp: implementation of the CSpeechRecognition class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "speech.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() 
{ 
	Destroy(); 
} 
 
BOOL CSpeechRecognition::Initialize(HWND hWnd, BOOL bIsShared) 
{ 
	// com library 
	if (FAILED(CoInitialize(NULL))) 
	{ 
		m_sError=_T("Error intialization COM"); 
		return FALSE; 
	} 
 
	// SR engine 
	HRESULT hr = S_OK; 
	if (bIsShared) 
	{ 
		// Shared reco engine. 
		// For a shared reco engine, the audio gets setup automatically 
		hr = m_cpRecoEngine.CoCreateInstance( CLSID_SpSharedRecognizer ); 
	} 
	else 
	{ 
		hr = m_cpRecoEngine.CoCreateInstance(CLSID_SpInprocRecognizer); 
	} 
 
	// RecoContext 
	if( SUCCEEDED( hr ) ) 
	{ 
		hr = m_cpRecoEngine->CreateRecoContext( &m_cpRecoCtxt ); 
	} 
 
	// Set recognition notification for dictation 
	if (SUCCEEDED(hr)) 
	{ 
		hr = m_cpRecoCtxt->SetNotifyWindowMessage( hWnd, WM_SREVENT, 0, 0 ); 
	} 
 
	if (SUCCEEDED(hr)) 
	{ 
		// when the engine has recognized something 
		const ULONGLONG ullInterest = SPFEI(SPEI_RECOGNITION); 
		hr = m_cpRecoCtxt->SetInterest(ullInterest, ullInterest); 
	} 
 
	// create default audio object 
	CComPtr<ISpAudio> cpAudio; 
	hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &cpAudio); 
	// set the input for the engine 
	hr = m_cpRecoEngine->SetInput(cpAudio, TRUE); 
	hr = m_cpRecoEngine->SetRecoState( SPRST_ACTIVE ); 
	// grammar 
	if (SUCCEEDED(hr)) 
	{ 
		// Specifies that the grammar we want is a dictation grammar. 
		// Initializes the grammar (m_cpDictationGrammar) 
		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(); 
	} 
	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(); 
} 
 
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; 
} 
 
void CSpeechRecognition::GetText(WCHAR **ppszCoMemText, ULONG ulStart, ULONG nlCount) 
{ 
	USES_CONVERSION; 
	CSpEvent event; 
	// Process all of the recognition events 
	while (event.GetFrom(m_cpRecoCtxt) == S_OK) 
	{ 
		switch (event.eEventId) 
		{ 
		case SPEI_RECOGNITION: 
			// There may be multiple recognition results, so get all of them 
			{ 
				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; 
		} 
	} 
} 
 
HRESULT CSpeechRecognition::InitTokenList(HWND hWnd, BOOL bIsComboBox) 
{ 
	if (bIsComboBox) 
		return SpInitTokenComboBox(hWnd, SPCAT_RECOGNIZERS); 
	else 
		return SpInitTokenListBox(hWnd, SPCAT_RECOGNIZERS); 
} 
 
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); 
}