C# 如何将文本字符串转换为语音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15387212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:37:20  来源:igfitidea点击:

How to convert text string to speech sound

c#.nettext-to-speech

提问by user2110292

I am looking for a way to convert text(string) in ENG to speech(sound) in c#. do anyone know for a way or some open-source lib that can help me with this task?

我正在寻找一种将 ENG 中的文本(字符串)转换为 C# 中的语音(声音)的方法。有没有人知道可以帮助我完成这项任务的方法或一些开源库?

采纳答案by One Man Crew

You can use .NET lib(System.Speech.Synthesis).

您可以使用 .NET 库(System.Speech.Synthesis)。

According to Microsoft:

根据微软的说法:

The System.Speech.Synthesis namespace contains classes that allow you to initialize and configure a speech synthesis engine, create prompts, generate speech, respond to events, and modify voice characteristics. Speech synthesis is often referred to as text-to-speech or TTS.

System.Speech.Synthesis 命名空间包含允许您初始化和配置语音合成引擎、创建提示、生成语音、响应事件和修改语音特征的类。语音合成通常称为文本到语音或 TTS。

A speech synthesizer takes text as input and produces an audio stream as output. Speech synthesis is also referred to as text-to-speech (TTS).

语音合成器将文本作为输入并产生音频流作为输出。语音合成也称为文本到语音 (TTS)。

A synthesizer must perform substantial analysis and processing to accurately convert a string of characters into an audio stream that sounds just as the words would be spoken. The easiest way to imagine how this works is to picture the front end and back end of a two-part system.

合成器必须进行大量的分析和处理,才能将一串字符准确地转换为听起来就像说话的音频流。想象这是如何工作的最简单的方法是描绘一个由两部分组成的系统的前端和后端。

Text Analysis

文本分析

The front end specializes in the analysis of text using natural language rules. It analyzes a string of characters to determine where the words are (which is easy to do in English, but not as easy in languages such as Chinese and Japanese). This front end also figures out grammatical details like functions and parts of speech. For instance, which words are proper nouns, numbers, and so forth; where sentences begin and end; whether a phrase is a question or a statement; and whether a statement is past, present, or future tense.

前端专门使用自然语言规则分析文本。它分析一串字符以确定单词的位置(这在英语中很容易,但在中文和日语等语言中却没有那么容易)。该前端还计算出语法细节,如功能和词性。例如,哪些词是专有名词、数字等;句子开始和结束的地方;一个短语是一个问题还是一个陈述;以及一个陈述是过去时、现在时还是将来时。

All of these elements are critical to the selection of appropriate pronunciations and intonations for words, phrases, and sentences. Consider that in English, a question usually ends with a rising pitch, or that the word "read" is pronounced very differently depending on its tense. Clearly, understanding how a word or phrase is being used is a critical aspect of interpreting text into sound. To further complicate matters, the rules are slightly different for each language. So, as you can imagine, the front end must do some very sophisticated analysis.

所有这些元素对于为单词、短语和句子选择合适的发音和语调都至关重要。考虑到在英语中,问题通常以升高的音调结束,或者“read”这个词的发音因时态而异。显然,了解单词或短语的使用方式是将文本解释为声音的关键方面。更复杂的是,每种语言的规则略有不同。所以,你可以想象,前端必须做一些非常复杂的分析。

Sound Generation

声音生成

