Example #1
0
        public void SetPluginHost(IPluginHost Host)
        {
            this.FHost = Host;

            //Create outputs
            this.FHost.CreateStringOutput("Word", TSliceMode.Dynamic, TPinVisibility.True, out this.FPinOutWord);

            this.FHost.CreateValueOutput("Bang", 1, null, TSliceMode.Single, TPinVisibility.True, out this.FPinOutBang);
            this.FPinOutBang.SetSubType(0, 1, 0, 0, true, false, true);

            this.FHost.CreateStringOutput("Status", TSliceMode.Single, TPinVisibility.OnlyInspector, out this.FPinOutStatus);

            try
            {
                //Load the Speech context
                this.FContext = new SpeechLib.SpSharedRecoContext();
                this.FGrammar = this.FContext.CreateGrammar(0);

                //Just to avoid double event
                this.FContext.Recognition -= OnRecognition;
                this.FContext.Recognition += OnRecognition;
                this.FPinOutStatus.SetString(0, "OK");
            }
            catch (Exception ex)
            {
                this.FPinOutStatus.SetString(0, "Error: " + ex.Message);
            }

            this.OnSetPluginHost();
        }
Example #2
0
        private SpeechRecognition()
        {
            ssrContex = new SpSharedRecoContext();
            isrg      = ssrContex.CreateGrammar(1);
            _ISpeechRecoContextEvents_RecognitionEventHandler recHandle =
                new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition);

            ssrContex.Recognition += recHandle;
        }
Example #3
0
        private void startSpeech()
        {
            if (RecoContext == null)
            {
                RecoContext = new SpSharedRecoContext();
                grammar     = RecoContext.CreateGrammar(1);
                grammar.DictationLoad();
            }

            grammar.DictationSetState(SpeechRuleState.SGDSActive);    // Opens up diction possibility.
        }
