当前位置: 代码网 > it编程>编程语言>Asp.net > 基于C#实现文字转语音功能

基于C#实现文字转语音功能

2025年02月08日 Asp.net 我要评论
好多年前自己研究的了,今天突然翻出来了,记录下简单说明在c#中,集成了一个语音对象speechsynthesizer。speechsynthesizer可以根据填入的文字内容自动解析成语音并使用系统扬

好多年前自己研究的了,今天突然翻出来了,记录下

简单说明

在c#中,集成了一个语音对象speechsynthesizer。

speechsynthesizer可以根据填入的文字内容自动解析成语音并使用系统扬声器进行语音播报。

程序说明

支持手动输入文字,转换为语音。

可以调整语速。

对kv、sf6、.等关键词进行转义(kv-千伏;sf6-六氟化硫;.-点)

支持生成语音文件(这个没有对关键字进行转义)

效果图

语音试听实现

        /// <summary>
        /// 试听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnlisten_click(object sender, eventargs e)
        {
            string text = textbox1.text;
            rate = this.cbxrate.selectedindex;

            if (text.trim().length == 0)
            {
                messagebox.show("不能阅读空内容!", "错误提示");
                return;
            }

            if (btnlisten.text == "语音试听")
            {
                speech = new speechsynthesizer();
                new thread(speak).start();
                btnlisten.text = "停止试听";
            }
            else if (btnlisten.text == "停止试听")
            {
                speech.speakasynccancelall();//停止阅读
                btnlisten.text = "语音试听";
            }
        }

        private void speak()
        {
            promptbuilder builder = getbuiltprompt(this.textbox1.text.tostring().trim());
            speech.rate = rate;
            speech.selectvoice("microsoft huihui desktop");//设置播音员(中文)
            speech.volume = value;
            speech.speakasync(builder);//语音阅读方法
            speech.speakcompleted += speech_speakcompleted;//绑定事件
        }
        
        private promptbuilder getbuiltprompt(string keywords)
        {
            //from http://msdn.microsoft.com/msdnmag/issues/06/01/speechinwindowsvista/ 
            //this prompt is quite complicated
            //so i'm going to build it first, and then render it.
            promptbuilder myprompt = new promptbuilder();

            //start the main speaking style
            promptstyle mainstyle = new promptstyle();
            mainstyle.rate = promptrate.notset;
            mainstyle.volume = promptvolume.medium;
            // mainstyle.emphasis=promptemphasis.reduced;
            myprompt.startstyle(mainstyle);
            convert(myprompt, keywords);
            myprompt.endstyle();
            return myprompt;
        }

 		/// <summary>
        /// 转义关键字
        /// </summary>
        private void convert(promptbuilder myprompt, string keywords)
        {
            //判断是否存在要转换的关键字
            dictionary<int, string> convertcharacter = new dictionary<int, string>();
            var kv_index = keywords.indexof("kv");
            var sf6_index = keywords.indexof("sf6");
            var point_index = keywords.indexof(".");
            if (kv_index > -1)
            {
                convertcharacter.add(kv_index, "kv");
            }
            if (sf6_index > -1)
            {
                convertcharacter.add(sf6_index, "sf6");
            }
            if (point_index > -1)
            {
                convertcharacter.add(point_index, ".");
            }
            convertarray(convertcharacter, myprompt, keywords);
        }
        
		/// <summary>
        /// 转义关键字
        /// </summary>
        private void convertarray(dictionary<int, string> convertcharacter, promptbuilder myprompt, string devicename)
        { 
            string r = @"[0-9]+";
            regex reg = new regex(r, regexoptions.ignorecase | regexoptions.singleline);//2秒后超时
            matchcollection mc = reg.matches(devicename);//设定要查找的字符串 

            //按关键字升序排列
            convertcharacter = convertcharacter.orderby(t => t.key).todictionary(p => p.key, o => o.value);
            int haslen = 0;//记录已经截取的长度
            int splitnum = convertcharacter.count;//要拆分的段数

            //针对有需要转换的地方做特殊处理
            if (splitnum > 0)
            {
                for (var i = 0; i < splitnum; i++)
                {
                    var startindex = convertcharacter.elementat(i).key;//关键字开始的索引
                    var keyvalue = convertcharacter.elementat(i).value.tolower();//关键字
                    var keylen = keyvalue.length;//关键字对应的长度 

                    //先把关键字前面的追加到标题
                    myprompt.appendtext(devicename.substring(haslen, startindex - haslen)); 
                    haslen = startindex + keylen;//留做截取不是关键字的长度 
                    //判断关键字的类型,确定转换方式
                    if (keyvalue == "kv")
                    {
                        myprompt.appendssmlmarkup("<say-as interpret-as = \"kv\">千伏</say-as>");
                    }
                    else if (keyvalue == "sf6")
                    {
                        myprompt.appendtextwithalias("sf6", "六氟化硫");
                    }
                    else if (keyvalue == "-")
                    {
                        myprompt.appendtextwithhint("-", sayas.numbercardinal);
                    }
                    else if (keyvalue == ".")
                    {
                        myprompt.appendtextwithalias(".", "点");
                    }  
                    //如果循环到最后一个关键字,再把最后一个关键字后边的数追加上
                    if (i == splitnum - 1)
                    {
                        myprompt.appendtext(devicename.substring(haslen));
                    }
                } 
            }
            else
            {
                myprompt.appendtext(devicename);
            }
        }

  		/// <summary>
        /// 语音阅读完成触发此事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void speech_speakcompleted(object sender, speakcompletedeventargs e)
        {
            //改变按钮文本值
            changebtntext changebtntext = changetext;
            this.btnlisten.begininvoke(changebtntext);
        }
        
        /// <summary>
        /// 委托方法,改变按钮值
        /// </summary>
        public void changetext()
        {
            btnlisten.text = "语音试听";
        }

以上就是基于c#实现文字转语音功能的详细内容,更多关于c#文字转语音的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com