- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Speech.Synthesis.TtsEngine;
- using System.Speech.Synthesis;
- using System.Speech.AudioFormat;
- using System.IO;
- namespace TTSServer
- {
- // 需要添加引用:System.Speech
- class TTS
- {
- ///
- /// 获取系统已安装的声音信息
- ///
- ///
- public static List getVoiceInfos()
- {
- List voiceList = new List();
- using (SpeechSynthesizer synth = new SpeechSynthesizer())
- {
- foreach (InstalledVoice voice in synth.GetInstalledVoices())
- {
- voiceList.Add(voice.VoiceInfo);
- }
- }
- return voiceList;
- }
- ///
- /// TTS
- ///
- ///
- public static void tts(TTSConf ttsConf)
- {
- SpeechSynthesizer ss = new SpeechSynthesizer();
- PromptBuilder pb = new PromptBuilder();
- pb.StartStyle(ttsConf.style);
- pb.StartVoice(ttsConf.voice);
- pb.AppendText(ttsConf.text);
- pb.EndVoice();
- pb.EndStyle();
- // 将TTS结果保存到流中(可以从流中读取byte并保存到byte[]中)
- MemoryStream ms = new MemoryStream();
- ss.SetOutputToWaveStream(ms);
- ss.Speak(pb);
- ss.SetOutputToNull();
- // 将TTS结果保存到wav文件中
- /*
- ss.SetOutputToWaveFile("D:\\a.wav");
- ss.Speak(pb);
- ss.SetOutputToNull();
- */
- // 将TTS结果输出到音频设备(播放)
- /*
- ss.Speak(pb);
- ss.SetOutputToNull();
- */
- // 释放资源
- ss.Dispose();
- }
- ///
- /// TTS请求参数
- ///
- public class TTSConf
- {
- public TTSConf(string text)
- {
- this.text = text;
- }
- public string text; // 内容
- public string voice;// 声音
- public PromptStyle style = new PromptStyle(); // 样式
- public void setRate(String rateStr)
- {
- if (rateStr != null && rateStr != "")
- {
- PromptRate rate = (PromptRate)Enum.Parse(typeof(PromptRate), rateStr);
- this.style.Rate = rate;
- }
- }
- public void setVolume(String volumeStr)
- {
- if (volumeStr != null && volumeStr != "")
- {
- PromptVolume volume = (PromptVolume)Enum.Parse(typeof(PromptVolume), volumeStr);
- this.style.Volume = volume;
- }
- }
- public void setEmphasis(String emphasisStr)
- {
- if (emphasisStr != null && emphasisStr != "")
- {
- PromptEmphasis emphasis = (PromptEmphasis)Enum.Parse(typeof(PromptEmphasis), emphasisStr);
- this.style.Emphasis = emphasis;
- }
- }
- }
- }
-----------------------下面写下我在开发中遇到的问题----------------------
其实代码本身并没有什么好说的,网上一大堆,这里值得一说的是我在开发中遇到的问题,在这些问题上折腾了好久,希望大家看到这些问题后可以避免并能解决思路。
开发环境:Win7 64bit
开发工具:VS 2010
开发语言:C#
问题一、无法使用NeoSpeech语音包
说明:SAPI(Microsoft Speech API.)本身默认使用的是微软系统自带的语音包,所以在使用时如果选择声音(SpeechSynthesizer类的SelectVoice方法),将使用默认语音。当然,也可以引用系统上安装的第三方语音包(可参考“朗读女”软件)。
由于NeoSpeech语音包的效果比较好,于是我在本机上安装了NeoSpeech语音包(中文女声_Lily,网上可下载),安装后发现在控制面板中并没有VW Lily选项(查看路径:控制面板\轻松访问\语音识别\文件到语音转换):
当然也无法在C#中的使用该语音,运行时会报如下异常:
System.ArgumentException: 不能设置语音。未安装匹配的语音,或语音被禁用。
在 System.Speech.Synthesis.SpeechSynthesizer.SelectVoice(String name)
后来在网上查找资源才发现,NeoSpeech语音包是32位的(不知道有没有64位的),而我打开的是64位的控制面板(64位系统默认使用64位面板,也可以打开32位面板),C#也是使用64位编译的,所以无法使用32位的语音包(按理说64应该可以引用32的才是啊?具体原来我也不太清楚)。
后来打开32位的控制面板(C:\Windows\SysWOW64\Speech\SpeechUX\sapi.cpl),终于在其中可以看到新安装的NeoSpeech语音包了:
既然已经安装好了,并且似乎只能在32位下使用,那么我就把C#程序改为使用32位编译,然后发现终于可以正常使用NeoSpeech的语音包了!
改32位编译方法:
在VS中右击项目->属性,在Build中将Platform target(目标平台)改为x86,如图:
参考资料:
http://wenku.baidu.com/link?url=OU6fSEOYxFWNJyWRoxHYxXG4kym1-jJiIkCRqQ7WMpje3OHGVxRtrjJQgYdsJhZO9dQenkHI5yr0tI_FZ5ALWAt68iCFiE78S4T7EZdi6gO
其他资料:http://www.bkjia.com/C_jc/827720.html
https://www.cnblogs.com/cyrix/articles/1819370.html