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();
            }
        }
        private async Task <SpeechRecognitionCompilationResult> setupGrammarConstraintsAsync()
        {
            var grammarConstraints = await SpeechConstraints.GetConstraintsAsync();

            foreach (var constraint in grammarConstraints)
            {
                if (constraint != null)
                {
                    recognizer.Constraints.Add(constraint);
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Received a null grammar constraint");
                }
            }
            return(await recognizer.CompileConstraintsAsync().AsTask());
        }
Beispiel #3
0
        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);
        }