【开发实例】C#调用SAPI实现语音合成的两种方法
- public class Speach
- {
- private static Speach _Instance = null ;
- private SpeechLib.SpVoiceClass voice =null; //SAPI5.1
- private SpeechLib.SpVoice voice = null;//SAPI 5.4
- 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<strSpeak.Length;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)) ;
- }
- }
第二种使用.NET类库和系统API的代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Speech.Synthesis;
- using System.Speech;
- namespace StudyBeta
- {
- public class SRead
- {
- public SpeechSynthesizer synth; //语音合成对象
- public SRead()
- {
- synth = new SpeechSynthesizer();
- }
- public SRead(int m, int n)
- {
- //使用 synth 设置朗读音量 [范围 0 ~ 100]
- synth.Volume = m;
- //使用 synth 设置朗读频率 [范围 -10 ~ 10]
- synth.Rate = n;
- }
- public void SpeakChina(string ggg)
- {
- //SpVoice Voice = new SpVoice();
- synth.SelectVoice("Microsoft Lili");
- //Voice.Speak(ggg, SpFlags);
- synth.SpeakAsync(ggg);
- //String speechPeople = synth.Voice;
- //使用 synth 设置朗读音量 [范围 0 ~ 100]
- // synth.Volume = 80;
- //使用 synth 设置朗读频率 [范围 -10 ~ 10]
- // synth.Rate = 0;
- //使用synth 合成 wav 音频文件:
- //synth.SetOutputToWaveFile(string path);
- }
- public void SpeakEnglish(string ggg)
- {
- //SpVoice Voice = new SpVoice();
- synth.SelectVoice("VW Julie");
- synth.Speak(ggg); //ggg为要合成的内容
- }
- public int m
- {
- get
- {
- return synth.Volume;
- }
- set
- {
- synth.Volume = value;
- }
- }
- public int n
- {
- get
- {
- return synth.Rate;
- }
- set
- {
- synth.Rate = value;
- }
- }
- }
【开发实例】C#调用SAPI实现语音合成的两种方法的更多相关文章
- 织梦首页、列表页调用文章body内容的两种方法
http://blog.csdn.net/langyu1021/article/details/52261411 关于首页.列表页调用文章body内容的两种方法,具体方法如下: 第一种方法: {ded ...
- ant中调用外部ant任务的两种方法
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- ios开发——实用技术OC篇》倒计时实现的两种方法
倒计时实现的两种方法 timeFireMethod函数,timeFireMethod进行倒计时的一些操作,完成时把timer给invalidate掉就ok了,代码如下: secondsCountDow ...
- 【Win 10 应用开发】将墨迹保存到图像的两种方法
IT界最近这几年,各种乱七八糟的东西不断出现,其中能用在实际工作与生活中的,大概也就那么几个.Web 前端也冒出各种框架,这就为那些喜欢乱用框架的公司提供了很好的机会,于是造成很多项目体积越来越庞大, ...
- [ARM-Linux开发]Linux下加载.ko驱动模块的两种方法:insmod与modprobe
假设要加载的驱动程序模块名为SHT21.ko 加载驱动模块 方法一: 进入SHT21.ko驱动模块文件所在的目录,然后直接 insmod SHT21.ko 即可 方法二: 将SHT21.ko文 ...
- C# 调用WCF服务的两种方法
项目简介 之前领导布置一个做单点登录的功能给我,实际上就是医院想做一个统一的平台来实现在这个统一的平台登录后不需要在His.Emr.Lis等系统一个个登录,直接可以登录到对应的系统,然后进行相应的操作 ...
- [转]Delphi调用cmd的两种方法
delphi调用cmd的两种方法vars:string;begins:='cmd.exe /c '+edit1.Text+' >c:\1.txt';winexec(pchar(s),sw_hid ...
- C++调用DLL有两种方法——静态调用和动态调用
C++调用DLL有两种方法——静态调用和动态调用 标签: dllc++winapinullc 2011-09-09 09:49 11609人阅读 评论(0) 收藏 举报 分类: cpp(30) [ ...
- 转载]PhpCms V9调用指定栏目子栏目文章的两种方法
PhpCms V9调用指定栏目子栏目文章的两种方法 第一种.直接写子栏目id ,用cat in {pc:get sql="SELECT * from v9_news where status ...
随机推荐
- 网页元素定位神器之Xpath详解
摘要: 经常在工作中会使用到XPath的相关知识,但每次总会在一些关键的地方不记得或不太清楚,所以免不了每次总要查一些零碎的知识,感觉即很烦又浪费时间,所以对XPath归纳及总结一下. ... ...
- javascript防止SQL注入
<SCRIPT language="javascript">function Check(theform){ if (theform.UserName.value== ...
- ctype库试运行
from ctypes import * msvcrt=cdll.msvcrt message_string="Hello world!\n" msvcrt.wprintf(&qu ...
- qqq
http://blog.sina.com.cn/s/blog_6f0774010100wawd.html http://www.2cto.com/kf/201302/189135.html http: ...
- Hadoop2.2 federnation联盟的搭建
联盟实际上是一个单独的集群,集群里面包含很多的NameService共享同样的DataNode,同一份数据只上传一份,block块相同,一个集群中删除,另一个集群中还是存在的.同一份数据只是在name ...
- iOS9对SDK的影响(iOS9适配必看)
1.大部分社交平台接口不支持https协议. 问题描述:在iOS9下,系统默认会拦截对http协议接口的访问,因此无法获取http协议接口的数据.对ShareSDK来说,具体表现可能是,无法授权. ...
- ECMAScript 6 入门学习笔记(持续更新)
1.let命令(作用同var) ES6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. { let a = 10; var b = 1; } ...
- <转>Linux环境进程间通信(三)
原文链接:http://www.ibm.com/developerworks/cn/linux/l-ipc/part3/index.html 原文内容: 消息队列(也叫做报文队列)能够克服早期unix ...
- utf8 和 UTF-8 的区别
只有在MySQL中可以使用“utf-8”的别名“utf8”,但是在其他地方一律使用大写“UTF-8”.
- 配置超级用户口令(Cisco IOS系统)
命令一共有以下几条: (1)enable secret enable secret 0 密码明文 enable secret 5 密码密文(MD5加密) (2)enable password enab ...