C#中调用SAPI实现语音识别的2种方法
通过微软的SAPI,不仅仅可以实现语音合成TTS,同样可以实现语音识别SR。下面我们就介绍并贴出相关代码。主要有两种方式:
1、使用COM组件技术,不管是C++,C#,Delphi都能玩的转,开发出来的东西在XP和WIN7都能跑。(注意要引入系统组件SpeechLib,XP要安装识别引擎)
2、使用WIN7的windowsapi,其实最终还是调用了SAPI,所以开发出来的东西就只能在WIN7上面跑。
其实不管是哪一种,都是调用SAPI,可能后一种代码比较简单。
使用第一种方式,需要注意在COM选项卡里面的MicrosoftSpeech object library引用
publicclassSpRecognition { privatestaticSpRecognition_Instance=null; privateSpeechLib.ISpeechRecoGrammarisrg; privateSpeechLib.SpSharedRecoContextClassssrContex=null; publicdelegatevoidStringEvent(stringstr); publicStringEventSetMessage; privateSpRecognition() { ssrContex=newSpSharedRecoContextClass(); isrg=ssrContex.CreateGrammar(1); SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandlerrecHandle= new_ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition); ssrContex.Recognition+=recHandle; } publicvoidBeginRec() { isrg.DictationSetState(SpeechRuleState.SGDSActive); } publicstaticSpRecognitioninstance() { if(_Instance==null) _Instance=newSpRecognition(); return_Instance; } publicvoidCloseRec() { isrg.DictationSetState(SpeechRuleState.SGDSInactive); } privatevoidContexRecognition(intiIndex,objectobj,SpeechLib.SpeechRecognitionTypetype,SpeechLib.ISpeechRecoResultresult) { if(SetMessage!=null) { SetMessage(result.PhraseInfo.GetText(0,-1,true)); } } }
第二种同样需要引入,不过引入的是Win7中的.NET3.5类库
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Speech; usingSystem.Speech.Recognition; usingSystem.Globalization; usingSystem.Windows.Forms; namespaceStudyBeta { publicclassSRecognition { publicSpeechRecognitionEnginerecognizer=null;//语音识别引擎 publicDictationGrammardictationGrammar=null;//自然语法 publicSystem.Windows.Forms.ControlcDisplay;//显示控件 publicSRecognition(string[]fg)//创建关键词语列表 { CultureInfomyCIintl=newCultureInfo("en-US"); foreach(RecognizerInfoconfiginSpeechRecognitionEngine.InstalledRecognizers())//获取所有语音引擎 { if(config.Culture.Equals(myCIintl)&&config.Id=="MS-1033-80-DESK") { recognizer=newSpeechRecognitionEngine(config); break; }//选择美国英语的识别引擎 } if(recognizer!=null) { InitializeSpeechRecognitionEngine(fg);//初始化语音识别引擎 dictationGrammar=newDictationGrammar(); } else { MessageBox.Show("创建语音识别失败"); } } privatevoidInitializeSpeechRecognitionEngine(string[]fg) { recognizer.SetInputToDefaultAudioDevice();//选择默认的音频输入设备 GrammarcustomGrammar=CreateCustomGrammar(fg); //根据关键字数组建立语法 recognizer.UnloadAllGrammars(); recognizer.LoadGrammar(customGrammar); //加载语法 recognizer.SpeechRecognized+=newEventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized); recognizer.SpeechHypothesized+=newEventHandler<SpeechHypothesizedEventArgs>(recognizer_SpeechHypothesized); } publicvoidBeginRec(ControltbResult)//关联窗口控件 { TurnSpeechRecognitionOn(); TurnDictationOn(); cDisplay=tbResult; } publicvoidover()//停止语音识别引擎 { TurnSpeechRecognitionOff(); } publicvirtualGrammarCreateCustomGrammar(string[]fg)//创造自定义语法 { GrammarBuildergrammarBuilder=newGrammarBuilder(); grammarBuilder.Append(newChoices(fg)); returnnewGrammar(grammarBuilder); } privatevoidTurnSpeechRecognitionOn()//启动语音识别函数 { if(recognizer!=null) { recognizer.RecognizeAsync(RecognizeMode.Multiple); //识别模式为连续识别 } else { MessageBox.Show("创建语音识别失败"); } } privatevoidTurnSpeechRecognitionOff()//关闭语音识别函数 { if(recognizer!=null) { recognizer.RecognizeAsyncStop(); TurnDictationOff(); } else { MessageBox.Show("创建语音识别失败"); } } privatevoidrecognizer_SpeechRecognized(objectsender,SpeechRecognizedEventArgse) { //识别出结果完成的动作,通常把识别结果传给某一个控件 stringtext=e.Result.Text; cDisplay.Text=text; } privatevoidTurnDictationOn() { if(recognizer!=null) { recognizer.LoadGrammar(dictationGrammar); //加载自然语法 } else { MessageBox.Show("创建语音识别失败"); } } privatevoidTurnDictationOff() { if(dictationGrammar!=null) { recognizer.UnloadGrammar(dictationGrammar); //卸载自然语法 } else { MessageBox.Show("创建语音识别失败"); } } } }