Friday Speech Recognition
The Friday AI is a project that I am working on for myself to help with things on my computer. I started this project to see what I can do with coding and console based applications and having them control different parts of my computer by just saying something. The commands are limited to preset commands that I type out and if these commands are not said or pronounced correctly it won't be recognized. I am working on making it recognize any speech.
# hey
class FridaySpeech { static SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine(); static SpeechSynthesizer synth = new SpeechSynthesizer(); public static Choices commands = new Choices(); public static Choices words = new Choices(); [DllImport("user32.dll")] static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); string GetActiveWindowTitle() { const int nChars = 256; StringBuilder Buff = new StringBuilder(nChars); IntPtr handle = GetForegroundWindow(); if (GetWindowText(handle, Buff, nChars) > 0) { return Buff.ToString(); } return null; } public static void Start() { try { commands.Add(new string[] { "hello", "add one", "my name","search for", "your name", "time", "close","thank you","friday","help","quit","stop","end","exit" }); commands.Add(new string[] { "what", "what's", "the", "is", "up", "it", "they're", "they", "this", "that", "well", "with", "search", "how", "where", "when", "how","how's", "who" ,"hey","because","drawing","draw","chrome","google","tell","discord"}); //foreach(string thing in Program) //{ //} synth.SetOutputToDefaultAudioDevice(); GrammarBuilder gBuilder = new GrammarBuilder(); gBuilder.Append(commands); gBuilder.AppendWildcard(); //gBuilder.Append(words); gBuilder.AppendDictation(); Grammar grammar = new Grammar(gBuilder); recognitionEngine.LoadGrammarAsync(grammar); recognitionEngine.SetInputToDefaultAudioDevice(); recognitionEngine.SpeechRecognized += recognitionEngine_SpeechRecognition; Console.Write("Voice Status: "); Program.TypeWriter("Operational", ConsoleColor.Green); Console.WriteLine(); return; } catch (Exception e) { Console.Write("Voice Status: "); Program.TypeWriter(e.Message, ConsoleColor.Red); Console.WriteLine(); return; } } public static void Stop() { recognitionEngine.RecognizeAsyncStop(); Program.Messages(); } ///<summary> ///<para>line1</para> ///<para>line2</para> ///</summary> public static void Begin() { try { Discord.MainAsync().GetAwaiter().GetResult(); string m = Program.Uppercasefirst(Variables.Bot_Greetings[Program.PickIndex(Variables.Bot_Greetings)]); Program.SendMessage(m + " Brett" ,""); synth.SpeakAsync(m + " Brett"); //Program.SendMessage(,""); recognitionEngine.RecognizeAsync(RecognizeMode.Multiple); //there are two voices for windows //Program.SendMessage(" Name: " + synth.Voice.Name,""); //Program.SendMessage(" Culture: " + synth.Voice.Culture, ""); //Program.SendMessage(" Age: " + synth.Voice.Age, ""); //Program.SendMessage(" Gender: " + synth.Voice.Gender, ""); //Program.SendMessage(" Description: " + synth.Voice.Description, ""); //Program.SendMessage(" ID: " + synth.Voice.Id, ""); //Program.SendMessage(" Others: " + synth.GetInstalledVoices(),""); Program.SendMessage("I can hear you now.", ""); synth.SpeakAsync("I can hear you now"); Program.Messages(); }catch(Exception e) { Program.TypeWriter("Error: ", ConsoleColor.Red); Console.Write(e.Message); Console.WriteLine(); Program.SendMessage("I am not able to hear you. Check your input and output devices.", ""); Program.Messages(); } } static void recognitionEngine_SpeechRecognition(object sender, SpeechRecognizedEventArgs e) { Console.Write("Confidence: " + e.Result.Confidence.ToString() + ", Predicted: " + e.Result.Text); Console.WriteLine(); if(e.Result.Confidence >= 0.50) { //if (e.Result.Text.Contains("friday")) //{ if (e.Result.Text.Contains("thank you")) { Program.SendMessage("You are welcome, Brett.", "thank you friday"); }else if(e.Result.Text == "friday") { Program.SendMessage("Yes Brett?", "friday"); }else if(e.Result.Text.Contains("close") || e.Result.Text.Contains("quit") || e.Result.Text.Contains("stop") || e.Result.Text.Contains("exit")) { synth.Speak("Closing"); Program.TypeWriter("Closing....", ConsoleColor.Yellow, ConsoleColor.White, 10); synth.Dispose(); Discord.discord.ClearPresence(); Discord.discord.Dispose(); recognitionEngine.RecognizeAsyncStop(); Environment.Exit(0); }else if(e.Result.Text.Contains("what is the date")) { synth.SpeakAsync("Today's date is " + string.Format("{0:dddd, dd MMMM yyyy}",DateTime.Now)); Program.SendMessage("Today's date is " + string.Format("{0:dddd, dd MMMM yyyy}", DateTime.Now),"what is the date"); }else if(e.Result.Text.Contains("open chrome")) { synth.SpeakAsync("opening google chrome"); Process.Start("Chrome.exe", "google.ca"); Program.SendMessage("Opening Google Chrome","open chrome"); } /*else { string m = Program.Uppercasefirst(Variables.Bot_TryAgain[Program.PickIndex(Variables.Bot_TryAgain)]); synth.SpeakAsync(m); Program.TypeWriter(m); Console.WriteLine(); }*/ bool found = false; foreach (string time in Variables.Synonyms.WhatTime) { if (e.Result.Text.Contains(time.ToLower())) { synth.SpeakAsync("The time is " + string.Format("{0:h:mm:ss tt}", DateTime.Now)); Program.SendMessage("The time is " + string.Format("{0:h:mm:ss tt}", DateTime.Now), time); found = true; break; } } foreach(string trigger in Variables.Synonyms.Triggers) { if (e.Result.Text.Contains(trigger.ToLower())) { string message = Program.Uppercasefirst(Variables.Bot_Greetings[Program.PickIndex(Variables.Bot_Greetings)]); Program.SendMessage(message, trigger); synth.SpeakAsync(message); found = true; break; } } if (!found) { string m = Program.Uppercasefirst(Variables.Bot_TryAgain[Program.PickIndex(Variables.Bot_TryAgain)]); synth.SpeakAsync(m); Program.TypeWriter(m); Console.WriteLine(); } //} /*switch (e.Result.Text) { case "help": Program.TypeWriter(commands.ToString()); Console.WriteLine(); break; case "search for": Process.Start("Chrome.exe", "www.brettanda.ca"); break; case "what's my name": Program.SendMessage("Your name is Brett", "what's my name"); break; case "what's your name": Program.SendMessage("My name is Friday", "what's your name"); break; case "what time is it": Program.SendMessage("The time is " + string.Format("{0:h:mm:ss tt}", DateTime.Now), "what time is it"); break; }*/ } else { Program.TypeWriter(Program.Uppercasefirst(Variables.Bot_TryAgain[Program.PickIndex(Variables.Bot_TryAgain)])); synth.SpeakAsync( Program.Uppercasefirst(Variables.Bot_TryAgain[Program.PickIndex(Variables.Bot_TryAgain)])); } } public static void Loops() { } public static Boolean isCommand(string message) { string prefix = "friday"; if (message.ToLower().Contains(prefix) && Program.IsOwnWord(prefix,message)){ return true; } else { return false; } } }