2 ways to implement speech recognition by calling SAPI in C #

Source: Internet
Author: User
Tags sapi

This article mainly introduced in C # 2 methods of calling SAPI to realize speech recognition, this article directly gives the implementation code, the need for friends can refer to the following

Through Microsoft's SAPI, not only can TTS be realized, but also speech recognition SR can be realized. Here we will introduce and post the relevant code. There are two main ways:

1, using COM component technology, whether it is c++,c#,delphi can play the turn, developed things in XP and WIN7 can run. (Note to introduce system components SPEECHLIB,XP to install the identification engine)

2, the use of WIN7 Windows API, in fact, eventually called the SAPI, so developed things can only run on the WIN7.

In fact, either way, it is called SAPI, which may be a simpler code.

Using the first approach, you need to be aware that the Microsoft Speech Object Library Reference in the COM tab

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The " and "" "The public class Sprecognition {private static sprecognition _instance = null; private Speechlib.ispeechrecogrammar ISRG; private Speechlib.spsharedrec Ocontextclass Ssrcontex = null;   Public delegate void Stringevent (String str); Public Stringevent Setmessage;   Private sprecognition () {Ssrcontex = new Spsharedrecocontextclass (); ISRG = Ssrcontex.creategrammar (1); Speechlib._ispeechrecocontextevents_recognitioneventhandler rechandle = new _ispeechrecocontextevents_ Recognitioneventhandler (contexrecognition); Ssrcontex.recognition + = Rechandle; public void Beginrec () {Isrg. Dictationsetstate (speechrulestate.sgdsactive); public static Sprecognition instance () {if (_instance = null) _instance = new Sprecognition (), return _instance; pub LIC void Closerec () {iSrg. Dictationsetstate (speechrulestate.sgdsinactive); } private void Contexrecognition (int iindex, object obj, Speechlib.speechrecognitiontype type, Speechlib.ispeechrecoresult result) {if (setmessage!= null) {setmessage. Phraseinfo.gettext (0,-1, true)); } } }

The second one also needs to be introduced, but the introduction is in the Win7. NET3.5 Class Library

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 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 grammar public System.Windows.Forms.Control cdisplay; Display controls   public srecognition (string[] FG)//Create key words list {CultureInfo myciintl = new CultureInfo ("en-us"); foreach (Reco Gnizerinfo config in Speechrecognitionengine. Installedrecognizers ())//Gets all speech engine {if (config). Culture.equals (myciintl) && config. Id = = "Ms-1033-80-desk") {recognizer = new speechrecognitionengine (config); Select the United States English Identification engine} if (recognizer!= null) {Initializespeechrecognitionengine (FG);//Initialize speech recognition engine Dictationgrammar = new Dictat Iongrammar (); else {MessageBox.Show ("Create speech recognition Failed");} private void Initializespeechrecognitionengine (string[] FG) {RECOgnizer. Setinputtodefaultaudiodevice ()//Select the default audio input device Grammar Customgrammar = Createcustomgrammar (FG); The syntax recognizer is established according to the key word group. Unloadallgrammars (); Recognizer. Loadgrammar (Customgrammar); Load syntax recognizer. SpeechRecognized + + new EventHandler <SpeechRecognizedEventArgs> (recognizer_speechrecognized); Recognizer. Speechhypothesized + + new EventHandler <SpeechHypothesizedEventArgs> (recognizer_speechhypothesized); The public void Beginrec (Control Tbresult)//association window Controls {turnspeechrecognitionon (); Turndictationon (); CDisplay = Tbresult; public void over ()/stop speech recognition engine {Turnspeechrecognitionoff ();} public virtual Grammar Createcustomgrammar (string[) FG)//Chuang Create custom Syntax {Grammarbuilder grammarbuilder = new Grammarbuilder (); Grammarbuilder.append (new Choices (FG)); return new grammar (Grammarbuilder); The private void Turnspeechrecognitionon ()//Starts speech recognition function {if (recognizer!= null) {recognizer. Recognizeasync (recognizemode.multiple); Recognition mode is continuous recognition} else {MessageBox.Show ("Create speech recognition Failed");} private voidTurnspeechrecognitionoff ()//Turns off speech recognition function {if (recognizer!= null) {recognizer. Recognizeasyncstop (); Turndictationoff (); else {MessageBox.Show ("Create speech recognition Failed");} private void Recognizer_speechrecognized (object sender, SpeechRecognized EventArgs e) {//identifies the action that results are completed, usually passing the recognition result to a control string text = E.result.text; Cdisplay.text = Text; private void Turndictationon () {if (recognizer!= null) {recognizer. Loadgrammar (Dictationgrammar); Load natural Syntax} else {MessageBox.Show ("Create speech recognition Failed");} private void Turndictationoff () {if (Dictationgrammar!= null) {recognizer. Unloadgrammar (Dictationgrammar); Uninstall Natural Syntax} else {MessageBox.Show ("Create speech recognition Failed");} } }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.