1

I have made a voice recognition program in C#. It was all working fine and then randomly stopped working. Now it doesn't respond to any input or say the first output s.Speak("hello, how may i assist you?"); when it's started like it's supposed to.

I have ran the program on my friend's Windows 10 system and it runs fine. This leads me to believe that it's a problem with my system. I am using Windows 7 by the way.

Heres the code -

using System;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Diagnostics;
using System.Xml;
using System.Media;

namespace JarvisTest
{
public partial class Form1 : Form
{

    SpeechSynthesizer s = new SpeechSynthesizer();

    Boolean wake = false;

    String name = "Rhys";


    String temp;
    String condition;
    String high;
    String low;
    Choices list = new Choices();


    public Form1()
    {

        SpeechRecognitionEngine rec = new SpeechRecognitionEngine();

        list.Add(new String[] { "hello", "bye", "how are you", "great", "what time is it", "whats the date", "open google", "open youtube", "wake", "sleep", "restart", "update", "whats the weather", "whats the temperature", "jarvis", "shut down", "exit", "play", "pause", "spotify", "last track", "next track", "whats todays high", "whats todays low", "whats todays weather", "whats your favorite song", "never mind", "whats my name", "minimise", "maximise"});

        Grammar gr = new Grammar(new GrammarBuilder(list));

        try
        {
            rec.RequestRecognizerUpdate();
            rec.LoadGrammar(gr);
            rec.SpeechRecognized += rec_SpeachRecognized;
            rec.SetInputToDefaultAudioDevice();
            rec.RecognizeAsync(RecognizeMode.Multiple);
        }
        catch { return; }

        s.SelectVoiceByHints(VoiceGender.Male);

        s.Speak("hello, how may i assist you?");

        InitializeComponent();
    }

    public String GetWeather(String input)
    {
        String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text=' Guernsey')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");

        XmlDocument wData = new XmlDocument();
        try
        {
            wData.Load(query);
        }
        catch
        {
            //MessageBox.Show("No internet connection");
            return "No internet connection detected";
        }

        XmlNamespaceManager manager = new XmlNamespaceManager(wData.NameTable);
        manager.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");

        XmlNode channel = wData.SelectSingleNode("query").SelectSingleNode("results").SelectSingleNode("channel");
        XmlNodeList nodes = wData.SelectNodes("query/results/channel");
        try
        {
            int rawtemp = int.Parse(channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["temp"].Value);
            int rawhigh = int.Parse(channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["high"].Value);
            int rawlow = int.Parse(channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", manager).Attributes["low"].Value);
            temp = (rawtemp - 32) * 5/9 + "";
            high = (rawhigh - 32) * 5 / 9 + "";
            low = (rawlow - 32) * 5 / 9 + "";
            condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", manager).Attributes["text"].Value;

            if (input == "temp")
            {
                return temp;
            }
            if (input == "high")
            {
               return high;
            }
            if (input == "low")
            {
                  return low;
              }
            if (input == "cond")
            {
                return condition;
            }
        }
        catch
        {
            return "Error Reciving data";
        }
        return "error";
    }

    public void restart()
    {
        Process.Start(@"C:\Users\Rhys-Le-P\Documents\Visual Studio 2017\Projects\JarvisTest\JarvisTest\obj\Debug\JarvisTest.exe");
        Environment.Exit(1);
    }
    public void shutdown()
    {
        Environment.Exit(1);
    }

    public void say(String h)
    {
        s.Speak(h);
        wake = false;
    }

    String[] greetings = new String[3] { "hi", "hello", "hi, how are you" };

    public String greetings_action()
    {
        Random r = new Random();
        return greetings[r.Next(3)];

    }

    //Commands

