private void Recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { if (_voiceCommandState == VoiceCommandState.ListenModeOff && (e.Result.Text == $"{GrammarDictionary.WakeupWord} {GrammarDictionary.ListenMode} {GrammarDictionary.ChoiceOnOff.On}")) { Console.Beep(500, 300); Console.Beep(700, 300); Console.ForegroundColor = ConsoleColor.Yellow; _voiceCommandState = VoiceCommandState.ListenModeOn; Console.WriteLine($"Voice command state {_voiceCommandState}"); Console.ForegroundColor = ConsoleColor.Gray; return; } if (_voiceCommandState == VoiceCommandState.ListenModeOn && e.Result.Text == $"{GrammarDictionary.WakeupWord} {GrammarDictionary.ListenMode} {GrammarDictionary.ChoiceOnOff.Off}") { Console.Beep(600, 300); Console.Beep(300, 300); Console.ForegroundColor = ConsoleColor.Red; _voiceCommandState = VoiceCommandState.ListenModeOff; Console.WriteLine($"Voice command state {_voiceCommandState}"); Console.ForegroundColor = ConsoleColor.Gray; return; } if (_voiceCommandState == VoiceCommandState.ListenModeOff) { return; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"Recognized {e.Result.Text}"); Message message = _sentenceParser.AnalyzeRecognizedText(e.Result.Text); if (message != null) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"SUCCESS!!!"); Console.WriteLine(e.Result.Text); Console.Write($"topic=[{message.Topic}] command=["); Console.ForegroundColor = "HIGH".Equals(message.Command) ? ConsoleColor.Yellow : ConsoleColor.Red; Console.Write(message.Command); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("]"); Console.WriteLine(); this.MessageRecognized?.Invoke(this, message); Console.Beep(400, 200); } Console.ForegroundColor = ConsoleColor.Gray; }
public SpeechRecognitionRunner() { _voiceCommandState = VoiceCommandState.ListenModeOff; _sentenceParser = new SentenceParser(); _recognizer = new SpeechRecognitionEngine(); var choicesOnOff = new Choices(new[] { GrammarDictionary.ChoiceOnOff.On, GrammarDictionary.ChoiceOnOff.Off }); var choicesSwitchTurn = new Choices(new[] { GrammarDictionary.SwitchTurn.Switch, GrammarDictionary.SwitchTurn.Turn }); _recognizer.LoadGrammar(CreateActivationGrammar(choicesOnOff)); _recognizer.LoadGrammar(CreateSonoffGrammar(choicesSwitchTurn, choicesOnOff)); _recognizer.LoadGrammar(CreateDiodesGrammar(choicesSwitchTurn, choicesOnOff)); _recognizer.SpeechRecognized += Recognizer_SpeechRecognized; _recognizer.AudioStateChanged += Recognizer_AudioStateChanged; _recognizer.SpeechHypothesized += Recognizer_SpeechHypothesized; _recognizer.SetInputToDefaultAudioDevice(); }