using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Windows.Forms;

namespace VoiceRecognize
{  
    class MyVoiceRecognize
    {
        public delegate void _GetRecognize(string Recognize);
        public event _GetRecognize GetRecognize;
       
        private SpeechLib.SpSharedRecoContext objRecoContext;
        private SpeechLib.ISpeechRecoGrammar grammar;
        private SpeechLib.ISpeechGrammarRule menuRule;
        object PropValue;       
        int count;
        string Recognize;

        public MyVoiceRecognize()
        {
            objRecoContext = null;
            grammar = null;
            menuRule = null;
            PropValue = "";
            count = 0;
            Recognize = string.Empty;
        }

        public void ListenStart()
        {                  
            objRecoContext = new SpeechLib.SpSharedRecoContext();           
            objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event);

            grammar = objRecoContext.CreateGrammar(0);
            menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
           
            menuRule.InitialState.AddWordTransition(null, "Exit", " ", SpeechGrammarWordType.SGLexical, "Exit", ++count, ref PropValue, 1.0F);           
            grammar.Rules.Commit();
            grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
        }

        public void ListenStop()
        {
            objRecoContext.Recognition -= Reco_Event;
            objRecoContext = null;
        }


        public void AddWord(string addword)
        {           
            menuRule.InitialState.AddWordTransition(null, addword, " ", SpeechGrammarWordType.SGLexical, addword, ++count, ref PropValue, 1.0F);
            grammar.Rules.Commit();
            grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
        }

        private void Reco_Event(int StreamNumber, object StreamPosition, SpeechRecognitionType RecognitionType, ISpeechRecoResult Result)
        {           
            Recognize = Result.PhraseInfo.GetText(0, -1, true);
            GetRecognize(Recognize);           
        }
    }
}

Posted by ... XJAPAN