Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C#] Code for project involving voice recognition and arduino

  1. Jul 31, 2015 #1
    A friend of mine wrote some code in C# that he is wanting to use in order to basically make a house controlled by voice commands he would input via microphone connected to his computer. He wrote some code and asked for my opinion and I have no idea how to read C#. Anybody help me find where he is going wrong? Thanks for any and all help!
    Code (Text):
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Speech;
    using System.Speech.Synthesis;
    using System.Speech.Recognition;
    using System.Threading;

    namespace voicehouse2._4
    {
        public partial class Form1 : Form
        {
            //for declarations
            SpeechSynthesizer ss = new SpeechSynthesizer();
            PromptBuilder pb = new PromptBuilder();
            SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
            bool frontporchlight = false;
            bool livingRoomlight = false;
            bool gregRoomlight = false;
            bool bathroomlight = false;
            bool hallwaylight = false;
            bool kitchenlight = false;
            bool zachroomlight = false;
            bool kyleroomlight = false;
            bool diningroomlight = false;
            bool backporchlight = false;


            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void button1_Click(object sender, EventArgs e)
            {
                 // start ( button _ click
                button1.Enabled = false;
                button2.Enabled = true;
                Choices clist = new Choices();
                clist.Add(new string[] { smarthousenametextBox.ToString() ,"hello","open steam","turn the bathroom lights on", "lock the front door" , " unlock the front door" , "lock the back door", "unlock the back door","turn the bathroom lights off", "can you hear me","mute","turn volume up", "turn the front bedroom light on", "turn the front bedroom light off", " how are you doing","no thank you","run c cleaner"," open chrome","how are you","why are you not listening","how are you doing","turn the living room lights on", " turn the living room lights off","what time is it", "thank you", " turn the front porch light on", " turn the front porch light off", "turn the hallway lights on", "turn the hallway lights off", "turn the middle bedroom lights on", " turn the middle bedroom lights off", "turn the back bedroom light on" , " turn the back bedroom light off", "turn the kitchen lights on" , " turn the kitchens lights off", "turn the back porch lights on", " turn the back porch lights off" , " turn the dining room lights on", " turn the dining room lights off" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                string smarthousename;
                smarthousename = smarthousenametextBox.ToString ();
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message,"error");
                }

        }

    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        switch (e.Result.Text.ToString())
        {

            case "hello":
                ss.SpeakAsync("nice to see you again");
                housereplybox.AppendText(" hello kyle ");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "how are you":
                ss.SpeakAsync("im doing great i think. i could probably use more ram");
                housereplybox.AppendText(" im doing great i think. i could probably use more ram");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "how are you doing":
                ss.SpeakAsync("good");
                housereplybox.AppendText("good");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "can you hear me":
                ss.SpeakAsync("yes i can hear you");
                housereplybox.AppendText("yes i can hear you");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "why are you not listening":
                ss.SpeakAsync("because im horrible programmed");
                housereplybox.AppendText("because im horrible programmed");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "what time is it":
                ss.SpeakAsync("current time is " + DateTime.Now.ToLongTimeString());
                housereplybox.AppendText("current time is " + DateTime.Now.ToLongTimeString());
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "thank you":
                ss.SpeakAsync("no problem");
                housereplybox.AppendText("no problem");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "no thank you":
                ss.SpeakAsync(" i will get back to running your house then");
                housereplybox.AppendText(" i will get back to running your house then");
                housereplybox.AppendText(Environment.NewLine);
                break;
                //locking front door
            case "lock the front door":
                ss.SpeakAsync("i have locked the front door, will there be anything else");
                housereplybox.AppendText("i have locked the front door, will there be anything else");
                frontdoorlocktextBox.Text = "LOCKED";


                housereplybox.AppendText(Environment.NewLine);
                break;
            case "unlock the front door":
                ss.SpeakAsync("i have unlocked the front door, will there be anything else");
                housereplybox.AppendText("i have unlocked the front door, will there be anything else");
                frontdoorlocktextBox.Text = "UNLOCKED";
                housereplybox.AppendText(Environment.NewLine);
                break;
                //unlocking back door
            case "unlock the back door":
                ss.SpeakAsync("i have unlocked the back door, will there be anything else");
                housereplybox.AppendText("i have unlocked the back door, will there be anything else");
                backdoorlocktextbox.Text = "UNLOCKED";
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "lock the back door":
                ss.SpeakAsync("i have locked the back door, will there be anything else");
                housereplybox.AppendText("i have locked the back door, will there be anything else");
                backdoorlocktextbox.Text = "LOCKED";
                housereplybox.AppendText(Environment.NewLine);
                break;
            //backporch
            case "turn the back porch lights off":
                ss.SpeakAsync("i have turned the back porch lights off, will there be anything else");
                housereplybox.AppendText("i have turned back porch lights off, will there be anything else");
                backporchlighonpictureBox.Visible = false;
                backporchlightoffpictureBox.Visible = true;
                backporchtextBox.Text = "OFF";
                backporchlight = false;

                housereplybox.AppendText(Environment.NewLine);
                break;

            case "turn the back porch lights on":
                ss.SpeakAsync("i have turned the back porch lights on, will there be anything else");
                housereplybox.AppendText("i have turned back porch lights on, will there be anything else");
                backporchlighonpictureBox.Visible = true;
                backporchlightoffpictureBox.Visible = false;
                backporchtextBox.Text = "ON";
                backporchlight = true;

                housereplybox.AppendText(Environment.NewLine);
                break;
               
                //middle bedrom
            case "turn the middle bedroom lights on":
                ss.SpeakAsync("i have turned the middle bedroom lights off, will there be anything else");
                housereplybox.AppendText("i have turned middle bedroom lights off, will there be anything else");
                zackbedroomlightonpicturebox.Visible = true;
                zackbedrromlightoffpictureBox.Visible = false;
                zachroomtextBox.Text = "OFF";
                zachroomlight = false;

                housereplybox.AppendText(Environment.NewLine);
                break;

                //kitchen
            case "turn the kitchen lights off":
                ss.SpeakAsync("i have turned the kitchen lights off, will there be anything else");
                housereplybox.AppendText("i have turned kitchen lights off, will there be anything else");
                kitchenlightoffpicturebox.Visible = true;
                kitchenlightonpictureBox.Visible = false;
                kitchentextBox.Text = "OFF";
                kitchenlight=false;
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "turn the kitchen lights on":
                ss.SpeakAsync("i have turned the kitchen lights ON, will there be anything else");
                housereplybox.AppendText("i have turned kitchen lights ON, will there be anything else");
                kitchenlightoffpicturebox.Visible = false;
                kitchenlightonpictureBox.Visible = true;
                kitchentextBox.Text = "ON";
                kitchenlight = true;
                housereplybox.AppendText(Environment.NewLine);
                break;


                //dinning room
            case "turn the dining room lights off":
                ss.SpeakAsync("i have turned the dinning room lights off, will there be anything else");
                housereplybox.AppendText("i have turned dinning room lights off, will there be anything else");
                dinningroomlightoffpictureBox.Visible = true;
                dinningroomlightonpictureBox.Visible = false;
                dinningroomtextBox.Text = "OFF";
                diningroomlight=false;
                housereplybox.AppendText(Environment.NewLine);
                break;

            case "turn the dining room lights on":
                ss.SpeakAsync("i have turned the dinning room lights on, will there be anything else");
                housereplybox.AppendText("i have turned dinning room lights on, will there be anything else");
                dinningroomlightoffpictureBox.Visible = false;
                dinningroomlightonpictureBox.Visible = true;
                dinningroomtextBox.Text = "ON";
                diningroomlight = true;
                housereplybox.AppendText(Environment.NewLine);
                break;


                //kyle room
            case "turn the back bedroom lights off":
                ss.SpeakAsync("i have turned the kyle bedroom lights off, will there be anything else");
                housereplybox.AppendText("i have turned kyle bedroom lights off, will there be anything else");
                kylebedroomlightoffpictureBox.Visible = true;
                kylebedroomlightonpicturebox.Visible = false;
                kylesroomtextBox.Text = "off";
                kyleroomlight = false;
                housereplybox.AppendText(Environment.NewLine);
                break;

            case "turn the back bedroom lights on":
                ss.SpeakAsync("i have turned the kyle bedroom lights ON, will there be anything else");
                housereplybox.AppendText("i have turned kyle bedroom lights ON, will there be anything else");
                kylebedroomlightoffpictureBox.Visible = false;
                kylebedroomlightonpicturebox.Visible = true;
                kylesroomtextBox.Text = "ON";
                kyleroomlight = true;
                housereplybox.AppendText(Environment.NewLine);
                break;


                // hallway
            case "turn the hallway lights off":
                ss.SpeakAsync("i have turned the hallway lights off, will there be anything else");
                housereplybox.AppendText("i have turned hallway lights off, will there be anything else");
                hallwaylightoffpictureBox.Visible = true;
                hallwaylightonpicturebox.Visible = false;
                hallwaytextBox.Text = "OFF";
                hallwaylight = false;
                housereplybox.AppendText(Environment.NewLine);
                break;

            case "turn the hallway lights on":
                ss.SpeakAsync("i have turned the hallway lights on, will there be anything else");
                housereplybox.AppendText("i have turned hallway lights on, will there be anything else");
                hallwaylightoffpictureBox.Visible = false;
                hallwaylightonpicturebox.Visible = true;
                hallwaytextBox.Text = "ON";
                hallwaylight = true;
                housereplybox.AppendText(Environment.NewLine);
                break;


                //bathroom
            case "turn the bathroom lights off":
                ss.SpeakAsync("i have turned the bathroom lights off, will there be anything else");
                housereplybox.AppendText("i have turned bathroom lights off, will there be anything else");
                bathroomlightOFfpictureBox.Visible = true;
                bathroomlightONpictureBox.Visible = false;
                bathroomtextBox.Text = "off";
                bathroomlight = false;
                housereplybox.AppendText(Environment.NewLine);
                break;

            case "turn the bathroom lights on":
                ss.SpeakAsync("i have turned the bathroom lights on, will there be anything else");
                housereplybox.AppendText("i have turned bathroom lights on, will there be anything else");
                bathroomlightOFfpictureBox.Visible = false;
                bathroomlightONpictureBox.Visible = true;
                bathroomtextBox.Text = "ON";
                bathroomlight = true;
                housereplybox.AppendText(Environment.NewLine);
                break;


                //greg bedroom
            case "turn the front bedroom light off":
                ss.SpeakAsync("i have turned the greg bedroom lights off, will there be anything else");
                housereplybox.AppendText("i have turned greg bedroom lights off, will there be anything else");
                gregonpictureBox.Visible = false;
                gregoffpictureBox.Visible = true;
                gregtextBox.Text = "Off";
                gregRoomlight = false;
                housereplybox.AppendText(Environment.NewLine);
                break;

            case "turn the front bedroom light on":
                ss.SpeakAsync("i have turned the greg bedroom lights on, will there be anything else");
                housereplybox.AppendText("i have turned greg bedroom lights on, will there be anything else");
                gregonpictureBox.Visible = true;
                gregoffpictureBox.Visible = false;
                gregtextBox.Text = "ON";
                gregRoomlight = true;
                housereplybox.AppendText(Environment.NewLine);
                break;

                //livingroom lights
            case "turn the living room lights off":
                ss.SpeakAsync("i have turned the living room lights off, will there be anything else");
                housereplybox.AppendText("i have turned the living room lights off, will there be anything else");
                livingroomONpictureBox.Visible = false;
                livingroomoffpictureBox.Visible = true;
                livingRoomlight = false;
                housereplybox.AppendText(Environment.NewLine);
                break;

            case "turn the living room lights on":
                ss.SpeakAsync("i have turned the living room lights on, will there be anything else");
                housereplybox.AppendText("i have turned the living room lights on, will there be anything else");
                livingtextBox.Text = "ON";
                livingroomONpictureBox.Visible = true;
                livingroomoffpictureBox.Visible = false;
                livingRoomlight = true;
                housereplybox.AppendText(Environment.NewLine);
                break;



                //porch lights
            case "turn the front porch light on":
                ss.SpeakAsync("front porch light has been turned on");
                housereplybox.AppendText("front porch light has been turned on");
                frontlightBox.Clear();
                frontlightBox.AppendText("ON");
                frontporchlight = true;
                housereplybox.AppendText(Environment.NewLine);
                frontONpictureBox.Visible = true;
                frontoffpictureBox.Visible = false;
                break;

            case "turn the front porch light off":
                ss.SpeakAsync("front porch light has been turned OFF");
                housereplybox.AppendText("front porch light has been turned OFF");
                frontporchlight = false;
                frontlightBox.Clear();
                frontlightBox.AppendText("OFF");
                housereplybox.AppendText(Environment.NewLine);
                frontONpictureBox.Visible = false;
                frontoffpictureBox.Visible = true;
                break;
            case " turn volume up":
                break;
            case " open chrome":
                System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
                ss.SpeakAsync("opening chrome");
                housereplybox.AppendText("opening chrome");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case " open steam":
                System.Diagnostics.Process.Start(@"D:\Program Files (x86)\Steam\Steam.exe");
                ss.SpeakAsync("opening Steam");
                housereplybox.AppendText("opening Steam");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "run c cleaner":
                System.Diagnostics.Process.Start(@"C:\Program Files\CCleaner\CCleaner64.exe");
                ss.SpeakAsync("opening c cleaner");
                housereplybox.AppendText("opening c cleanere");
                housereplybox.AppendText(Environment.NewLine);
                break;
            case "mute":

                ss.SpeakAsync("opening c cleaner");
                break;
        }
        textBox1.Text += e.Result.Text.ToString() + Environment.NewLine;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        //stop button
        sre.RecognizeAsyncStop();
        button1.Enabled = true;
        button2.Enabled = false;
    }

    private void backporchlightoffpictureBox_Click(object sender, EventArgs e)
    {

    }

    private void label12_Click(object sender, EventArgs e)
    {

    }
    }
    }
     
     
  2. jcsd
  3. Jul 31, 2015 #2
    The code is poorly commented, and it is unclear how the basic voice recognition strategy is supposed to work.

    Several debugging strategies are possible to isolate where the bug may be, but I'm not sure they can be effectively communicated through a third party.
     
  4. Aug 6, 2015 #3

    Mark44

    Staff: Mentor

    @NaughtyBear, the code you posted uses Microsoft .Net Framework, so the appropriate SDK for speech needs to be downloaded and installed. Assuming that the relevant DLLs are in place, your friend needs to get up to speed using the Visual Studio debugger to see what is going wrong.

    The various speech recognition and speech synthesis classes are mostly asynchronous, and rely on callbacks to signal the calling routine when they are done. It's possible that the code is completing before some of the async callbacks have returned. For example, speech recognition can't take place until the grammar has been loaded.

    As far as the code you posted is concerned, I believe that your friend is using the wrong DLLs for speech recognition and synthesis. Instead of the server-based speech DLLS (System.Speech.Xxx) he should be using the DLLs in the Microsoft Speech Platform (Microsoft.Speech.Xxx).

    Documentation root -- https://msdn.microsoft.com/en-us/library/hh361572(v=office.14).aspx
    Speech recognition: https://msdn.microsoft.com/en-us/library/hh378380(v=office.14).aspx
    Speech synthesis: https://msdn.microsoft.com/en-us/library/hh362831(v=office.14).aspx
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C#] Code for project involving voice recognition and arduino
  1. Critique my C++ code (Replies: 11)

Loading...