/// <summary> /// Default constructor. Sets up the voice recognizer with default settings. /// /// Namely, default options are: en-US, default input device, listen always, confidence level at .90 /// </summary> public VoiceRecognizer() { if (SpeechRecognitionEngine.InstalledRecognizers().Count == 0) { SetupError = "You don't appear to have any Microsoft Speech Recognizer packs installed on your system."; State = VoiceRecognizerState.Error; return; } try { var installedRecognizers = SpeechRecognitionEngine.InstalledRecognizers(); RecognizerInfo speechRecognizer = null; switch (CultureInfo.InstalledUICulture.Name) { case "en-GB": speechRecognizer = (installedRecognizers.FirstOrDefault(x => x.Culture.Name == "en-GB") ?? installedRecognizers.FirstOrDefault(x => x.Culture.TwoLetterISOLanguageName == "en")); break; case "en-US": default: speechRecognizer = installedRecognizers.FirstOrDefault(x => x.Culture.TwoLetterISOLanguageName == "en"); break; } if (speechRecognizer == null) { SetupError = String.Format("You don't appear to have the {0} Speech Recognizer installed. Articulate requires this recognizer to be present in order to function correctly.", "English"); State = VoiceRecognizerState.Error; return; } // Setup members ConfidenceLock = new Object(); EngineShuttingDown = new AutoResetEvent(false); State = VoiceRecognizerState.Paused; // Create a new SpeechRecognitionEngine instance. Engine = new SpeechRecognitionEngine(speechRecognizer); try { // Setup the audio device Engine.SetInputToDefaultAudioDevice(); } catch (InvalidOperationException ex) { // No default input device Trace.WriteLine(ex.Message); SetupError = "Check input device.\n\n"; State = VoiceRecognizerState.Error; return; } // Set the confidence setting ConfidenceMargin = 90; // Create the Grammar instance and load it into the speech recognition engine. Grammar g = new Grammar(CommandPool.BuildSrgsGrammar(speechRecognizer.Culture)); Engine.LoadGrammar(g); // Register a handlers for the SpeechRecognized and SpeechRecognitionRejected event Engine.SpeechRecognized += sre_SpeechRecognized; Engine.SpeechRecognitionRejected += sre_SpeechRecognitionRejected; Engine.RecognizeCompleted += sre_RecognizeCompleted; StartListening(); } catch (Exception ex) { // Something went wrong setting up the voiceEngine. Trace.WriteLine(ex.Message); SetupError = String.Format("{0}\nCurrent Culture: {1}\nAvailable Recognizers: {2}\nStack Trace:\n{3}", ex.Message, CultureInfo.InstalledUICulture.Name, SpeechRecognitionEngine.InstalledRecognizers().Select(x => x.Culture.Name).Aggregate((x, y) => x + ", " + y), ex.StackTrace); State = VoiceRecognizerState.Error; } }
/// <summary> /// Default constructor. Sets up the voice recognizer with default settings. /// /// Namely, default options are: en-US, default input device, listen always, confidence level at .90 /// </summary> public VoiceRecognizer() { try { // detect the system locale and use the best recognizer for the job. CultureInfo cultureInfo = null; foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers()) { // TODO: change to support more languages as they get added in if (ri.Culture.Equals(CultureInfo.CurrentCulture) && ri.Culture.TwoLetterISOLanguageName.Equals("en")) { cultureInfo = ri.Culture; } } // default to en-US if (cultureInfo == null) { cultureInfo = new CultureInfo("en-US"); } // Setup members ConfidenceLock = new Object(); EngineShuttingDown = new AutoResetEvent(false); State = VoiceRecognizerState.Paused; // Create a new SpeechRecognitionEngine instance. Engine = new SpeechRecognitionEngine(cultureInfo); try { // Setup the audio device Engine.SetInputToDefaultAudioDevice(); } catch (InvalidOperationException ex) { // No default input device Trace.WriteLine(ex.Message); SetupError = "Check input device.\n\n"; State = VoiceRecognizerState.Error; return; } // Set the confidence setting ConfidenceMargin = 90; // Create the Grammar instance and load it into the speech recognition engine. Grammar g = new Grammar(CommandPool.BuildSrgsGrammar(cultureInfo)); Engine.LoadGrammar(g); // Register a handlers for the SpeechRecognized and SpeechRecognitionRejected event Engine.SpeechRecognized += sre_SpeechRecognized; Engine.SpeechRecognitionRejected += sre_SpeechRecognitionRejected; Engine.RecognizeCompleted += sre_RecognizeCompleted; StartListening(); } catch (Exception ex) { // Something went wrong setting up the voiceEngine. Trace.WriteLine(ex.Message); SetupError = ex.ToString(); State = VoiceRecognizerState.Error; } }