一个在线文字转语音工具

一个在线文字转语音工具

因为参加比赛过程中要频繁使用文字转语音的功能,遂写了这个工具,暂时先用着。

工具基于百度API , 目前只支持 1K 以内的文字转语音。

后续版本拟增加讯飞接口,批量转换,语音识别,在线更新等功能,在在线更新功能上线之前,以本文为更新源。

运行截图:

一个在线文字转语音工具_第1张图片

代码:

public partial class Form1 : Form
    {
        string APP_ID = "BAIDU_TTS_APPID";
        string API_KEY = "BAIDU_TTS_APIKEY";
        string SECRET_KEY = "BAIDU_TTS_SECRET_KEY";
        string storeLocation = null;
        string fileName = null;
        string toConvert;
        string macAddress;
        Baidu.Aip.Speech.Tts client;
        int voiceSpeed = 5;
        int voiceTune = 5;
        int volum = 5 ;
        int character = 0 ;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            client = new Baidu.Aip.Speech.Tts(API_KEY, SECRET_KEY);
            client.Timeout = 60000;  // 修改超时时间
            macAddress = GetMacAddressByNetworkInformation();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            shellTextbox.AppendText(System.DateTime.Now.ToString() + "开始转换\n");
            if(storeLocation == null)
            {
                shellTextbox.AppendText(System.DateTime.Now.ToString() + "转换失败,请检查音频文件存贮位置\n");
                return;
            }
            else if(textBox1.Text == null)
            {
                shellTextbox.AppendText(System.DateTime.Now.ToString() + "转换失败,请检查需要转换的文字\n");
                return;
            }

            toConvert = textBox1.Text;
            Tts();

        }

        public void Tts()
        {
            // 可选参数
            var option = new Dictionary<string, object>()
            {
                {"spd", voiceSpeed}, // 语速
                {"vol", volum}, // 音量
                {"per", character},  // 发音人,4:情感度丫丫童声
                { "cuid" , macAddress},
                { "pit" , voiceTune}
             };
            var result = client.Synthesis(toConvert, option);

            if (result.ErrorCode == 0)  // 或 result.Success
            {
                File.WriteAllBytes( storeLocation + fileName + ".mp3", result.Data);
            }
            else
            {
                shellTextbox.AppendText(System.DateTime.Now.ToString() + "转换失败,ErrorCode :" +
                    result.ErrorCode.ToString() + "\n");

            }


        }

        public string GetMacAddressByNetworkInformation()
        {
            string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
            string macAddress = string.Empty;
            try
            {
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                    if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
                        && adapter.GetPhysicalAddress().ToString().Length != 0)
                    {
                        string fRegistryKey = key + adapter.Id + "\\Connection";
                        RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
                        if (rk != null)
                        {
                            string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                            int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                            if (fPnpInstanceID.Length > 3 &&
                                fPnpInstanceID.Substring(0, 3) == "PCI")
                            {
                                macAddress = adapter.GetPhysicalAddress().ToString();
                                for (int i = 1; i < 6; i++)
                                {
                                    macAddress = macAddress.Insert(3 * i - 1, ":");
                                }
                                break;
                            }
                        }

                    }
                }
            }
            catch (Exception ex)
            {
                //这里写异常的处理

                return "could_not_get_mac_addr";
            }
            return macAddress;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(textBox2.Text == "请在此处填写音频文件存储位置")
            {
                shellTextbox.AppendText(System.DateTime.Now.ToString() + "请检查音频文件存贮位置\n");
                return;
            }

            if(textBox3.Text == "请在此处填写音频文件名(无.mp3)")
            {
                shellTextbox.AppendText(System.DateTime.Now.ToString() + "请检查音频文件名\n");
                return;
            }

            storeLocation = textBox2.Text;
            fileName = textBox3.Text;

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            voiceSpeed = comboBox1.SelectedIndex;

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            voiceTune = comboBox1.SelectedIndex;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            volum = comboBox1.SelectedIndex;
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            character = comboBox1.SelectedIndex;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://blog.csdn.net/Apheal"); //在浏览器中打开链接
        }

        private void button3_Click(object sender, EventArgs e)
        {
            shellTextbox.AppendText(System.DateTime.Now.ToString() + "目前无法在线更新,请到CSDN查看\n");
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(" http://puretool.picp.io/");
        }
    }

下载链接(成品+源码S):

CSDN:https://download.csdn.net/download/apheal/10627138

蓝奏云:https://www.lanzous.com/i1qi6of

你可能感兴趣的:(其他,C#)