求教:C# 音频合成思路及实现方式 c#音频 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我们都知道现在的语音合成TTS是可以通过微软的SAPI实现的,好处我就不多说了,方便而已,因为在微软的操作系统里面就自带了这个玩意,主要的方式有两种:1、使用COM组件技术,不管是C++,C#,Delphi都能玩的转,开发出来的东西在XP和WIN7都能跑。(要引入SpeechLib,好像在项目上点引用,然后选到系统COM吧,好久没弄,记不清楚了)2、使用WIN7的windows api,其实最终还是调用了SAPI,所以开发出来的东西就只能在WIN7上面跑。其实不管是哪一种,都是调用SAPI,可能后一种代码比较简单,使用已经安装的TTS引擎,现在一般用NeoSpeech,这个就不解释了,太强大了这个发音COM组件技术: 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; } } } 楼上可能会错意了,不是TTS。 你可以看看Directx,好像有个托管的叫manager directx。 求大神帮忙,做一个 c# 购车还贷计算器应用程序 急用! 水晶报表问题,在线等 写个冰封王座的平台,指个方向吧 如何将datagridview中某个单元格的值清空c# [编程] 引用 怎样判断两个图片是否相等?即同一张图片 视图设计器打不开 如何在不刷新的情况下更改ListView 请教 C# 如何实现曲线的放大? [菜鸟提问]:请教各位高手,现在学C#还是学java好。 如何将世界杯嵌入博客 C# 接口继承
2、使用WIN7的windows api,其实最终还是调用了SAPI,所以开发出来的东西就只能在WIN7上面跑。其实不管是哪一种,都是调用SAPI,可能后一种代码比较简单,使用已经安装的TTS引擎,现在一般用NeoSpeech,这个就不解释了,太强大了这个发音COM组件技术:
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;
}
}
}