TTS语音合成的实现

         在.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);
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值