中英文語音合成與中文語音識別技術在c#中的應用(一)
2024-07-21 02:24:47
供稿:網友
在.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<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方法,我們發音主要就是給他傳遞一個字符串,它負責讀出該字符串,如下所示。
private void speak(string strspeack)
{
try
{
voice.speak(strspeack,speechvoicespeakflags.svsflagsasync) ;
}
catch(exception err)
{
throw(new exception("發生一個錯誤:"+err.message)) ;
}
}
其中speechvoicespeakflags.svsflagsasync表示異步發音。
中國最大的web開發資源網站及技術社區,