DotNetSpeech----文本转wave语音文件

wav操作
引入dll
(DotNetSpeech.dll),引入以后需要选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型”设置为False。不然会提示无法嵌入互操作类型"SpeechLib.SpVoiceClass".请改用适用的接口.

DotNetSpeech.SpeechAudioFormatType.SAFTCCITT_uLaw_11kHzMono
表示音频编码格式为G711U
           var text = "我来测试一下!AB连续字母加空格";
            var path = Path.Combine(@"C:Z_VoiceFile", $"1.wav");
            text = AddKongGeToPlateNo(text).Trim();
            SpVoiceUtil SpVoiceUtil = new SpVoiceUtil();
            SpVoiceUtil.setRate(0);
            SpVoiceUtil.setVolume(100);
            SpVoiceUtil.WreiteToWAV(path, text, DotNetSpeech.SpeechAudioFormatType.SAFTCCITT_uLaw_11kHzMono); //SAFT11kHz16BitMono 生成wav文件



       // 连续字母中加空格
        private static string AddKongGeToPlateNo(string s)
        {
            int length = s.Length;
            string[] letters = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
            for (int i = 0; i < length - 1; i++)
            {
                string str = s.Substring(i, 1);
                string str1 = s.Substring(i + 1, 1);
                if (letters.Contains(str) && letters.Contains(str1))
                {
                    s = s.Substring(0, i + 1) + " " + s.Substring(i + 1);
                    length = length + 1;
                }
            }
            return s;
        }
        

帮助类

 class SpVoiceUtil
    {
        SpVoice voice = new DotNetSpeech.SpVoiceClass();

        public delegate void CallBack(bool b, int InputWordPosition, int InputWordLength);

        /// <summary>
        /// 朗读文本
        /// </summary>
        /// <param name="str">要朗读的文本</param>
        /// <param name="CallBack">回调地址</param>
        /// <returns>返回bool</returns>
        public bool Speak(string str, CallBack CallBack)
        {
            int n = voice.Speak(str, SpeechVoiceSpeakFlags.SVSFlagsAsync);
            Thread thread = new Thread(new ParameterizedThreadStart(Call));
            thread.IsBackground = true;
            thread.Start((Object)CallBack);
            return !(n != 1);
        }


        /// <summary>
        /// 回调函数线程子程序
        /// </summary>
        /// <param name="callBack"></param>
        private void Call(Object callBack)
        {
            int InputWordLength = 0;    //局部_朗读长度
            int InputWordPosition = 0; //局部_朗读位置

            CallBack CallBack = (CallBack)callBack;

            while ((int)voice.Status.RunningState != 1)
            {
                if (InputWordPosition != voice.Status.InputWordPosition || InputWordLength != voice.Status.InputWordLength)
                {
                    InputWordPosition = voice.Status.InputWordPosition;
                    InputWordLength = voice.Status.InputWordLength;

                    //回调                  
                    CallBack(false, InputWordPosition, InputWordLength);
                }
            }
            CallBack(true, InputWordPosition, InputWordLength);
        }

        /// <summary>
        /// 获取语音库
        /// </summary>
        /// <returns>List<string></returns>
        public List<string> getDescription()
        {
            List<string> list = new List<string>();
            DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
            int count = obj.Count;//获取语音库总数
            for (int i = 0; i < count; i++)
            {
                string desc = obj.Item(i).GetDescription(); //遍历语音库
                list.Add(desc);
            }
            return list;
        }

        /// <summary>
        /// 设置当前使用语音库
        /// </summary>
        /// <returns>bool</returns>
        public bool setDescription(string name)
        {
            List<string> list = new List<string>();
            DotNetSpeech.ISpeechObjectTokens obj = voice.GetVoices();
            int count = obj.Count;//获取语音库总数
            bool result = false;
            for (int i = 0; i < count; i++)
            {
                string desc = obj.Item(i).GetDescription(); //遍历语音库
                if (desc.Equals(name))
                {
                    voice.Voice = obj.Item(i);
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 设置语速
        /// </summary>
        /// <param name="n"></param>
        public void setRate(int n)
        {
            voice.Rate = n;
        }

        /// <summary>
        /// 设置声音大小
        /// </summary>
        /// <param name="n"></param>
        public void setVolume(int n)
        {
            voice.Volume = n;
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            voice.Pause();
        }

        /// <summary>
        /// 继续
        /// </summary>
        public void Resume()
        {
            voice.Resume();
        }

        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }


        /// <summary>
        /// 输出WAV
        /// </summary>
        /// <param name="path">保存路径</param>
        /// <param name="str">要转换的文本内容</param>
        /// <returns></returns>
        public bool WreiteToWAV(string path, string str, SpeechAudioFormatType SpAudioType)
        {
            SpeechStreamFileMode SpFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
            SpFileStream SpFileStream = new SpFileStream();
            SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpAudioFormat SpAudio = new DotNetSpeech.SpAudioFormat();
            SpAudio.Type = SpAudioType;
            SpFileStream.Format = SpAudio;
            SpFileStream.Open(path, SpFileMode, false);
            voice.AudioOutputStream = SpFileStream;
            voice.Speak(str, SpFlags);//阅读函数
            voice.WaitUntilDone(Timeout.Infinite);
            SpFileStream.Close();
            return File.Exists(path);
        }
    }
原文地址:https://www.cnblogs.com/macT/p/11771177.html