private async Task changeStateAsync(ListeningState state) { if (listeningState != state) { System.Diagnostics.Debug.WriteLine($"Changing CommandInterpreter listening state to {state}"); if (state == ListeningState.Hypothesis) { preHypothesisListeningState = listeningState; } listeningState = state; await listener.StopListeningAsync(); switch (state) { case ListeningState.Move: SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.MoveCommands, true); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.PieceConfirmation, false); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.YesNoCommands, false); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.CancelCommand, false); break; case ListeningState.PieceConfirmation: SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.PieceConfirmation, true); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.MoveCommands, false); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.YesNoCommands, false); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.CancelCommand, true); break; case ListeningState.Hypothesis: SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.YesNoCommands, true); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.MoveCommands, false); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.PieceConfirmation, false); SpeechConstraints.EnableGrammar(recognizer.Constraints, GrammarMode.CancelCommand, true); break; default: await listener.StartListeningAsync(); throw new Exception("Tried to change CommandInterpreter state to an unknown listening state"); } await listener.StartListeningAsync(); } }
public static async Task <CommandInterpreter> CreateAsync() { var speechRecognizer = new SpeechRecognizer(); var interpreter = new CommandInterpreter(speechRecognizer, new Communicator()); var grammarCompilationResult = await interpreter.setupGrammarConstraintsAsync(); if (grammarCompilationResult.Status != SpeechRecognitionResultStatus.Success) { throw new FormatException($"Could not compile grammar constraints. Received error {grammarCompilationResult.Status}"); } // Disable unnecessary grammars SpeechConstraints.EnableGrammar(speechRecognizer.Constraints, GrammarMode.MoveCommands, true); SpeechConstraints.EnableGrammar(speechRecognizer.Constraints, GrammarMode.PieceConfirmation, false); SpeechConstraints.EnableGrammar(speechRecognizer.Constraints, GrammarMode.YesNoCommands, false); SpeechConstraints.EnableGrammar(speechRecognizer.Constraints, GrammarMode.CancelCommand, false); return(interpreter); }