73 SPSTATEHANDLE hStateTravel = NULL;
78 hr = cpRecoGrammar->GetRule(w_Rule, 0, SPRAF_TopLevel | SPRAF_Active, TRUE, &hStateTravel);
82 MMechostr(MSKDEBUG,
"Error: Can't add word\n");
89 hr = cpRecoGrammar->AddWordTransition(hStateTravel, NULL, w_Word, L
" ", SPWT_LEXICAL, 1, NULL);
94 MMechostr(MSKDEBUG,
"Error: Can't add word\n");
99 hr = cpRecoGrammar->Commit(NULL);
103 MMechostr(MSKDEBUG,
"Error: Can't add word\n");
108 hr = cpRecoGrammar->SetGrammarState(SPGS_ENABLED);
112 MMechostr(MSKDEBUG,
"Error: Can't add word\n");
118 hr = cpRecoGrammar->SetRuleState(NULL, NULL, SPRS_ACTIVE);
128 hr = pRecog.CoCreateInstance(CLSID_SpInprocRecognizer);
131 MMechostr(MSKDEBUG,
"Error: Can't create SAPI Speech Recognizer (ISpRecognizer)\n") ;
136 hr = pRecog->CreateRecoContext(&cpRecoContext);
139 MMechostr(MSKDEBUG,
"Error: Cannot create SAPI Recognition Context (ISpRecoContext)");
144 hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &cpAudio);
147 MMechostr(MSKDEBUG,
"Error: Can't create default audio object\n");
152 hr = pRecog->SetInput(cpAudio, TRUE);
155 MMechostr(MSKDEBUG,
"Error: Can't init audio object\n");
159 hr = cpAudio->SetVolumeLevel(1000);
162 MMechostr(MSKDEBUG,
"Error: volume reco\n");
166 hr = cpRecoContext->SetAudioOptions(SPAO_NONE, NULL, NULL);
171 hr = pRecog->SetRecoState(SPRST_ACTIVE);
174 MMechostr(MSKDEBUG,
"Error: Can't active recognition\n");
182 if (hr = cpRecoContext->CreateGrammar(NULL, &cpRecoGrammar) != S_OK)
184 MMechostr(MSKDEBUG,
"Error: Failed to create grammar\n");
191 hr = cpRecoGrammar->LoadDictation(NULL, SPLO_STATIC);
195 cpRecoGrammar->SetDictationState(SPRS_ACTIVE);
200 hr = cpRecoContext->SetNotifyCallbackFunction(&
sapi_callback, 0, LPARAM(
this));
203 MMechostr(MSKDEBUG,
"Error: Cannot set notify callback function. (SetNofifyCallbackFunction)");
209 const ULONGLONG ullInterest = SPFEI(SPEI_SOUND_START) |
210 SPFEI(SPEI_SOUND_END) |
211 SPFEI(SPEI_RECOGNITION);
212 hr = cpRecoContext->SetInterest(ullInterest, ullInterest);
216 MMechostr(MSKDEBUG,
"Error: Cannot correctly set notifications for the Speech Recognizer");
220 return (hr == S_OK) ? true :
false;
239 while (evt.GetFrom(cpRecoContext) == S_OK)
241 switch (evt.eEventId)
243 case SPEI_SOUND_START:
261 case SPEI_RECOGNITION:
264 const USHORT MY_MAX_ALTERNATES = 10;
265 CComPtr<ISpPhraseAlt> pcpPhraseAlt[MY_MAX_ALTERNATES];
267 std::string betterResult;
268 float ConfidenceMax = 0.0;
270 std::list<pTextRec*> lPhraseRec;
274 hr = evt.RecoResult()->GetPhrase(&pPhrase);
278 hr = evt.RecoResult()->GetAlternates(pPhrase->Rule.ulFirstElement,
279 pPhrase->Rule.ulCountOfElements,
281 (ISpPhraseAlt**) pcpPhraseAlt,
287 for (
unsigned int i = 0; i < ulCount; i++)
289 SPPHRASE* pPhraseAlt;
290 CSpDynamicString pwszAlternate;
293 pcpPhraseAlt[i]->GetPhrase(&pPhraseAlt);
296 hr = pcpPhraseAlt[i]->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &pwszAlternate, NULL);
299 float confidence = pPhraseAlt->pElements->SREngineConfidence;
301 std::string content = pwszAlternate.CopyToChar();
302 paramTextRec->
altText = content;
304 lPhraseRec.push_back(paramTextRec);
305 if (ConfidenceMax < confidence)
307 ConfidenceMax = confidence;
308 betterResult = content;