测试TTS(文本到语音功能)

由于要求程序在windows 2003 server这种低版本系统上运行,所以用了MS Speech SDK 51,质量差点,但是也够用了.

第一部份:如何调用

        private void btnTestTTS_Click(object sender, RoutedEventArgs e)
        {
            //_spVoice.SpeakXml("This sounds normal  but the pitch drops half way through");
            _spVoice.SpeakXml(",你的验证码是1432");
        }

        private void btnTestTTS2Wave_Click(object sender, RoutedEventArgs e)
        {
            _spVoice.Save(",你的验证码是1432",
                "e:\\temp.wav",
                "e:\\a.wav");
        }


第二部份:我封装的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpeechLib;
using System.Threading;
using System.Diagnostics;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;

namespace WpfApplicationTestTTS
{
    /*
     * Title:测试TTS(文本到语音功能)
     * Author:Kagula
     * Date:2015-12-08
     * Environment:VS2010SP1、SpeechSDK51、Win7SP1、Win2003
     *             SpeechSDK51LangPack(用于支持中文)
     *             .NetFramework 4.0 Client Profile.
     * Reference:
     * 《关于Microsoft Speech SDK 中TTS的研究 [转] 》
     * http://www.cnblogs.com/lxinxuan/archive/2007/03/17/678606.html
     * */
    internal class SpVoiceClass
    {
        SpVoice voice = new SpVoice();
        //直接在Speak上播放
        internal void SpeakXml(string xml)
        {
            voice.Speak(xml, SpeechVoiceSpeakFlags.SVSFIsXML | SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }

        //保存音频到wav文件
        internal void Save(string xml, string tempFileName, string fileName)
        {
            SpFileStream stream = new SpFileStream();
            stream.Open(tempFileName, SpeechStreamFileMode.SSFMCreateForWrite, false);
            voice.AudioOutputStream = stream;            
            voice.Speak(xml, SpeechVoiceSpeakFlags.SVSFlagsAsync | SpeechVoiceSpeakFlags.SVSFIsXML);
            voice.WaitUntilDone(Timeout.Infinite);
            stream.Close();

            //输出文件默认采样率为22khz,16bit精度,单声道,所以这里要转换为8khz的音频.
            //用到了NAudio第三方库,下载地址为http://naudio.codeplex.com/
            //下载后会发现两个dll,只需引入NAudio.dll.
            using (var reader = new AudioFileReader(tempFileName))
            {
                var resampler = new WdlResamplingSampleProvider(reader, 8000);
                WaveFileWriter.CreateWaveFile16(fileName, resampler);
            }
            //经测试,2秒时长的audio可以从88k size减少到32k size.
        }
    }
}

注意:

[1]如果要在Win2003上运行上面的代码,SpVoice实例的生命期必须是在函数体内的,如果像上面的代码一样生命期是在Class实例内,会抛出异常.

[2]上面的代码在Win7SP1下测试通过.

你可能感兴趣的:(C#)