TTS异步+同步

微软TTS使用说明

.SAPI SDK的介绍

SAPI,全称是The Microsoft Speech API。就是微软的语音API。由Windows Speech SDK提供。

       Windows Speech SDK包含语音识别SR引擎和语音合成SS引擎两种语音引擎。语音识别引擎用于识别语音命令,调用接口完成某个功能,实现语音控制。语音合成引擎用于将文字转换成语音输出。

目前最常用的Windows Speech SDK版本有三种:5.1、5.3和5.4。

       Windows Speech SDK 5.1版本支持xp系统和server 2003系统,需要下载安装。XP系统默认只带了个Microsoft Sam英文男声语音库,想要中文引擎就需要安装Windows Speech SDK 5.1。

       Windows Speech SDK 5.3版本支持Vista系统和Server 2008系统,已经集成到系统里。Vista和Server 2003默认带Microsoft lili中文女声语音库和Microsoft Anna英文女声语音库。

       Windows Speech SDK 5.4版本支持Windows7系统,也已经集成到系统里,不需要下载安装。Win7系统同样带了Microsoft lili中文女声语音库和Microsoft Anna英文女声语音库。Microsoft lili支持中英文混读。

  微软SAPI使用官方手册:http://msdn.microsoft.com/zh-cn/library/system.speech.synthesis.speechsynthesizer(v=vs.110).aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1

.SAPI SDK的下载和安装 

如果是在XP系统下进行开发则需要下载Microsoft Speech SDK 5.1,下载地址为:http://www.microsoft.com/download/en/details.aspx?id=10121

如果想要在Vista或Win7系统下使用Mike、Mary和Microsoft Simplified Chinese中文男声语音库也可以下载相应的文件安装。

.VS环境配置

       如果是在XP系统下开发,先安装SpeechSDK51.exe再安装SpeechSDK51LangPach.exe,假设安装路径为默认的C:Program FilesMicrosoft Speech SDK 5.1,则接下来需要配置VS,以VS2010为例,在Include Directories处输入”C:Program FilesMicrosoft Speech SDK 5.1Include“,Library Directories处输入”C:Program FilesMicrosoft Speech SDK 5.1libi386“。

  最后,在程序中使用语音引擎以前包含相应的库和头文件。

如果是在Vista、Win7或者win8系统中开发的话,因为头文件和lib库所在路径已默认附加到编译器了,所以不需手动添加,直接在程序中使用相应的接口即可。

.API接口说明及依赖平台相关

参考《SAPI接口说明文档及依赖平台.pdf》





异步C#代码:


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.SetOutputToWaveFile(@"C:TestSample.wav"); // Register for the SpeakCompleted event. synth.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(synth_SpeakCompleted); // Build a prompt. PromptBuilder builder = new PromptBuilder(); builder.AppendText("This sample asynchronously speaks a prompt to a WAVE file."); // Speak the string asynchronously. synth.SpeakAsync(builder); Console.WriteLine(); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } // Handle the SpeakCompleted event. static void synth_SpeakCompleted(object sender, SpeakCompletedEventArgs e) { // Create a SoundPlayer instance to play the output audio file. System.Media.SoundPlayer m_SoundPlayer = new System.Media.SoundPlayer(@"C:TestSample.wav"); // Play the output file. m_SoundPlayer.Play(); } } }


同步C#代码:


using System;
using System.Linq;
using System.Text;
using System.Speech.Synthesis;
using System.Speech.AudioFormat;

namespace SAPITest
{
public class SapiImpl/*:ISapi*/
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Too less Params: " + args.Length);
return;
}

String textContent = args[0];
String path = args[1];
String NameFomat = args[2];
// String textContent = "返回的字符串值,与需要调用的类的方法名一致";
// String path = "D:\temp\";
// String NameFomat = "b.avi";

/*Console.WriteLine(textContent + " " + path + " " + NameFomat);*/
mytts(textContent, path, NameFomat);
/* Console.WriteLine("Done...");*/
/*Console.WriteLine("Press any key to exit...");
Console.ReadKey();*/
}

static void mytts(String textContent , String path , String NameFomat)
{
//初始化SpeechSynthesizer实例
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// 配置音频输出路径
String savePath = path + NameFomat;
synth.SetOutputToWaveFile(savePath);
//synth.SetOutputToWaveFile(savePath,
// new SpeechAudioFormatInfo(32000, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

// 创建内容
PromptBuilder builder = new PromptBuilder();
builder.AppendText(textContent);

// 输出音频文件
synth.Speak(builder);

// 为输出音频创建播放器实例
/*System.Media.SoundPlayer m_SoundPlayer =
new System.Media.SoundPlayer(savePath);*/
//播放输出的音频文件
//m_SoundPlayer.Play();
}
}
}
}

C++代码参见:

http://blog.csdn.net/itcastcpp/article/details/5313204

原文地址:https://www.cnblogs.com/freedesert/p/3922608.html