Using Microsoft's Speech API (SAPI) in C#
There are many articles that discuss using SAPI for command and control. i.e. Basically recognizing specific commands. However, there are not many articles that talk about doing Speech Recognition for dictation using SAPI. This post is a simple example to get you up and running using SAPI.To get started create the following instance variables:
using SpeechLib;

private SpSharedRecoContext recognitionContext = null;
private ISpRecoGrammar recognitionGrammar = null;

Next I will create a call back function that is called when SAPI recognizes some speech:
 public void dictationRecognized(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
        {
            string myText = "";
            myText += Result.PhraseInfo.GetText(0, -1, true);
            dictationTextBox.Text += myText + Environment.NewLine;

        }
Finally add the code that initializes and starts the speech recognition:
recognitionContext = new SpSharedRecoContextClass();

            // Create a grammar          
            recognitionGrammar = recognitionContext.CreateGrammar(0) as ISpRecoGrammar;
            
            // Set the grammar as being in dictation mode
            recognitionGrammar.SetDictationState(SpeechLib.SPRULESTATE.SPRS_ACTIVE);
            recognitionGrammar.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED);

	    // Register a callback when some text is recognized
            recognitionContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(this.dictationRecognized);

That's all there is to it! Now you have a very simple dictaphone. :)If you want the computer to repeat (speak) the text back that's easy to do too ...
SpVoice Voice = new SpVoice();
Voice.Speak("Hello, I'm a computer.", SpeechVoiceSpeakFlags.SVSFDefault);

Comments...

Like to leave a comment ?


Title

Comment

Name