The back end has quite a different task. It takes the analysis done by the front end and, through some non-trivial analysis of its own, generates the appropriate sounds for the input text. Older synthesizers (and today's synthesizers with the smallest footprints) generate the individual sounds algorithmically, resulting in a very robotic sound. Modern synthesizers, such as the one in Windows Vista and Windows 7, use a database of sound segments built from hours and hours of recorded speech. The effectiveness of the back end depends on how good it is at selecting the appropriate sound segments for any given input and smoothly splicing them together.

后端有一个完全不同的任务。它接受前端完成的分析,并通过自己的一些重要分析,为输入文本生成适当的声音。较旧的合成器(以及当今具有最小足迹的合成器)通过算法生成单个声音,从而产生非常机械化的声音。现代合成器(例如 Windows Vista 和 Windows 7 中的合成器)使用一个声音片段数据库,该数据库是根据数小时的录音语音构建的。后端的有效性取决于它在为任何给定输入选择合适的声音片段并将它们平滑地拼接在一起方面的效果。

Ready to Use

可以用了

The text-to-speech capabilities described above are built into the Windows Vista and Windows 7 operating systems, allowing applications to easily use this technology. This eliminates the need to create your own speech engines. You can invoke all of this processing with a single function call. See Speak the Contents of a String.

上述文本转语音功能内置于 Windows Vista 和 Windows 7 操作系统中,允许应用程序轻松使用此技术。这消除了创建自己的语音引擎的需要。您可以通过单个函数调用来调用所有这些处理。请参阅朗读字符串的内容。

try this code:

试试这个代码:

using System.Speech.Synthesis;

namespace ConsoleApplication5
{
    class Program
    {

        static void Main(string[] args)
        {
            SpeechSynthesizer synthesizer = new SpeechSynthesizer();
            synthesizer.Volume = 100;  // 0...100
            synthesizer.Rate = -2;     // -10...10

            // Synchronous
            synthesizer.Speak("Hello World");

            // Asynchronous
            synthesizer.SpeakAsync("Hello World");



        }

    }
}

回答by David

This functionality exists in the main Class Library in the System.Speechnamespace. Particularly, look in System.Speech.Synthesis.

此功能存在于System.Speech命名空间的主类库中。特别是,查看System.Speech.Synthesis

Note that you will likely need to add a reference to System.Speech.dll.

请注意,您可能需要添加对System.Speech.dll的引用。

The SpeechSynthesizer class provides access to the functionality of a speech synthesis engine that is installed on the host computer. Installed speech synthesis engines are represented by a voice, for example Microsoft Anna. A SpeechSynthesizer instance initializes to the default voice. To configure a SpeechSynthesizer instance to use one of the other installed voices, call the SelectVoice or SelectVoiceByHints methods. To get information about which voices are installed, use the GetInstalledVoices method.

SpeechSynthesizer 类提供对安装在主机上的语音合成引擎功能的访问。已安装的语音合成引擎由语音表示,例如 Microsoft Anna。SpeechSynthesizer 实例初始化为默认语音。要将 SpeechSynthesizer 实例配置为使用其他已安装的语音之一,请调用 SelectVoice 或 SelectVoiceByHints 方法。要获取有关安装了哪些语音的信息,请使用 GetInstalledVoices 方法。

As with all MSDN documentation, there are code samples to use. The following is from the System.Speech.Synthesis.SpeechSynthesizer class.

与所有 MSDN 文档一样,有代码示例可供使用。以下内容来自 System.Speech.Synthesis.SpeechSynthesizer 类。

using System;
using System.Speech.Synthesis;

namespace SampleSynthesis
{
  class Program
  {
    static void Main(string[] args)
    {

      // Initialize a new instance of the SpeechSynthesizer.
      SpeechSynthesizer synth = new SpeechSynthesizer();

      // Configure the audio output. 
      synth.SetOutputToDefaultAudioDevice();

      // Speak a string.
      synth.Speak("This example demonstrates a basic use of Speech Synthesizer");

      Console.WriteLine();
      Console.WriteLine("Press any key to exit...");
      Console.ReadKey();
    }
  }
}

回答by Pran

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis; // first import this package

    namespace textToSpeech
    {
        public partial class home : Form
        {
            public string s = "pran"; // storing string (pran) to s

            private void home_Load(object sender, EventArgs e)
                {
                    speech(s); // calling the function with a string argument
                }

            private void speech(string args) // defining the function which will accept a string parameter
                {
                    SpeechSynthesizer synthesizer = new SpeechSynthesizer();
                    synthesizer.SelectVoiceByHints(VoiceGender.Male , VoiceAge.Adult); // to change VoiceGender and VoiceAge check out those links below
                    synthesizer.Volume = 100;  // (0 - 100)
                    synthesizer.Rate = 0;     // (-10 - 10)
                    // Synchronous
                    synthesizer.Speak("Now I'm speaking, no other function'll work");
                    // Asynchronous
                    synthesizer.SpeakAsync("Welcome" + args); // here args = pran
                }       
         }
    }
  • It'll be better choice to use "SpeakAsync" because when "Speak" function is executing/running none of other function will work until it finishes it's work (personally recommended)
  • 使用“SpeakAsync”会是更好的选择,因为当“Speak”功能正在执行/运行时,其他功能都不会工作,直到它完成它的工作(个人推荐)

Change VoiceGender
Change VoiceAge

改变 VoiceGender
改变 VoiceAge

回答by user4340666

You can do that using System.Speechlibrary. take a look at this example

您可以使用System.Speech库来做到这一点。看看这个例子

回答by HABJAN

Recently Google published Google Cloud Text To Speech.

最近 Google 发布了Google Cloud Text To Speech

.NETClient version of Google.Cloud.TextToSpeech can be found here: https://github.com/jhabjan/Google.Cloud.TextToSpeech.V1

可以在此处找到 Google.Cloud.TextToSpeech 的.NET客户端版本:https: //github.com/jhabjan/Google.Cloud.TextToSpeech.V1

Nuget: Install-Package JH.Google.Cloud.TextToSpeech.V1

纽吉特: Install-Package JH.Google.Cloud.TextToSpeech.V1

Here is short example how to use the client:

以下是如何使用客户端的简短示例:

GoogleCredential credentials =
    GoogleCredential.FromFile(Path.Combine(Program.AppPath, "jhabjan-test-47a56894d458.json"));

TextToSpeechClient client = TextToSpeechClient.Create(credentials);

SynthesizeSpeechResponse response = client.SynthesizeSpeech(
    new SynthesisInput()
    {
        Text = "Google Cloud Text-to-Speech enables developers to synthesize natural-sounding speech with 32 voices"
    },
    new VoiceSelectionParams()
    {
        LanguageCode = "en-US",
        Name = "en-US-Wavenet-C"
    },
    new AudioConfig()
    {
        AudioEncoding = AudioEncoding.Mp3
    }
);

string speechFile = Path.Combine(Directory.GetCurrentDirectory(), "sample.mp3");

File.WriteAllBytes(speechFile, response.AudioContent);

回答by Ashin

You can do this with the help of System.Speech.Synthesisnamespace. For that, you need to add a reference to System.speech.dllfirst.

您可以在System.Speech.Synthesis命名空间的帮助下完成此操作。为此,您需要先添加对System.speech.dll的引用。

Try this:

尝试这个:

using System.Speech.Synthesis;

namespace TextToSpeech
{
   public partial class Form1 : Form
   {
     SpeechSynthesizer speak;
     public Form1()
     {
        InitializeComponent();
        speak = new SpeechSynthesizer();
     }

     private void button1_Click(object sender, EventArgs e)
     {
        string text="Speak this";
        speak.SpeakAsync(text);
     }
  }
}