標籤:
C# 語音辨識(文字to語音、語音to文字)
最近打算研究一下語音辨識,但是發現網上很少有C#的完整代碼,就把自己的學習心得放上來,和大家分享一下。
下載API:
1)SpeechSDK51.exe (67.0 MB)
2)SpeechSDK51LangPack.exe (81.0 MB)
API可以不下載,但是如果你的VS是英文版,但是想使用中文的語音,那你就需要下載API,按順序安裝好。
(PS:我的VS是英文的,不能說中文,為了這個我糾結了一上午。API,感謝:XAF ,http://smartsoft.5d6d.com/thread-8819-1-1.html)
文字to語音:
這個相當的簡單。
1)在COM選項卡裡面的Microsoft Speech object library引用
2)using SpeechLib;
3)SpVoiceClass voice = new SpVoiceClass();//SAPI 5.1
SpVoice voice = new SpVoice();//SAPI 5.4
4)voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(3);
5)voice.Speak(“你要說的話”);
PS:在第四步的時候是選擇語言,不同API可能不一樣,網上有說是0,但是我使用的API卻是3。
語音to文字:
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)); } } }
希望上面代碼對大家有用。s
C# 語音辨識(文字to語音、語音to文字)