C#英文语音合成与中文语音识别技术

开发 后端
在.NET中,C#英文语音有较好的实现,但是对中文语音的支持还没有加入进来,我们要想实现中文发音或中文语音识别。

C#英文语音合成与中文语音识别技术的实现,先安装微软的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 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)) ;   
}   
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.

其中SpeechVoiceSpeakFlags.SVSFlagsAsync表示异步发音。

但是,这个方法本身并不知道你给的字符串是什么语言,所以需要我们它这个字符串用什么语言读出。SpVoiceClass 类的Voice 属性就是用来设置语种的,我们可以通过SpVoiceClass 的GetVoices方法得到所有的语种列表,然后在根据参数选择相应的语种,比如设置语种为汉语如下所示:

private void SetChinaVoice()   
{   
voicevoice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ;   
}  
  • 1.
  • 2.
  • 3.
  • 4.

0表示是汉用,1234都表示英语,就是口音不同。

这样,我们就设置了语种,如果结合发音方法,我们就可以设计出一个只发汉语语音的方法

private void SpeakChina(string strSpeak)   
{   
SetChinaVoice() ;   
Speak(strSpeak) ;   
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

只发英语语音的方法也是类似的,上面程序里有。

对于一段中英文混合的语言,我们让程序读出混合语音的方法就是:编程把这段语言的中英文分开,对于中文调用SpeakChina方法,英文调用SpeakEnglishi方法;至于怎样判断一个字符是英文还是中文,我采用的是判断asc码的方法,具体的类方法是通过AnalyseSpeak实现的。

这样,对于一段中英文混合文字,我们只需把它作为参数传递给AnalyseSpeak就可以了,他能够完成中英文的混合发音。

当然,对于发音的暂定、继续、停止等操作,上面也给出了简单的方法调用,很容易明白。

下面简单介绍一下C#英文、中文语音识别的方法:

先把该语音识别的类源代码贴在下面,然后再做说明:

public class SpRecognition   
{   
private static SpRecognition _Instance = null ;   
private SpeechLib.ISpeechRecoGrammar isrg ;   
private SpeechLib.SpSharedRecoContextClass ssrContex =null;   
private System.Windows.Forms.Control cDisplay ;   
private SpRecognition()   
{   
ssrContex = new SpSharedRecoContextClass() ;   
isrg = ssrContex.CreateGrammar(1) ;   
SpeechLib._ISpeechRecoContextEvents_RecognitionEventHandler recHandle =   
new _ISpeechRecoContextEvents_RecognitionEventHandler(ContexRecognition) ;   
ssrContex.Recognition += recHandle ;   
}   
public void BeginRec(Control tbResult)   
{   
isrg.DictationSetState(SpeechRuleState.SGDSActive) ;   
cDisplay = tbResult ;   
}   
public static SpRecognition instance()   
{   
if (_Instance == null)   
_Instance = new SpRecognition() ;   
return _Instance ;   
}   
public void CloseRec()   
{   
isrg.DictationSetState(SpeechRuleState.SGDSInactive) ;   
}   
private void ContexRecognition(int iIndex,object obj,SpeechLib.SpeechRecognitionType type,SpeechLib.ISpeechRecoResult result)   
{   
cDisplay.Text += result.PhraseInfo.GetText(0,-1,true) ;   
}   
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

我们定义了ssrContex 和isrg为语音识别的上下文和语法,通过设置isrg的DictationSetState方法,我们可以开始或结束识别,在上面的程序中是BeginRec和CloseRec方法。cDisplay 是我们用来输出识别结果的地方,为了能够在大部分控件上都可以显示结果,我用了一个Control 类来定义它。当然,每次语音识别后都会触发ISpeechRecoContextEvents_RecognitionEventHandler 事件,我们定义了一个这样的方法ContexRecognition来响应事件,并且在这个方法里输出识别结果。

这样,C#英文和中文语音处理的一些最基本的问题就有了一个简单的解决方法,当然,这种方法还有很多不完善的地方,希望大家多提出批评意见,共同提高。

【编辑推荐】

  1. 如何用C#和ADO.NET访问
  2. 浅析C# Switch语句
  3. C#验证输入方法详解
  4. 简单介绍C# 匿名方法
  5. C# FileSystemWatcher对象
责任编辑:彭凡 来源: CSDN
相关推荐

2016-02-17 10:39:18

语音识别语音合成语音交互

2022-12-05 07:17:14

人工智能语音合成

2024-11-11 08:50:24

2022-12-01 07:03:22

语音识别人工智能技术

2021-11-17 10:37:39

语音识别技术人工智能

2017-09-06 10:51:22

Facebook

2024-03-11 11:32:38

语音识别

2017-08-30 09:20:47

深度学习语音合成Siri

2022-09-15 09:59:55

火山语音语音建模

2022-11-03 16:31:08

语音智能语音识别

2022-08-29 10:57:09

语音识苹果频率

2019-10-29 08:00:00

语音识别

2023-02-28 12:12:21

语音识别技术解码器

2020-11-12 17:24:21

微软智能语音

2022-10-19 12:47:05

深度学习语音合成

2011-05-31 16:38:47

Android 实现语音

2015-11-03 16:31:28

搜狗

2015-05-11 11:35:58

2017-03-16 16:57:56

点赞
收藏

51CTO技术栈公众号