1+ using System ;
2+ using System . Globalization ;
3+ using System . Speech . Recognition ;
4+ using Speech . Properties ;
5+
6+ namespace Speech
7+ {
8+ public delegate void SpeechRecognized ( string text ) ;
9+
10+ public class Recognizer
11+ {
12+ private readonly SpeechRecognitionEngine m_recognizer ; //语音识别引擎
13+ private readonly DictationGrammar m_grammar ; //自然语法
14+
15+ public Recognizer ( )
16+ {
17+ var myCIintl = new CultureInfo ( "en-US" ) ;
18+ var rs = SpeechRecognitionEngine . InstalledRecognizers ( ) ;
19+ if ( rs . Count > 0 )
20+ {
21+ foreach ( var config in rs ) //获取所有语音引擎
22+ {
23+ if ( config . Culture . Equals ( myCIintl ) && config . Id == "MS-1033-80-DESK" )
24+ {
25+ m_recognizer = new SpeechRecognitionEngine ( config ) ;
26+ break ;
27+ } //选择美国英语的识别引擎
28+ }
29+ if ( m_recognizer == null ) //如果没有适合的语音引擎,则选用第一个
30+ m_recognizer = new SpeechRecognitionEngine ( rs [ 0 ] ) ;
31+ }
32+ if ( m_recognizer != null )
33+ {
34+ var kws = Settings . Default . Keywords ;
35+ var fg = new string [ kws . Count ] ;
36+ kws . CopyTo ( fg , 0 ) ;
37+ InitializeSpeechRecognitionEngine ( fg ) ; //初始化语音识别引擎
38+ m_grammar = new DictationGrammar ( ) ;
39+ }
40+ else
41+ {
42+ Console . WriteLine ( "创建语音识别失败" ) ;
43+ }
44+ }
45+
46+ private void InitializeSpeechRecognitionEngine ( string [ ] fg )
47+ {
48+ m_recognizer . SetInputToDefaultAudioDevice ( ) ; //选择默认的音频输入设备
49+ var customGrammar = CreateCustomGrammar ( fg ) ;
50+ //根据关键字数组建立语法
51+ m_recognizer . UnloadAllGrammars ( ) ;
52+ m_recognizer . LoadGrammar ( customGrammar ) ;
53+ //加载语法
54+ m_recognizer . SpeechRecognized += recognizer_SpeechRecognized ;
55+ //m_recognizer.SpeechHypothesized += recognizer_SpeechHypothesized;
56+ }
57+
58+ public SpeechRecognized OnRecognized ;
59+
60+ private void recognizer_SpeechRecognized ( object sender , SpeechRecognizedEventArgs e )
61+ {
62+ Console . WriteLine ( "SpeechRecognized:" + e . Result . Text ) ;
63+ OnRecognized ? . Invoke ( e . Result . Text ) ;
64+ }
65+
66+ private void recognizer_SpeechHypothesized ( object sender , SpeechHypothesizedEventArgs e )
67+ {
68+
69+ }
70+
71+ /// <summary>
72+ /// 录音并识别
73+ /// </summary>
74+ public void BeginRec ( )
75+ {
76+ Console . WriteLine ( "BeginRec" ) ;
77+ TurnSpeechRecognitionOn ( ) ;
78+ TurnDictationOn ( ) ;
79+ }
80+
81+ private void TurnDictationOn ( )
82+ {
83+ if ( m_recognizer != null )
84+ {
85+ m_recognizer . LoadGrammar ( m_grammar ) ;
86+ //加载自然语法
87+ }
88+ else
89+ {
90+ Console . WriteLine ( "创建语音识别失败" ) ;
91+ }
92+ }
93+
94+ private void TurnSpeechRecognitionOn ( ) //启动语音识别函数
95+ {
96+ if ( m_recognizer != null )
97+ {
98+ m_recognizer . RecognizeAsync ( RecognizeMode . Multiple ) ;
99+ //识别模式为连续识别
100+ }
101+ else
102+ {
103+ Console . WriteLine ( "创建语音识别失败" ) ;
104+ }
105+ }
106+
107+ public void EndRec ( ) //停止语音识别引擎
108+ {
109+ Console . WriteLine ( "EndRec" ) ;
110+ TurnSpeechRecognitionOff ( ) ;
111+ }
112+
113+ private void TurnSpeechRecognitionOff ( ) //关闭语音识别函数
114+ {
115+ if ( m_recognizer != null )
116+ {
117+ m_recognizer . RecognizeAsyncStop ( ) ;
118+ TurnDictationOff ( ) ;
119+ }
120+ else
121+ {
122+ Console . WriteLine ( "创建语音识别失败" ) ;
123+ }
124+ }
125+
126+ private void TurnDictationOff ( )
127+ {
128+ if ( m_grammar != null )
129+ {
130+ m_recognizer . UnloadGrammar ( m_grammar ) ;
131+ //卸载自然语法
132+ }
133+ else
134+ {
135+ Console . WriteLine ( "创建语音识别失败" ) ;
136+ }
137+ }
138+
139+ private Grammar CreateCustomGrammar ( string [ ] fg ) //创造自定义语法
140+ {
141+ var grammarBuilder = new GrammarBuilder ( ) ;
142+ grammarBuilder . Append ( new Choices ( fg ) ) ;
143+ return new Grammar ( grammarBuilder ) ;
144+ }
145+ }
146+ }
0 commit comments