zoukankan      html  css  js  c++  java
  • 我的工具:文本转音频文件

    需求:

    根据英语单词,语句生成wav文件。

    根据文本,生成wav文件。

    主要代码:

             其实就是一个类,调用的是System.Speech.Synthesis.SpeechSynthesizer类,我们重新包装一下这个类,方便我们自己调用,代码如下:

    /// <summary>
    /// 文本转语音并保存wav和MP3文件
    /// </summary>
    public class MySpeech
    {
        VoiceClass _setViceClass;
        public MySpeech(VoiceClass setViceClass)
        {
            _setViceClass = setViceClass;
        }
        private SpeechSynthesizer synth = null;
        /// <summary>
        /// 返回一个SpeechSynthesizer对象
        /// </summary>
        /// <returns></returns>
        private SpeechSynthesizer GetSpeechSynthesizerInstance()
        {
            if (synth == null)
            {
                synth = new SpeechSynthesizer();
            }
            return synth;
        }
        /// <summary>
        /// 开始朗读 放在线程中
        /// </summary>
        /// <param name="VoiceObject"></param>
        private void RingVoice(object VoiceObject)
        {
            try
            {
                VoiceClass voiceClass = (VoiceClass)VoiceObject;
                synth = GetSpeechSynthesizerInstance();
                //synth.SelectVoice(voiceClass.VoiceName);
                if (_setViceClass.Rate > 0)
                    synth.Rate = _setViceClass.Rate;
                if (_setViceClass.Volume > 0)
                    synth.Volume = _setViceClass.Volume;

                synth.SpeakAsync(voiceClass.VoiceText);
            }
            catch (Exception er)
            {
                Console.WriteLine(er.ToString());
            }
        }
        /// <summary>
        ///  播放
        /// </summary>
        public void Play()
        {
            Thread thread = new Thread(RingVoice);
            thread.Start(_setViceClass);
        }
        public void SaveMp3()
        {
            try
            {
                synth = GetSpeechSynthesizerInstance();

                //synth.SelectVoice(_setViceClass.VoiceName);
                if (_setViceClass.Rate > 0)
                    synth.Rate = _setViceClass.Rate;
                if (_setViceClass.Volume > 0)
                    synth.Volume = _setViceClass.Volume;

                string str = System.AppDomain.CurrentDomain.BaseDirectory + "\" + _setViceClass.VoiceText + ".wav";
               
                synth.SetOutputToWaveFile(str);
                synth.Speak(_setViceClass.VoiceText);
                synth.SetOutputToNull();
            }
            catch (Exception er)
            {
                Console.WriteLine(er.ToString());
            }
        }
    }

    /// <summary>
    ///  设置:语速 音量 播放人  播放文本
    /// </summary>
    public class VoiceClass
    {
        /// <summary>
        /// 播放人

       ///  该操作使用 Windows 2000 和 Windows XP 的旧式 Sam 语音,以及新 Anna 和 Windows Vista 的 Microsoft® Lili 语音。
        /// Microsoft Sam
        /// Microsoft Anna
        /// Microsoft Lili
        /// </summary>
        public string VoiceName { get; set; }
        /// <summary>
        /// 语速
        /// -10 到 10
        /// </summary>
        public int Rate { get; set; }
        /// <summary>
        /// 音量
        /// 0 到 100
        /// </summary>
        public int Volume { get; set; }
        /// <summary>
        /// 播放文本
        /// </summary>
        public string VoiceText { get; set; }
    }


     

    主要调用:

    image

    image

    效果图:

    目录:

    image

    请先在English.txt中输入你要转换的文本,在打开exe文件如下图:

    image

    获取:

    下载地址:https://onedrive.live.com/?cid=FC5F01C33230B327&id=FC5F01C33230B327%21361

  • 相关阅读:
    【Python专题】python入门之代码编辑器和输入输出
    【Python专题】python入门之一:搭建开发环境
    制作你的专属BB8机器人
    《专题:C++语法基础》篇五:循环程序设计
    《专题:C++语法基础》篇四:C++分支程序设计
    《专题:C++语法基础》篇三:C++中的算术运算、赋值运算
    数据库SQL Server2012笔记(八)——Statement与PreparedStatement的区别,JDBC方式操作数据库
    数据库SQL Server2012笔记(七)——java 程序操作sql server
    数据库SQL Server2012笔记(六)——修改表、数据的备份和恢复
    数据库SQL Server2012笔记(三)——表的复杂查询
  • 原文地址:https://www.cnblogs.com/luomingui/p/4070653.html
Copyright © 2011-2022 走看看