在.net中要实现语音合成可以利用微软的语音技术,利用微软的这一技术,需要安装Microsoft Speech SDK和语音库,安装之后,调用com中的Microsoft Speech Object Library,即可实现语音合成。
MicrosoftSpeech SDK 5.1的下载地址:
SDK 5.1 下载, 这里
SDK 5.1 语言包,这里
SDK 5.1 语音文件,这里
下面是调用语音合成的一个类
using System;
using SpeechLib;
namespace SpeechTest
{
/// <summary>
/// TTS语音合成类
/// </summary>
public class TTSSpeech
{
//语音合成对象
SpVoice voice = null;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="rate">发音频率(-10~10)</param>
/// <param name="volume">音量大小(0~100)</param>
/// <param name="voiceName">播音员名称</param>
public TTSSpeech(int rate,int volume,string voiceName)
{
voice = new SpVoice();
//设定音量大小
voice.Volume = volume;
//设定朗读频率
voice.Rate = rate;
//设定播音员
foreach (ISpeechObjectToken oneIOT in voice.GetVoices(string.Empty, string.Empty))
{
string[] pVoice = oneIOT.Id.Split(new char[] { '\\' });
int count = pVoice.Length;
string vName = pVoice[count - 1];
if (sVoice == voiceName)
{
voice.Voice = (SpObjectToken)oneIOT;
}
}
}
/// <summary>
/// 根据文本生成音频文件
/// </summary>
/// <param name="txtSound">需要转化为声音的文本</param>
/// <param name="filePath">音频文件路径</param>
public void CreateSoundFile(string txtSound,string filePath)
{
//如果存在音频文件,则删除
if (File.Exists(filePath))
{
File.Delete(filePath);
}
SpeechStreamFileMode spFileMode = SpeechStreamFileMode.SSFMCreateForWrite;
SpFileStream spFileStream = new SpFileStream();
spFileStream.Open(filePath, spFileMode, false);
voice.AudioOutputStream = spFileStream;
SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags.SVSFlagsAsync;
int n = voice.Speak(txtSound, flag);
voice.WaitUntilDone(1000);
spFileStream.Close();
}
/// <summary>
/// 播放声音
/// </summary>
/// <param name="txtSound">需要转化为声音的文本</param>
public void PlaySound(string txtSound)
{
SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags.SVSFlagsAsync;
voice.Speak(txtSound, flag);
}
}
}