日期:2014-05-18  浏览次数:21236 次

用C#能不能做语音识别程序?
如题:C#能不能做到语音识别?
就是某个人说句话,根据这人说话的特征来判断他的身份?
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   { 
   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