Example #4
0
        public CommandRecognizer()
        {
            m_context = new SpSharedRecoContext();

            // We want to know when a phrase is recognized and also when one fails to be recognized
            m_context.EventInterests = SpeechRecoEvents.SRERecognition | SpeechRecoEvents.SREFalseRecognition;
            m_context.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecognitionHandler);
            m_context.FalseRecognition += new _ISpeechRecoContextEvents_FalseRecognitionEventHandler(FailedRecognitionHandler);

            m_grammar = m_context.CreateGrammar(0);
            m_grammar.Reset(0);
        }
        /// <summary>
        /// Starts the listening
        /// </summary>
        public void Start()
        {
            recoContext              = new SpSharedRecoContextClass();
            recoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(recoContext_Recognition);

            recoGrammar     = recoContext.CreateGrammar(0);
            recoGrammarRule = recoGrammar.Rules.Add("VoiceCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);

            object propValue = "";

            for (int i = 0; i < commands.Count; i++)
            {
                recoGrammarRule.InitialState.AddWordTransition(null, commands[i].ToString(), " ", SpeechGrammarWordType.SGLexical, Commands[i].ToString(), i, ref propValue, 1.0F);
            }

            recoGrammar.Rules.Commit();
            recoGrammar.CmdSetRuleState("VoiceCommands", SpeechRuleState.SGDSActive);
        }
        public NamesFromSpeech(Form1 parent, List <string> names)
        {
            // SAPI 5.4 Overview: http://msdn.microsoft.com/en-us/library/ee125077%28v=vs.85%29.aspx
            // Some code from https://github.com/kring/Voodoo-Voice/blob/master/VoiceRecognition/CommandRecognizer.cs

            m_parent = parent;
            m_names  = new List <string>();

            m_context = new SpSharedRecoContext();
            m_context.EventInterests = SpeechRecoEvents.SRERecognition;
            m_context.Recognition   += context_Recognition;

            m_grammar = m_context.CreateGrammar();
            m_grammar.Reset();

            foreach (string name in names)
            {
                AddRuleForName(name);
            }

            CommitAndActivate();
        }
        public bool RebuildGrammar(ISpeechRecoGrammar grammar, bool speechEnabled,
            SpSharedRecoContext objRecoContext, ISpeechGrammarRule ruleListItemsDefault)
        {
            Debug.WriteLine("RebuildGrammar " + this.state.ToString());

            if (!speechEnabled) {
                return false;
            }

            //grammar = objRecoContext.CreateGrammar(grammarId);
            ////ruleTopLevel = grammar.Rules.Add("TopLevelRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
            object PropValue = "";

            switch (this.state) {
                case State.init:
                    //ruleListItemsNames = grammar.Rules.Add("ListItemsNameRule", SpeechRuleAttributes.SRADynamic, 2);
                    //ruleListItemsIDs = grammar.Rules.Add("ListItemsIDRule", SpeechRuleAttributes.SRADynamic, 3);
                    //ruleListItemsDefault = grammar.Rules.Add("ListItemsDefaultRule", SpeechRuleAttributes.SRADynamic, 2);

                    //SpeechLib.ISpeechGrammarRuleState stateName;
                    //SpeechLib.ISpeechGrammarRuleState stateID;
                    ////SpeechLib.ISpeechGrammarRuleState stateDefault;

                    //stateName = ruleTopLevel.AddState();
                    //stateID = ruleTopLevel.AddState();
                    ////stateDefault = ruleListItemsDefault.AddState();

                    //object PropValue1 = "";
                    //object PropValue2 = "";
                    ////PropValue = "";

                    //ruleTopLevel.InitialState.AddWordTransition(stateName, "name", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue1, 1.0F);
                    //ruleTopLevel.InitialState.AddWordTransition(stateID, "id", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue2, 1.0F);
                    ////ruleTopLevel.InitialState.AddWordTransition(stateDefault, " ", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue, 1.0F);

                    //PropValue1 = "";
                    //PropValue2 = "";
                    ////PropValue = "";

                    //stateName.AddRuleTransition(null, ruleListItemsNames, "", 1, ref PropValue1, 0F);
                    //stateID.AddRuleTransition(null, ruleListItemsIDs, "", 1, ref PropValue2, 0F);
                    ////stateDefault.AddRuleTransition(null, ruleListItemsDefault, "", 1, ref PropValue, 0F);

                    try {
                        //ruleListItemsNames.Clear();
                        //ruleListItemsIDs.Clear();
                        ruleListItemsDefault.Clear();

                        //int i = 0;
                        //foreach (string patientName in patientenNameList) {
                        //    string word = patientName;
                        //    ruleListItemsNames.InitialState.AddWordTransition(null, word, " ", SpeechGrammarWordType.SGLexical, word, i, ref PropValue1, 1F);
                        //    i++;
                        //}

                        //i = 0;
                        //foreach (long patientID in patientIDList) {
                        //    long ID = patientID;
                        //    ruleListItemsIDs.InitialState.AddWordTransition(null, ID.ToString(), " ", SpeechGrammarWordType.SGLexical, ID.ToString(), i, ref PropValue2, 1F);
                        //    i++;
                        //}

                        int i = 0;
                        foreach (string patientName in patientenNameList) {
                            string word = "name " + patientName;
                            ruleListItemsDefault.InitialState.AddWordTransition(null, word, " ", SpeechGrammarWordType.SGLexical, word, i, ref PropValue, 1F);
                            i++;
                        }

                        foreach (long patientID in patientIDList) {
                            string word = "id " + patientID.ToString();
                            ruleListItemsDefault.InitialState.AddWordTransition(null, word, " ", SpeechGrammarWordType.SGLexical, word, i, ref PropValue, 1F);
                            i++;
                        }

                        grammar.Rules.Commit();

                        ////grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive);
                    } catch (Exception e) {
                        System.Windows.Forms.MessageBox.Show(
                            "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n"
                            + e.ToString(),
                            "Error");
                    }
                    break;

                case State.cognitied:
                    //ruleListItemsDefault = grammar.Rules.Add("ListItemsDefaultRule", SpeechRuleAttributes.SRADynamic, 2);

                    //SpeechLib.ISpeechGrammarRuleState stateCognitied;
                    //stateCognitied = ruleTopLevel.AddState();

                    //PropValue = "";

                    //ruleTopLevel.InitialState.AddWordTransition(stateCognitied, "", " ", SpeechGrammarWordType.SGLexical, "", 0, ref PropValue, 1.0F);

                    //PropValue = "";

                    //stateCognitied.AddRuleTransition(null, ruleListItemsDefault, "", 1, ref PropValue, 0F);

                    try {
                        ruleListItemsDefault.Clear();

                        ruleListItemsDefault.InitialState.AddWordTransition(null, "master", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "operation", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "visit", " ", SpeechGrammarWordType.SGLexical, "visit", 2, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 3, ref PropValue, 1F);
                        grammar.Rules.Commit();

                        //grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive);
                    } catch (Exception e) {
                        System.Windows.Forms.MessageBox.Show(
                            "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n"
                            + e.ToString(),
                            "Error");
                    }
                    break;

                case State.master:
                    try {
                        ruleListItemsDefault.Clear();

                        ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F);
                        grammar.Rules.Commit();

                        //grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive);
                    } catch (Exception e) {
                        System.Windows.Forms.MessageBox.Show(
                            "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n"
                            + e.ToString(),
                            "Error");
                    }
                    break;

                case State.lastOperation:
                    try {
                        ruleListItemsDefault.Clear();

                        ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "more", " ", SpeechGrammarWordType.SGLexical, "reset", 3, ref PropValue, 1F);
                        grammar.Rules.Commit();

                        //grammar.CmdSetRuleState("TopLevelRule", SpeechRuleState.SGDSActive);
                    } catch (Exception e) {
                        System.Windows.Forms.MessageBox.Show(
                            "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n"
                            + e.ToString(),
                            "Error");
                    }
                    break;

                case State.lastVisit:
                    try {
                        ruleListItemsDefault.Clear();

                        ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "more", " ", SpeechGrammarWordType.SGLexical, "reset", 3, ref PropValue, 1F);
                        grammar.Rules.Commit();
                    } catch (Exception e) {
                        System.Windows.Forms.MessageBox.Show(
                            "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n"
                            + e.ToString(),
                            "Error");
                    }
                    break;

                case State.allOperation:
                    try {
                        ruleListItemsDefault.Clear();

                        ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F);
                        grammar.Rules.Commit();
                    } catch (Exception e) {
                        System.Windows.Forms.MessageBox.Show(
                            "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n"
                            + e.ToString(),
                            "Error");
                    }
                    break;

                case State.allVisit:
                    try {
                        ruleListItemsDefault.Clear();

                        ruleListItemsDefault.InitialState.AddWordTransition(null, "repeat", " ", SpeechGrammarWordType.SGLexical, "master", 0, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "back", " ", SpeechGrammarWordType.SGLexical, "operation", 1, ref PropValue, 1F);
                        ruleListItemsDefault.InitialState.AddWordTransition(null, "reset", " ", SpeechGrammarWordType.SGLexical, "reset", 2, ref PropValue, 1F);
                        grammar.Rules.Commit();
                    } catch (Exception e) {
                        System.Windows.Forms.MessageBox.Show(
                            "Exception caught when rebuilding dynamic listbox rule.\r\n\r\n"
                            + e.ToString(),
                            "Error");
                    }
                    break;

                default:

                    break;
            }

            return true;
        }
