- #1
NaughtyBear
- 17
- 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:
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(" I am 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 I am horrible programmed");
housereplybox.AppendText("because I am 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)
{
}
}
}