Hai Xu
2004-01-14 04:05:35 UTC
Hello,
The following source code shows a simple console program that takes wav file
to reco into text.
however the statement
" hr = cpRecoResult->GetPhrase((SPPHRASE**)&pPhrase); "
crashes the program with segmentation violation error,
any one knows why ?
Thanks
Hai
I've removed all status check and comments to make it shorter
================
#include "stdafx.h"
#include <windows.h>
#include <sapi.h>
#include <spdebug.h>
// SAPI Header Files
#include <sphelper.h>
#include <spddkhlp.h>
int main(int argc, char* argv[])
{
HRESULT hr;
CComPtr<ISpStream> cpInputStream;
CComPtr<ISpRecognizer> cpRecognizer;
CComPtr<ISpRecoContext> cpRecoContext;
CComPtr<ISpRecoGrammar> cpRecoGrammar;
CComPtr<ISpRecoResult> cpRecoResult;
CComPtr<ISpPhrase> pPhrase;
WCHAR *pwszText;
CoInitialize(NULL);
hr = cpInputStream.CoCreateInstance(CLSID_SpStream);
CSpStreamFormat sInputFormat(SPSF_22kHz8BitMono, &hr);
hr = cpInputStream->BindToFile(L"test.wav",
SPFM_OPEN_READONLY,
&sInputFormat.FormatId(),
sInputFormat.WaveFormatExPtr(),
SPFEI_ALL_EVENTS); // SPFEI_ALL_EVENTS
hr = cpRecognizer.CoCreateInstance(CLSID_SpInprocRecognizer);
hr = cpRecognizer->SetInput(cpInputStream, TRUE);
hr = cpRecognizer->CreateRecoContext(&cpRecoContext);
hr = cpRecoContext->CreateGrammar(NULL, &cpRecoGrammar);
hr = cpRecoGrammar->LoadDictation(NULL,SPLO_STATIC);
hr = cpRecoContext->SetInterest(SPFEI(SPEI_RECOGNITION) |
SPFEI(SPEI_END_SR_STREAM), SPFEI(SPEI_RECOGNITION) |
SPFEI(SPEI_END_SR_STREAM));
hr = cpRecoContext->SetNotifyWin32Event();
hr = cpRecoGrammar->SetDictationState(SPRS_ACTIVE);
BOOL fEndStreamReached = FALSE;
while (!fEndStreamReached)
{
hr = cpRecoContext->WaitForNotifyEvent(INFINITE);
CSpEvent spEvent;
while (!fEndStreamReached && S_OK == spEvent.GetFrom(cpRecoContext))
{
switch (spEvent.eEventId)
{
case SPEI_RECOGNITION:
hr = cpRecoResult->GetPhrase((SPPHRASE**)&pPhrase);
hr = pPhrase->GetText(SP_GETWHOLEPHRASE,
SP_GETWHOLEPHRASE, TRUE, &pwszText, NULL);
printf("result ==> %s\n", pwszText);
break;
case SPEI_END_SR_STREAM:
fEndStreamReached = TRUE;
break;
}
spEvent.Clear();
}
}
hr = cpRecoGrammar->SetDictationState(SPRS_INACTIVE);
hr = cpRecoGrammar->UnloadDictation();
hr = cpInputStream->Close();
cpRecognizer.Release();
CoUninitialize();
return 0;
}
===============================
The following source code shows a simple console program that takes wav file
to reco into text.
however the statement
" hr = cpRecoResult->GetPhrase((SPPHRASE**)&pPhrase); "
crashes the program with segmentation violation error,
any one knows why ?
Thanks
Hai
I've removed all status check and comments to make it shorter
================
#include "stdafx.h"
#include <windows.h>
#include <sapi.h>
#include <spdebug.h>
// SAPI Header Files
#include <sphelper.h>
#include <spddkhlp.h>
int main(int argc, char* argv[])
{
HRESULT hr;
CComPtr<ISpStream> cpInputStream;
CComPtr<ISpRecognizer> cpRecognizer;
CComPtr<ISpRecoContext> cpRecoContext;
CComPtr<ISpRecoGrammar> cpRecoGrammar;
CComPtr<ISpRecoResult> cpRecoResult;
CComPtr<ISpPhrase> pPhrase;
WCHAR *pwszText;
CoInitialize(NULL);
hr = cpInputStream.CoCreateInstance(CLSID_SpStream);
CSpStreamFormat sInputFormat(SPSF_22kHz8BitMono, &hr);
hr = cpInputStream->BindToFile(L"test.wav",
SPFM_OPEN_READONLY,
&sInputFormat.FormatId(),
sInputFormat.WaveFormatExPtr(),
SPFEI_ALL_EVENTS); // SPFEI_ALL_EVENTS
hr = cpRecognizer.CoCreateInstance(CLSID_SpInprocRecognizer);
hr = cpRecognizer->SetInput(cpInputStream, TRUE);
hr = cpRecognizer->CreateRecoContext(&cpRecoContext);
hr = cpRecoContext->CreateGrammar(NULL, &cpRecoGrammar);
hr = cpRecoGrammar->LoadDictation(NULL,SPLO_STATIC);
hr = cpRecoContext->SetInterest(SPFEI(SPEI_RECOGNITION) |
SPFEI(SPEI_END_SR_STREAM), SPFEI(SPEI_RECOGNITION) |
SPFEI(SPEI_END_SR_STREAM));
hr = cpRecoContext->SetNotifyWin32Event();
hr = cpRecoGrammar->SetDictationState(SPRS_ACTIVE);
BOOL fEndStreamReached = FALSE;
while (!fEndStreamReached)
{
hr = cpRecoContext->WaitForNotifyEvent(INFINITE);
CSpEvent spEvent;
while (!fEndStreamReached && S_OK == spEvent.GetFrom(cpRecoContext))
{
switch (spEvent.eEventId)
{
case SPEI_RECOGNITION:
hr = cpRecoResult->GetPhrase((SPPHRASE**)&pPhrase);
hr = pPhrase->GetText(SP_GETWHOLEPHRASE,
SP_GETWHOLEPHRASE, TRUE, &pwszText, NULL);
printf("result ==> %s\n", pwszText);
break;
case SPEI_END_SR_STREAM:
fEndStreamReached = TRUE;
break;
}
spEvent.Clear();
}
}
hr = cpRecoGrammar->SetDictationState(SPRS_INACTIVE);
hr = cpRecoGrammar->UnloadDictation();
hr = cpInputStream->Close();
cpRecognizer.Release();
CoUninitialize();
return 0;
}
===============================