SAPI,全稱是The Microsoft Speech API。就是微軟的語音API。由Windows Speech SDK提供。html
Windows Speech SDK包含語音識別SR引擎和語音合成SS引擎兩種語音引擎。語音識別引擎用於識別語音命令,調用接口完成某個功能,實現語音控制。語音合成引擎用於將文字轉換成語音輸出。api
目前最經常使用的Windows Speech SDK版本有三種:5.一、5.3和5.4。less
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英文女聲語音庫。async
Windows Speech SDK 5.4版本支持Windows7系統,也已經集成到系統裏,不須要下載安裝。Win7系統一樣帶了Microsoft lili中文女聲語音庫和Microsoft Anna英文女聲語音庫。Microsoft lili支持中英文混讀。tcp
微軟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-1ui
若是是在XP系統下進行開發則須要下載Microsoft Speech SDK 5.1,下載地址爲:http://www.microsoft.com/download/en/details.aspx?id=10121。spa
若是想要在Vista或Win7系統下使用Mike、Mary和Microsoft Simplified Chinese中文男聲語音庫也能夠下載相應的文件安裝。.net
若是是在XP系統下開發,先安裝SpeechSDK51.exe再安裝SpeechSDK51LangPach.exe,假設安裝路徑爲默認的C:\Program Files\Microsoft Speech SDK 5.1,則接下來須要配置VS,以VS2010爲例,在Include Directories處輸入」C:\Program Files\Microsoft Speech SDK 5.1\Include「,Library Directories處輸入」C:\Program Files\Microsoft Speech SDK 5.1\lib\i386「。code
最後,在程序中使用語音引擎之前包含相應的庫和頭文件。
若是是在Vista、Win7或者win8系統中開發的話,由於頭文件和lib庫所在路徑已默認附加到編譯器了,因此不需手動添加,直接在程序中使用相應的接口便可。
參考《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:\Test\Sample.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:\Test\Sample.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