当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

在C#中实现语音合成与识别技术

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:45:35

:2010-12-15 10:52:51

如果我们自己能编写一个中文发音和中文语音识别的软件,那该有多酷!本文就介绍了在C#中借助于微软公司开发的一个SASDK软件来实现这个功能。

首先,我们需要安装微软的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可以选择安装支持的语言。  安装好以后,我们就可以开始进行语音程序的开发了。

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

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

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

 

[1] [2]  下一页


在C#中实现语音合成与识别技术