using System;

using Microsoft.Speech.Synthesis; 
// x86 version. if 64-bit, must adjust Build properties
using Microsoft.Speech.Recognition; using System.Globalization;
// Speech recognition and synthesis demo. C# Console Application. // To run this you need to install four packages. // // I recommend x86 versions even on x64 machines. // 1.) Microsoft Speech Platform 11 Runtime (to run speech recognition and synthesis on your machine) // 2.) Microsoft Speech Platform 11 SDK (to create in Visual Studio) // 3.) Microsoft Speech Platform 11 Runtime Language MSSpeech_SR_en-US_TELE.msi (to recognize speech) // 4.) Microsoft Speech Platform 11 Runtime Language MSSpeech_TTS_en-US_Helen.msi (to speak via synthesis) // Add a reference to Microsoft.Speech.dll which is // typically at C:\Program Files (x86)\Microsoft SDKs\Speech\v11.0\Assembly // // You can use this code however you wish subject to the usual disclaimers // (use at your own risk, etc.)
namespace ConsoleSpeech { class ConsoleSpeechProgram { static SpeechSynthesizer ss = new SpeechSynthesizer(); static SpeechRecognitionEngine sre; static bool done = false; static bool speechOn = true; static void Main(string[] args) { try { ss.SetOutputToDefaultAudioDevice(); ss.Speak("I am awake"); CultureInfo ci = new CultureInfo("en-us"); sre = new SpeechRecognitionEngine(ci); sre.SetInputToDefaultAudioDevice(); sre.SpeechRecognized += sre_SpeechRecognized; Choices ch_StartStopCommands = new Choices(); ch_StartStopCommands.Add("speech on"); ch_StartStopCommands.Add("speech off"); ch_StartStopCommands.Add("klatu barada nikto"); GrammarBuilder gb_StartStop = new GrammarBuilder(); gb_StartStop.Append(ch_StartStopCommands); Grammar g_StartStop = new Grammar(gb_StartStop); Choices ch_Numbers = new Choices(); ch_Numbers.Add("1"); ch_Numbers.Add("2"); ch_Numbers.Add("3"); ch_Numbers.Add("4"); GrammarBuilder gb_WhatIsXplusY = new GrammarBuilder(); gb_WhatIsXplusY.Append("What is"); gb_WhatIsXplusY.Append(ch_Numbers); gb_WhatIsXplusY.Append("plus"); gb_WhatIsXplusY.Append(ch_Numbers); Grammar g_WhatIsXplusY = new Grammar(gb_WhatIsXplusY); sre.LoadGrammarAsync(g_StartStop); sre.LoadGrammarAsync(g_WhatIsXplusY); sre.RecognizeAsync(RecognizeMode.Multiple); while (done == false) { ; } Console.WriteLine("\nHit to close shell\n"); Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } // Main static void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { string txt = e.Result.Text; double confidence = e.Result.Confidence; Console.WriteLine("\nRecognized: " + txt); if (confidence < 0.60) return; if (txt.IndexOf("speech on") >= 0) { Console.WriteLine("Speech is now ON"); speechOn = true; } if (txt.IndexOf("speech off") >= 0) { Console.WriteLine("Speech is now OFF"); speechOn = false; } if (speechOn == false) return; if (txt.IndexOf("klatu") >= 0 && txt.IndexOf("barada") >= 0) { ((SpeechRecognitionEngine)sender).RecognizeAsyncCancel(); done = true; ss.Speak("Farewell"); } if (txt.IndexOf("What") >= 0 && txt.IndexOf("plus") >= 0) // what is 2 plus 3 { string[] words = txt.Split(' '); // or use e.Result.Words int num1 = int.Parse(words[2]); int num2 = int.Parse(words[4]); int sum = num1 + num2; Console.WriteLine("Speaking: " + words[2] + " plus " + words[4] + " equals " + sum); ss.SpeakAsync(words[2] + " plus " + words[4] + " equals " + sum); } } // sre_SpeechRecognized } // Program } // ns