在.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表示异步发音。
Posted on 2006-04-01 12:11  李通通  阅读(410)  评论(0编辑  收藏  举报