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通过如下图所示添加到引用中

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

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

  1. public class Speach   
  2. {   
  3. private static Speach _Instance = null ;   
  4. private SpeechLib.SpVoiceClass voice =null;   
  5. private Speach()   
  6. {   
  7. BuildSpeach() ;   
  8. }   
  9. public static Speach instance()   
  10. {   
  11. if (_Instance == null)   
  12. _Instance = new Speach() ;   
  13. return _Instance ;   
  14. }   
  15. private void SetChinaVoice()   
  16. {   
  17. voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ;   
  18. }   
  19. private void SetEnglishVoice()   
  20. {   
  21. voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ;   
  22. }   
  23. private void SpeakChina(string strSpeak)   
  24. {   
  25. SetChinaVoice() ;   
  26. Speak(strSpeak) ;   
  27. }   
  28. private void SpeakEnglishi(string strSpeak)   
  29. {   
  30. SetEnglishVoice() ;   
  31. Speak(strSpeak) ;   
  32. }   
  33. public void AnalyseSpeak(string strSpeak)   
  34. {   
  35. int iCbeg = 0 ;   
  36. int iEbeg = 0 ;   
  37. bool IsChina = true ;   
  38. for(int i=0;i
  39. {   
  40. char chr = strSpeak[i] ;   
  41. if (IsChina)   
  42. {   
  43. if (chr<=122&&chr>=65)   
  44. {   
  45. int iLen = i - iCbeg ;   
  46. string strValue = strSpeak.Substring(iCbeg,iLen) ;   
  47. SpeakChina(strValue) ;   
  48. iEbeg = i ;   
  49. IsChina = false ;   
  50. }   
  51. }   
  52. else   
  53. {   
  54. if (chr>122||chr<65)   
  55. {   
  56. int iLen = i - iEbeg ;   
  57. string strValue = strSpeak.Substring(iEbeg,iLen) ;   
  58. this.SpeakEnglishi(strValue) ;   
  59. iCbeg = i ;   
  60. IsChina = true ;   
  61. }   
  62. }   
  63. }//end for   
  64. if (IsChina)   
  65. {   
  66. int iLen = strSpeak.Length - iCbeg ;   
  67. string strValue = strSpeak.Substring(iCbeg,iLen) ;   
  68. SpeakChina(strValue) ;   
  69. }   
  70. else   
  71. {   
  72. int iLen = strSpeak.Length - iEbeg ;   
  73. string strValue = strSpeak.Substring(iEbeg,iLen) ;   
  74. SpeakEnglishi(strValue) ;   
  75. }   
  76. }   
  77. private void BuildSpeach()   
  78. {   
  79. if (voice == null)   
  80. voice = new SpVoiceClass() ;   
  81. }   
  82. public int Volume   
  83. {   
  84. get   
  85. {   
  86. return voice.Volume ;   
  87. }   
  88. set   
  89. {   
  90. voice.SetVolume((ushort)(value)) ;   
  91. }   
  92. }   
  93. public int Rate   
  94. {   
  95. get   
  96. {   
  97. return voice.Rate ;   
  98. }   
  99. set   
  100. {   
  101. voice.SetRate(value) ;   
  102. }   
  103. }   
  104. private void Speak(string strSpeack)   
  105. {   
  106. try   
  107. {   
  108. voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;   
  109. }   
  110. catch(Exception err)   
  111. {   
  112. throw(new Exception("发生一个错误:"+err.Message)) ;   
  113. }   
  114. }   
  115. public void Stop()   
  116. {   
  117. voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ;   
  118. }   
  119. public void Pause()   
  120. {   
  121. voice.Pause() ;   
  122. }   
  123. public void Continue()   
  124. {   
  125. voice.Resume() ;   
  126. }   
  127. }//end class   
  128. 在 private SpeechLib.SpVoiceClass voice =null;这里,我们定义个一个用来发音的类,并且在第一次调用该类时,对它用BuildSpeach方法进行了初始化。   
  129. 我们还定义了两个属性Volume和Rate,能够设置音量和语速。   
  130. 我们知道,SpVoiceClass 有一个Speak方法,我们发音主要就是给他传递一个字符串,它负责读出该字符串,如下所示。   
  131.  
  132. private void Speak(string strSpeack)   
  133. {   
  134. try   
  135. {   
  136. voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;   
  137. }   
  138. catch(Exception err)   
  139. {   
  140. throw(new Exception("发生一个错误:"+err.Message)) ;   
  141. }   
  142. }  

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

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

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

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

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

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

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

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

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

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

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

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

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

人工智能语音合成

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

语音识别

2022-09-15 09:59:55

火山语音语音建模

2011-05-31 16:38:47

Android 实现语音

2022-10-19 12:47:05

深度学习语音合成

2019-10-29 08:00:00

语音识别

2023-02-28 12:12:21

语音识别技术解码器

2022-11-03 16:31:08

语音智能语音识别

2017-08-30 09:20:47

深度学习语音合成Siri

2020-11-12 17:24:21

微软智能语音

2022-08-29 10:57:09

语音识苹果频率

2015-11-03 16:31:28

搜狗

2017-03-16 16:57:56

2011-01-18 11:52:25

Linux语音识别

2021-05-06 11:13:06

人工智能语音识别
点赞
收藏

51CTO技术栈公众号