zoukankan      html  css  js  c++  java
  • 【开发实例】C#调用SAPI实现语音合成的两种方法

    我们都知道现在的语音合成TTS是可以通过微软的SAPI实现的,好处我就不多说了,方便而已,因为在微软的操作系统里面就自带了这个玩意,主要的方式有两种:  1、使用COM组件技术,不管是C++,C#,Delphi都能玩的转,开发出来的东西在XP和WIN7都能跑。(要引入SpeechLib,好像在项目上点引用,然后选到系统COM吧,好久没弄,记不清楚了)  2、使用WIN7的windows api,其实最终还是调用了SAPI,所以开发出来的东西就只能在WIN7上面跑。  其实不管是哪一种,都是调用SAPI,可能后一种代码比较简单,使用已经安装的TTS引擎,现在一般用NeoSpeech,这个就不解释了,太强大了这个发音。。。  COM组件技术: 
    C#代码  收藏代码
    1. public class Speach   
    2. {   
    3. private static Speach _Instance = null ;   
    4. private SpeechLib.SpVoiceClass voice =null; //SAPI5.1  
    5. private SpeechLib.SpVoice voice = null;//SAPI 5.4  
    6. private Speach()   
    7. {   
    8. BuildSpeach() ;   
    9. }   
    10. public static Speach instance()   
    11. {   
    12. if (_Instance == null)   
    13. _Instance = new Speach() ;   
    14. return _Instance ;   
    15. }  
    16.   
    17. private void SetChinaVoice()   
    18. {   
    19. voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ;   
    20. }   
    21.   
    22. private void SetEnglishVoice()   
    23. {   
    24. voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ;   
    25. }   
    26.   
    27. private void SpeakChina(string strSpeak)   
    28. {   
    29. SetChinaVoice() ;   
    30. Speak(strSpeak) ;   
    31. }   
    32.   
    33. private void SpeakEnglishi(string strSpeak)   
    34. {   
    35. SetEnglishVoice() ;   
    36. Speak(strSpeak) ;   
    37. }   
    38.   
    39.   
    40.   
    41. public void AnalyseSpeak(string strSpeak)   
    42. {   
    43. int iCbeg = 0 ;   
    44. int iEbeg = 0 ;   
    45. bool IsChina = true ;   
    46. for(int i=0;i<strSpeak.Length;i++)   
    47. {   
    48. char chr = strSpeak[i] ;   
    49. if (IsChina)   
    50. {   
    51. if (chr<=122&&chr>=65)   
    52. {   
    53. int iLen = i - iCbeg ;   
    54. string strValue = strSpeak.Substring(iCbeg,iLen) ;   
    55. SpeakChina(strValue) ;   
    56. iEbeg = i ;   
    57. IsChina = false ;   
    58. }   
    59. }   
    60. else   
    61. {   
    62. if (chr>122||chr<65)   
    63. {   
    64. int iLen = i - iEbeg ;   
    65. string strValue = strSpeak.Substring(iEbeg,iLen) ;   
    66. this.SpeakEnglishi(strValue) ;   
    67. iCbeg = i ;   
    68. IsChina = true ;   
    69. }   
    70. }   
    71. }//end for   
    72. if (IsChina)   
    73. {   
    74. int iLen = strSpeak.Length - iCbeg ;   
    75. string strValue = strSpeak.Substring(iCbeg,iLen) ;   
    76. SpeakChina(strValue) ;   
    77. }   
    78. else   
    79. {   
    80. int iLen = strSpeak.Length - iEbeg ;   
    81. string strValue = strSpeak.Substring(iEbeg,iLen) ;   
    82. SpeakEnglishi(strValue) ;   
    83. }   
    84. }   
    85.   
    86. private void BuildSpeach()   
    87. {   
    88. if (voice == null)   
    89. voice = new SpVoiceClass() ;   
    90. }  
    91.   
    92. public int Volume   
    93. {   
    94. get   
    95. {   
    96. return voice.Volume ;   
    97. }   
    98.   
    99. set   
    100. {   
    101. voice.SetVolume((ushort)(value)) ;   
    102. }   
    103. }   
    104.   
    105. public int Rate   
    106. {   
    107. get   
    108. {   
    109. return voice.Rate ;   
    110. }   
    111. set   
    112. {   
    113. voice.SetRate(value) ;   
    114. }   
    115. }   
    116.   
    117. private void Speak(string strSpeack)   
    118. {   
    119. try   
    120. {   
    121. voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;   
    122. }   
    123. catch(Exception err)   
    124. {   
    125. throw(new Exception("发生一个错误:"+err.Message)) ;   
    126. }   
    127. }   
    128.   
    129. public void Stop()   
    130. {   
    131. voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ;   
    132. }   
    133.   
    134. public void Pause()   
    135.   
    136. {   
    137. voice.Pause() ;   
    138. }   
    139.   
    140. public void Continue()   
    141. {   
    142. voice.Resume() ;   
    143. }   
    144. }//end class   
    在 private SpeechLib.SpVoiceClass voice =null;这里,我们定义个一个用来发音的类,并且在第一次调用该类时,对它用BuildSpeach方法进行了初始化。 
    我们还定义了两个属性Volume和Rate,能够设置音量和语速。 
    我们知道,SpVoiceClass 有一个Speak方法,我们发音主要就是给他传递一个字符串,它负责读出该字符串,如下所示。 
    C#代码  收藏代码
    1. private void Speak(string strSpeack)   
    2. {   
    3. try   
    4. {   
    5. voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;   
    6. }   
    7. catch(Exception err)   
    8. {   
    9. throw(new Exception("发生一个错误:"+err.Message)) ;   
    10. }  
    11. }   
    第二种使用.NET类库和系统API的代码如下: 
    C#代码  收藏代码
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Speech.Synthesis;  
    6. using System.Speech;  
    7.   
    8. namespace StudyBeta  
    9. {  
    10.     public class SRead  
    11.     {  
    12.         public SpeechSynthesizer synth; //语音合成对象  
    13.         public SRead()  
    14.         {  
    15.             synth = new SpeechSynthesizer();  
    16.         }  
    17.         public SRead(int m, int n)  
    18.         {  
    19.             //使用 synth 设置朗读音量 [范围 0 ~ 100]  
    20.             synth.Volume = m;  
    21.             //使用 synth 设置朗读频率 [范围 -10 ~ 10]  
    22.             synth.Rate = n;  
    23.         }  
    24.         public void SpeakChina(string ggg)  
    25.         {  
    26.             //SpVoice Voice = new SpVoice();  
    27.             synth.SelectVoice("Microsoft Lili");  
    28.             //Voice.Speak(ggg, SpFlags);  
    29.             synth.SpeakAsync(ggg);  
    30.             //String speechPeople = synth.Voice;  
    31.             //使用 synth 设置朗读音量 [范围 0 ~ 100]  
    32.             // synth.Volume = 80;  
    33.             //使用 synth 设置朗读频率 [范围 -10 ~ 10]  
    34.             //      synth.Rate = 0;  
    35.             //使用synth 合成 wav 音频文件:  
    36.             //synth.SetOutputToWaveFile(string path);  
    37.         }  
    38.         public void SpeakEnglish(string ggg)  
    39.         {  
    40.             //SpVoice Voice = new SpVoice();  
    41.             synth.SelectVoice("VW Julie");  
    42.             synth.Speak(ggg); //ggg为要合成的内容  
    43.         }  
    44.         public int m  
    45.         {  
    46.             get  
    47.             {  
    48.                 return synth.Volume;  
    49.             }  
    50.             set  
    51.             {  
    52.                 synth.Volume = value;  
    53.             }  
    54.         }  
    55.         public int n  
    56.         {  
    57.             get  
    58.             {  
    59.                 return synth.Rate;  
    60.             }  
    61.             set  
    62.             {  
    63.                 synth.Rate = value;  
    64.             }  
    65.         }  
    66. }  
     
  • 相关阅读:
    关于++i和i++的左值、右值问题
    运算符优先级
    计算机中的数及其编码
    递归函数
    PHP读取excel(4)
    替换元素节点replaceChild()
    子结点childNodes
    插入节点insertBefore()
    创建节点createElement
    插入节点appendChild()
  • 原文地址:https://www.cnblogs.com/gc2013/p/3816915.html
Copyright © 2011-2022 走看看