C#基于Microsoft Speech SDK Version 5.1 的开发

   
            //Microsoft Speech SDK Version 5.1
            #endregion
            private SpeechLib.SpSharedRecoContext objRecoContext = null;

            #region ISpeechRecoGrammar说明
            //The ISpeechRecoGrammar automation interface enables applications to manage the words and phrases for the SR engine.
            //为语音识别引擎自动管理应用程序单词和短语
            /*       
            属性
            Id Property Returns                     the Id assigned to the grammar when it was created.
            RecoContext Property                    Returns the RecoContext object that created this grammar.
            Rules Property                          Returns the collection of grammar rules in the RecoGrammar.
            State Property                          Gets and sets the operational status of the speech grammar.

            方法
            CmdLoadFromFile Method                  Loads a command and control grammar from the specified file.
            CmdLoadFromMemory Method                Loads a compiled speech grammar from memory.
            CmdLoadFromObject Method                Loads a speech grammar from a COM object.
            CmdLoadFromProprietaryGrammar Method    Loads a proprietary speech grammar.
            CmdLoadFromResource Method              Loads a command and control grammar from a Win32 resource.
            CmdSetRuleIdState Method                Activates or deactivates a rule by its rule ID.
            CmdSetRuleState Method                  Activates or deactivates a rule by its rule name.
            DictationLoad Method                    Loads a dictation topic into the grammar.
            DictationSetState Method                Sets the dictation topic state.
            DictationUnload Method                  Unloads the active dictation topic from the grammar.
            IsPronounceable Method                  Determines if a word has a pronunciation.
            Reset Method                            Clears all grammar rules and resets the grammar's language to NewLanguage.
            SetTextSelection Method                 Sets the range of text selection information in a word sequence data buffer.
            SetWordSequenceData Method              Defines a word sequence data buffer for use by the SR engine.
             */
            #endregion
            private SpeechLib.ISpeechRecoGrammar grammar = null;

            #region ISpeechGrammarRule说明
            //The ISpeechGrammarRule automation interface defines the properties and methods of a speech grammar rule
            //自动定义语音属性和方法的接口
            /*        
            属性
            Attributes Property                     Returns information about the attributes of a speech grammar rule.
            Id Property                             Specifies the ID of the speech grammar rule.
            InitialState Property                   Specifies the initial state of the speech grammar rule.
            Name Property                           Specifies the name of the speech grammar rule.

            方法
            AddResource Method                      Adds a string to a speech rule.
            AddState Method                         Adds a state to a speech rule.
            Clear Method                            Clears a rule, leaving only its initial state.
             */
            #endregion
            private SpeechLib.ISpeechGrammarRule menuRule = null;

        #endregion

        private void cmdEnable_Click(object sender, System.EventArgs e)
  {
   
   // 得到一个RecoContext实例.
   objRecoContext = new SpeechLib.SpSharedRecoContext();
            // 指派一个事件给Hypothesis Event(中间层暂定的识别,即,初级的,临时的识别).
   objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event);
   // 指派一个事件给语音识别.
   objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event);
   //创建grammer实例.
   grammar = objRecoContext.CreateGrammar(0);
   
   label3.Text = "Speak Out one of the follwoing./r/n1. New 2. Open 3. Close 4. Exit/r/n5. Cut 6. Copy 7. Paste 8. Delete";

   //激活菜单命令.   
   menuRule = grammar.Rules.Add("MenuCommands",SpeechRuleAttributes.SRATopLevel|SpeechRuleAttributes.SRADynamic,1);

   object      PropValue = "";
   menuRule.InitialState.AddWordTransition(null,"New"," ",SpeechGrammarWordType.SGLexical,"New", 1, ref PropValue, 1.0F );
   menuRule.InitialState.AddWordTransition(null,"Open"," ",SpeechGrammarWordType.SGLexical,"Open", 2, ref PropValue, 1.0F );
   menuRule.InitialState.AddWordTransition(null,"Close"," ",SpeechGrammarWordType.SGLexical,"Close",3, ref PropValue, 1.0F );
   menuRule.InitialState.AddWordTransition(null,"Exit"," ",SpeechGrammarWordType.SGLexical,"Exit",4, ref PropValue, 1.0F );
   menuRule.InitialState.AddWordTransition(null,"Cut"," ",SpeechGrammarWordType.SGLexical,"Cut",5, ref PropValue, 1.0F );
   menuRule.InitialState.AddWordTransition(null,"Copy"," ",SpeechGrammarWordType.SGLexical,"Copy",6, ref PropValue, 1.0F );
   menuRule.InitialState.AddWordTransition(null,"Paste"," ",SpeechGrammarWordType.SGLexical,"Paste",7, ref PropValue, 1.0F );
   menuRule.InitialState.AddWordTransition(null,"Delete"," ",SpeechGrammarWordType.SGLexical,"Delete",8, ref PropValue, 1.0F );

   grammar.Rules.Commit();
   grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive);
        }

        #region 初级和正式的语音识别事件处理

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

        private void Hypo_Event(int StreamNumber, object StreamPosition, ISpeechRecoResult Result)
  {
   txtHyp.Text = Result.PhraseInfo.GetText(0, -1, true);

  }

        #endregion

        #region 清空控件和使关闭语音识别

        private void cmdDiable_Click(object sender, System.EventArgs e)
  {
            ClearControls(label3, objRecoContext);
        }

        #endregion

        #region 自定义函数

        ///


        /// 清空数据
        ///

        /// 需要清空的变量及控件
        public void ClearControls(params object[] args)
        {
            foreach (object obj in args)
            {
                if (obj is TextBox)
                {
                    TextBox t = (TextBox)obj;
                    t.Text = "";
                }
                if (obj is ListBox)
                {
                    ListBox l = (ListBox)obj;
                    l.Items.Clear();
                }
                if (obj is Label)
                {
                    Label l = (Label)obj;
                    l.Text = "";
                }
                if (obj is SpeechLib.SpSharedRecoContext)
                {
                    SpeechLib.SpSharedRecoContext s = (SpeechLib.SpSharedRecoContext)obj;
                    s = null;
                }
            }
        }

        #endregion
    }
}

你可能感兴趣的:(.net技术,语音交互开发)