简体   繁体   中英

jarvis takes too long to respond

I am making jarvis using c#. I coded it and jarvis is taking too long to respond back once we say anything. It takes like 10-15 mins to respond back and sometimes it don't even respond back. I want jarvis to respond quickly as soon as I give speech commands.

 using System;
    using System.IO;
    using System.Speech.Recognition;
    using System.Speech.Synthesis;
    using System.Windows.Forms;

    namespace Jarvis
    {
        public partial class Form1 : Form
        {
            SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
            SpeechSynthesizer JARVIS = new SpeechSynthesizer();
            string QEvent;
            string ProcWindow;
            double timer = 10;
            int count = 1;
            Random rnd = new Random();
            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                _recognizer.SetInputToDefaultAudioDevice();
                _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\Users\Ashwini\Desktop\Jarvis\Jarvis\bin\Debug\commands.txt")))));
                _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
                _recognizer.RecognizeAsync(RecognizeMode.Multiple);
            }
            void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
                int ranNum = rnd.Next(1, 10);
                string speech = e.Result.Text;
                switch (speech)
                {

                    case "hello":
                    case "hello jarvis":
                        if (ranNum < 6) { JARVIS.Speak("Hello sir"); }
                        else if (ranNum > 5) { JARVIS.Speak("Hello"); }
                        break;
                    case "goodbye":
                    case "goodbye jarvis":
                    case "close":
                    case "close jarvis":
                        JARVIS.Speak("Until next time");
                        Close();
                        break;
                    case "jarvis":
                        if (ranNum < 5) { QEvent = ""; JARVIS.Speak("Yes sir"); }
                        else if (ranNum > 4) { QEvent = ""; JARVIS.Speak("Yes?"); }
                        break;

                    //WEBSITES
                    case "open google":
                        System.Diagnostics.Process.Start("https://www.google.com/");
                        break;
                    case "open my facebook":
                        System.Diagnostics.Process.Start("https://www.facebook.com/");
                        break;
                    case "open gmail":
                        System.Diagnostics.Process.Start("https://www.google.com/gmail/");
                        break;

                    //SHELL COMMANDS
                    case "open this pc":
                        System.Diagnostics.Process.Start("This PC");
                        JARVIS.Speak("Loading");
                        break;

                    case "open my folder":
                        System.Diagnostics.Process.Start("F:");
                        JARVIS.Speak("Loading");
                        break;

                    //CLOSE PROGRAMS
                    case "close this pc":
                        ProcWindow = "This PC";
                        StopWindow();
                        break;

                    //CONDITION OF DAY
                    case "what time is it":
                        DateTime now = DateTime.Now;
                        string time = now.GetDateTimeFormats('t')[0];
                        JARVIS.Speak(time);
                        break;
                    case "what day is it":
                        JARVIS.Speak(DateTime.Today.ToString("dddd"));
                        break;
                    case "whats the date":
                    case "whats todays date":
                        JARVIS.Speak(DateTime.Today.ToString("dd-MM-yyyy"));
                        break;

                    //OTHER COMMANDS
                    case "go fullscreen":
                        FormBorderStyle = FormBorderStyle.None;
                        WindowState = FormWindowState.Maximized;
                        TopMost = true;
                        JARVIS.Speak("expanding");
                        break;
                    case "exit fullscreen":
                        FormBorderStyle = FormBorderStyle.Sizable;
                        WindowState = FormWindowState.Normal;
                        TopMost = false;
                        break;
                    case "switch window":
                        SendKeys.Send("%{TAB " + count + "}");
                        count += 1;
                        break;

                    case "out of the way":
                        if (WindowState == FormWindowState.Normal || WindowState == FormWindowState.Maximized)
                        {
                            WindowState = FormWindowState.Minimized;
                            JARVIS.Speak("My apologies");
                        }
                        break;
                    case "come back":
                        if (WindowState == FormWindowState.Minimized)
                        {
                            JARVIS.Speak("Alright?");
                            WindowState = FormWindowState.Normal;
                        }
                        break;
                    case "show commands":
                        string[] commands = (File.ReadAllLines(@"C:\Users\Ashwini\Desktop\Jarvis\Jarvis\bin\Debug\commands.txt"));
                        JARVIS.Speak("Very well");
                        lstCommands.Items.Clear();
                        lstCommands.SelectionMode = SelectionMode.None;
                        lstCommands.Visible = true;
                        foreach (string command in commands)
                        {
                            lstCommands.Items.Add(command);
                        }
                        break;
                    case "hide listbox":
                        lstCommands.Visible = false;
                        break;

                }
            }

            private void StopWindow()
            {

            }
        }
    }

I dont know what is wrong with this code. Please help me out.

For an in-depth tutorial on Speech Recognition see my tutorial https://youtu.be/BJkymbvxlJs

When using speech recognition you need to use the Async method. Jarvis.SpeakAsync the Async method will give continuing command and dictate.

Using only Jarvis.Speak you have to wait for the engine to complete. Example:

  Jarvis.Speak("this is what Jarvis will say after a  command is spoken");

Using Jarvis.Speak you can not execute any commands while it's synthesizing.

   Jarvis.SpeakAsync("this is what Jarvis will say after a command is spoken");

When using Jarvis.SpeakAsync this gives you the ability to pause, stop and/or interrupt the speech engine during the synthesis.

The tutorial link above shows a basic format of the SpeakAsync and how to use it in the application for speech recognition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM