• .NET 语音转文字 文字转语音


    文字转语音

    这个比较简单只要引用COM中的 Microsoft Speech objcet Library

    using SpeechLib;
         public ActionResult speak(string speechSounds)
            {
                SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
                SpVoice spVoice = new SpVoice();
                spVoice.Rate = spVoice.Rate - 5;
                if (spVoice.Volume < 100)
                {
                    spVoice.Volume = spVoice.Volume + 10;
                }
    
                if (spVoice.Volume > 9)
                {
                    spVoice.Volume = spVoice.Volume - 10;
                }
                spVoice.Speak(speechSounds, SpFlags);
                return Content("成功");
            }

    文字生成语音文件

    引用程序集中的 System.Speech

    using System.Speech.Synthesis;
    private SpeechSynthesizer synth = null;
            /// <summary>
            /// 返回一个SpeechSynthesizer对象
            /// </summary>
            /// <returns></returns>
            private SpeechSynthesizer GetSpeechSynthesizerInstance()
            {
                if (synth == null)
                {
                    synth = new SpeechSynthesizer();
                }
                return synth;
            }
            /// <summary>
            ///  播放
            /// </summary>
            public void Play(string text)
            {
                Thread thread = new Thread(new ParameterizedThreadStart(SaveMp3));
                thread.Start(text);
            }
            /// <summary>
            /// 保存语音文件
            /// </summary>
            /// <param name="text"></param>
            public void SaveMp3(object text)
            {
                    synth = GetSpeechSynthesizerInstance();
                    string spText = text.ToString();
                    synth.Rate = 1;
                    synth.Volume = 100;
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
                    string str = "C:\Users\admin1\Desktop\新建文件夹\" + filename + ".wav";
                    synth.SetOutputToWaveFile(str);
                    synth.Speak(spText);
                    synth.SetOutputToNull();
                    //调用语音转文字
                    //Thread threadVoice = new Thread(VoiceToText);
                    //threadVoice.Start(str);
            }

    语音转文本

    using System.Speech.Recognition;
            private SpeechRecognitionEngine SRE = new SpeechRecognitionEngine();
            /// <summary>
            //  语音转文本
            /// </summary>
            /// <param name="str"></param>
            private void VoiceToText(object str)
            {
                try
                {
                    string filepath = str.ToString(); ;
                    SRE.SetInputToWaveFile(filepath);         //<=======默认的语音输入设备,你可以设定为去识别一个WAV文件。
                    GrammarBuilder GB = new GrammarBuilder();
                    //需要判断的文本(相当于语音库)
                    GB.Append(new Choices(new string[] { "时间", "电话", "短信", "定位", "天气", "帮助" }));
                    Grammar G = new Grammar(GB);
                    G.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(G_SpeechRecognized);
                    SRE.LoadGrammar(G);
                    SRE.RecognizeAsync(RecognizeMode.Multiple); //<=======异步调用识别引擎,允许多次识别(否则程序只响应你的一句话)
                }
                catch (Exception ex)
                {
                    string s = ex.ToString();
                }
            }
    
    
            /// <summary>
            /// 判断语音并转化为需要输出的文本
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void G_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            {
                string result = e.Result.Text;
                string RetSpeck = string.Empty;
                switch (result)
                {
                    case "时间":
                        RetSpeck = "你输入了时间";
                        break;
                    case "电话":
                        RetSpeck = "你输入了电话";
                        break;
                    case "短信":
                        RetSpeck = "你输入了短信";
                        break;
                    case "定位":
                        RetSpeck = "你输入了定位";
                        break;
                    case "天气":
                        RetSpeck = "你输入了天气";
                        break;
                    case "帮助":
                        RetSpeck = "你输入了帮助";
                        break;
                }
                speak(RetSpeck);
            }
  • 相关阅读:
    人脸识别常用的性能评价指标
    【计算机视觉】seetaFace
    【error】'isnan' was not declared in this scope
    【opencv基础】图像的几何变换
    [c++]base64编解码 and image
    【leetcode】232-Implement Queue using Stacks
    【leetcode】231-power-of-two
    C++中vector容器的常用操作方法实例总结
    【leetcode】226-invert-binary-tree
    第3章 文件I/O(2)_文件I/O系统调用及文件描述符
  • 原文地址:https://www.cnblogs.com/-maomao/p/6861447.html
Copyright © 2020-2023  润新知