public void InitializeSpeechRecognition(RecognizerInfo recognizer, ISpeechGrammar grammar) { RecognizerInfo ri = recognizer; if (ri == null) { MessageBox.Show( @"There was a problem initializing Speech Recognition. Ensure you have the Microsoft Speech SDK installed.", "Failed to load Speech SDK", MessageBoxButton.OK, MessageBoxImage.Error); return; } try { speechRecognizer = new SpeechRecognitionEngine(ri.Id); } catch { MessageBox.Show( @"There was a problem initializing Speech Recognition. Ensure you have the Microsoft Speech SDK installed and configured.", "Failed to load Speech SDK", MessageBoxButton.OK, MessageBoxImage.Error); } var phrases = new Choices(); foreach(String word in grammar.GrammarWords()) { phrases.Add(word); } var gb = new GrammarBuilder(); //Specify the culture to match the recognizer in case we are running in a different culture. gb.Culture = ri.Culture; gb.Append(phrases); // Create the actual Grammar instance, and then load it into the speech recognizer. var g = new Grammar(gb); speechRecognizer.LoadGrammar(g); speechRecognizer.SpeechRecognized += SreSpeechRecognized; speechRecognizer.SpeechHypothesized += SreSpeechHypothesized; speechRecognizer.SpeechRecognitionRejected += SreSpeechRecognitionRejected; this.readyTimer = new DispatcherTimer(); this.readyTimer.Tick += this.ReadyTimerTick; this.readyTimer.Interval = new TimeSpan(0, 0, 4); this.readyTimer.Start(); }
public Grammar CreateGrammar(ISpeechGrammar grammar) { var phrases = new Choices(); foreach (String word in grammar.GrammarWords()) { phrases.Add(word); } RecognizerInfo recognizer = GetKinectRecognizer(); RecognizerInfo ri = recognizer; var gb = new GrammarBuilder(); //Specify the culture to match the recognizer in case we are running in a different culture. gb.Culture = ri.Culture; gb.Append(phrases); // Create the actual Grammar instance, and then load it into the speech recognizer. var g = new Grammar(gb); return g; }