Using System; Using System. Collections. Generic; Using System. Linq; Using System. Text; Using System. Speech; Using System. Speech. Recognition; Using System. Globalization; Using System. Windows. Forms; Namespace StudyBeta { Public class SRecognition { Public SpeechRecognitionEngine recognizer = null; // speech recognition engine Public DictationGrammar dictationGrammar = null; // natural syntax Public System. Windows. Forms. Control cDisplay; // Display Control Public SRecognition (string [] fg) // create a key word list { CultureInfo myCIintl = new CultureInfo ("en-US "); Foreach (RecognizerInfo config in SpeechRecognitionEngine. InstalledRecognizers () // obtain all voice Engines { If (config. Culture. Equals (myCIintl) & config. Id = "MS-1033-80-DESK ") { Recognizer = new SpeechRecognitionEngine (config ); Break; } // Select the American English recognition engine } If (recognizer! = Null) { InitializeSpeechRecognitionEngine (fg); // initialize the speech recognition engine DictationGrammar = new DictationGrammar (); } Else { MessageBox. Show ("failed to create speech recognition "); } } Private void InitializeSpeechRecognitionEngine (string [] fg) { Recognizer. setinputtodefaauaudiodevice (); // select the default audio input device Grammar customGrammar = CreateCustomGrammar (fg ); // Create a syntax based on the keyword Array Recognizer. UnloadAllGrammars (); Recognizer. LoadGrammar (customGrammar ); // Load the syntax Recognizer. SpeechRecognized + = new EventHandler <SpeechRecognizedEventArgs> (recognizer_SpeechRecognized ); Recognizer. SpeechHypothesized + = new EventHandler <SpeechHypothesizedEventArgs> (recognizer_SpeechHypothesized ); } Public void BeginRec (Control tbResult) // associate the window Control { TurnSpeechRecognitionOn (); TurnDictationOn (); CDisplay = tbResult; } Public void over () // stop the speech recognition engine { TurnSpeechRecognitionOff (); } Public virtual Grammar CreateCustomGrammar (string [] fg) // create a custom syntax { GrammarBuilder grammarBuilder = new GrammarBuilder (); GrammarBuilder. Append (new Choices (fg )); Return new Grammar (grammarBuilder ); } Private void TurnSpeechRecognitionOn () // start the speech recognition function { If (recognizer! = Null) { Recognizer. RecognizeAsync (RecognizeMode. Multiple ); // The recognition mode is continuous recognition. } Else { MessageBox. Show ("failed to create speech recognition "); } } Private void TurnSpeechRecognitionOff () // disable the speech recognition function { If (recognizer! = Null) { Recognizer. RecognizeAsyncStop (); TurnDictationOff (); } Else { MessageBox. Show ("failed to create speech recognition "); } } Private void recognizer_SpeechRecognized (object sender, SpeechRecognized EventArgs e) { // Identify the action completed by the result. Generally, the recognition result is passed to a control. String text = e. Result. Text; CDisplay. Text = text; } Private void TurnDictationOn () { If (recognizer! = Null) { Recognizer. LoadGrammar (dictationGrammar ); // Load the natural syntax } Else { MessageBox. Show ("failed to create speech recognition "); } } Private void TurnDictationOff () { If (dictationGrammar! = Null) { Recognizer. UnloadGrammar (dictationGrammar ); // Uninstall the natural syntax } Else { MessageBox. Show ("failed to create speech recognition "); } } } } |