使用C#开发TTS应用

最近项目需要,需要使用C#开发一个简单的TTS服务器,用于响应客户端的tts请求。
本人对这块也不熟悉,需要人头开始搞,在网上找了一些资料,经过几天折腾终于搞出来了。

其实代码本身非常简单,核心代码也就几行而已,很多网站上都有贴出来,这里借花献佛也贴出来一下,不过有部分小改动。



先贴出代码吧:
[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Speech.Synthesis.TtsEngine;  
  6. using System.Speech.Synthesis;  
  7. using System.Speech.AudioFormat;  
  8. using System.IO;  
  9.   
  10. namespace TTSServer  
  11. {  
  12.       
  13.     // 需要添加引用:System.Speech  
  14.       
  15.     class TTS  
  16.     {  
  17.   
  18.         ///   
  19.         /// 获取系统已安装的声音信息  
  20.         ///   
  21.         ///   
  22.         public static List getVoiceInfos()  
  23.         {  
  24.             List voiceList = new List();  
  25.   
  26.             using (SpeechSynthesizer synth = new SpeechSynthesizer())  
  27.             {  
  28.                 foreach (InstalledVoice voice in synth.GetInstalledVoices())  
  29.                 {  
  30.                     voiceList.Add(voice.VoiceInfo);  
  31.                 }  
  32.             }  
  33.   
  34.             return voiceList;  
  35.         }  
  36.   
  37.         ///   
  38.         /// TTS  
  39.         ///   
  40.         ///   
  41.         public static void tts(TTSConf ttsConf)  
  42.         {  
  43.             SpeechSynthesizer ss = new SpeechSynthesizer();  
  44.   
  45.             PromptBuilder pb = new PromptBuilder();  
  46.             pb.StartStyle(ttsConf.style);  
  47.             pb.StartVoice(ttsConf.voice);  
  48.             pb.AppendText(ttsConf.text);  
  49.             pb.EndVoice();  
  50.             pb.EndStyle();  
  51.   
  52.             // 将TTS结果保存到流中(可以从流中读取byte并保存到byte[]中)  
  53.             MemoryStream ms = new MemoryStream();  
  54.             ss.SetOutputToWaveStream(ms);  
  55.             ss.Speak(pb);  
  56.             ss.SetOutputToNull();  
  57.   
  58.               
  59.             // 将TTS结果保存到wav文件中  
  60.             /* 
  61.             ss.SetOutputToWaveFile("D:\\a.wav"); 
  62.             ss.Speak(pb); 
  63.             ss.SetOutputToNull(); 
  64.             */  
  65.               
  66.             // 将TTS结果输出到音频设备(播放)  
  67.             /* 
  68.             ss.Speak(pb); 
  69.             ss.SetOutputToNull(); 
  70.             */  
  71.               
  72.             // 释放资源  
  73.             ss.Dispose();  
  74.         }  
  75.   
  76.     ///   
  77.     /// TTS请求参数  
  78.     ///   
  79.     public class TTSConf  
  80.     {  
  81.   
  82.         public TTSConf(string text)  
  83.         {  
  84.             this.text = text;  
  85.         }  
  86.   
  87.         public string text; // 内容  
  88.         public string voice;// 声音  
  89.         public PromptStyle style = new PromptStyle(); // 样式  
  90.   
  91.         public void setRate(String rateStr)  
  92.         {  
  93.             if (rateStr != null && rateStr != "")  
  94.             {  
  95.                 PromptRate rate = (PromptRate)Enum.Parse(typeof(PromptRate), rateStr);  
  96.                 this.style.Rate = rate;  
  97.             }  
  98.         }  
  99.   
  100.         public void setVolume(String volumeStr)  
  101.         {  
  102.             if (volumeStr != null && volumeStr != "")  
  103.             {  
  104.                 PromptVolume volume = (PromptVolume)Enum.Parse(typeof(PromptVolume), volumeStr);  
  105.                 this.style.Volume = volume;  
  106.             }  
  107.         }  
  108.   
  109.         public void setEmphasis(String emphasisStr)  
  110.         {  
  111.             if (emphasisStr != null && emphasisStr != "")  
  112.             {  
  113.                 PromptEmphasis emphasis = (PromptEmphasis)Enum.Parse(typeof(PromptEmphasis), emphasisStr);  
  114.                 this.style.Emphasis = emphasis;  
  115.             }  
  116.         }  
  117.     }  
  118.   
  119. }  

-----------------------下面写下我在开发中遇到的问题----------------------

其实代码本身并没有什么好说的,网上一大堆,这里值得一说的是我在开发中遇到的问题,在这些问题上折腾了好久,希望大家看到这些问题后可以避免并能解决思路。


开发环境:Win7 64bit

开发工具:VS 2010

开发语言:C#


问题一、无法使用NeoSpeech语音包

说明:SAPI(Microsoft Speech API.)本身默认使用的是微软系统自带的语音包,所以在使用时如果选择声音(SpeechSynthesizer类的SelectVoice方法),将使用默认语音。当然,也可以引用系统上安装的第三方语音包(可参考“朗读女”软件)。


由于NeoSpeech语音包的效果比较好,于是我在本机上安装了NeoSpeech语音包(中文女声_Lily,网上可下载),安装后发现在控制面板中并没有VW Lily选项(查看路径:控制面板\轻松访问\语音识别\文件到语音转换):


扫描二维码关注公众号,回复: 3168696 查看本文章


当然也无法在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

猜你喜欢

转载自blog.csdn.net/dragonpeng2008/article/details/79818135
今日推荐