Advertisement
TranzRail

Speech Recognition

Aug 11th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Diagnostics; // Used to access the "Performance Counter".
  15. using System.Speech.Recognition; // Used to access the Speech API.
  16. using System.Speech.Synthesis; // Used to access the Speech
  17.  
  18. namespace Terminal_47_Alpha
  19. {
  20.     /// <summary>
  21.     /// Interaction logic for SystemMonitor.xaml
  22.     /// </summary>
  23.     public partial class SystemMonitor : UserControl
  24.     {  
  25.         // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
  26.        
  27.         // Speech Recognition
  28.         private SpeechRecognitionEngine recognitionEngine; // Defines a new instance of the SpeechRecognitionEngine.
  29.        
  30.         // Speech Synthesis
  31.         SpeechSynthesizer julie = new SpeechSynthesizer(); // Creates the julie instance for Speech Synthesis. 
  32.         int Volume = 100; // Sets the Volume level for Speech Synthesis output.
  33.         int Rate = -1; // Sets the Speed for Speech Synthesis output.
  34.        
  35.         // PerformanceCounter
  36.         string CPUUsage = ""; // Stores the current CPU Usage in Percent.
  37.         string CPUTemp = ""; // Stores the current CPU Temperature in Celcius.
  38.         string RAMUsage = ""; // Stores the current Memory Usage in Percent.
  39.         string RAMAvailable = ""; // Stores the amount of available Memory in Percent.
  40.        
  41.         PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true); // Pulls the "Processor Usage" from the Performance Counter.
  42.         PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes", true); // Pulls the amount of "Available Memory" from the Performance Counter.
  43.        
  44.         // commandCodes
  45.         string activeCommandCode = "";
  46.        
  47.         // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
  48.        
  49.         public SystemMonitor() // Main Method.
  50.         {
  51.             SystemMonitorTimer(null, null); // Calls the SystemMonitorTimer method.
  52.            
  53.             loadSpeechRecognitionEngine(null, null); // Calls the loadSpeechRecognitionEngine method.
  54.             // startEngine(null, null); // Starts the SpeechRecognitionEngine.
  55.            
  56.             this.InitializeComponent(); // Loads Visual Elements. DO NOT REMOVE.
  57.         }  
  58.        
  59.         // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
  60.        
  61.         // Creates the SystemMonitorTimer
  62.         private void SystemMonitorTimer(object sender, EventArgs e)
  63.         {
  64.             System.Windows.Threading.DispatcherTimer myDispatcherTimer = new System.Windows.Threading.DispatcherTimer(); // Creates Timer (to refresh the Debug Menu)  
  65.             myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1, 0); // (days, hours, minutes, seconds, milliseconds) Int32 datatype. Currently set to update every 995ms (was intially 1000ms, but was changed to reduce lag).
  66.             myDispatcherTimer.Tick += new EventHandler(RefreshData);
  67.             myDispatcherTimer.Start();         
  68.         }
  69.        
  70.         // Called by the SystemMonitorTimer
  71.         private void RefreshData(object sender, EventArgs e)
  72.         {
  73.             GetCPUUsage(null, null);
  74.             GetRAMUsage(null, null);
  75.             // GetHDDUsage(null, null); //Future feature. Gets the usage data for locally attached storage.
  76.         }
  77.        
  78.         // Used to change the time string. Add's a 0 if the time has only one digit.
  79.         string twoDigit(string input)
  80.         {
  81.             if (input.Length < 2)
  82.                 return "o" + " " + input;
  83.             else
  84.                 return input;
  85.         }
  86.        
  87.         // Gets the CPU Usage.
  88.         public void GetCPUUsage(object sender, EventArgs e)
  89.         {
  90.             CPUUsage = Convert.ToInt32(cpuCounter.NextValue()).ToString();
  91.         }
  92.        
  93.         // Gets the Memory Usage.
  94.         public void GetRAMUsage(object sender, EventArgs e)
  95.         {          
  96.             double totalRAM = 1024; // Total Available RAM in Megabytes. TO-DO: Make this configurable outside of program source. 8GB = 8192, 1GB = 1024.
  97.             double availableRAM = Convert.ToInt32(ramCounter.NextValue()); // Gets currently Available Memory in Megabytes.
  98.             double freeRAM = 0; // Stores Available RAM in Megabytes. Used to convert to percentage.
  99.             int percentFree = 0; // RAM Available in percent (%). Must use ".ToString()" when displaying in a textblock.
  100.             int percentUsed = 0; // RAM Usage in percent (%). Must use ".ToString()" when displaying in a textblock.
  101.        
  102.             freeRAM = (availableRAM / totalRAM) * 100.0; // Find the amount of RAM available and convert to a percent.
  103.             percentFree = Convert.ToInt32(freeRAM); // Stores the percentage of RAM available. "Convert.ToInt" removes the decimal places from the percentage.
  104.             percentUsed = (100 - percentFree); // Stores the percentage of RAM used.
  105.        
  106.             RAMAvailable = percentFree.ToString(); // Sends the Available RAM percentage with the RAMAvaiable variable.
  107.             RAMUsage = percentUsed.ToString(); // Sends the Used RAM percentage with the RAMUsage variable.
  108.         }
  109.            
  110.         // Gets the HDD Usage.
  111.         public void GetHDDUsage(object sender, EventArgs e)
  112.         {
  113.            
  114.         }
  115.        
  116.         // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
  117.        
  118.         // Loads the SpeechRecognitionEngine.
  119.         public void loadSpeechRecognitionEngine(object sender, EventArgs e)
  120.         {
  121.             recognitionEngine = new SpeechRecognitionEngine(); // Creates a new instance of the SpeechRecognitionEngine.
  122.             recognitionEngine.SetInputToDefaultAudioDevice(); // Sets the SpeechReognitionEngine Input to the System Default (controlled by OS).
  123.             recognitionEngine.LoadGrammar(CreateMainGrammarObject()); // Loads the Main XML Grammar File.
  124.             recognitionEngine.LoadGrammarCompleted +=new EventHandler<LoadGrammarCompletedEventArgs>(recognitionEngine_LoadGrammarCompleted); // Specifies the Event Handler for the LoadGrammarCompleted event.
  125.             recognitionEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognitionEngine_SpeechRecognized); // Specifies the Event Handler for SpeechRecognized event.
  126.             recognitionEngine.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recognitionEngine_SpeechRecognitionRejected); // Specifies the Event Handler for SpeechRecognized event.
  127.             recognitionEngine.AudioSignalProblemOccurred += new EventHandler<AudioSignalProblemOccurredEventArgs>(recognitionEngine_AudioSignalProblemOccurred); // Specifies the Event Handler for AudioSignalProblem event.
  128.         }
  129.        
  130.         // Defines the MainGrammar.
  131.         public Grammar CreateMainGrammarObject()
  132.         {
  133.             Grammar mainGrammar = new Grammar(".//Resources//Config//MainGrammar.xml");
  134.             return mainGrammar;
  135.         }
  136.        
  137.         // Defines the CommandCodeGrammar.
  138.         public Grammar CreateCommandCodeGrammarObject()
  139.         {
  140.             Grammar commandCodeGrammar = new Grammar(".//Resources//Config//CommandCodeGrammar.xml");
  141.             return commandCodeGrammar;
  142.         }
  143.        
  144.         // Starts the SpeechRecognitionEngine.
  145.         private void startEngine(object sender, EventArgs e)
  146.         {
  147.             recognitionEngine.RecognizeAsync(RecognizeMode.Multiple);
  148.         }
  149.        
  150.         // Stops the SpeechRecognitionEngine.
  151.         private void stopEngine(object sender, EventArgs e)
  152.         {
  153.             recognitionEngine.RecognizeAsyncStop();
  154.         }
  155.        
  156.         // Unloads All Loaded Grammars
  157.         private void unloadAllGrammars(object sender, EventArgs e)
  158.         {
  159.             recognitionEngine.UnloadAllGrammars();
  160.         }
  161.        
  162.         // Called when the LoadGrammarCompleted event is raised.
  163.         void recognitionEngine_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
  164.         {
  165.        
  166.         }
  167.        
  168.         // Called when the SpeechRecognized event is raised.
  169.         void recognitionEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  170.         {
  171.             // Defines what to do when speech is recognized.
  172.            
  173.             confidenceLevel.Text = e.Result.Confidence.ToString(); // Outputs the Confidence level to a textblock on the UC.
  174.             txtOutput.Text += e.Result.Text;
  175.             txtOutput.Text += Environment.NewLine;
  176.        
  177.             if (e.Result.Confidence < 0.940f) // Checks to see if Confidence is below the set threshold.
  178.             {
  179.                 computerRejected(null, null); // Play the wave file.
  180.                 //MessageBox.Show("Confidence hasn't been met" + e.Result.Confidence.ToString()); -- Uncomment for debugging purposes.
  181.             }
  182.             else
  183.             {
  184.                 string keyword = "computer ";
  185.                 var hearHello = e.Result.Text.ToString() == keyword + "hello";
  186.                 var hearGoodMorning = e.Result.Text.ToString() == keyword + "good morning";
  187.        
  188.                 if (hearHello)
  189.                 {
  190.                     computerAcknowledge(null, null);
  191.                     HearGreetingHello(null, null);
  192.                 }
  193.                 else if (hearGoodMorning)
  194.                 {
  195.                     computerAcknowledge(null, null);
  196.                 }
  197.             }
  198.            
  199.         }
  200.        
  201.         // Called when the SpeechRecognitionRejected event is raised.
  202.         void recognitionEngine_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
  203.         {
  204.        
  205.         }
  206.        
  207.         // Called when the AudioSignalProblemOccurred event is raised.
  208.         void recognitionEngine_AudioSignalProblemOccurred(object sender, AudioSignalProblemOccurredEventArgs e)
  209.         {
  210.        
  211.         }      
  212.        
  213.         // Sound Player for Acknowledgement.
  214.         private void computerAcknowledge(object sender, EventArgs e)
  215.         {
  216.             System.Media.SoundPlayer soundAcknowledge = new System.Media.SoundPlayer(); // Creates a sound player for the audio anouncements. Used for Static Audio File Playing.
  217.             soundAcknowledge.SoundLocation = ".\\Resources\\Sounds\\Alerts\\comp_ackn.wav"; // Sets the wave file to play.
  218.            
  219.             soundAcknowledge.PlaySync(); // Plays the wave file.
  220.         }
  221.        
  222.         // Sound Player for Rejected.
  223.         private void computerRejected(object sender, EventArgs e)
  224.         {
  225.             System.Media.SoundPlayer soundRejected = new System.Media.SoundPlayer(); // Creates a sound player for the audio anouncements. Used for Static Audio File Playing.
  226.             soundRejected.SoundLocation = ".\\Resources\\Sounds\\Alerts\\comp_deny.wav"; // Sets the wave file to play.
  227.            
  228.             soundRejected.PlaySync(); // Plays the wave file.
  229.         }
  230.        
  231.         // -------------------------------------------------------------------------------------------------------------------------------------------------------------------
  232.        
  233.         // Hear Command Code Accepted Acknowledgement.
  234.         private void commandCodeAccepted(object sender, EventArgs e)
  235.         {
  236.             julie.Volume = Volume;
  237.             julie.Rate = Rate;
  238.             julie.SpeakAsync("Command Code Accepted.");
  239.         }
  240.        
  241.         // Hear Command Code Rejected Acknowledgement. 
  242.         private void commandCodeDenied(object sender, EventArgs e)
  243.         {
  244.             julie.Volume = Volume;
  245.             julie.Rate = Rate;
  246.             julie.SpeakAsync("Command Code Denied.");
  247.         }
  248.        
  249.         // Hear the CPU Usage.
  250.         private void HearCPUUsage(object sender, EventArgs e)
  251.         {                              
  252.             julie.Volume = Volume;
  253.             julie.Rate = Rate;
  254.            
  255.             if ((activeCommandCode == "level_1") | (activeCommandCode == "level_2") | (activeCommandCode == "level_3")) // Checks to see what command code the user has.
  256.             {
  257.                 julie.SpeakAsync("Core processor usage is at  " + CPUUsage.ToString() + "percent."); // Synthesised speech.
  258.             }
  259.         }
  260.        
  261.         // Hear the Memory Usage.
  262.         private void HearRAMUsage(object sender, EventArgs e)
  263.         {
  264.             julie.Volume = Volume;
  265.             julie.Rate = Rate;
  266.             julie.SpeakAsync("Core memory usage is at  " + RAMUsage.ToString() + "percent."); // Synthesised speech.
  267.         }
  268.        
  269.         // Hear the Time.
  270.         private void HearCurrentTime(object o, EventArgs sender)
  271.         {
  272.             string hour = twoDigit(DateTime.Now.Hour.ToString()); // Gets the hour.
  273.             string minute = twoDigit(DateTime.Now.Minute.ToString()); // Gets the minute.
  274.             string second = twoDigit(DateTime.Now.Second.ToString()); // Gets the second.
  275.             string combined = hour + " " + minute; // Combines the time into a string with white space to avoid the time being synthesised as one word.
  276.        
  277.             julie.Volume = Volume;
  278.             julie.Rate = Rate;
  279.             julie.SpeakAsync("The time is " + combined + "hours"); // Synthesised speech.
  280.         }
  281.            
  282.         // Plays the Hello Greeting.
  283.         private void HearGreetingHello(object sender, EventArgs e)
  284.         {
  285.             julie.Volume = Volume;
  286.             julie.Rate = Rate;
  287.             julie.SpeakAsync("Hello" + Config.currentUsernameSetting);
  288.         }
  289.     }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement