using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.IO; using System.Speech; using System.Speech.Recognition; using System.Speech.Synthesis; using System.Threading; namespace Fusion_Control_Centre_UberMDX { public partial class MainMDX : Form { #region Voice Control #region Initialize the Voice Recognition Engine public void SetupSpeech() { try { if (!Speech_MasterEnable) { return; } Thread t = new Thread(new ThreadStart(SetupSpeechThread)); t.IsBackground = true; t.Start(); //SetupSpeechThread(); //System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback()); } catch (Exception) { DisplayMessageBox("Please Make sure the .NET 3.5 Framework is installed for Voice Control"); } } void SetupSpeechThread() { try { Speech_MainRecognizer.SetInputToDefaultAudioDevice(); Grammar CustomizedGrammarForSpeechEngine = ReadDaGrammar(); Speech_MainRecognizer.UnloadAllGrammars(); Speech_MainRecognizer.LoadGrammar(CustomizedGrammarForSpeechEngine); Speech_MainRecognizer.SpeechRecognized += new EventHandler(Speech_MainRecognizer_SpeechRecognized); Speech_MainRecognizer.SpeechHypothesized += new EventHandler(Speech_MainRecognizer_SpeechHypothesized); Speech_MainRecognizer.SpeechRecognitionRejected += new EventHandler(Speech_MainRecognizer_SpeechRecognitionRejected); Speech_Start(); } catch (Exception e) { MessageBox.Show("Error Initializing Speech Setup: \r\n" + e.ToString()); } } #endregion #region Event - Recognized Text void Speech_MainRecognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { String RecognizedText = e.Result.Text; if (Debug_SpeechCommands_Recognized != null) { BeginInvoke(Debug_SpeechCommands_Recognized, new object[] { "Recognized -- " + RecognizedText }); //Debug_SpeechCommands_Recognized("Recognized -- " + RecognizedText); } DoWhatWasSaid(RecognizedText); } #endregion #region Event - Hypothesized Event void Speech_MainRecognizer_SpeechHypothesized(object sender, SpeechHypothesizedEventArgs e) { String HypothesizedText = e.Result.Text; if (Debug_SpeechCommands_Hypothesized != null) { BeginInvoke(Debug_SpeechCommands_Hypothesized, new object[] { "Hypothesized -- " + HypothesizedText }); //Debug_SpeechCommands_Hypothesized("Hypothesized -- " + HypothesizedText); } } #endregion #region Event - Rejected Event void Speech_MainRecognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e) { String RejectedText = e.Result.Text; if (Debug_Speech_CommandsRejected != null) { if (RejectedText == "") { RejectedText = "NOT EVEN CLOSE"; } BeginInvoke(Debug_Speech_CommandsRejected, new object[] { "Rejected -- " + RejectedText }); //Debug_Speech_CommandsRejected("Rejected -- " + RejectedText); } } #endregion #region Read Grammar public Grammar ReadDaGrammar() { GrammarBuilder FusionSpeechGrammarBuilderOfDoom = new GrammarBuilder(); if (Speech_StaticSetupClass.generic_system.Count == 0) { Speech_StaticSetupClass.generic_system.Add(""); } if (Speech_StaticSetupClass.generic_digital_output_turn_on.Count == 0) { Speech_StaticSetupClass.generic_digital_output_turn_on.Add("turn on"); } if (Speech_StaticSetupClass.generic_digital_output_turn_off.Count == 0) { Speech_StaticSetupClass.generic_digital_output_turn_off.Add("turn off"); } if (Speech_StaticSetupClass.generic_digital_output.Count == 0) { Speech_StaticSetupClass.generic_digital_output.Add("output"); } if (Speech_StaticSetupClass.generic_analogue_input_read.Count == 0) { Speech_StaticSetupClass.generic_analogue_input_read.Add("What is the value of"); } if (Speech_StaticSetupClass.generic_analogue_input.Count == 0) { Speech_StaticSetupClass.generic_analogue_input.Add("input"); } for (int i_generic_system_count = 0; i_generic_system_count < Speech_StaticSetupClass.generic_system.Count; i_generic_system_count++) { for (int i_generic_digital_output_turn_on_count = 0; i_generic_digital_output_turn_on_count < Speech_StaticSetupClass.generic_digital_output_turn_on.Count; i_generic_digital_output_turn_on_count++) { for (int i_generic_digital_output_count = 0; i_generic_digital_output_count < Speech_StaticSetupClass.generic_digital_output.Count; i_generic_digital_output_count++) { for (int i_brain_count = 0; i_brain_count < AllBrains.Count; i_brain_count++) { for (int i_digital_output_channel = 0; i_digital_output_channel < AllDigitalOutputs.Count; i_digital_output_channel++) { if (AllDigitalOutputs[i_digital_output_channel].BrainID.ToLower() != AllBrains[i_brain_count].humanName.ToLower()) { continue; } int _NUM = AllDigitalOutputs[i_digital_output_channel].Port + (FUSIONBRAIN_NUMBER_OF_DIGITAL_OUTPUTS(AllBrains[i_brain_count].version) * i_brain_count); string possibleSpeechCommandString = Speech_StaticSetupClass.generic_system[i_generic_system_count] + " " + Speech_StaticSetupClass.generic_digital_output_turn_on[i_generic_digital_output_turn_on_count] + " " + Speech_StaticSetupClass.generic_digital_output[i_generic_digital_output_count] + " " + _NUM.ToString(); SpeechPossibility _possibleSpeechCommandFull = new SpeechPossibility(); _possibleSpeechCommandFull.monitor_type = "digital output"; _possibleSpeechCommandFull.monitor_id = AllDigitalOutputs[i_digital_output_channel].id.ToLower(); _possibleSpeechCommandFull.vote_opinion = "on"; _possibleSpeechCommandFull.response_or_query = SpeechDirection.QUERY; _possibleSpeechCommandFull.possible_queries.Add(possibleSpeechCommandString); AllSpeechCommands.Add(_possibleSpeechCommandFull); } } } for (int i_custom_digital_output_count = 0; i_custom_digital_output_count < Speech_StaticSetupClass.custom_definition_digital_outputs.Count; i_custom_digital_output_count++) { for (int i_digital_output_channel = 0; i_digital_output_channel < AllDigitalOutputs.Count; i_digital_output_channel++) { if (AllDigitalOutputs[i_digital_output_channel].id.ToLower() != Speech_StaticSetupClass.custom_definition_digital_outputs[i_custom_digital_output_count].systemName.ToLower()) { continue; } string possibleSpeechCommandString = Speech_StaticSetupClass.generic_system[i_generic_system_count] + " " + Speech_StaticSetupClass.generic_digital_output_turn_on[i_generic_digital_output_turn_on_count] + " " + Speech_StaticSetupClass.custom_definition_digital_outputs[i_custom_digital_output_count].spokenName; SpeechPossibility _possibleSpeechCommandFull = new SpeechPossibility(); _possibleSpeechCommandFull.monitor_type = "digital output"; _possibleSpeechCommandFull.monitor_id = AllDigitalOutputs[i_digital_output_channel].id.ToLower(); _possibleSpeechCommandFull.vote_opinion = "on"; _possibleSpeechCommandFull.response_or_query = SpeechDirection.QUERY; _possibleSpeechCommandFull.possible_queries.Add(possibleSpeechCommandString); AllSpeechCommands.Add(_possibleSpeechCommandFull); } } } for (int i_generic_digital_output_turn_off_count = 0; i_generic_digital_output_turn_off_count < Speech_StaticSetupClass.generic_digital_output_turn_off.Count; i_generic_digital_output_turn_off_count++) { for (int i_generic_digital_output_count = 0; i_generic_digital_output_count < Speech_StaticSetupClass.generic_digital_output.Count; i_generic_digital_output_count++) { for (int i_brain_count = 0; i_brain_count < AllBrains.Count; i_brain_count++) { for (int i_digital_output_channel = 0; i_digital_output_channel < AllDigitalOutputs.Count; i_digital_output_channel++) { if (AllDigitalOutputs[i_digital_output_channel].BrainID.ToLower() != AllBrains[i_brain_count].humanName.ToLower()) { continue; } int _NUM = AllDigitalOutputs[i_digital_output_channel].Port + (FUSIONBRAIN_NUMBER_OF_DIGITAL_OUTPUTS(AllBrains[i_brain_count].version) * i_brain_count); string possibleSpeechCommandString = Speech_StaticSetupClass.generic_system[i_generic_system_count] + " " + Speech_StaticSetupClass.generic_digital_output_turn_off[i_generic_digital_output_turn_off_count] + " " + Speech_StaticSetupClass.generic_digital_output[i_generic_digital_output_count] + " " + _NUM.ToString(); SpeechPossibility _possibleSpeechCommandFull = new SpeechPossibility(); _possibleSpeechCommandFull.monitor_type = "digital output"; _possibleSpeechCommandFull.monitor_id = AllDigitalOutputs[i_digital_output_channel].id.ToLower(); _possibleSpeechCommandFull.vote_opinion = "off"; _possibleSpeechCommandFull.response_or_query = SpeechDirection.QUERY; _possibleSpeechCommandFull.possible_queries.Add(possibleSpeechCommandString); AllSpeechCommands.Add(_possibleSpeechCommandFull); } } } for (int i_custom_digital_output_count = 0; i_custom_digital_output_count < Speech_StaticSetupClass.custom_definition_digital_outputs.Count; i_custom_digital_output_count++) { for (int i_digital_output_channel = 0; i_digital_output_channel < AllDigitalOutputs.Count; i_digital_output_channel++) { if (AllDigitalOutputs[i_digital_output_channel].id.ToLower() != Speech_StaticSetupClass.custom_definition_digital_outputs[i_custom_digital_output_count].systemName.ToLower()) { continue; } string possibleSpeechCommandString = Speech_StaticSetupClass.generic_system[i_generic_system_count] + " " + Speech_StaticSetupClass.generic_digital_output_turn_off[i_generic_digital_output_turn_off_count] + " " + Speech_StaticSetupClass.custom_definition_digital_outputs[i_custom_digital_output_count].spokenName; SpeechPossibility _possibleSpeechCommandFull = new SpeechPossibility(); _possibleSpeechCommandFull.monitor_type = "digital output"; _possibleSpeechCommandFull.monitor_id = AllDigitalOutputs[i_digital_output_channel].id.ToLower(); _possibleSpeechCommandFull.vote_opinion = "off"; _possibleSpeechCommandFull.response_or_query = SpeechDirection.QUERY; _possibleSpeechCommandFull.possible_queries.Add(possibleSpeechCommandString); AllSpeechCommands.Add(_possibleSpeechCommandFull); } } } for (int i_generic_analogue_input_query_count = 0; i_generic_analogue_input_query_count < Speech_StaticSetupClass.generic_analogue_input_read.Count; i_generic_analogue_input_query_count++) { for (int i_generic_analogue_input_count = 0; i_generic_analogue_input_count < Speech_StaticSetupClass.generic_analogue_input.Count; i_generic_analogue_input_count++) { for (int i_brain_count = 0; i_brain_count < AllBrains.Count; i_brain_count++) { for (int i_analogue_input_channel = 0; i_analogue_input_channel < AllAnalogueInputs.Count; i_analogue_input_channel++) { if (AllAnalogueInputs[i_analogue_input_channel].BrainID.ToLower() != AllBrains[i_brain_count].humanName.ToLower()) { continue; } int _NUM = AllAnalogueInputs[i_analogue_input_channel].Port + (FUSIONBRAIN_NUMBER_OF_ANALOGUE_INPUTS(AllBrains[i_brain_count].version) * i_brain_count); string possibleSpeechCommandString = Speech_StaticSetupClass.generic_system[i_generic_system_count] + " " + Speech_StaticSetupClass.generic_analogue_input_read[i_generic_analogue_input_query_count] + " " + Speech_StaticSetupClass.generic_analogue_input[i_generic_analogue_input_count] + " " + _NUM.ToString(); SpeechPossibility _possibleSpeechCommandFull = new SpeechPossibility(); _possibleSpeechCommandFull.monitor_type = "analogue input"; _possibleSpeechCommandFull.monitor_id = AllAnalogueInputs[i_analogue_input_channel].id.ToLower(); _possibleSpeechCommandFull.vote_opinion = "n/a"; _possibleSpeechCommandFull.response_or_query = SpeechDirection.RESPONSE; _possibleSpeechCommandFull.possible_queries.Add(possibleSpeechCommandString); _possibleSpeechCommandFull.possible_responses.Add(new Speech_Response("The current value of analogue input number " + _NUM.ToString() + " is", "volts")); AllSpeechCommands.Add(_possibleSpeechCommandFull); } } } } foreach (Speech_StaticSetupClass.customQuery _cQ in Speech_StaticSetupClass.custom_query_list) { List build_queries = new List(); if (_cQ.builder_beginnings.Count == 0) { _cQ.builder_beginnings.Add(""); } if (_cQ.builder_middles.Count == 0) { _cQ.builder_middles.Add(""); } if (_cQ.builder_endings.Count == 0) { _cQ.builder_endings.Add(""); } foreach (string build_beginning in _cQ.builder_beginnings) { foreach (string build_middle in _cQ.builder_middles) { foreach (string build_ending in _cQ.builder_endings) { string build_full = Speech_StaticSetupClass.generic_system[i_generic_system_count] + " " + build_beginning + " " + build_middle + " " + build_ending; while (build_full.Contains(" ")) { build_full = build_full.Replace(" ", " "); } build_queries.Add(build_full); } } } SpeechPossibility _possibleSpeechCommandFull = new SpeechPossibility(); _possibleSpeechCommandFull.monitor_type = _cQ.monitor_type.ToLower(); _possibleSpeechCommandFull.monitor_id = _cQ.monitor_id.ToLower(); _possibleSpeechCommandFull.vote_opinion = "n/a"; _possibleSpeechCommandFull.response_or_query = SpeechDirection.RESPONSE; _possibleSpeechCommandFull.possible_queries = new List(build_queries.ToArray()); _possibleSpeechCommandFull.possible_responses = new List(_cQ.possible_responses.ToArray()); AllSpeechCommands.Add(_possibleSpeechCommandFull); } } #region Global Speech Possibilities if (Speech_EnableGlobalPolite) { AllSpeechCommands.Add(new SpeechPossibility(new List(new Speech_Response[] { new Speech_Response("Hello sir", ""), new Speech_Response("Welcome sir", "") }), new List(new string[] { "Hello", "hi", "greetings" }))); AllSpeechCommands.Add(new SpeechPossibility(new List(new Speech_Response[] { new Speech_Response("Good morning sir", "") }), new List(new string[] { "Good morning", "morning" }))); AllSpeechCommands.Add(new SpeechPossibility(new List(new Speech_Response[] { new Speech_Response("Good afternoon sir", "") }), new List(new string[] { "Good afternoon" }))); AllSpeechCommands.Add(new SpeechPossibility(new List(new Speech_Response[] { new Speech_Response("You are welcome", ""), new Speech_Response("My pleasure", "") }), new List(new string[] { "thank you", "thanks" }))); AllSpeechCommands.Add(new SpeechPossibility(new List(new Speech_Response[] { new Speech_Response("I am well, thankyou. How are you?", ""), new Speech_Response("I am well. How are you?", "") }), new List(new string[] { "how are you", "and how are you", "how are you today", "and how are you today" }))); } #endregion List _tempAllPossible = new List(); foreach (SpeechPossibility speechPossibilityInitializationRoot in AllSpeechCommands) { for (int j_speech_query_instance_count = 0; j_speech_query_instance_count < speechPossibilityInitializationRoot.possible_queries.Count; j_speech_query_instance_count++) { _tempAllPossible.Add(speechPossibilityInitializationRoot.possible_queries[j_speech_query_instance_count]); } } DEBUG_AllPossibleSpeechStrings = new List(_tempAllPossible.ToArray()); FusionSpeechGrammarBuilderOfDoom.Append(new Choices(_tempAllPossible.ToArray())); return new Grammar(FusionSpeechGrammarBuilderOfDoom); } #endregion #region Evaluate the Command public void DoWhatWasSaid(String input_ReceivedCommand) { string myCommand = input_ReceivedCommand.ToLower().Trim(); SpeechPossibility myCommandItem = null; int iii = -1; foreach (SpeechPossibility _sP in AllSpeechCommands) { iii++; foreach (string _pQ in _sP.possible_queries) { if (_pQ.ToLower().Trim() == myCommand) { myCommandItem = _sP; break; } } if (myCommandItem != null) { break; } } if (myCommandItem == null) { return; } try { switch (myCommandItem.response_or_query) { case SpeechDirection.GLOBAL: { int responseInt = MDX_Random.Next(); responseInt %= myCommandItem.possible_responses.Count; string _fullToSpeak = myCommandItem.possible_responses[responseInt].beginnning + myCommandItem.possible_responses[responseInt].ending; SpeakMyText(_fullToSpeak); } break; case SpeechDirection.QUERY: { switch (myCommandItem.monitor_type.ToLower()) { case "digital output": { DoSomethingWithLogicCommand("trigger output", myCommandItem.monitor_id.ToLower(), myCommandItem.vote_opinion, Vote_Priority.MEDIUM_PRIORITY, "absolute", null, DateTime.MinValue, 0); } break; default: //Error or Unknown break; } } break; case SpeechDirection.RESPONSE: { string _valueToSpeak = "unknown"; object _o = BaseSelectionOffOf_Function(myCommandItem.monitor_type.ToLower(), myCommandItem.monitor_id.ToLower()); if (_o != null) { _valueToSpeak = _o.ToString(); float _tempValue = 0.0f; if (float.TryParse(_valueToSpeak, out _tempValue)) { bool isNegative = false; if (_valueToSpeak.StartsWith("-")) { isNegative = true; _valueToSpeak = _valueToSpeak.Substring(1); } string[] _isDecimal = _valueToSpeak.Split(new char[] { ',', '.' }); if (_isDecimal.Length == 2) { if (_isDecimal[1].Length > 0) { _isDecimal[1] = _isDecimal[1].Substring(0, 1); } } _valueToSpeak = ""; if (isNegative) { _valueToSpeak += "negative "; } _valueToSpeak += _isDecimal[0]; if (_isDecimal.Length == 2) { _valueToSpeak += (" point " + _isDecimal[1]); } } else { } int responseInt = MDX_Random.Next(); responseInt %= myCommandItem.possible_responses.Count; string _fullToSpeak = myCommandItem.possible_responses[responseInt].beginnning + " " + _valueToSpeak + " " + myCommandItem.possible_responses[responseInt].ending; SpeakMyText(_fullToSpeak); return; } } break; } } catch (InvalidCastException) { } catch (Exception) { } } #endregion #region Speak String public void SpeakMyText(String TextToSpeak) { try { if (!TextToSpeak.EndsWith(".")) { TextToSpeak += "."; } Speech_MainRecognizer.RecognizeAsyncCancel(); Speech_CurrentlyEnabled = false; Speech_MainSynthesizer.Speak(TextToSpeak); Speech_Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } #endregion #region Start Speech Recognition public bool Speech_Start() { try { Speech_MainRecognizer.RecognizeAsync(RecognizeMode.Multiple); Speech_CurrentlyEnabled = false; return true; } catch (Exception) { if (!Speech_MasterEnable) { try { Speech_MasterEnable = true; SetupSpeech(); ; } catch (Exception) { return false; } } return false; } } #endregion #region Stop Speech Recognition public bool Speech_Stop() { try { Speech_MainRecognizer.RecognizeAsyncStop(); Speech_CurrentlyEnabled = false; return true; } catch (Exception) { return false; } } #endregion #endregion } }