public void Start() { var _commands = Commands; //if (IncludeCommonWords) //{ // if (string.IsNullOrEmpty(_commands)) _commands = Extensions.GetStringFromResource(typeof(MSSpeechPlugin), "commands.txt"); // if (!string.IsNullOrEmpty(_commands)) _commands += "\n" + Extensions.GetStringFromResource(typeof(MSSpeechPlugin), "commands.txt"); //} if (!string.IsNullOrEmpty(_commands) || IncludeCommonWords) { var gBuilder = new System.Speech.Recognition.GrammarBuilder(); //if (IncludeCommonWords) //{ // gBuilder.AppendDictation(); //} if (!string.IsNullOrEmpty(_commands)) { var commands = new System.Speech.Recognition.Choices(); var array = _commands.Split(new char[] { '\n', '\r' }).Where(x => !string.IsNullOrEmpty(x)).ToArray(); commands.Add(array); gBuilder.Append(commands); } if (IncludeCommonWords) { gBuilder.AppendDictation(); } var grammer = new System.Speech.Recognition.Grammar(gBuilder); recEngine.UnloadAllGrammars(); recEngine.LoadGrammarAsync(grammer); recEngine.RecognizeAsync(System.Speech.Recognition.RecognizeMode.Multiple); } }
public void speechToTextInit() { //This method is used for converting speech into text //Creates an instance of the system.speech speech recognition engine for use with speech to text stt_sre = new System.Speech.Recognition.SpeechRecognitionEngine(); //Uses dictation grammar to allow freedom of speech for entering any word detected System.Speech.Recognition.Grammar dictationGrammar = new System.Speech.Recognition.DictationGrammar(); //Loads the dictation grammar into the speech recognition engine stt_sre.LoadGrammar(dictationGrammar); try { //Try catch is used here to catch any invalid operation exceptions that could occur when detecting speech to text stt_sre.SetInputToDefaultAudioDevice(); //Saves result from speech recognition as a recognition result object System.Speech.Recognition.RecognitionResult result = stt_sre.Recognize(); if (result != null) { //Speech result is set to the string of the result to be used in speech to text speechResult = result.Text; //Removes any spaces to prevent inconsistencies speechResult.Replace(",", ""); Console.WriteLine("The result is: " + speechResult); try { //Used in passing results from speech to text to other classes speechInputCheck(speechResult); } catch (NullReferenceException null_ref) { //Used in catching if the speechResult is null, showing that the exception has been thrown and its source Console.WriteLine("NullReferenceException thrown in speech: " + null_ref.Message + null_ref.Source); } } } catch (InvalidOperationException invalid) { Console.WriteLine("InvalidOperationException in speech: " + invalid.Message + invalid.Source); } finally { //Unloads the speech recognition engine once finished stt_sre.UnloadAllGrammars(); } }