How to Create a Simple Dictation Pad in Delphi2009+Vista
the code are not so complecated..
private
{ Private declarations }
SpSharedRecoContext1 : TSpSharedRecoContex开发者_开发问答t;
fMyGrammar : ISpeechRecoGrammar;
procedure SpSharedRecoContext1Recognition(ASender: TObject; StreamNumber: Integer;
StreamPosition: OleVariant;
RecognitionType: SpeechRecognitionType;
const Result: ISpeechRecoResult);
procedure SpSharedRecoContext1Hypothesis(ASender: TObject; StreamNumber: Integer;
StreamPosition: OleVariant;
const Result: ISpeechRecoResult);
procedure TForm1.FormCreate(Sender: TObject); begin SpSharedRecoContext1 := TSpSharedRecoContext.Create(self); SpSharedRecoContext1.OnHypothesis := SpSharedRecoContext1Hypothesis; SpSharedRecoContext1.OnRecognition :=SpSharedRecoContext1Recognition; fMyGrammar := SpSharedRecoContext1.CreateGrammar(0); fMyGrammar.DictationSetState(SGDSActive); end; procedure TForm1.SpSharedRecoContext1Recognition(ASender: TObject; StreamNumber: Integer; StreamPosition: OleVariant; RecognitionType: SpeechRecognitionType; const Result: ISpeechRecoResult); begin Memo1.Text := Result.PhraseInfo.GetText(0,-1,true); end; procedure TForm1.SpSharedRecoContext1Hypothesis(ASender: TObject; StreamNumber: Integer; StreamPosition: OleVariant; const Result: ISpeechRecoResult); begin Memo1.Text := Result.PhraseInfo.GetText(0,-1,true); end;
My Problem, was the vista-OS voice command will intercept on my program. if i say "START", instead of writing start on memo1 it press the start menu on my desktop. or what ever command like START CANCEL EDIT DELETE SELECT etc. please help..... sorry for my english
You need to use an in-process recognizer, rather than the shared recognizer. Look at the SpInprocRecoContext object.
In particular, you need to also set the AudioInput property of the recognizer, so that the inproc recognizer knows where to get the audio from.
A fully worked example for simple dictation is part of the Windows 7 or Windows Vista SDK - after you install it, it's in $(WindowsSdkDir)\Samples\winui\speech\simpledictation.
The samples are in C++, but you should be able to use that as a launching point.
It would seem that the useful bit of code is:
HRESULT hr = S_OK;
CComPtr<ISpRecognizer> cpRecoEngine;
hr = cpRecoEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
if( SUCCEEDED( hr ) )
{
hr = cpRecoEngine->CreateRecoContext( &m_cpRecoCtxt );
}
// Set recognition notification for dictation
if (SUCCEEDED(hr))
{
hr = m_cpRecoCtxt->SetNotifyWindowMessage( hDlg, WM_RECOEVENT, 0, 0 );
}
if (SUCCEEDED(hr))
{
// This specifies which of the recognition events are going to trigger notifications.
// Here, all we are interested in is the beginning and ends of sounds, as well as
// when the engine has recognized something
const ULONGLONG ullInterest = SPFEI(SPEI_RECOGNITION);
m_cpRecoCtxt->SetInterest(ullInterest, ullInterest);
}
// create default audio object
CComPtr<ISpAudio> cpAudio;
SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &cpAudio);
// set the input for the engine
cpRecoEngine->SetInput(cpAudio, TRUE);
hr = cpRecoEngine->SetRecoState( SPRST_ACTIVE );
But how would we translate this into Delphi?
精彩评论