首页 > 学院 > 开发设计 > 正文

【开发实例】C#调用SAPI实现语音合成的两种方法

2019-11-17 03:04:34
字体:
来源:转载
供稿:网友
【开发实例】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. publicclassSpeach
  2. {
  3. PRivatestaticSpeach_Instance=null;
  4. privateSpeechLib.SpVoiceClassvoice=null;//SAPI5.1
  5. privateSpeechLib.SpVoicevoice=null;//SAPI5.4
  6. privateSpeach()
  7. {
  8. BuildSpeach();
  9. }
  10. publicstaticSpeachinstance()
  11. {
  12. if(_Instance==null)
  13. _Instance=newSpeach();
  14. return_Instance;
  15. }
  16. privatevoidSetChinaVoice()
  17. {
  18. voice.Voice=voice.GetVoices(string.Empty,string.Empty).Item(0);
  19. }
  20. privatevoidSetEnglishVoice()
  21. {
  22. voice.Voice=voice.GetVoices(string.Empty,string.Empty).Item(1);
  23. }
  24. privatevoidSpeakChina(stringstrSpeak)
  25. {
  26. SetChinaVoice();
  27. Speak(strSpeak);
  28. }
  29. privatevoidSpeakEnglishi(stringstrSpeak)
  30. {
  31. SetEnglishVoice();
  32. Speak(strSpeak);
  33. }
  34. publicvoidAnalyseSpeak(stringstrSpeak)
  35. {
  36. intiCbeg=0;
  37. intiEbeg=0;
  38. boolIsChina=true;
  39. for(inti=0;i<strSpeak.Length;i++)
  40. {
  41. charchr=strSpeak[i];
  42. if(IsChina)
  43. {
  44. if(chr<=122&&chr>=65)
  45. {
  46. intiLen=i-iCbeg;
  47. stringstrValue=strSpeak.Substring(iCbeg,iLen);
  48. SpeakChina(strValue);
  49. iEbeg=i;
  50. IsChina=false;
  51. }
  52. }
  53. else
  54. {
  55. if(chr>122||chr<65)
  56. {
  57. intiLen=i-iEbeg;
  58. stringstrValue=strSpeak.Substring(iEbeg,iLen);
  59. this.SpeakEnglishi(strValue);
  60. iCbeg=i;
  61. IsChina=true;
  62. }
  63. }
  64. }//endfor
  65. if(IsChina)
  66. {
  67. intiLen=strSpeak.Length-iCbeg;
  68. stringstrValue=strSpeak.Substring(iCbeg,iLen);
  69. SpeakChina(strValue);
  70. }
  71. else
  72. {
  73. intiLen=strSpeak.Length-iEbeg;
  74. stringstrValue=strSpeak.Substring(iEbeg,iLen);
  75. SpeakEnglishi(strValue);
  76. }
  77. }
  78. privatevoidBuildSpeach()
  79. {
  80. if(voice==null)
  81. voice=newSpVoiceClass();
  82. }
  83. publicintVolume
  84. {
  85. get
  86. {
  87. returnvoice.Volume;
  88. }
  89. set
  90. {
  91. voice.SetVolume((ushort)(value));
  92. }
  93. }
  94. publicintRate
  95. {
  96. get
  97. {
  98. returnvoice.Rate;
  99. }
  100. set
  101. {
  102. voice.SetRate(value);
  103. }
  104. }
  105. privatevoidSpeak(stringstrSpeack)
  106. {
  107. try
  108. {
  109. voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync);
  110. }
  111. catch(Exceptionerr)
  112. {
  113. throw(newException("发生一个错误:"+err.Message));
  114. }
  115. }
  116. publicvoidStop()
  117. {
  118. voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
  119. }
  120. publicvoidPause()
  121. {
  122. voice.Pause();
  123. }
  124. publicvoidContinue()
  125. {
  126. voice.Resume();
  127. }
  128. }//endclass
在 private SpeechLib.SpVoiceClass voice =null;这里,我们定义个一个用来发音的类,并且在第一次调用该类时,对它用BuildSpeach方法进行了初始化。我们还定义了两个属性Volume和Rate,能够设置音量和语速。我们知道,SpVoiceClass 有一个Speak方法,我们发音主要就是给他传递一个字符串,它负责读出该字符串,如下所示。C#代码Flashvars" value="clipboard=private%20
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表