一个超简单的语音识别编程,听写程序
Posted 尹成
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个超简单的语音识别编程,听写程序相关的知识,希望对你有一定的参考价值。
CSpeechRecognition类封装了语音识别操作所需调用的几个接口,使用它进行语音识别编程很方便,也很简洁。
CSpeechRecognition类的定义如下:
///
// active speech engine
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include <sapi.h>
#include <sphelper.h>
#include <spuihelp.h>
///
// speech message
#define WM_SREVENT WM_USER+102
class CSpeechRecognition
public:
CSpeechRecognition();
virtual ~CSpeechRecognition();
// initialize
BOOL Initialize(HWND hWnd = NULL, BOOL bIsShared = TRUE);
void Destroy();
// start and stop
BOOL Start();
BOOL Stop();
BOOL IsDictationOn()
return m_bOnDictation;
// event handler
void GetText(WCHAR **ppszCoMemText, ULONG ulStart = 0, ULONG nlCount = -1);
// voice training
HRESULT VoiceTraining(HWND hWndParent);
// microphone setup
HRESULT MicrophoneSetup(HWND hWndParent);
// token list
HRESULT InitTokenList(HWND hWnd, BOOL bIsComboBox = FALSE);
// error string
CString GetErrorString()
return m_sError;
// interface
CComPtr<ISpRecognizer> m_cpRecoEngine; // SR engine
CComPtr<ISpRecoContext> m_cpRecoCtxt; //Recognition contextfor dictation
CComPtr<ISpRecoGrammar> m_cpDictationGrammar; // Dictation grammar
private:
CString m_sError;
BOOL m_bOnDictation;
;
其中定义的消息WM_SREVENT用于指示语音识别事件,该消息将通知到初始化函数指定的响应窗口。
类中定义了3个接口指针m_cpRecoEngine,m_cpRecoCtxt和m_cpDictationGrammar,分别用于引用语音识别引擎的3个重要接口IspRecognizer,ISpRecoContext和IspRecoGrammar。
初始化函数Initialize设定了语音识别引擎的基本工作环境,包括引擎、识别上下文、语法、音频和事件等的初始化:
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);
释放函数Destroy被类的析构函数调用,释放了类所引用的所有接口:
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是获取从语音中已识别出的文字的关键 以上是关于一个超简单的语音识别编程,听写程序的主要内容,如果未能解决你的问题,请参考以下文章