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

use*_*292 44 .net c# text-to-speech

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

One*_*rew 54

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

根据微软的说法:

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

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

合成器必须执行实质性的分析和处理,以准确地将一串字符转换为听起来就像说出的单词一样的音频流.想象它是如何工作的最简单的方法是描绘两部分系统的前端和后端.

文本分析

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

所有这些元素对于选择单词,短语和句子的适当发音和语调至关重要.考虑一下,在英语中,一个问题通常以一个上升的音调结束,或者"阅读"这个词的发音非常不同,这取决于它的时态.显然,理解单词或短语的使用方式是将文本解释为声音的关键方面.更复杂的是,每种语言的规则略有不同.因此,正如您可以想象的那样,前端必须进行一些非常复杂的分析.

声音生成

后端有一个完全不同的任务.它需要前端进行分析,并通过一些非常重要的分析,为输入文本生成适当的声音.较旧的合成器(以及当今具有最小占地面积的合成器)通过算法生成单独的声音,从而产生非常机器人的声音.现代合成器,例如Windows Vista和Windows 7中的合成器,使用由记录语音的小时和小时构建的声音段数据库.后端的有效性取决于为任何给定输入选择合适的声音片段并将它们平滑地拼接在一起时的好坏程度.

可以用了

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

试试这段代码:

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");



        }

    }
}
Run Code Online (Sandbox Code Playgroud)


Dav*_*vid 20

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

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

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

与所有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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*ran 5

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
                }       
         }
    }
Run Code Online (Sandbox Code Playgroud)
  • 使用"SpeakAsync"是更好的选择,因为当"Speak"功能正在执行/运行时,其他任何功能都无法工作,直到完成它的工作(个人推荐)

改变VoiceGender
改变VoiceAge