C#中調用SAPI實現語音辨識的2種方法

來源:互聯網
上載者:User

C#中調用SAPI實現語音辨識的2種方法

   這篇文章主要介紹了C#中調用SAPI實現語音辨識的2種方法,本文直接給出實現代碼,需要的朋友可以參考下

  通過微軟的SAPI,不僅僅可以實現語音合成TTS,同樣可以實現語音辨識SR。下面我們就介紹並貼出相關代碼。主要有兩種方式:

  1、使用COM組件技術,不管是C++,C#,Delphi都能玩的轉,開發出來的東西在XP和WIN7都能跑。(注意要引入系統組件SpeechLib,XP要安裝識別引擎)

  2、使用WIN7的windows api,其實最終還是調用了SAPI,所以開發出來的東西就只能在WIN7上面跑。

  其實不管是哪一種,都是調用SAPI,可能後一種代碼比較簡單。

  使用第一種方式,需要注意在COM選項卡裡面的Microsoft Speech object 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

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 type, SpeechLib.ISpeechRecoResult result)

{

if (SetMessage != null)

{

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

}

}

}

  第二種同樣需要引入,不過引入的是Win7中的.NET3.5類庫

  ?

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;//語音辨識引擎

public DictationGrammar dictationGrammar = null; //自然文法

public System.Windows.Forms.Control cDisplay; //顯示控制項

 

public SRecognition(string[] fg) //建立關鍵詞語列表

{

CultureInfo myCIintl = new CultureInfo("en-US");

foreach (RecognizerInfo config in SpeechRecognitionEngine. InstalledRecognizers())//擷取所有語音引擎

{

if (config.Culture.Equals(myCIintl) && config.Id == "MS-1033-80-DESK" )

{

recognizer = new SpeechRecognitionEngine(config);

break;

}//選擇美國英語的識別引擎

}

if (recognizer != null)

{

InitializeSpeechRecognitionEngine(fg);//初始化語音辨識引擎

dictationGrammar = new DictationGrammar();

}

else

{

MessageBox.Show("建立語音辨識失敗");

}

}

private void InitializeSpeechRecognitionEngine(string[] fg)

{

recognizer.SetInputToDefaultAudioDevice();//選擇預設的音訊輸入裝置

Grammar customGrammar = CreateCustomGrammar(fg);

//根據關鍵字數組建立文法

recognizer.UnloadAllGrammars();

recognizer.LoadGrammar(customGrammar);

//載入文法

recognizer.SpeechRecognized += new EventHandler <SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);

recognizer.SpeechHypothesized += new EventHandler <SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized);

}

public void BeginRec(Control tbResult)//關聯視窗控制項

{

TurnSpeechRecognitionOn();

TurnDictationOn();

cDisplay = tbResult;

}

public void over()//停止語音辨識引擎

{

TurnSpeechRecognitionOff();

}

public virtual Grammar CreateCustomGrammar(string[] fg) //創造自訂文法

{

GrammarBuilder grammarBuilder = new GrammarBuilder();

grammarBuilder.Append(new Choices(fg));

return new Grammar(grammarBuilder);

}

private void TurnSpeechRecognitionOn()//啟動語音辨識函數

{

if (recognizer != null)

{

recognizer.RecognizeAsync(RecognizeMode.Multiple);

//識別模式為連續識別

}

else

{

MessageBox.Show("建立語音辨識失敗");

}

}

private void TurnSpeechRecognitionOff()//關閉語音辨識函數

{

if (recognizer != null)

{

recognizer.RecognizeAsyncStop();

TurnDictationOff();

}

else

{

MessageBox.Show("建立語音辨識失敗");

}

}

private void recognizer_SpeechRecognized(object sender, SpeechRecognized EventArgs e)

{

//識別出結果完成的動作,通常把識別結果傳給某一個控制項

string text = e.Result.Text;

cDisplay.Text = text;

}

private void TurnDictationOn()

{

if (recognizer != null)

{

recognizer.LoadGrammar(dictationGrammar);

//載入自然文法

}

else

{

MessageBox.Show("建立語音辨識失敗");

}

}

private void TurnDictationOff()

{

if (dictationGrammar != null)

{

recognizer.UnloadGrammar(dictationGrammar);

//卸載自然文法

}

else

{

MessageBox.Show("建立語音辨識失敗");

}

}

}

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.