首页 > 开发 > 综合 > 正文

c#应用(1):语音合成

2024-07-21 02:26:58
字体:
来源:转载
供稿:网友

因为挑战杯的项目和语音有关,所以在网上找了一些这方面的资料。发现微软的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:发现这样更简单:项目->添加引用->com 选择microsoft speech object library,就添加了speechlib(和生成的dotnetspeech一样)*/

namespace 中文语音应用程序
...{
    /**//// <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>
        /// 必需的设计器变量。
        /// </summary>
        private system.componentmodel.container components = null;

        public form1()
        ...{
            //
            // windows 窗体设计器支持所必需的
            //
            initializecomponent();

            //
            // todo: 在 initializecomponent 调用后添加任何构造函数代码
            //
        }

        /**//// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        protected override void dispose( bool disposing )
        ...{
            if( disposing )
            ...{
                if (components != null)
                ...{
                    components.dispose();
                }
            }
            base.dispose( disposing );
        }

        windows 窗体设计器生成的代码#region windows 窗体设计器生成的代码
        /**//// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </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>
        /// 应用程序的主入口点。
        /// </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);
            }
        }
    }
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表