    private void rec_SpeachRecognized(object sender, SpeechRecognizedEventArgs e)
    {

        String r = e.Result.Text;
        if (r == "jarvis")
        {
            wake = true;
            //say(greetings_action());
            SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Rhys-Le-P\Downloads\Beep.wav");
            simpleSound.Play();
            }

            // if (r == "wake")
            // {
            //     wake = true;
            //      say("i am awake");
            // }

            //if (r == "sleep")
            //{
            //      wake = false;
            //     say("zzz");
            // }

            if (wake == true)
            {

            if (r == "minimise")
            {
                this.WindowState = FormWindowState.Minimized;
            }

            if (r == "maximise")
            {
                this.WindowState = FormWindowState.Maximized;
            }

            if (r == "whats my name")
            {
                say("your name is " + name);
            }

            if (r == "next track")
            {
                SendKeys.Send("^{RIGHT}");
                //say("playing next track");
            }

            if(r == "last track")
            {
                SendKeys.Send("^{LEFT}");
                //say("playing last track");
            }

            if(r == "spotify")
            {
                Process.Start(@"C:\Users\Rhys-Le-P\AppData\Roaming\Spotify\Spotify.exe");
            }

            //{
            // input
            if (r == "play" || r == "pause")
                {
                SendKeys.Send(" ");
                }

            // input
            if (r == "hello")
                {
                    //output
                    say("hi");
                }

                // input
                if (r == "how are you")
                {
                    //output
                    say("good, and you?");
                }

                // input
                if (r == "bye")
                {
                    //output
                    say("good bye!");
                }

                // input
                if (r == "great")
                {
                    //output
                    say("good to hear!");
                }

                // input
                if (r == "what time is it")
                {
                    //output
                    say(DateTime.Now.ToString("h:mm tt"));
                }

                // input
                if (r == "whats the date")
                {
                    //output
                    say(DateTime.Now.ToString("d/M/yyyy"));
                }

                // input
                if (r == "open google")
                {
                    //output
                    say("opening google");
                    Process.Start("http://www.google.com");
                }

                // input
                if (r == "open youtube")
                {
                    //output
                    say("opening youtube");
                    Process.Start("http://www.youtube.com");
                }

                // input
                if (r == "restart" || r == "update")
                {
                    //output
                    say("restarting");
                    restart();
                }
                if (r == "whats todays weather")
                 {
                say("Todays weather is, " + GetWeather("cond") + "with a high of" + GetWeather("high") + "degrees and a low of" + GetWeather("low") +"degrees");
                }
                if (r == "whats the weather")
                {
                    say("The sky is, " + GetWeather("cond") + ".");
                }

                if (r == "whats the temperature")
                {
                    say("it is, " + GetWeather("temp") + "degrees.");
                }
                if (r == "whats todays high")
                {
                   say("Todays high is, " + GetWeather("high") + "degrees.");
                }
                if (r == "whats todays low")
                {
                   say("Todays low is, " + GetWeather("low") + "degrees.");
                }

                 // input
                if (r == "shut down" || r == "exit")
                {
                    //output
                    say("Shutting Down System");
                    shutdown();
                }
                if (r == "whats your favorite song")
                {
                say("I love this song!");
                SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\Rhys-      Le-P\Downloads\BSIM.wav");
                simpleSound.Play();
                }
                if (r == "never mind")
                {
                say("Ok sir");
                }
        }

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
}
}

Any help is greatly appreciated! :)

splattne
  • 102,760
  • 52
  • 202
  • 249
theRealRLP
  • 11
  • 4
  • As issues at the level of form you're running at eg the constructor are prone to issue, have you considered for your sanity, placing a button on the form to activate and do all that stuff and check its not erroring? – BugFinder Mar 21 '17 at 16:14
  • The goal was to make this program so that it was completely hands free. Similar to Amazon Echo. – theRealRLP Mar 21 '17 at 22:51
  • Yes, but for diagnosis.. start at the start.... your code will open the form even if the create code errors.. but you will have continued regardless – BugFinder Mar 21 '17 at 23:16

0 Answers0