public voiceCommander() { InitializeComponent(); commandReco = new SpSharedRecoContext(); commandReco.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(writeHypothesis); commandReco.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(writeCommand); }
private void cmdEnable_Click(object sender, System.EventArgs e) { label4.Text = "Initializing Speech Engine...."; // Get an insance of RecoContext. I am using the shared RecoContext. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Assign a eventhandler for the Hypothesis Event. objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event); // Assign a eventhandler for the Recognition Event. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event); //Creating an instance of the grammer object. 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"; //Activate the Menu Commands. 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); menuRule.InitialState.AddWordTransition(null, "Continue", " ", SpeechGrammarWordType.SGLexical, "Continue", 9, ref PropValue, 1.0F); grammar.Rules.Commit(); grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive); label4.Text = "Speech Engine Ready for Input"; }
private void GetProfiles() { string requiredAtributes = ""; string optionalAtributes = ""; SpSharedRecoContext recoContext; ISpeechObjectTokens tokens; SpObjectToken token; this.profiles = new SortedList <string, SpObjectToken>(); string profileName; selectedProfile = 0; // Attempt to get profile list try { recoContext = new SpeechLib.SpSharedRecoContext(); tokens = recoContext.Recognizer.GetProfiles(requiredAtributes, optionalAtributes); for (int i = 0; i < tokens.Count; ++i) { token = tokens.Item(i); profileName = token.GetDescription(0); this.profiles.Add(profileName, token); } selectedProfile = this.profiles.IndexOfKey(recoContext.Recognizer.Profile.GetDescription(0)); } catch { // If no profiles could be retreived or an error occurred, clear the list and add a default profile with no token profiles.Clear(); profiles.Add("Default", null); } }
public void stopEngine() { menuRule = null; grammar = null; objRecoContext = null; mEngineStarted = false; }
private void InitializeSpeech() { try { // First of all, let's create the main reco context object. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Then, let's set up the event handler. We only care about // Hypothesis and Recognition events in this sample. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler( RecoContext_Recognition); grammar = objRecoContext.CreateGrammar(grammarId); grammar.DictationLoad("", SpeechLoadOption.SLOStatic); grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSActive); speechInitialized = true; statusBar.Text = "Speech recognition initialized"; } catch (Exception e) { MessageBox.Show( "Exception caught when initializing SAPI." + " This application may not run correctly.\r\n\r\n" + e.ToString(), "Error"); } }
private void VoiceCalibration() { // Get an insance of RecoContext. I am using the shared RecoContext. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Assign a eventhandler for the Hypothesis Event. objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event); // Assign a eventhandler for the Recognition Event. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event); //Creating an instance of the grammer object. grammar = objRecoContext.CreateGrammar(0); //Activate the Menu Commands. menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object PropValue = ""; menuRule.InitialState.AddWordTransition(null, "Cancel", " ", SpeechGrammarWordType.SGLexical, "Cancel", 1, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Yes", " ", SpeechGrammarWordType.SGLexical, "Yes", 2, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "No", " ", SpeechGrammarWordType.SGLexical, "No", 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "All Lights Off", " ", SpeechGrammarWordType.SGLexical, "All Lights Off", 4, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "All Lights On", " ", SpeechGrammarWordType.SGLexical, "All Lights On", 5, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "First Light On", " ", SpeechGrammarWordType.SGLexical, "First Light On", 6, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Second Light On", " ", SpeechGrammarWordType.SGLexical, "Second Light On", 7, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Third Light On", " ", SpeechGrammarWordType.SGLexical, "Third Light On", 8, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "First Light Off", " ", SpeechGrammarWordType.SGLexical, "First Light Off", 9, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Second Light Off", " ", SpeechGrammarWordType.SGLexical, "Second Light Off", 10, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Third Light Off", " ", SpeechGrammarWordType.SGLexical, "Third Light Off", 11, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Recalibrate", " ", SpeechGrammarWordType.SGLexical, "Recalibrate", 12, ref PropValue, 1.0F); grammar.Rules.Commit(); grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive); }
private SpRecognition() { ssrContex = new SpSharedRecoContext(); isrg = ssrContex.CreateGrammar(1); SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle = new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition); ssrContex.Recognition += recHandle; }
void MenuVoiceTrainingClick(object sender, System.EventArgs e) { if (objRecoContext == null) { objRecoContext = new SpeechLib.SpSharedRecoContext(); } object obstr = null; objRecoContext.Recognizer.DisplayUI(this.Handle.ToInt32(), "Prelude@# Voice training", "UserTraining", ref obstr); }
/** * Initializes the Text to Speech and Speech to Text engines. * Finalize commands must be called before the Speech to Text engine will * work. */ public Voice() { this.voice = new SpVoice(); objRecoContext = new SpSharedRecoContext(); objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoEvent); grammar = objRecoContext.CreateGrammar(0); menuRule = grammar.Rules.Add("cogitoCommands",SpeechRuleAttributes.SRATopLevel|SpeechRuleAttributes.SRADynamic,1); this.commandCount = 1; this._buffer = new Queue(); }
/// <summary> /// 开始侦听 /// </summary> public void BeginRec() { ssrContex = new SpSharedRecoContext(); ssrContex.EventInterests = SpeechRecoEvents.SREAllEvents;//在"语音事件"中有说明 isrgammar = ssrContex.CreateGrammar(0); //isrgammar.CmdLoadFromFile("D:\\SpeechGammar.xml", SpeechLoadOption.SLODynamic);//读入规则 isrgammar.CmdLoadFromFile("", SpeechLoadOption.SLODynamic);//读入规则 isrgammar.CmdSetRuleState(isrgammar.Rules.Item(0).Name, SpeechRuleState.SGDSActive);//激活规则 ssrContex.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition); ssrContex.State = SpeechRecoContextState.SRCS_Enabled; isrgammar.DictationSetState(SpeechRuleState.SGDSActive); IsStart = true; }
public void startEngine() { // Get an insance of RecoContext. I am using the shared RecoContext. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Assign a eventhandler for the Hypothesis Event. objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event); // Assign a eventhandler for the Recognition Event. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event); //Creating an instance of the grammer object. grammar = objRecoContext.CreateGrammar(0); //Activate the Menu Commands. menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object PropValue = ""; menuRule.InitialState.AddWordTransition(null, "Nothing", " ", SpeechGrammarWordType.SGLexical, "Nothing", 1, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Continue", " ", SpeechGrammarWordType.SGLexical, "Continue", 2, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Stop", " ", SpeechGrammarWordType.SGLexical, "Stop", 3, ref PropValue, 1.0F); grammar.Rules.Commit(); grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive); }
public void startEngine() { if (mEngineStarted) { return; } // Get an insance of RecoContext. I am using the shared RecoContext. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Assign a eventhandler for the Hypothesis Event. objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event); // Assign a eventhandler for the Recognition Event. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event); //Creating an instance of the grammer object. grammar = objRecoContext.CreateGrammar(0); //Activate the Menu Commands. menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object PropValue = ""; menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.NO_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.NO_COMMAND, 1, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.REPEAT_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.REPEAT_COMMAND, 2, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.STOP_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.STOP_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.PAUSE_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.PAUSE_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.BACK_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.BACK_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.FORWARD_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.FORWARD_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.CALIBRATE_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.CALIBRATE_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.SLOWMO_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.SLOWMO_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.TRAINING_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.TRAINING_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.CONTINUE_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.CONTINUE_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.SCORING_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.SCORING_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Current", " ", SpeechGrammarWordType.SGLexical, "Current", 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Video", " ", SpeechGrammarWordType.SGLexical, "Video", 3, ref PropValue, 1.0F); grammar.Rules.Commit(); grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive); mEngineStarted = true; }
public void startEngine() { if (mEngineStarted) return; // Get an insance of RecoContext. I am using the shared RecoContext. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Assign a eventhandler for the Hypothesis Event. objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event); // Assign a eventhandler for the Recognition Event. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event); //Creating an instance of the grammer object. grammar = objRecoContext.CreateGrammar(0); //Activate the Menu Commands. menuRule = grammar.Rules.Add("MenuCommands", SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1); object PropValue = ""; menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.NO_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.NO_COMMAND, 1, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.REPEAT_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.REPEAT_COMMAND, 2, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.STOP_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.STOP_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.PAUSE_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.PAUSE_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.BACK_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.BACK_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.FORWARD_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.FORWARD_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.CALIBRATE_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.CALIBRATE_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.SLOWMO_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.SLOWMO_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.TRAINING_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.TRAINING_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.CONTINUE_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.CONTINUE_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, VoiceCommandEventArgs.SCORING_COMMAND, " ", SpeechGrammarWordType.SGLexical, VoiceCommandEventArgs.SCORING_COMMAND, 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Current", " ", SpeechGrammarWordType.SGLexical, "Current", 3, ref PropValue, 1.0F); menuRule.InitialState.AddWordTransition(null, "Video", " ", SpeechGrammarWordType.SGLexical, "Video", 3, ref PropValue, 1.0F); grammar.Rules.Commit(); grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive); mEngineStarted = true; }
public void startMicTrainingWizard() { menuRule = null; grammar = null; objRecoContext = null; }
/// <summary> /// Create the main object (SpSharedRecoContext), init the gramar /// And activating the events ... /// 1.Recognition => if any phrase recognized /// 2.AudioLevel => to watch the mic volume /// </summary> private void initSAPI() { try { objRecoContext = new SpeechLib.SpSharedRecoContext(); objRecoContext.AudioLevel+= new _ISpeechRecoContextEvents_AudioLevelEventHandler(RecoContext_VUMeter); objRecoContext.Recognition+= new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition); objRecoContext.EventInterests=SpeechLib.SpeechRecoEvents.SRERecognition | SpeechLib.SpeechRecoEvents.SREAudioLevel; //create grammar interface with ID = 0 grammar=objRecoContext.CreateGrammar(0); } catch(Exception ex) { MessageBox.Show("Exception \n"+ex.ToString(),"Error - initSAPI"); } }
/// <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"); } }
//STARTS THE RECOGNITION ENGINE private void initSAPI() { try { objRecoContext = new SpeechLib.SpSharedRecoContext(); objRecoContext.AudioLevel += new _ISpeechRecoContextEvents_AudioLevelEventHandler(RecoContext_VUMeter); objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(RecoContext_Recognition); objRecoContext.EventInterests = SpeechLib.SpeechRecoEvents.SRERecognition | SpeechLib.SpeechRecoEvents.SREAudioLevel; // objRecoContext.StartStream += new _ISpeechRecoContextEvents_StartStreamEventHandler(RecoContext_StartStream); //create grammar interface with ID = 0 grammar = objRecoContext.CreateGrammar(0); } catch (Exception ex) { throw new Exception(ex.Message); //Label1.Text = "Exeption (Init SAPI)\n" + ex.ToString(); } }
private void cmdDiable_Click(object sender, System.EventArgs e) { label3.Text = ""; objRecoContext = null; label4.Text = ""; }
public void stopEngine() { menuRule = null; grammar = null; objRecoContext = null; }
private void frmProfileChange_Load(object sender, System.EventArgs e) { //get objRecoContext from this.Tag objRecoContext=(SpeechLib.SpSharedRecoContext)this.Tag; //write all profiles to checkBoxList string str10="",str11=""; objTokens=objRecoContext.Recognizer.GetProfiles(str10,str11); for (int i=0;i<objTokens.Count;i++) { checkedListBox1.Items.Add(objTokens.Item(i).GetDescription(0)); if (objRecoContext.Recognizer.Profile.GetDescription(0)==objTokens.Item(i).GetDescription(0)) checkedListBox1.SetItemChecked(i,true); } }
private void GetProfiles() { string requiredAtributes = ""; string optionalAtributes = ""; SpSharedRecoContext recoContext; ISpeechObjectTokens tokens; SpObjectToken token; this.profiles = new SortedList<string, SpObjectToken>(); string profileName; selectedProfile = 0; // Attempt to get profile list try { recoContext = new SpeechLib.SpSharedRecoContext(); tokens = recoContext.Recognizer.GetProfiles(requiredAtributes, optionalAtributes); for (int i = 0; i < tokens.Count; ++i) { token = tokens.Item(i); profileName = token.GetDescription(0); this.profiles.Add(profileName, token); } selectedProfile = this.profiles.IndexOfKey(recoContext.Recognizer.Profile.GetDescription(0)); } catch { // If no profiles could be retreived or an error occurred, clear the list and add a default profile with no token profiles.Clear(); profiles.Add("Default", null); } }
/// <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"); } }
private void cmdEnable_Click(object sender, System.EventArgs e) { label4.Text = "Initializing Speech Engine...."; // Get an insance of RecoContext. I am using the shared RecoContext. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Assign a eventhandler for the Hypothesis Event. objRecoContext.Hypothesis += new _ISpeechRecoContextEvents_HypothesisEventHandler(Hypo_Event); // Assign a eventhandler for the Recognition Event. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(Reco_Event); //Creating an instance of the grammer object. 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"; //Activate the Menu Commands. 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 ); menuRule.InitialState.AddWordTransition(null, "Continue", " ", SpeechGrammarWordType.SGLexical, "Continue", 9, ref PropValue, 1.0F); grammar.Rules.Commit(); grammar.CmdSetRuleState("MenuCommands", SpeechRuleState.SGDSActive); label4.Text = "Speech Engine Ready for Input"; }
private void InitializeSpeech() { try { // First of all, let's create the main reco context object. objRecoContext = new SpeechLib.SpSharedRecoContext(); // Then, let's set up the event handler. We only care about // Hypothesis and Recognition events in this sample. objRecoContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler( RecoContext_Recognition); grammar = objRecoContext.CreateGrammar(grammarId); grammar.DictationLoad("", SpeechLoadOption.SLOStatic); grammar.DictationSetState(SpeechLib.SpeechRuleState.SGDSActive); speechInitialized = true; statusBar.Text = "Speech recognition initialized"; } catch(Exception e) { MessageBox.Show( "Exception caught when initializing SAPI." + " This application may not run correctly.\r\n\r\n" + e.ToString(), "Error"); } }
void MenuVoiceTrainingClick(object sender, System.EventArgs e) { if(objRecoContext == null) objRecoContext = new SpeechLib.SpSharedRecoContext(); object obstr = null; objRecoContext.Recognizer.DisplayUI(this.Handle.ToInt32(), "Prelude@# Voice training", "UserTraining", ref obstr); }
private void cmdDiable_Click(object sender, System.EventArgs e) { label3.Text = ""; objRecoContext = null; label4.Text=""; }