日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

用SAPI实现Speech Recognition(SR) - 听写模式

發布時間:2025/5/22 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用SAPI实现Speech Recognition(SR) - 听写模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

摘選自:“北極悠藍”的博客《C++使用SAPI實現語音合成和語音識別的方法和代碼》

微軟的語音識別,在這里我們簡稱它為SR(speech recognition),SR分為兩種模式的監聽:第一種模式:聽寫模式,即隨意輸入語音,監聽對象將最為接近的字或者詞,句反饋出來;第二種模式:命令與控制模式,劃定范圍監聽,制定一組被選項做為監聽的,用戶的語音輸入被反饋成最為接近的一個選項。說得通俗一些:第一種是填空題,第二種是選擇題目。
下面是第一種模式的代碼:

#include <windows.h> #include <sapi.h> #include <stdio.h> #include <string.h> #include <atlbase.h> #include "sphelper.h" inline HRESULT BlockForResult(ISpRecoContext * pRecoCtxt, ISpRecoResult ** ppResult) {HRESULT hr = S_OK;CSpEvent event;while (SUCCEEDED(hr) && SUCCEEDED(hr = event.GetFrom(pRecoCtxt)) && hr == S_FALSE){hr = pRecoCtxt->WaitForNotifyEvent(INFINITE);}*ppResult = event.RecoResult();if (*ppResult){(*ppResult)->AddRef();}return hr; }const WCHAR * StopWord() {const WCHAR * pchStop;LANGID LangId = ::SpGetUserDefaultUILanguage();switch (LangId){case MAKELANGID(LANG_JAPANESE, SUBLANG_DEFAULT):pchStop = L"}42N86\0b70e50fc0ea0e70fc/05708504608a087046";;break;default:pchStop = L"Stop";break;}return pchStop; }int main(int argc, char* argv[]) {HRESULT hr = E_FAIL;bool fUseTTS = true; // turn TTS play back on or off bool fReplay = true; // turn Audio replay on or off // Process optional arguments if (argc > 1){int i;for (i = 1; i < argc; i++){if (_stricmp(argv[i], "-noTTS") == 0){fUseTTS = false;continue;}if (_stricmp(argv[i], "-noReplay") == 0){fReplay = false;continue;}printf("Usage: %s [-noTTS] [-noReplay] ", argv[0]);return hr;}}if (SUCCEEDED(hr = ::CoInitialize(NULL))){{CComPtr<ISpRecoContext> cpRecoCtxt;CComPtr<ISpRecoGrammar> cpGrammar;CComPtr<ISpVoice> cpVoice;hr = cpRecoCtxt.CoCreateInstance(CLSID_SpSharedRecoContext);if (SUCCEEDED(hr)){hr = cpRecoCtxt->GetVoice(&cpVoice);}if (cpRecoCtxt && cpVoice &&SUCCEEDED(hr = cpRecoCtxt->SetNotifyWin32Event()) &&SUCCEEDED(hr = cpRecoCtxt->SetInterest(SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION))) &&SUCCEEDED(hr = cpRecoCtxt->SetAudioOptions(SPAO_RETAIN_AUDIO, NULL, NULL)) &&SUCCEEDED(hr = cpRecoCtxt->CreateGrammar(0, &cpGrammar)) &&SUCCEEDED(hr = cpGrammar->LoadDictation(NULL, SPLO_STATIC)) &&SUCCEEDED(hr = cpGrammar->SetDictationState(SPRS_ACTIVE))){USES_CONVERSION;const WCHAR * const pchStop = StopWord();CComPtr<ISpRecoResult> cpResult;printf("I will repeat everything you say. Say %s to exit. ", W2A(pchStop));while (SUCCEEDED(hr = BlockForResult(cpRecoCtxt, &cpResult))){cpGrammar->SetDictationState(SPRS_INACTIVE);CSpDynamicString dstrText;if (SUCCEEDED(cpResult->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &dstrText, NULL))){printf("I heard: %s ", W2A(dstrText));if (fUseTTS){cpVoice->Speak(L"I heard", SPF_ASYNC, NULL);cpVoice->Speak(dstrText, SPF_ASYNC, NULL);}if (fReplay){if (fUseTTS)cpVoice->Speak(L"when you said", SPF_ASYNC, NULL);elseprintf(" when you said ");cpResult->SpeakAudio(NULL, 0, NULL, NULL);}cpResult.Release();}if (_wcsicmp(dstrText, pchStop) == 0){break;}cpGrammar->SetDictationState(SPRS_ACTIVE);}}}::CoUninitialize();}return hr; }

總結

以上是生活随笔為你收集整理的用SAPI实现Speech Recognition(SR) - 听写模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。