C# 語音識別(文字to語音、語音to文字)


 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM