我们都知道现在的语音合成TTS是可以通过微软的SAPI实现的,好处我就不多说了,方便而已,因为在微软的操作系统里面就自带了这个玩意,主要的方式有两种:  1、使用COM组件技术,不管是C++,C#,Delphi都能玩的转,开发出来的东西在XP和WIN7都能跑。(要引入SpeechLib,好像在项目上点引用,然后选到系统COM吧,好久没弄,记不清楚了)  2、使用WIN7的windows api,其实最终还是调用了SAPI,所以开发出来的东西就只能在WIN7上面跑。  其实不管是哪一种,都是调用SAPI,可能后一种代码比较简单,使用已经安装的TTS引擎,现在一般用NeoSpeech,这个就不解释了,太强大了这个发音。。。  COM组件技术:

  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. private void SetChinaVoice()
  17. {
  18. voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(0) ;
  19. }
  20. private void SetEnglishVoice()
  21. {
  22. voice.Voice = voice.GetVoices(string.Empty,string.Empty).Item(1) ;
  23. }
  24. private void SpeakChina(string strSpeak)
  25. {
  26. SetChinaVoice() ;
  27. Speak(strSpeak) ;
  28. }
  29. private void SpeakEnglishi(string strSpeak)
  30. {
  31. SetEnglishVoice() ;
  32. Speak(strSpeak) ;
  33. }
  34. public void AnalyseSpeak(string strSpeak)
  35. {
  36. int iCbeg = 0 ;
  37. int iEbeg = 0 ;
  38. bool IsChina = true ;
  39. for(int i=0;i<strSpeak.Length;i++)
  40. {
  41. char chr = strSpeak[i] ;
  42. if (IsChina)
  43. {
  44. if (chr<=122&&chr>=65)
  45. {
  46. int iLen = i - iCbeg ;
  47. string strValue = 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. int iLen = i - iEbeg ;
  58. string strValue = strSpeak.Substring(iEbeg,iLen) ;
  59. this.SpeakEnglishi(strValue) ;
  60. iCbeg = i ;
  61. IsChina = true ;
  62. }
  63. }
  64. }//end for
  65. if (IsChina)
  66. {
  67. int iLen = strSpeak.Length - iCbeg ;
  68. string strValue = strSpeak.Substring(iCbeg,iLen) ;
  69. SpeakChina(strValue) ;
  70. }
  71. else
  72. {
  73. int iLen = strSpeak.Length - iEbeg ;
  74. string strValue = strSpeak.Substring(iEbeg,iLen) ;
  75. SpeakEnglishi(strValue) ;
  76. }
  77. }
  78. private void BuildSpeach()
  79. {
  80. if (voice == null)
  81. voice = new SpVoiceClass() ;
  82. }
  83. public int Volume
  84. {
  85. get
  86. {
  87. return voice.Volume ;
  88. }
  89. set
  90. {
  91. voice.SetVolume((ushort)(value)) ;
  92. }
  93. }
  94. public int Rate
  95. {
  96. get
  97. {
  98. return voice.Rate ;
  99. }
  100. set
  101. {
  102. voice.SetRate(value) ;
  103. }
  104. }
  105. private void Speak(string strSpeack)
  106. {
  107. try
  108. {
  109. voice.Speak(strSpeack,SpeechVoiceSpeakFlags.SVSFlagsAsync) ;
  110. }
  111. catch(Exception err)
  112. {
  113. throw(new Exception("发生一个错误:"+err.Message)) ;
  114. }
  115. }
  116. public void Stop()
  117. {
  118. voice.Speak(string.Empty,SpeechLib.SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak) ;
  119. }
  120. public void Pause()
  121. {
  122. voice.Pause() ;
  123. }
  124. public void Continue()
  125. {
  126. voice.Resume() ;
  127. }
  128. }//end class

在 private SpeechLib.SpVoiceClass voice =null;这里,我们定义个一个用来发音的类,并且在第一次调用该类时,对它用BuildSpeach方法进行了初始化。 
我们还定义了两个属性Volume和Rate,能够设置音量和语速。 
我们知道,SpVoiceClass 有一个Speak方法,我们发音主要就是给他传递一个字符串,它负责读出该字符串,如下所示。

  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的代码如下:

  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. namespace StudyBeta
  8. {
  9. public class SRead
  10. {
  11. public SpeechSynthesizer synth; //语音合成对象
  12. public SRead()
  13. {
  14. synth = new SpeechSynthesizer();
  15. }
  16. public SRead(int m, int n)
  17. {
  18. //使用 synth 设置朗读音量 [范围 0 ~ 100]
  19. synth.Volume = m;
  20. //使用 synth 设置朗读频率 [范围 -10 ~ 10]
  21. synth.Rate = n;
  22. }
  23. public void SpeakChina(string ggg)
  24. {
  25. //SpVoice Voice = new SpVoice();
  26. synth.SelectVoice("Microsoft Lili");
  27. //Voice.Speak(ggg, SpFlags);
  28. synth.SpeakAsync(ggg);
  29. //String speechPeople = synth.Voice;
  30. //使用 synth 设置朗读音量 [范围 0 ~ 100]
  31. // synth.Volume = 80;
  32. //使用 synth 设置朗读频率 [范围 -10 ~ 10]
  33. //      synth.Rate = 0;
  34. //使用synth 合成 wav 音频文件:
  35. //synth.SetOutputToWaveFile(string path);
  36. }
  37. public void SpeakEnglish(string ggg)
  38. {
  39. //SpVoice Voice = new SpVoice();
  40. synth.SelectVoice("VW Julie");
  41. synth.Speak(ggg); //ggg为要合成的内容
  42. }
  43. public int m
  44. {
  45. get
  46. {
  47. return synth.Volume;
  48. }
  49. set
  50. {
  51. synth.Volume = value;
  52. }
  53. }
  54. public int n
  55. {
  56. get
  57. {
  58. return synth.Rate;
  59. }
  60. set
  61. {
  62. synth.Rate = value;
  63. }
  64. }
  65. }
 

【开发实例】C#调用SAPI实现语音合成的两种方法的更多相关文章

  1. 织梦首页、列表页调用文章body内容的两种方法

    http://blog.csdn.net/langyu1021/article/details/52261411 关于首页.列表页调用文章body内容的两种方法,具体方法如下: 第一种方法: {ded ...

  2. ant中调用外部ant任务的两种方法

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  3. ios开发——实用技术OC篇》倒计时实现的两种方法

    倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...

  4. 【Win 10 应用开发】将墨迹保存到图像的两种方法

    IT界最近这几年,各种乱七八糟的东西不断出现,其中能用在实际工作与生活中的,大概也就那么几个.Web 前端也冒出各种框架,这就为那些喜欢乱用框架的公司提供了很好的机会,于是造成很多项目体积越来越庞大, ...

  5. [ARM-Linux开发]Linux下加载.ko驱动模块的两种方法:insmod与modprobe

    假设要加载的驱动程序模块名为SHT21.ko 加载驱动模块 方法一:  进入SHT21.ko驱动模块文件所在的目录,然后直接  insmod SHT21.ko  即可 方法二:  将SHT21.ko文 ...

  6. C# 调用WCF服务的两种方法

    项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...

  7. [转]Delphi调用cmd的两种方法

    delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hid ...

  8. C++调用DLL有两种方法——静态调用和动态调用

    C++调用DLL有两种方法——静态调用和动态调用 标签: dllc++winapinullc 2011-09-09 09:49 11609人阅读 评论(0) 收藏 举报  分类: cpp(30)  [ ...

  9. 转载]PhpCms V9调用指定栏目子栏目文章的两种方法

    PhpCms V9调用指定栏目子栏目文章的两种方法 第一种.直接写子栏目id ,用cat in {pc:get sql="SELECT * from v9_news where status ...

随机推荐

  1. 笔记《Java程序性能优化 让你的Java程序更快、更稳定》 第二章 设计调优

    2.1 善用设计模式 23 (1) 1. 设计模式好处: 2.1.1 单例模式 23 (6) 1. 单例模式是一种对象创建模式,用于产生一个对象的具体实例,它可以确保系统中一个类只产生一个实例: 2. ...

  2. IntelliJ IDEA 13 Keygen

    import java.math.BigInteger; import java.util.Date; import java.util.Random; import java.util.zip.CR ...

  3. 内存溢出(heap corruption detected:)

    今天又遇到了上次出现的bug,然后百度了一下,想起来这是内存溢出的毛病,故记录下来! 出现的问题就是这样: heap corruption detected: after normal block(# ...

  4. Mysql explain分析SQL语句之字段属性说明

    在 explain的帮助下,您就知道什么时候该给表添加索引,以使用索引来查找记录从而让select 运行更快.如果由于不恰当使用索引而引起一些问题的话,可以运行 analyze table来更新该表的 ...

  5. 项目常用jquery/easyui函数小结

    #项目常用jquery/easyui函数小结 ##背景 项目中经常需要使用到一些功能,封装.重构.整理后形成代码沉淀,在此进行分享 ##代码 ```javascript /** * @author g ...

  6. 在JSP中使用CKEditor网页编辑器

    为了在我的一个项目使用CKEditor网页编辑器,我开始了寻找应用之法. 我下载了ckeditor_4.3.3_standard和ckeditor-java-core-3.5.3. 之前的版本和现在版 ...

  7. 移动端rem布局

    手机页面——分辨率特别乱: 1.定宽320px——优点:简单,缺点:不能适应 2.百分比——优点:能适应各种分辨率,缺点:太麻烦 3.rem——优点:方便.适应各种分辨率(首先定义一个“根大小”htm ...

  8. 第二百二十九天 how can I 坚持

    百度-让人更容易的获取信息,腾讯-让人更方便的交流,阿里-让人更方便的消费,每个公司都有自己的使命,每个公司的使命都是围绕着人. 创新-其实应该是在每个人的内心深处都或多或少有一些新的想法,但是什么是 ...

  9. E:Package 'Vim' has no installation candidate问题解决

    问题描述: root@zhouls-virtual-machine:~# apt-get install vimReading package lists... DoneBuilding depend ...

  10. 您的IP不在有效范围 ip:port为 [10.15.22.15]