/// <summary>
 /// Event handler used to display what's being heard in the main screen's text box
 /// </summary>
 /// <param name="recognizer"></param>
 /// <param name="args"></param>
 private static void Recognizer_HypothesisGenerated(SpeechRecognizer recognizer, SpeechRecognitionHypothesisGeneratedEventArgs args)
 {
     if (StringUtils.Contains(args.Hypothesis.Text, activatorString) || StringUtils.AreEqual(SpokenText, activatorString))
     {
         Utils.RunOnMainThread(() =>
         {
             if (commandBox != null)
             {
                 commandBox.Text = SpokenText + " " + args.Hypothesis.Text;
             }
         });
     }
 }
Beispiel #2
0
 /// <summary>
 /// Event handler used to display what's being heard in the main screen's text box
 /// </summary>
 /// <param name="recognizer"></param>
 /// <param name="args"></param>
 private static void Recognizer_HypothesisGenerated(SpeechRecognizer recognizer, SpeechRecognitionHypothesisGeneratedEventArgs args)
 {
     if (StringUtils.Contains(args.Hypothesis.Text, activatorString) || StringUtils.AreEqual(SpokenText, activatorString))
     {
         Utils.RunOnMainThread(() =>
         {
             if (commandBox != null)
             {
                 string tempText = SpokenText + " " + args.Hypothesis.Text;
                 EnsureSpokenTextDoesNotContainStuffBeforeActivatorString(ref tempText);
                 if (!StringUtils.StartsWith(tempText, activatorString))
                 {
                     tempText = activatorString + " " + tempText.Trim();
                 }
                 commandBox.Text = tempText;
             }
         });
     }
 }
        /// <summary>
        /// Starts up the speech recognizer in a background thread to listen for input speech. if speech is heard and contains "hey bob", then when the user stops speaking the resulting text is used to find and perform a corresponding action.
        /// This method does not run if it has already started
        /// </summary>
        /// <param name="speechInputFunction"></param>
        /// <param name="box"></param>
        public static async void StartLooping(Action <string> speechInputFunction, TextBox box)
        {
            if (!IsStarted)
            {
                IsStarted  = true;
                commandBox = box;
                recognizer = new SpeechRecognizer();
                // compile the grammar and speech contstraings. TODO we may want to create our own grammar file. I don't know how much effort that will take up though
                await recognizer.CompileConstraintsAsync();

                recognizer.HypothesisGenerated += Recognizer_HypothesisGenerated;
                // add a second delay for the user to have pauses in their speech
                recognizer.ContinuousRecognitionSession.AutoStopSilenceTimeout += new TimeSpan(1_000_000); // 1,000,000 ticks = 1 second
                SpeechRecognitionResult result = null;

                thread = new Thread(new ThreadStart(async() =>
                {
                    while (IsStarted)
                    {
                        try
                        {
                            result = await recognizer.RecognizeAsync();
                            if (result != null && StringUtils.Contains(result.Text, activatorString))
                            {
                                SpokenText = result.Text;
                                // if the result is only "hey bob", then listen again
                                if (StringUtils.AreEqual(SpokenText, activatorString))
                                {
                                    result      = await recognizer.RecognizeAsync();
                                    SpokenText += " " + result.Text;
                                }
                                // clear the command box and run the command
                                Utils.RunOnMainThread(() =>
                                {
                                    AudioPlayer.PlaySound("bob_activate");
                                    // give the sound enough time to play
                                    Thread.Sleep(750);
                                    speechInputFunction.Invoke(SpokenText);
                                    // clear the spoken text variable to prevent the text box from holding old and new commands at once
                                    SpokenText = "";
                                });
                            }
                        }
                        catch (ObjectDisposedException)
                        {
                            // the page was changed, or we were stopped. Either way, be sure to mark us as not started just in case
                            IsStarted = false;
                        }
                        catch (Exception exception)
                        {
                            if ((uint)exception.HResult == HResultPrivacyStatementDeclined)
                            {
                                var message = new MessageDialog("The privacy statement was declined." +
                                                                "Go to Settings -> Privacy -> Speech, inking and typing, and ensure you" +
                                                                "have viewed the privacy policy, and 'Get To Know You' is enabled.");
                                await message.ShowAsync();
                            }
                        }
                    }
                }));
                thread.IsBackground = true;
                thread.Start();
            }
        }
Beispiel #4
0
        /// <summary>
        /// Starts up the speech recognizer in a background thread to listen for input speech. if speech is heard and contains "hey bob", then when the user stops speaking the resulting text is used to find and perform a corresponding action.
        /// This method does not run if it has already started
        /// </summary>
        /// <param name="speechInputFunction"></param>
        /// <param name="box"></param>
        public static async void StartLooping(Action <string> speechInputFunction, TextBox box)
        {
            if (!IsStarted)
            {
                IsStarted  = true;
                commandBox = box;
                recognizer = new SpeechRecognizer();
                // compile the grammar and speech contstraings. TODO we may want to create our own grammar file. I don't know how much effort that will take up though
                await recognizer.CompileConstraintsAsync();

                recognizer.HypothesisGenerated += Recognizer_HypothesisGenerated;
                // add a second delay for the user to have pauses in their speech
                recognizer.ContinuousRecognitionSession.AutoStopSilenceTimeout += new TimeSpan(1_000_000);                 // 1,000,000 ticks = 1 second
                SpeechRecognitionResult result = null;

                thread = new Thread(new ThreadStart(async() =>
                {
                    while (IsStarted)
                    {
                        try
                        {
                            result = await recognizer.RecognizeAsync();
                            if (result != null && StringUtils.Contains(result.Text.Trim(), activatorString))
                            {
                                SpokenText = result.Text.Trim();
                                // if the result is only "hey bob", then listen again
                                if (StringUtils.AreEqual(SpokenText, activatorString))
                                {
                                    result      = await recognizer.RecognizeAsync();
                                    SpokenText += " " + result.Text;
                                }
                                EnsureSpokenTextDoesNotContainStuffBeforeActivatorString(ref SpokenText);
                                // clear the command box and run the command
                                Utils.RunOnMainThread(() =>
                                {
                                    AudioPlayer.PlaySound("bob_activate");
                                    // give the sound enough time to play
                                    Thread.Sleep(750);
                                    speechInputFunction.Invoke(SpokenText);
                                    // clear the spoken text variable to prevent the text box from holding old and new commands at once
                                    SpokenText = "";
                                });
                            }
                        }
                        catch (ObjectDisposedException)
                        {
                            // the page was changed, or we were stopped. Either way, be sure to mark us as not started just in case
                            IsStarted = false;
                        }
                        catch (Exception exception)
                        {
                            if ((uint)exception.HResult == HResultPrivacyStatementDeclined)
                            {
                                Utils.RunOnMainThread(async() =>
                                {
                                    // turn off speech recognition in bob
                                    Setting speechRecognitionSetting = StoredProcedures.QuerySettingByName("Voice Activation");
                                    speechRecognitionSetting.SelectOption("Disabled");
                                    StoredProcedures.SelectOption(speechRecognitionSetting.SettingID, speechRecognitionSetting.GetSelectedOption().OptionID);
                                    var message = new MessageDialog("Microsoft's privacy statement was declined." +
                                                                    "Go to Settings -> Privacy -> Speech, and turn on online speech recognition.\nBob's speech recognition capabilities will be turned off. To turn it back on, you must manually re-enable it in app settings.");
                                    await message.ShowAsync();
                                });
                                break;
                            }
                        }
                    }
                }));
                thread.IsBackground = true;
                thread.Start();
            }
        }