我正在寻找一种将C#中的英文文本(字符串)转换为语音(声音)的方法。是否有人知道可以帮助我完成这个任务的方法或开源库?
我正在寻找一种将C#中的英文文本(字符串)转换为语音(声音)的方法。是否有人知道可以帮助我完成这个任务的方法或开源库?
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");
}
}
}
PromptBuilder
对象将文本字符串转换为SSML。该页面指出,prosody元素目前不支持“轮廓”,但可能可以连接具有不同prosody元素的元素以实现正确的重音。总的来说,prosody是指节奏和抑扬顿挫的变化,这些变化会导致单词在不同的语境下发音不同。 - supercat该功能在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();
}
}
}
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
}
}
}
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);
}
}
}
最近,谷歌发布了Google Cloud Text To Speech。
可以在此处找到 Google.Cloud.TextToSpeech 的 .NET 客户端版本: https://github.com/jhabjan/Google.Cloud.TextToSpeech.V1
Nuget: Install-Package JH.Google.Cloud.TextToSpeech.V1
以下是如何使用客户端的简短示例:
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);