【开发实例】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 ...
随机推荐
- new 动态分配数组空间 .xml
pre{ line-height:1; color:#3c3c3c; background-color:#d2c39b; font-size:16px;}.sysFunc{color:#627cf6; ...
- matlab 之基础使用
dir(xxx,'.jpg') :读取某文件所有jpg格式的图片,并获取图片属性信息 size(x,1) :获得x矩阵多少行 cell(): 申明数组 注释选定代码的快捷操作:Ctrl+R
- HTML 学习进度备忘
书签:”HTML 高级教程“及后面的内容尚未学习,另外跳过的内容有待跟进 __________________ 学习资源:W3School. 开始时间:2013.11.20 简述:此网址做为学习教程相 ...
- strtok和strtok_r
1.strtok()函数的用法 函数原型:char *strtok(char *s, const char *delim); Function:分解字符串为一组字符串.s为要分解的字符串,delim为 ...
- 封装cookie.js、EventUtil.js、
最近学习了javascript,封装好的东西看起来舒服,以备需要的时候拉出来,jquery对javascript做了很好的封装!以后会多用jquery多些 var CookieUtil = { get ...
- C# html互转mht
using System;using System.Runtime.InteropServices;using System.Text;using System.IO;namespace HTMLCo ...
- Transact-SQL
Transact-SQL(又称T-SQL),是在Microsoft SQL Server和Sybase SQL Server上的ANSI SQL实现,与Oracle的PL/SQL性质相近(不只是实现A ...
- Message Forwarding
[Preprocess] 在使用forwarding机制前,会先经历2个步骤,只有当这2个步骤均失败的情况下,才会激活forwarding. 1.+(BOOL)resolveInstanceMetho ...
- vimdiff
[vimdiff] 启动方法 首先保证系统中的diff命令是可用的.Vim的diff模式是依赖于diff命令的.Vimdiff的基本用法就是: # vimdiff FILE_LEFT FILE_RIG ...
- HDU3874Necklace(树状数组+离线操作)
此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...