Esempio n. 1
0
        /// <summary>
        ///     This function will create the main SpSharedRecoContext object
        ///     and other required objects like Grammar and rules.
        ///     In this sample, we are building grammar dynamically since
        ///     listbox content can change from time to time.
        ///     If your grammar is static, you can write your grammar file
        ///     and ask SAPI to load it during run time. This can reduce the
        ///     complexity of your code.
        /// </summary>
        private void InitializeSpeech()
        {
            Debug.WriteLine("Initializing SAPI objects...");

            try
            {
                // First of all, let's create the main reco context object.
                // In this sample, we are using shared reco context. Inproc reco
                // context is also available. Please see the document to decide
                // which is best for your application.
                objRecoContext = new SpeechLib.SpSharedRecoContext();

                // Then, let's set up the event handler. We only care about
                // Hypothesis and Recognition events in this sample.
                objRecoContext.Hypothesis += new
                                             _ISpeechRecoContextEvents_HypothesisEventHandler(
                    RecoContext_Hypothesis);

                objRecoContext.Recognition += new
                                              _ISpeechRecoContextEvents_RecognitionEventHandler(
                    RecoContext_Recognition);

                // Now let's build the grammar.
                // The top level rule consists of two parts: "select <items>".
                // So we first add a word transition for the "select" part, then
                // a rule transition for the "<items>" part, which is dynamically
                // built as items are added or removed from the listbox.
                grammar      = objRecoContext.CreateGrammar(grammarId);
                ruleTopLevel = grammar.Rules.Add("TopLevelRule",
                                                 SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
                ruleListItems = grammar.Rules.Add("ListItemsRule",
                                                  SpeechRuleAttributes.SRADynamic, 2);

                SpeechLib.ISpeechGrammarRuleState stateAfterSelect;
                stateAfterSelect = ruleTopLevel.AddState();

                object PropValue = "";
                ruleTopLevel.InitialState.AddWordTransition(stateAfterSelect,
                                                            PreCommandString, " ", SpeechGrammarWordType.SGLexical,
                                                            "", 0, ref PropValue, 1.0F);

                PropValue = "";
                stateAfterSelect.AddRuleTransition(null, ruleListItems, "",
                                                   1, ref PropValue, 0F);

                // Now add existing list items to the ruleListItems
                RebuildGrammar();

                // Now we can activate the top level rule. In this sample, only
                // the top level rule needs to activated. The ListItemsRule is
                // referenced by the top level rule.
                grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive);
                speechInitialized = true;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(
                    "Exception caught when initializing SAPI."
                    + " This application may not run correctly.\r\n\r\n"
                    + e.ToString(),
                    "Error");
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     This function will create the main SpInProcRecoContext object
        ///     and other required objects like Grammar and rules.
        ///     In this sample, we are building grammar dynamically since
        ///     listbox content can change from time to time.
        ///     If your grammar is static, you can write your grammar file
        ///     and ask SAPI to load it during run time. This can reduce the
        ///     complexity of your code.
        /// </summary>
        private void InitializeSpeech()
        {
            Debug.WriteLine("Initializing SAPI objects...");

            try
            {
                // First of all, let's create the main reco context object.
                // In this sample, we are using inproc reco context. Shared reco
                // context is also available. Please see the document to decide
                // which is best for your application.
                objRecoContext = new SpeechLib.SpInProcRecoContext();

                SpeechLib.SpObjectTokenCategory objAudioTokenCategory = new SpeechLib.SpObjectTokenCategory();
                objAudioTokenCategory.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryAudioIn, false);

                SpeechLib.SpObjectToken objAudioToken = new SpeechLib.SpObjectToken();
                objAudioToken.SetId(objAudioTokenCategory.Default, SpeechLib.SpeechStringConstants.SpeechCategoryAudioIn, false);

                objRecoContext.Recognizer.AudioInput = objAudioToken;

                // Then, let's set up the event handler. We only care about
                // Hypothesis and Recognition events in this sample.
                //objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(RecoContext_Hypothesis);

                objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition);

                // Now let's build the grammar.
                // The top level rule consists of two parts: "select <items>".
                // So we first add a word transition for the "select" part, then
                // a rule transition for the "<items>" part, which is dynamically
                // built as items are added or removed from the listbox.
                grammar       = objRecoContext.CreateGrammar(grammarId);
                ruleTopLevel  = grammar.Rules.Add("TopLevelRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
                ruleCommand   = grammar.Rules.Add("CommandRule", SpeechRuleAttributes.SRADynamic, 2);
                ruleNumbers   = grammar.Rules.Add("NumberRule", SpeechRuleAttributes.SRADynamic, 3);
                ruleListItems = grammar.Rules.Add("ListItemsRule", SpeechRuleAttributes.SRADynamic, 4);

                //Prepare states
                SpeechLib.ISpeechGrammarRuleState stateAfterPre;
                SpeechLib.ISpeechGrammarRuleState stateAfterCommand;
                SpeechLib.ISpeechGrammarRuleState stateAfterNumber;
                stateAfterPre     = ruleTopLevel.AddState();
                stateAfterCommand = ruleTopLevel.AddState();
                stateAfterNumber  = ruleTopLevel.AddState();

                //Add keywords: add,set,delete
                object PropValue = "";
                ruleTopLevel.InitialState.AddWordTransition(stateAfterPre, PreCommandString, null, SpeechGrammarWordType.SGLexicalNoSpecialChars, "", 0, ref PropValue, 1.0F);

                String word;
                PropValue = "";
                word      = "Add";
                ruleCommand.InitialState.AddWordTransition(null, word, "", SpeechGrammarWordType.SGLexicalNoSpecialChars, word, 0, ref PropValue, 1f);

                word = "Set";
                ruleCommand.InitialState.AddWordTransition(null, word, "", SpeechGrammarWordType.SGLexicalNoSpecialChars, word, 1, ref PropValue, 1f);

                word = "Remove";
                ruleCommand.InitialState.AddWordTransition(null, word, "", SpeechGrammarWordType.SGLexicalNoSpecialChars, word, 2, ref PropValue, 1f);

                stateAfterPre.AddRuleTransition(stateAfterCommand, ruleCommand, "", 1, ref PropValue, 1F);

                PropValue = "";
                for (int x = 0; x <= 100; x++)
                {
                    word = Convert.ToString(x);

                    // Note: if the same word is added more than once to the same
                    // rule state, SAPI will return error. In this sample, we
                    // don't allow identical items in the list box so no need for
                    // the checking, otherwise special checking for identical words
                    // would have to be done here.
                    ruleNumbers.InitialState.AddWordTransition(null, word, "", SpeechGrammarWordType.SGLexicalNoSpecialChars, word, x, ref PropValue, 1F);
                }

                stateAfterCommand.AddRuleTransition(stateAfterNumber, ruleNumbers, "", 2, ref PropValue, 1.0F);

                PropValue = "";
                stateAfterNumber.AddRuleTransition(null, ruleListItems, "", 3, ref PropValue, 1.0F);

                // Now add existing list items to the ruleListItems
                RebuildGrammar();

                // Now we can activate the top level rule. In this sample, only
                // the top level rule needs to activated. The ListItemsRule is
                // referenced by the top level rule.
                grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive);
                speechInitialized = true;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(
                    "Exception caught when initializing SAPI."
                    + " This application may not run correctly.\r\n\r\n"
                    + e.ToString(),
                    "Error");

                throw;
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     This function will create the main SpSharedRecoContext object 
        ///     and other required objects like Grammar and rules. 
        ///     In this sample, we are building grammar dynamically since 
        ///     listbox content can change from time to time.
        ///     If your grammar is static, you can write your grammar file 
        ///     and ask SAPI to load it during run time. This can reduce the 
        ///     complexity of your code.
        /// </summary>
        private void InitializeSpeech()
        {
            Debug.WriteLine("Initializing SAPI objects...");

            try
            {
                // First of all, let's create the main reco context object. 
                // In this sample, we are using shared reco context. Inproc reco
                // context is also available. Please see the document to decide
                // which is best for your application.
                objRecoContext = new SpeechLib.SpSharedRecoContext();

                // Then, let's set up the event handler. We only care about
                // Hypothesis and Recognition events in this sample.
                objRecoContext.Hypothesis += new 
                    _ISpeechRecoContextEvents_HypothesisEventHandler(
                    RecoContext_Hypothesis);

                objRecoContext.Recognition += new 
                    _ISpeechRecoContextEvents_RecognitionEventHandler(
                    RecoContext_Recognition);

                // Now let's build the grammar.
                // The top level rule consists of two parts: "select <items>". 
                // So we first add a word transition for the "select" part, then 
                // a rule transition for the "<items>" part, which is dynamically 
                // built as items are added or removed from the listbox.
                grammar = objRecoContext.CreateGrammar(grammarId);
                ruleTopLevel = grammar.Rules.Add("TopLevelRule", 
                    SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
                ruleListItems = grammar.Rules.Add("ListItemsRule", 
                    SpeechRuleAttributes.SRADynamic, 2);

                SpeechLib.ISpeechGrammarRuleState   stateAfterSelect;
                stateAfterSelect = ruleTopLevel.AddState();

                object      PropValue = "";
                ruleTopLevel.InitialState.AddWordTransition(stateAfterSelect,
                    PreCommandString, " ", SpeechGrammarWordType.SGLexical,
                    "", 0, ref PropValue, 1.0F );

                PropValue = "";
                stateAfterSelect.AddRuleTransition(null, ruleListItems, "", 
                    1, ref PropValue, 0F);

                // Now add existing list items to the ruleListItems
                RebuildGrammar();

                // Now we can activate the top level rule. In this sample, only 
                // the top level rule needs to activated. The ListItemsRule is 
                // referenced by the top level rule.
                grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive);
                speechInitialized = true;
            }
            catch(Exception e)
            {
                System.Windows.Forms.MessageBox.Show(
                    "Exception caught when initializing SAPI." 
                    + " This application may not run correctly.\r\n\r\n"
                    + e.ToString(),
                    "Error");
            }
        }