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

原创
小哥 2年前 (2023-05-25) 阅读数 8 #大杂烩

文本转语音


这比较简单,只是引用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; ///

/// 返回一个SpeechSynthesizer对象 /// /// private SpeechSynthesizer GetSpeechSynthesizerInstance() { if (synth == null) { synth = new SpeechSynthesizer(); } return synth; } /// /// 播放 /// public void Play(string text) { Thread thread = new Thread(new ParameterizedThreadStart(SaveMp3)); thread.Start(text); } /// /// 保存语音文件 /// /// 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(); ///

// 语音转文本 /// /// 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(G_SpeechRecognized); SRE.LoadGrammar(G); SRE.RecognizeAsync(RecognizeMode.Multiple); //<=======异步呼叫识别引擎,允许多重识别(否则程序只会响应你的一句话) } catch (Exception ex) { string s = ex.ToString(); } }

    /// 
    /// 确定语音并将其转换为需要输出的文本
    /// 
    /// 
    /// 
    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);

    }

转自:https://www.cnblogs.com/-maomao/p/6861447.html

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除