using System;
using System.Speech.Synthesis;
namespace Speech_Synthesis
{
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
SpeechSynthesizer synthesizer = new SpeechSynthesizer();
PromptBuilder promptBuilder = new PromptBuilder();
promptBuilder.AppendTextWithHint("尹成", SayAs.SpellOut);
promptBuilder.AppendText("尹成大哥毕业于山东大学.");
promptBuilder.AppendBreak(new TimeSpan(0, 0, 2));
promptBuilder.AppendText("尹成大哥是谁");
promptBuilder.AppendTextWithHint(DateTime.Now.ToString("hh:mm"), SayAs.Time);
// Pause for 2 seconds
promptBuilder.AppendBreak(new TimeSpan(0, 0, 2));
promptBuilder.AppendText("尹成大哥硕士毕业于中科院?");
promptBuilder.StartVoice("Microsoft Sam");
promptBuilder.AppendTextWithHint("queue", SayAs.SpellOut);
promptBuilder.EndVoice();
promptBuilder.AppendText("Do it faster!");
promptBuilder.StartVoice("Microsoft Sam");
promptBuilder.StartStyle(new PromptStyle(PromptRate.ExtraFast));
promptBuilder.AppendTextWithHint("queue", SayAs.SpellOut);
promptBuilder.EndStyle();
promptBuilder.EndVoice();
// Speak all the content in the PromptBuilder
synthesizer.SpeakAsync(promptBuilder);
}
}
}
--------------------------------------------------------------
语音合成
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 );
}