automatically saving SAPI output with C#
Hi,
Im doing my final year project using SAPI to create words/phrases to be used in a program to test the correctness of different synthesiser's.
The brief i have been given excludes running SAPI in real time so all words/phrases have to be saved to .wav first and then be called back using soundplayer in c#.
I have been looking at various examples online and tried without much success to get SAPI to essentially output to file instead of speaking for say each word in a list of 20 words.
I know its possibly something very basic but any advice on this would be muxh appricated as its got me stumped and i havent had to code in 2 years so my skills are very much lacking.
Thanks
Myles
[699 byte] By [
MylesM] at [2008-1-29]
here is sample C++ code,
U can easily convert it to c#
CComPtr<ISpStream> cpInputStream;
CComPtr<ISpRecognizer> cpRecognizer;
CComPtr<ISpRecoContext> cpRecoContext;
CComPtr<ISpRecoGrammar> cpRecoGrammar;
// Create basic SAPI stream object
LONG hr = cpInputStream.CoCreateInstance(CLSID_SpStream);
CSpStreamFormat sInputFormat;
// generate WaveFormatEx structure, assuming the wav format is 22kHz, 16-bit, Stereo
hr = sInputFormat.AssignFormat(SPSF_11kHz16BitMono);
hr = cpInputStream->BindToFile(L"C:\\test.wav",
SPFM_OPEN_READONLY,
&sInputFormat.FormatId(),
sInputFormat.WaveFormatExPtr(),
SPFEI_ALL_EVENTS);
// Create in-process speech recognition engine
hr = cpRecognizer.CoCreateInstance(CLSID_SpInprocRecognizer);
// connect wav input to recognizer
// SAPI will negotiate mismatched engine/input audio formats using system audio codecs, so second parameter is not important - use default of TRUE
hr = cpRecognizer->SetInput(cpInputStream, TRUE);
hr = cpRecognizer->CreateRecoContext(&cpRecoContext);
hr = cpRecoContext->CreateGrammar(NULL, &cpRecoGrammar);
// Check hr
hr = cpRecoGrammar->LoadDictation(NULL, SPLO_STATIC);
// Check hr
// check for recognitions and end of stream event
hr = cpRecoContext->SetInterest(SPFEI(SPEI_RECOGNITION) | SPFEI(SPEI_END_SR_STREAM), SPFEI(SPEI_RECOGNITION) | SPFEI(SPEI_END_SR_STREAM));
// use Win32 events for command-line style application
hr = cpRecoContext->SetNotifyWin32Event();
// Check hr
// activate dictation, and begin recognition
hr = cpRecoGrammar->SetDictationState(SPRS_ACTIVE);
// Check hr
// while events occur, continue processing
// timeout should be greater than the audio stream length, or a reasonable amount of time expected to pass before no more recognitions are expected in an audio stream
BOOL fEndStreamReached = FALSE;
while (!fEndStreamReached && S_OK == cpRecoContext->WaitForNotifyEvent(104900))
{
CSpEvent spEvent;
CSpDynamicString dstrText;
// pull all queued events from the reco context's event queue
while (!fEndStreamReached && S_OK == spEvent.GetFrom(cpRecoContext))
{
// Check event type
switch (spEvent.eEventId)
{
// speech recognition engine recognized some audio
case SPEI_RECOGNITION:
if (SUCCEEDED(spEvent.RecoResult()->GetText(SP_GETWHOLEPHRASE,
SP_GETWHOLEPHRASE,
TRUE, &dstrText, NULL)))
{
ISpPhraseAlt *pPhrases = NULL;
ULONG ulCount = 0;
SPPHRASE *pCoMemPhrase = NULL;
BSTR bstrText = SysAllocString(dstrText);
CString strText(bstrText);
fstream ofile("C:\\reco.txt", ios::app);
ofile << strText << " "; ofile.close();
ofile.close();
SysFreeString(bstrText);
}
break;
// end of the wav file was reached by the speech recognition engine
case SPEI_END_SR_STREAM:
fEndStreamReached = TRUE;
break;
}
spEvent.Clear();
}
}
hr = cpRecoGrammar->SetDictationState(SPRS_INACTIVE);
hr = cpRecoGrammar->UnloadDictation();
hr = cpInputStream->Close();
Regards,
Sudeesh