C# 调用TTS 并输出WAV

来源:互联网 发布:排八字软件 编辑:程序博客网 时间:2024/04/30 10:41

.net4.0

using System.Speech;
using System.Speech.Synthesis;
using System.Windows.Forms;

1、输出语音

            Type type = Type.GetTypeFromProgID("SAPI.SpVoice");
            dynamic spVoice = Activator.CreateInstance(type);
            spVoice.Speak("孙悟空,白骨精,唐僧!哈哈哈哈");

2、输出WAV 文件

    SaveFileDialog  file = new SaveFileDialog();
            file.Title = "保存音频文件";
            file.InitialDirectory = ".\\";//对话框的初始目录
            file.Filter = "音频文件|*.wav";// 要在对话框中显示的文件筛选器
            file.RestoreDirectory = true;//控制对话框在关闭之前是否恢复当前目录
            file.FilterIndex = 1;//在对话框中选择的文件筛选器的索引,如果选第一项就设为1
            file.AddExtension = true;
            if (file.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {


                int cnVoice, enVoice;
                Type type = Type.GetTypeFromProgID("SAPI.SpVoice");
                dynamic spVoice = Activator.CreateInstance(type);
                dynamic colVoice = spVoice.GetVoices();//‘获得语音引擎集合
                spVoice.Volume = 100;// ‘设置音量,0到100,数字越大音量越大
                spVoice.Rate = 0;//
                string langCN = "MSSimplifiedChineseVoice";// ‘简体中文
                string langEN = "MSSam";// ‘如果安装了TTS Engines 5.1,还可以选择MSMike,MSMary
                for (int i = 0; i < colVoice.Count; i++)
                {
                    if (colVoice[i].Id == langCN)
                        cnVoice = i;
                    if (colVoice[i].Id == langEN)
                        enVoice = i;
                }
                int SSFMCreateForWrite = 3;
                int SAFT22kHz16BitMono = 22;
                int SVSFlagsAsync = 1;
                string strText = "测试TTS";
                Type type2 = Type.GetTypeFromProgID("SAPI.SpFileStream");
                dynamic objFileStream = Activator.CreateInstance(type2);
                objFileStream.Format.Type = SAFT22kHz16BitMono;
                objFileStream.Open(file.FileName, SSFMCreateForWrite, false);
                spVoice.AudioOutputStream = objFileStream;
                spVoice.Speak(strText, SVSFlagsAsync);
                spVoice.WaitUntilDone(-1);
                objFileStream.Close();

可参考:

http://blog.sina.com.cn/s/blog_54be98b80102v5yw.html

http://msdn.microsoft.com/en-us/library/ms723602(VS.85).aspx

0 0
原创粉丝点击