2013-03-13 93 views
32

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

+2

为什么开源? .Net的回答非常明显,你应该明确为什么它不在表格中。 – flup 2013-03-13 13:52:58

+0

NET选项对我来说是好的 – user2110292 2013-03-13 13:57:53

回答

38

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

根据Microsoft

的System.Speech.Synthesis命名空间包含类,使您 初始化和配置语音合成引擎,创建提示, 产生的讲话,对事件做出响应,并修改声音特点。 语音合成通常被称为文本到语音或TTS。

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

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

文本分析

前端擅长使用自然语言规则文本的分析。它分析一串字符以确定单词的位置(这很容易在英文中完成,但在中文和日文等语言中不容易)。这个前端还会计算语法细节,如函数和词性。例如,哪些词是专有名词,数字等等;句子开始和结束;一个短语是一个问题还是一个陈述;以及陈述是过去,现在还是将来。

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

声代

后端有相当不同的任务。它需要前端完成分析,并通过对其自身的一些非平凡分析,为输入文本生成适当的声音。较早的合成器(以及当今最小尺寸的合成器)通过算法生成单独的声音,从而产生非常机器人的声音。现代合成器(如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"); 



     } 

    } 
} 
+1

25年前,我用一个程序,用数字表达事物表达压力(所以“那是什么?”可能是[IIRC]“WHUH1T IH2IH4IH4ZAE1T。”或“WHUH3T IH1Z DHAE3T“,取决于重点所在)。听起来自然的韵律通常需要很多调整,但调整允许非常详细的控制水平。如果一个人的目标是通过以各种不同的顺序组合固定的文本字符串来产生语音,那么有没有什么好方法可以将数据提交给Speech API作为包含压力和韵律标记的语音字符串? – supercat 2013-04-15 18:09:20

+0

有趣的bug:直到我重新启动Visual Studio才会说出任何内容。 – 2014-08-30 00:50:23

+0

@supercat,不知道,但你在寻找SSML支持?合成器具有[SpeakSsml](https://msdn.microsoft.com/en-us/library/system.speech.synthesis.speechsynthesizer.speakssml(v = vs.110).aspx)方法。 [SSML](https://msdn.microsoft.com/en-us/library/vs/alm/hh361578%28v=office.14%29.aspx)似乎支持[韵律](https://msdn.microsoft .COM/EN-US /库/ VS/ALM/hh361583%28V = office.14%29.aspx)。 PS:我不知道这些意味着什么。 :) – publicgk 2016-09-05 15:37:10

19

此功能位于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(); 
    } 
    } 
} 
4
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 
       }  
     } 
    } 
  • 这将是更好的选择使用“SpeakAsync”,因为当“说话”功能正在执行/运行让利功能将工作,直到完成它的工作(个人推荐)

Change VoiceGender
Change VoiceAge

相关问题