#include "sphelper.h"
#include "windows.h"
<CComPtr> pRecoEngine;
<CComPtr> pRecoContext;
<CComPtr> pRecoGrammar;
<CComPtr> pInputToken;
<CComPtr> pRecoResult;
int main(void)
{
HRESULT hr = S_OK;
hr = ::CoInitialize(NULL);
if(SUCCEEDED(hr))
{
//create an inproc recognizer - for this type of recognizer you have to set the audio input object manually
hr = pRecoEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
if(FAILED(hr))
{
printf("--- FAILED to create InProcRecognizer \n");
return -1;
}
//create context
hr = pRecoEngine->CreateRecoContext(&pRecoContext);
if(FAILED(hr))
{
printf("--- FAILED to create Context \n");
return -1;
}
//create grammar
hr = pRecoContext->CreateGrammar(0, &pRecoGrammar);
if(FAILED(hr))
{
printf("--- FAILED to create Grammar \n");
return -1;
}
//set object which will receive notifications from the engine
hr = pRecoContext->SetNotifyWin32Event();
if(FAILED(hr))
{
printf("--- FAILED to SetNotifyWin32Event() \n");
return -1;
}
ULONGLONG events = SPFEI(SPEI_RECOGNITION)|
SPFEI(SPEI_FALSE_RECOGNITION)|
SPFEI(SPEI_PHRASE_START)|
SPFEI(SPEI_SOUND_START);
//set events we want to receive from the engine
hr = pRecoContext->SetInterest(events, events);
if(FAILED(hr))
{
printf("--- FAILED to SetInterest() \n");
return -1;
}
//get default audio input object
hr = SpGetDefaultTokenFromCategoryId(SPCAT_AUDIOIN, &pInputToken);
if(FAILED(hr))
{
printf("--- FAILED to Get default input token \n");
return -1;
}
else
{
//if input object got successfully, use it
hr = pRecoEngine->SetInput(pInputToken, FALSE);
if(FAILED(hr))
{
printf("--- FAILED to SET default input token \n");
}
}
GUID grammarGUID;
::CoCreateGuid(&grammarGUID);
//load a grammar - this is an example with proprietary grammars, which require GUIDs
//xml grammars do not require GUIDs, one could just use integer grammar ids
hr = pRecoGrammar->LoadCmdFromProprietaryGrammar(grammarGUID, L"digit", NULL, 0, SPLO_STATIC);
if(FAILED(hr))
{
printf("--- FAILED to Load Proprietary grammar \n");
return -1;
}
//set engine state to active
pRecoEngine->SetRecoState(SPRST_ACTIVE);
//enable context
pRecoContext->SetContextState(SPCS_ENABLED);
//enable grammar
pRecoGrammar->SetGrammarState(SPGS_ENABLED);
//activate grammar rules
pRecoGrammar->SetRuleState(NULL, NULL, SPRS_ACTIVE);
//these four steps are necessary for the SR engine to be able to start listening for user speech
bool bDone = false;
CSpEvent event;
while(!bDone)
{
//wait for 5 seconds for an event from the engine
hr = pRecoContext->WaitForNotifyEvent(5000);
if(hr == S_FALSE)
{
printf("--- Operation timeout \n");
}
if(hr == S_OK)
{
//get the event from the context
if(event.GetFrom(pRecoContext) == S_OK)
{
switch(event.eEventId)
{
case SPEI_SOUND_START:
{
printf("--- SPEI_SOUND_START \n");
}
break;
case SPEI_RECOGNITION:
{
//handle a successful recognition (print the result)
printf("--- SPEI_RECOGNITION \n");
WCHAR* pRecogStr = 0;
pRecoResult = event.RecoResult();
pRecoResult->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &pRecogStr, NULL);
printf("--- Result is: %ls \n", pRecogStr);
bDone = true;
}
break;
case SPEI_FALSE_RECOGNITION:
{
printf("--- SPEI_FALSE_RECOGNITION \n");
bDone = true;
}
break;
default:
break;
}
}
}
}
}
pRecoGrammar.Release();
pRecoContext.Release();
pRecoEngine.Release();
::CoUninitialize();
getchar();
return 0;
}
This should get one started with speech recognition. One should check the SAPI docs for more information on each function in the API.
Cheers!