中英文语音合成与中文语音识别技术在c#中的应用(一)

在.net中,对英文语音有较好的支持,但是对中文语音的支持还没有加入进来,我们要想实现中文发音或中文语音识别,必需先安装微软的Speech Application SDK(SASDK),它的最新版本是 SAPI 5.1 他能够识别中、日、英三种语言,你可以在这里下载: http://www.microsoft.com/speech/download/sdk51/ ,需要安装这两个文件Speech SDK 5.1和5.1 Language Pack,其中5.1 Language Pack可以选择安装支持的语言。

安装好以后,我们就可以开始进行语音程序的开发了,当然,在这之前我们需要把SAPI.dll通过如下图所示添加到引用中

下面我们设计一个能够朗读中英文混合语言的类:

我们将用单例模式实现该类,类的代码如下,我们将详细解释:

public class Speach

{

private static Speach _Instance = null ;

private SpeechLib.SpVoiceClass voice = null ;

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

  1<strspeak.length;i++) (chr<="122&amp;&amp;chr" (ischina)="" ;="" char="" chr="strSpeak[i]" if="" {="">=65) 
  2
  3{ 
  4
  5int  iLen = i - iCbeg ; 
  6
  7string  strValue = strSpeak.Substring(iCbeg,iLen) ; 
  8
  9SpeakChina(strValue) ; 
 10
 11iEbeg = i ; 
 12
 13IsChina =  false  ; 
 14
 15} 
 16
 17} 
 18
 19else 
 20
 21{ 
 22
 23if  (chr&gt;122||chr&lt;65) 
 24
 25{ 
 26
 27int  iLen = i - iEbeg ; 
 28
 29string  strValue = strSpeak.Substring(iEbeg,iLen) ; 
 30
 31this  .SpeakEnglishi(strValue) ; 
 32
 33iCbeg = i ; 
 34
 35IsChina =  true  ; 
 36
 37} 
 38
 39} 
 40
 41}  //end for 
 42
 43if  (IsChina) 
 44
 45{ 
 46
 47int  iLen = strSpeak.Length - iCbeg ; 
 48
 49string  strValue = strSpeak.Substring(iCbeg,iLen) ; 
 50
 51SpeakChina(strValue) ; 
 52
 53} 
 54
 55else 
 56
 57{ 
 58
 59int  iLen = strSpeak.Length - iEbeg ; 
 60
 61string  strValue = strSpeak.Substring(iEbeg,iLen) ; 
 62
 63SpeakEnglishi(strValue) ; 
 64
 65} 
 66
 67} 
 68
 69private  void  BuildSpeach() 
 70
 71{ 
 72
 73if  (voice ==  null  ) 
 74
 75voice =  new  SpVoiceClass() ; 
 76
 77} 
 78
 79public  int  Volume 
 80
 81{ 
 82
 83get 
 84
 85{ 
 86
 87return  voice.Volume ; 
 88
 89} 
 90
 91set 
 92
 93{ 
 94
 95voice.SetVolume((  ushort  )(  value  )) ; 
 96
 97} 
 98
 99} 
100
101public  int  Rate 
102
103{ 
104
105get 
106
107{ 
108
109return  voice.Rate ; 
110
111} 
112
113set 
114
115{ 
116
117voice.SetRate(  value  ) ; 
118
119} 
120
121} 
122
123private  void  Speak(  string  strSpeack) 
124
125{ 
126
127try 
128
129{ 
130
131voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ; 
132
133} 
134
135catch  (Exception err) 
136
137{ 
138
139throw  (  new  Exception("发生一个错误:"+err.Message)) ; 
140
141} 
142
143} 
144
145public  void  Stop() 
146
147{ 
148
149voice.Speak(  string  .Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ; 
150
151} 
152
153public  void  Pause() 
154
155{ 
156
157voice.Pause() ; 
158
159} 
160
161public  void  Continue() 
162
163{ 
164
165voice.Resume() ; 
166
167} 
168
169}  //end class 
170
171在  private  SpeechLib.SpVoiceClass voice =  null  ;这里,我们定义个一个用来发音的类,并且在第一次调用该类时,对它用BuildSpeach方法进行了初始化。 
172
173我们还定义了两个属性Volume和Rate,能够设置音量和语速。 
174
175我们知道,SpVoiceClass 有一个Speak方法,我们发音主要就是给他传递一个字符串,它负责读出该字符串,如下所示。 
176
177private  void  Speak(  string  strSpeack) 
178
179{ 
180
181try 
182
183{ 
184
185voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ; 
186
187} 
188
189catch  (Exception err) 
190
191{ 
192
193throw  (  new  Exception("发生一个错误:"+err.Message)) ; 
194
195} 
196
197} 
198
199其中SpeechVoiceSpeakFlags.SVSFlagsAsync表示异步发音。 
200
201![添加sapi引用](http://dev.csdn.net/Develop/ArticleImages/28/28881/CSDN_Dev_Image_2004-6-91423250.jpg)</strspeak.length;i++)>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus