语音合成(使用.net3.0中的System.Speech.Synthesis心得)
语音合成
1、使用语音合成
SpeechSynthesizer synth = new SpeechSynthesizer();
//获取本机上所安装的所有的Voice的名称
string voicestring = "";
foreach (InstalledVoice iv in synth.GetInstalledVoices())
{
voicestring += iv.VoiceInfo.Name + ",";
}
//根据Voice的name属性确定要使用的Voice
synth.SelectVoice("VW Lily");
//根据文字内容合成语音
synth.Speak(this.textBox1.Text);
synth.Speak("中华人民共和国湖北省");
2、构建SSML
PromptBuilder myPrompt = new PromptBuilder();
//Start the main speaking style
PromptStyle mainStyle = new PromptStyle();
mainStyle.Rate = PromptRate.Medium;
mainStyle.Volume = PromptVolume.Loud;
myPrompt.StartStyle(mainStyle);
//Alert the listener
myPrompt.AppendAudio(new Uri(
"file://c:""windows""media""notify.wav"), "Attention!"); //AppendAudio 功能使 WAV 文件与输出结合,
//如果未找到 WAV 文件,可以使用一个等效文本文件,即第二个参数
myPrompt.AppendText("Here are some important messages.");
//Here's the first important message
myPrompt.AppendTextWithPronunciation("WinFX", "wɪnɛfɛks"); //AppendTextWithPronunciation 功能允许您指定单词的正确发音
myPrompt.AppendText("is a great platform.");
//And the second one
myPrompt.AppendTextWithHint("ASP", SayAs.SpellOut); // AppendTextWithHint 功能为缩写词作标记
//SayAs枚举值举例: SayAs.NumberOrdinal SayAs.DayMonth SayAs.SpellOut SayAs.Telephone SayAs.Text SayAs.Time24等
myPrompt.AppendText(
"is an acronym for Active Server Pages. Whereas an ASP is a snake.");
myPrompt.AppendBreak();
//Let's emphasise how important these messages are
PromptStyle interimStyle = new PromptStyle();
interimStyle.Emphasis = PromptEmphasis.Strong;
myPrompt.StartStyle(interimStyle);
myPrompt.AppendText("Please remember these two things.");
myPrompt.EndStyle();
//Then we can revert to the main speaking style
myPrompt.AppendBreak();
myPrompt.AppendText("Thank you");
myPrompt.EndStyle();
//Now let's get the synthesizer to render this message
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SelectVoice("VW Lily");
synth.SpeakAsync(myPrompt); //与Speak不同的异步方法
3、将构建的SSML保存在SSML文件中
using (StreamWriter promptWriter = new StreamWriter("c:""prompt.ssml"))
{
promptWriter.Write(myPrompt.ToXml());
}
4、将构建的语音保存为一个WAV文件中
//若前面使用了SpeakAsync方法,则不能输出为WAV文件。
//必须等到语音播完后才能输出
synth.SetOutputToWaveFile("c:""message.wav");
synth.Speak(myPrompt);
synth.SetOutputToNull();
5、根据SSML文件中保存的信息还原为语音
SpeechSynthesizer synth = new SpeechSynthesizer();
PromptBuilder savedPrompt = new PromptBuilder();
savedPrompt.AppendSsml("c:""prompt.ssml");
synth.SelectVoice("VW Lily");
synth.Speak(savedPrompt);
6、通过语音进度事件高光显示正在阅读的文本位置
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SpeakProgress += new EventHandler<SpeakProgressEventArgs>(synth_SpeakProgress);
void synth_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
this.textBox1.HideSelection = false;
this.textBox1.Select(e.CharacterPosition,e.CharacterCount );
}