//########################################################################################## // // Program: Speech recognition and synthesis demo // Date: 20-12-06 // Language: C/C++ // Author: Chris Jones (modified from microsoft speech sdk 5.1 talkback example) // Purpose: Demionstrates how speech synthesis and recognition operations can be // produced very easily in C/C++ // //########################################################################################## #include #include #include #include #include #include #include "sphelper.h" #include int Say(char szData[500]); int Listen(char *buffer); inline HRESULT BlockForResult(ISpRecoContext * pRecoCtxt, ISpRecoResult ** ppResult); const WCHAR * StopWord(); //########################################################################################## // // Function: Main // Date: 20-12-06 // Language: C/C++ // Author: Chris Jones // Parameters: None // Returns: None // Purpose: Assume you already have the functions for Listen and Say, your C/C++ // program for speech recognition and synthesis is no more complicated # // than this - Thankyou Microsoft!!! // //########################################################################################## void main() { char szBuffer[8192]={'\0'}; //what you say is stored in this buffer Say ("Knock Knock"); // Listen (szBuffer); //you say "whos there" printf ("You said: %s",szBuffer); //print back what you said Say ("Doctor"); Listen (szBuffer); //you say Doctor who printf ("You said: %s",szBuffer); //print back what you said Say ("You just said it!!"); getch(); } //########################################################################################## // // Function: Say // Date: 20-12-06 // Language: C/C++ // Author: Chris Jones (modified from microsoft speech sdk 5.1 talkback example) // Parameters: szData[500] (what you want the computer to say) // Returns: int // Purpose: Pauses the program thread until it has said audibly what you wanted // it to say. Similar to the PlaySound function // //########################################################################################## int Say(char szData[500]) { HRESULT hr = E_FAIL; bool fUseTTS = true; // turn TTS play back on or off bool fReplay = true; // turn Audio replay on or off if (SUCCEEDED(hr = ::CoInitialize(NULL))) { { CComPtr cpRecoCtxt; CComPtr cpGrammar; CComPtr 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 cpResult; unsigned short *buffer; int buffer_size = (int)strlen(szData)+1; int estimated_phrase_duration=1000; for(int t=0;tSpeak(buffer, SPF_ASYNC, NULL); //Say the phrase you put into this function free(buffer); Sleep(estimated_phrase_duration); //halt this process until the phrase duration has elapsed } else { printf( "Can't allocate memory\n" ); //there was no memory fopr the malloc } } } ::CoUninitialize(); } return hr; } //########################################################################################## // // Function: Listen // Date: 20-12-06 // Language: C/C++ // Author: Chris Jones (modified from microsoft speech sdk 5.1 talkback example) // Parameters: *buffer (you return this back to the calling process) // Returns: int // Purpose: Pauses the program thread until you utter a word or words // and terminates once speaking has finished. What you said is // returned in a TCHAR string // //########################################################################################## int Listen(char *buffer) { HRESULT hr = E_FAIL; bool fUseTTS = true; // turn TTS play back on or off bool fReplay = true; // turn Audio replay on or off if (SUCCEEDED(hr = ::CoInitialize(NULL))) { { CComPtr cpRecoCtxt; CComPtr cpGrammar; CComPtr 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 cpResult; if (SUCCEEDED(hr = BlockForResult(cpRecoCtxt, &cpResult))) { cpGrammar->SetDictationState( SPRS_INACTIVE ); CSpDynamicString dstrText; if (SUCCEEDED(cpResult->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &dstrText, NULL))) { for(DWORD t=0;t<(DWORD)sizeof(dstrText)+10;t++) { buffer[t]=dstrText[t]; printf("\n%c",buffer[t]); } cpResult.Release(); } cpGrammar->SetDictationState( SPRS_ACTIVE ); } } } ::CoUninitialize(); } return hr; } 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"\x7d42\x4e86\\\x30b7\x30e5\x30fc\x30ea\x30e7\x30fc/\x3057\x3085\x3046\x308a\x3087\x3046";; break; default: pchStop = L"Stop"; break; } return pchStop; }