Example #8
0
        static void Ma()
        {
            SpSharedRecoContext ssrc = new SpSharedRecoContext();
            SpeechLib.ISpeechRecoGrammar isrg = null;

             //       ssrc = // SpSharedRecoContextClass();

            ssrc.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecognitionEvent);

            isrg = ssrc.CreateGrammar(1);
            isrg.DictationLoad(null, SpeechLib.SpeechLoadOption.SLOStatic);
            isrg.DictationSetState(SpeechLib.SpeechRuleState.SGDSActive);
        }
 /// <summary>
 /// Stops the listening
 /// </summary>
 public void Stop()
 {
     recoContext     = null;
     recoGrammar     = null;
     recoGrammarRule = null;
 }
        //private void initSpeech() {
        //    //Debug.WriteLine("Initializing SAPI");
        //    try {
        //        //create Main context Obj
        //        objRecoContext = new SpSharedRecoContext();
        //        objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(objRecoContext_Recognition);
        //        grammar = objRecoContext.CreateGrammar(0);
        //        string path = "Grammar.xml";
        //        grammar.CmdLoadFromFile(path, SpeechLoadOption.SLODynamic);
        //        //activate Top Level Rule
        //        grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive);
        //        //speechInitialized = true;
        //    } catch (Exception e) {
        //        logOut("Exception: " + e.ToString());
        //    }
        //}
        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

                grammar = objRecoContext.CreateGrammar(10);
                ruleTopLevel = grammar.Rules.Add("TopLevelRule", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
                ruleListItemsDefault = grammar.Rules.Add("ListItemsRule", SpeechRuleAttributes.SRADynamic, 2);

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

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

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

                voiceInfoAutomat.RebuildGrammar(this.grammar, this.speechEnabled, this.objRecoContext, ruleListItemsDefault);

                // 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");
            }
        }
 /// <summary>
 /// Stops the listening
 /// </summary>
 public void Stop()
 {
     recoContext = null;
     recoGrammar = null;
     recoGrammarRule = null;
 }
        /// <summary>
        /// Starts the listening
        /// </summary>
        public void Start()
        {
            recoContext = new SpSharedRecoContextClass();
            recoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(recoContext_Recognition);

            recoGrammar = recoContext.CreateGrammar(0);
            recoGrammarRule = recoGrammar.Rules.Add("VoiceCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);

            object propValue = "";

            for (int i = 0; i < commands.Count; i++)
                recoGrammarRule.InitialState.AddWordTransition(null, commands[i].ToString(), " ", SpeechGrammarWordType.SGLexical, Commands[i].ToString(), i, ref propValue, 1.0F);

            recoGrammar.Rules.Commit();
            recoGrammar.CmdSetRuleState("VoiceCommands", SpeechRuleState.SGDSActive);
        }
        private void startSpeech()
        {
            if (RecoContext == null)
            {
                RecoContext = new SpSharedRecoContext();
                grammar = RecoContext.CreateGrammar(1);
                grammar.DictationLoad();
            }

            grammar.DictationSetState(SpeechRuleState.SGDSActive);    // Opens up diction possibility.
        }