因為挑戰(zhàn)杯的項目和語音有關(guān),所以在網(wǎng)上找了一些這方面的資料。發(fā)現(xiàn)微軟的sapi是一個不錯的東西。
下面的程序大部分都是從msdn中得到的,自己都還沒有把sdk的文檔看明白:(
有興趣的可以參見:http://www.microsoft.com/china/community/program/originalarticles/techdoc/cnspeech.mspx
如果找不到tlbimp命令,可以參見:http://blog.csdn.net/wayne92/archive/2006/04/08/655420.aspx
程序如下:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.threading;
using dotnetspeech; //這個是要用tlbimp命令生成的。
/**//*ps:發(fā)現(xiàn)這樣更簡單:項目->添加引用->com 選擇microsoft speech object library,就添加了speechlib(和生成的dotnetspeech一樣)*/
namespace 中文語音應(yīng)用程序
...{
/**//// <summary>
/// form1 的摘要說明。
/// </summary>
public class form1 : system.windows.forms.form
...{
private system.windows.forms.groupbox groupbox1;
private system.windows.forms.button buttonsynthes;
private system.windows.forms.button buttonttstowave;
private system.windows.forms.textbox textbox1;
private radiobutton radiobuttonchina;
private radiobutton radiobuttonenglish;
/**//// <summary>
/// 必需的設(shè)計器變量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
...{
//
// windows 窗體設(shè)計器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
}
/**//// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.dispose();
}
}
base.dispose( disposing );
}
windows 窗體設(shè)計器生成的代碼#region windows 窗體設(shè)計器生成的代碼
/**//// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void initializecomponent()
...{
this.groupbox1 = new system.windows.forms.groupbox();
this.textbox1 = new system.windows.forms.textbox();
this.buttonsynthes = new system.windows.forms.button();
this.buttonttstowave = new system.windows.forms.button();
this.radiobuttonchina = new system.windows.forms.radiobutton();
this.radiobuttonenglish = new system.windows.forms.radiobutton();
this.groupbox1.suspendlayout();
this.suspendlayout();
//
// groupbox1
//
this.groupbox1.controls.add(this.textbox1);
this.groupbox1.location = new system.drawing.point(32, 16);
this.groupbox1.name = "groupbox1";
this.groupbox1.size = new system.drawing.size(360, 200);
this.groupbox1.tabindex = 1;
this.groupbox1.tabstop = false;
this.groupbox1.text = "請輸入要合成的文本";
//
// textbox1
//
this.textbox1.location = new system.drawing.point(8, 24);
this.textbox1.multiline = true;
this.textbox1.name = "textbox1";
this.textbox1.size = new system.drawing.size(336, 160);
this.textbox1.tabindex = 0;
//
// buttonsynthes
//
this.buttonsynthes.location = new system.drawing.point(40, 232);
this.buttonsynthes.name = "buttonsynthes";
this.buttonsynthes.size = new system.drawing.size(75, 23);
this.buttonsynthes.tabindex = 2;
this.buttonsynthes.text = "朗讀";
this.buttonsynthes.click += new system.eventhandler(this.buttonsynthes_click);
//
// buttonttstowave
//
this.buttonttstowave.location = new system.drawing.point(256, 232);
this.buttonttstowave.name = "buttonttstowave";
this.buttonttstowave.size = new system.drawing.size(120, 23);
this.buttonttstowave.tabindex = 3;
this.buttonttstowave.text = "生成聲音文件(wav)";
this.buttonttstowave.click += new system.eventhandler(this.buttonttstowave_click);
//
// radiobuttonchina
//
this.radiobuttonchina.autosize = true;
this.radiobuttonchina.checked = true;
this.radiobuttonchina.location = new system.drawing.point(137, 232);
this.radiobuttonchina.name = "radiobuttonchina";
this.radiobuttonchina.size = new system.drawing.size(47, 16);
this.radiobuttonchina.tabindex = 4;
this.radiobuttonchina.tabstop = true;
this.radiobuttonchina.text = "中文 ";
this.radiobuttonchina.usevisualstylebackcolor = true;
//
// radiobuttonenglish
//
this.radiobuttonenglish.autosize = true;
this.radiobuttonenglish.location = new system.drawing.point(137, 254);
this.radiobuttonenglish.name = "radiobuttonenglish";
this.radiobuttonenglish.size = new system.drawing.size(47, 16);
this.radiobuttonenglish.tabindex = 5;
this.radiobuttonenglish.text = "英文";
this.radiobuttonenglish.usevisualstylebackcolor = true;
//
// form1
//
this.acceptbutton = this.buttonsynthes;
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(416, 278);
this.controls.add(this.radiobuttonenglish);
this.controls.add(this.radiobuttonchina);
this.controls.add(this.buttonttstowave);
this.controls.add(this.buttonsynthes);
this.controls.add(this.groupbox1);
this.name = "form1";
this.text = "中文tts in .net framework";
this.load += new system.eventhandler(this.form1_load);
this.groupbox1.resumelayout(false);
this.groupbox1.performlayout();
this.resumelayout(false);
this.performlayout();
}
#endregion
/**//// <summary>
/// 應(yīng)用程序的主入口點。
/// </summary>
[stathread]
static void main()
...{
application.run(new form1());
}
private void form1_load(object sender, system.eventargs e)
...{
}
private void buttonsynthes_click(object sender, system.eventargs e)
...{
try
...{
speechvoicespeakflags spflags = speechvoicespeakflags.svsflagsasync;
spvoice voice = new spvoiceclass();
if ( radiobuttonenglish.checked == true )
voice.voice = voice.getvoices("language=409",string.empty).item(0);//英文
else
voice.voice = voice.getvoices("language=804",string.empty).item(0);//中文
voice.speak(this.textbox1.text,spflags); //朗讀
}
catch(exception er )
...{
messagebox.show("an error occured!","speechapp", messageboxbuttons.ok, messageboxicon.error);
}
}
private void buttonttstowave_click(object sender, system.eventargs e)
...{
try
...{
speechvoicespeakflags spflags = speechvoicespeakflags.svsflagsasync;
spvoice voice = new spvoiceclass();
savefiledialog sfd = new savefiledialog();
sfd.filter = "all files (*.*)|*.*|wav files (*.wav)|*.wav";
sfd.title = "save to a wave file";
sfd.filterindex = 2;
if( sfd.showdialog() == dialogresult.ok)
...{
speechstreamfilemode spfilemode = speechstreamfilemode.ssfmcreateforwrite;
spfilestream spfilestream = new spfilestreamclass();
spfilestream.open(sfd.filename, spfilemode, false);
voice.audiooutputstream = spfilestream;
voice.speak(this.textbox1.text,spflags);
voice.waituntildone(timeout.infinite);
spfilestream.close();
}
}
catch(exception er)
...{
messagebox.show("an error occured!","speechapp", messageboxbuttons.ok, messageboxicon.error);
}
}
}
}
新聞熱點
疑難解答
圖片精選