00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00036
00037 #include<stdio.h>
00038 #include<stdlib.h>
00039 #include "plugin.h"
00040 #include <iostream>
00041 #include <string.h>
00042
00043
00045 Recognition::Recognition()
00046 {
00047 TextReco=NULL;
00048 m_bGotReco = FALSE ;
00049 m_bInSound = FALSE;
00050 initializeObjects();
00051 }
00052
00053
00055 Recognition::~Recognition()
00056 {
00057 close();
00058 }
00059
00060
00062 void Recognition::addWord(char *s_Rule, char *s_Word)
00063 {
00064
00065 HRESULT hr = S_OK;
00066 SPSTATEHANDLE hStateTravel;
00067
00068 wchar_t * w_Rule = convertCharToLPCWSTR(s_Rule);
00069 hr = cpRecoGrammar->GetRule(w_Rule, 0, SPRAF_TopLevel | SPRAF_Active, TRUE, &hStateTravel);
00070 delete []w_Rule;
00071
00072 wchar_t * w_Word = convertCharToLPCWSTR(s_Word);
00073 hr = cpRecoGrammar->AddWordTransition(hStateTravel, NULL, w_Word, L" ", SPWT_LEXICAL, 1, NULL);
00074 delete []w_Word;
00075
00076 hr = cpRecoGrammar->Commit(NULL);
00077 hr = cpRecoGrammar->SetGrammarState(SPGS_ENABLED);
00078 hr = cpRecoGrammar->SetRuleState(0, 0, SPRS_ACTIVE);
00079 }
00080
00081
00083 bool Recognition::initializeObjects()
00084 {
00085 HRESULT hr;
00086 hr = pRecog.CoCreateInstance(CLSID_SpInprocRecognizer);
00087 if (FAILED(hr ))
00088 MMechostr(MSKDEBUG,"Error: Can't create SAPI Speech Recognizer (ISpRecognizer)\n") ;
00089
00090 if (SUCCEEDED(hr))
00091 hr = pRecog->CreateRecoContext(&cpRecoContext);
00092
00093 if (hr != S_OK)
00094 MMechostr(MSKDEBUG,"Error: Cannot create SAPI Recognition Context (ISpRecoContext)");
00095
00096 if (SUCCEEDED(hr))
00097 hr = cpRecoContext->SetNotifyCallbackFunction(&sapi_callback, 0, LPARAM(this));
00098
00099 if (hr != S_OK)
00100 MMechostr(MSKDEBUG,"Error: Cannot set notify callback function. (SetNofifyCallbackFunction)");
00101
00102 if (SUCCEEDED(hr))
00103 {
00104 const ULONGLONG ullInterest = SPFEI(SPEI_SOUND_START) | SPFEI(SPEI_SOUND_END) |
00105 SPFEI(SPEI_PHRASE_START) | SPFEI(SPEI_RECOGNITION) |
00106 SPFEI(SPEI_FALSE_RECOGNITION) | SPFEI(SPEI_HYPOTHESIS) |
00107 SPFEI(SPEI_INTERFERENCE) | SPFEI(SPEI_RECO_OTHER_CONTEXT) |
00108 SPFEI(SPEI_REQUEST_UI) | SPFEI(SPEI_RECO_STATE_CHANGE) |
00109 SPFEI(SPEI_PROPERTY_NUM_CHANGE) | SPFEI(SPEI_PROPERTY_STRING_CHANGE);
00110 hr = cpRecoContext->SetInterest(ullInterest, ullInterest);
00111 }
00112 if (hr != S_OK)
00113 MMechostr(MSKDEBUG,"Error: Cannot correctly set notifications for the Speech Recognizer");
00114
00115 hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &cpAudio);
00116
00117 if(FAILED(hr ))
00118 MMechostr(MSKDEBUG,"Error: Can't create default audio object\n") ;
00119
00120 else
00121 {
00122 hr = pRecog->SetInput(cpAudio, TRUE);
00123 hr = pRecog->SetRecoState( SPRST_ACTIVE );
00124 }
00125
00126 if (SUCCEEDED(hr))
00127 hr = cpRecoContext->CreateGrammar(1, &cpRecoGrammar);
00128
00129 if (hr != S_OK)
00130 MMechostr(MSKDEBUG,"Error: Failed to create grammar\n");
00131
00132 if (SUCCEEDED(hr))
00133 hr = cpRecoGrammar->LoadDictation(NULL, SPLO_STATIC);
00134
00135 if (SUCCEEDED(hr))
00136 hr = cpRecoGrammar->SetDictationState( SPRS_ACTIVE );
00137
00138
00139 hr = cpAudio->SetVolumeLevel(1000);
00140 if(FAILED(hr )) MMechostr(MSKDEBUG,"Error: volume reco\n") ;
00141
00142 return (hr == S_OK);
00143 }
00144
00145
00147 void _stdcall Recognition::sapi_callback(WPARAM wParam, LPARAM lParam)
00148 {
00149 Recognition* pThis = (Recognition*)lParam;
00150 pThis->callbackEventReco();
00151 }
00152
00153
00155 void Recognition::close()
00156 {
00157 pRecog.Release();
00158 cpRecoContext.Release();
00159 cpRecoGrammar.Release();
00160 }
00161
00162
00164 void Recognition::callbackEventReco()
00165 {
00166 USES_CONVERSION;
00167 CSpEvent evt;
00168 BYTE bDisplayAttr;
00169
00170 while (evt.GetFrom(cpRecoContext) == S_OK)
00171 {
00172 switch (evt.eEventId)
00173 {
00174 case SPEI_SOUND_START:
00175 {
00176 m_bInSound = TRUE;
00177 MMechostr(MSKDEBUG,"SPEI_SOUND_START\n");
00178 PostMessage(HScol, RECOGNITION_START_CB, (int)this,(LPARAM)NULL);
00179 }
00180 break;
00181
00182 case SPEI_SOUND_END:
00183 {
00184 if (m_bInSound)
00185 {
00186 m_bInSound = FALSE;
00187 MMechostr(MSKDEBUG,"SPEI_SOUND_END\n");
00188 PostMessage(HScol, RECOGNITION_END_CB, (int)this,(LPARAM)NULL);
00189 m_bGotReco = FALSE;
00190 }
00191 }
00192 break;
00193
00194 case SPEI_INTERFERENCE:
00195 {
00196 switch(evt.Interference())
00197 {
00198 case SPINTERFERENCE_NOISE:
00199 MMechostr(MSKDEBUG,"Interference - Noise\n") ;
00200 break;
00201 case SPINTERFERENCE_NOSIGNAL:
00202 MMechostr(MSKDEBUG,"Interference - A sound is received but it is of a constant intensity\n");
00203 break;
00204 case SPINTERFERENCE_TOOLOUD:
00205 MMechostr(MSKDEBUG,"Interference - A sound is received but the stream intensity is too high for discrete recognitio\n");
00206 break;
00207 case SPINTERFERENCE_TOOQUIET:
00208 MMechostr(MSKDEBUG,"Interference - A sound is received but the stream intensity is too low for discrete recognition\n");
00209 break;
00210 case SPINTERFERENCE_TOOFAST:
00211 MMechostr(MSKDEBUG,"Interference - The words are spoken too quickly for discrete recognition\n");
00212 break;
00213 case SPINTERFERENCE_TOOSLOW:
00214 MMechostr(MSKDEBUG,"Interference - he words are spoken too slowly and indicates excessive time between words\n");
00215 break;
00216 }
00217 }
00218 break;
00219
00220 case SPEI_FALSE_RECOGNITION:
00221 {
00222 MMechostr(MSKDEBUG,"No recognition\n") ;
00223 }
00224 break;
00225
00226 case SPEI_RECOGNITION:
00227 {
00228 MMechostr(MSKDEBUG,"SPEI_RECOGNITION\n");
00229 HRESULT hr = S_OK;
00230 const USHORT MY_MAX_ALTERNATES = 10;
00231 CComPtr<ISpPhraseAlt> pcpPhraseAlt[MY_MAX_ALTERNATES];
00232 SPPHRASE *pPhrase;
00233 CSpDynamicString betterResult;
00234 float ConfidenceMax = 0.0 ;
00235 ULONG ulCount;
00236 std::list <pTextRec > lPhraseRec;
00237 std::list <char* > lWordsRec;
00238
00239 hr = evt.RecoResult()->GetPhrase(&pPhrase);
00240
00241 if (SUCCEEDED(hr))
00242 {
00243 hr = evt.RecoResult()->GetAlternates(pPhrase->Rule.ulFirstElement,
00244 pPhrase->Rule.ulCountOfElements,
00245 MY_MAX_ALTERNATES,
00246 (ISpPhraseAlt**) pcpPhraseAlt,
00247 &ulCount);
00248 }
00249 if (SUCCEEDED(hr))
00250 {
00251 for (int i = 0; i < ulCount; i++)
00252 {
00253 SPPHRASE *pPhraseAlt;
00254 CSpDynamicString pwszAlternate;
00255 pcpPhraseAlt[i]->GetPhrase(&pPhraseAlt);
00256 hr = pcpPhraseAlt[i]->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &pwszAlternate, NULL);
00257 if (SUCCEEDED(hr))
00258 {
00259 float confidence = pPhraseAlt->pElements->SREngineConfidence;
00260 pTextRec paramTextRec ;
00261 paramTextRec.altText = pwszAlternate ;
00262 paramTextRec.altConfidence = confidence;
00263 lPhraseRec.push_back(paramTextRec);
00264 if(ConfidenceMax < confidence)
00265 {
00266 ConfidenceMax = confidence;
00267 betterResult = pwszAlternate ;
00268 }
00269 MMechostr(MSKDEBUG,"pcpPhraseAlt [%d] : %s\n", i, pwszAlternate.CopyToChar());
00270 MMechostr(MSKDEBUG,"pcpPhraseAlt , %f\n\n", confidence);
00271 }
00272 }
00273 }
00274
00275 MMechostr(MSKDEBUG,"better result : %s\n", betterResult.CopyToChar());
00276 MMechostr(MSKDEBUG,"ConfidenceMax , %f\n\n", ConfidenceMax);
00277 MMechostr(MSKDEBUG,"Ready to send Callback \n") ;
00278
00279 cbData * cbdataTextAlt = new cbData(lPhraseRec);
00280 PostMessage(HScol, RECOGNITION_TEXTS_ALT_CB, (int)this,(LPARAM)cbdataTextAlt);
00281
00282 cbData * cbdataText = new cbData(betterResult.CopyToChar());
00283 PostMessage(HScol, RECOGNITION_TEXT_CB, (int)this,(LPARAM)cbdataText);
00284
00285
00286
00287
00288 }
00289 break;
00290 }
00291 }
00292 }
00293
00294
00296 char* Recognition::getTextReco()
00297 {
00298 return this->TextReco;
00299 }
00300
00301
00303 int Recognition::getVolumeReco()
00304 {
00305 HRESULT hr = S_OK;
00306 int volumeReco = -1;
00307 hr = cpAudio->GetVolumeLevel((ULONG *)&volumeReco);
00308 if(FAILED(hr ))
00309 return -1;
00310 else
00311 return volumeReco / 100;
00312 }
00313
00314
00316 void Recognition::setvolumeReco(ULONG volumeReco)
00317 {
00318 HRESULT hr = S_OK;
00319 hr = cpAudio->SetVolumeLevel(volumeReco*100);
00320 if(FAILED(hr )) MMechostr(MSKDEBUG,"Error: volume reco\n") ;
00321 }