int voiceRecognition(string Qtext) { HRESULT hr = E_FAIL; int word=0; CSpDynamicString Qtextout; 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; CComPtr<ISpRecoResult> cpResult; Qtextout.operator=(Qtext.c_str()); cpVoice->Speak(Qtextout, SPF_ASYNC, NULL); if (SUCCEEDED(hr = BlockForResult(cpRecoCtxt, &cpResult))) { cpGrammar->SetDictationState(SPRS_INACTIVE); CSpDynamicString dstrText; if (SUCCEEDED(cpResult->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &dstrText, NULL))) { cpResult.Release(); } if (_wcsicmp(dstrText, L"True") == 0) { word = 1; //break; } if (_wcsicmp(dstrText, L"Two") == 0) { word = 1; //break; } if (_wcsicmp(dstrText, L"False") == 0) { word = 2; //break; } if (_wcsicmp(dstrText, L"Falls") == 0) { word = 2; //break; } if (_wcsicmp(dstrText, L"Follows") == 0) { word = 2; //break; } if (_wcsicmp(dstrText, L"A") == 0) { word = 3; //break; } if (_wcsicmp(dstrText, L"Eight") == 0) { word = 3; //break; } if (_wcsicmp(dstrText, L"B") == 0) { word = 4; //break; } if (_wcsicmp(dstrText, L"Bee") == 0) { word = 4; //break; } if (_wcsicmp(dstrText, L"C") == 0) { word = 5; //break; } if (_wcsicmp(dstrText, L"See") == 0) { word = 5; //break; } if (_wcsicmp(dstrText, L"Fire") == 0) { word = 6; //break; } if (_wcsicmp(dstrText, L"Leave") == 0) { word = 7; //break; } if (_wcsicmp(dstrText, L"Leave it") == 0) { word = 7; //break; } if (_wcsicmp(dstrText, L"We've") == 0) { word = 7; //break; } if (_wcsicmp(dstrText, L"Quit") == 0) { word = 7; //break; } if (_wcsicmp(dstrText, L"Quits") == 0) { word = 7; //break; } if (_wcsicmp(dstrText, L"Switch") == 0) { word = 8; //break; } if (_wcsicmp(dstrText, L"Change") == 0) { word = 8; //break; } cout << dstrText.CopyToChar(); cpGrammar->SetDictationState(SPRS_ACTIVE); } } } ::CoUninitialize(); } return word; }
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] \n", 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.\nSay \"%s\" to exit.\n", 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\n", 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); else printf ("\twhen you said...\n"); cpResult->SpeakAudio(NULL, 0, NULL, NULL); } cpResult.Release(); } if (_wcsicmp(dstrText, pchStop) == 0) { break; } cpGrammar->SetDictationState( SPRS_ACTIVE ); } } } ::CoUninitialize(); } return hr; }