C # two methods to call SAPI for Speech Recognition

Source: Internet
Author: User
Tags sapi

C # two methods to call SAPI for Speech Recognition

This article mainly introduces two methods for calling SAPI in C # To implement speech recognition. This article provides the implementation code directly. For more information, see

Microsoft's SAPI not only enables speech synthesis of TTS, but also enables Speech Recognition SR. Next we will introduce and paste the relevant code. There are two main methods:

1. Using COM component technology, both C ++, C #, and Delphi can play a role, and developed things can run on XP and WIN7. (Note that the system component SpeechLib should be introduced, and the identification engine should be installed for XP)

2. Using windows api of windows 7, in fact, the SAPI is still called, so the developed things can only run on windows 7.

In fact, both of them call SAPI, and the latter code may be relatively simple.

When using the first method, pay attention to 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

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

Public class SpRecognition

{

Private static SpRecognition _ Instance = null;

Private SpeechLib. ISpeechRecoGrammar isrg;

Private SpeechLib. SpSharedRecoContextClass 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;

}

Public void CloseRec ()

{

Isrg. DictationSetState (SpeechRuleState. SGDSInactive );

}

Private void ContexRecognition (int iIndex, object obj, SpeechLib. SpeechRecognitionType, SpeechLib. ISpeechRecoResult result)

{

If (SetMessage! = Null)

{

SetMessage (result. PhraseInfo. GetText (0,-1, true ));

}

}

}

The second type also needs to be introduced, but the. NET3.5 class library in Win7 is introduced.

?

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

43

44

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

84

85

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

119

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 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 ");

}

}

}

}

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.