萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> C#中調用SAPI實現語音合成的2種方法

C#中調用SAPI實現語音合成的2種方法

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

  我們都知道現在的語音合成TTS是可以通過微軟的SAPI實現的,好處我就不多說了,方便而已,因為在微軟的操作系統裡面就自帶了這個玩意,主要的方式有兩種:

  1、使用COM組件技術,不管是C++,C#,Delphi都能玩的轉,開發出來的東西在XP和WIN7都能跑。(要引入SpeechLib,好像在項目上點引用,然後選到系統COM吧,好久沒弄,記不清楚了)

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

  其實不管是哪一種,都是調用SAPI,可能後一種代碼比較簡單,使用已經安裝的TTS引擎,現在一般用NeoSpeech,這個就不解釋了,太強大了這個發音。。。

  COM組件技術:

  ?

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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 public class Speach { private static Speach _Instance = null ; private SpeechLib.SpVoiceClass voice =null; //SAPI5.1 private SpeechLib.SpVoice voice = null;//SAPI 5.4 private Speach() { BuildSpeach() ; } public static Speach instance() { if (_Instance == null) _Instance = new Speach() ; return _Instance ; }   private void SetChinaVoice() { voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ; }   private void SetEnglishVoice() { voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ; }   private void SpeakChina(string strSpeak) { SetChinaVoice() ; Speak(strSpeak) ; }   private void SpeakEnglishi(string strSpeak) { SetEnglishVoice() ; Speak(strSpeak) ; }       public void AnalyseSpeak(string strSpeak) { int iCbeg = 0 ; int iEbeg = 0 ; bool IsChina = true ; for(int i=0;i<strSpeak.Length;i++) { char chr = strSpeak[i] ; if (IsChina) { if (chr<=122&&chr>=65) { int iLen = i - iCbeg ; string strValue = strSpeak.Substring(iCbeg,iLen) ; SpeakChina(strValue) ; iEbeg = i ; IsChina = false ; } } else { if (chr>122||chr<65) { int iLen = i - iEbeg ; string strValue = strSpeak.Substring(iEbeg,iLen) ; this.SpeakEnglishi(strValue) ; iCbeg = i ; IsChina = true ; } } }//end for if (IsChina) { int iLen = strSpeak.Length - iCbeg ; string strValue = strSpeak.Substring(iCbeg,iLen) ; SpeakChina(strValue) ; } else { int iLen = strSpeak.Length - iEbeg ; string strValue = strSpeak.Substring(iEbeg,iLen) ; SpeakEnglishi(strValue) ; } }   private void BuildSpeach() { if (voice == null) voice = new SpVoiceClass() ; }   public int Volume { get { return voice.Volume ; }   set { voice.SetVolume((ushort)(value)) ; } }   public int Rate { get { return voice.Rate ; } set { voice.SetRate(value) ; } }   private void Speak(string strSpeack) { try { voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ; } catch(Exception err) { throw(new Exception("發生一個錯誤:"+err.Message)) ; } }   public void Stop() { voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ; }   public void Pause()   { voice.Pause() ; }   public void Continue() { voice.Resume() ; } }//end class

  在 private SpeechLib.SpVoiceClass voice =null;這裡,我們定義個一個用來發音的類,並且在第一次調用該類時,對它用BuildSpeach方法進行了初始化。

  我們還定義了兩個屬性Volume和Rate,能夠設置音量和語速。

  我們知道,SpVoiceClass 有一個Speak方法,我們發音主要就是給他傳遞一個字符串,它負責讀出該字符串,如下所示。

  ?

1 2 3 4 5 6 7 8 9 10 11 private void Speak(string strSpeack) { try {
copyright © 萬盛學電腦網 all rights reserved