文本转声音,TTS语音实现

最近公司做的棋牌游戏,领导说客户端的聊天内容要能够实现发音,也就是说玩家发的文本还要自动读出来,如果把语音包集成到客户端势必会造成客户端安装文件大增,经商量得出此方案:B/s端实现,大致过程这样客户端请求B/S端,B/S端生成语音文件,客户端再下载。

在做文本转语音,之前用的是匿名类型+反射,不过生成的语音文件有时没声音,文件大小也只有几个字节,生成不成功,原因未知 
!代码如下:

   if (! string.IsNullOrEmpty(context.Request.QueryString[ " txt "]) && ! string.IsNullOrEmpty(context.Request.QueryString[ " type "]))
            {
                 try
                {
                     string type = context.Request.QueryString[ " type "];
                     string txt = context.Request.QueryString[ " txt "];
                     string fileName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(type + txt,  " MD5 ");
                     string filePath =  " /files/ " + fileName +  " .wav ";
                     if (File.Exists(context.Server.MapPath(filePath)))
                    {
                        context.Response.Write(fileName); ;
                    }
                     else
                    {
                        dynamic synth = System.Activator.CreateInstance(System.Type.GetTypeFromProgID( " SAPI.SpVoice "));
                        dynamic fileStream = System.Activator.CreateInstance(System.Type.GetTypeFromProgID( " SAPI.SpFileStream "));

                         if (type ==  " 2 ")
                        {
                            synth.Voice = synth.GetVoices( " Name=VW Lily ").Item( 0);
                        }
                         else
                        {
                            synth.Voice = synth.GetVoices( " Name=VW Liang ").Item( 0);
                        }
                        synth.Rate = - 1;

                        fileStream.Open(context.Server.MapPath(filePath), SpeechStreamFileMode.SSFMCreateForWrite,  false);
                        synth.AudioOutputStream = fileStream;
                        synth.Speak(txt);
                        synth.WaitUntilDone( 1000);
                        synth.Dispose();
                        fileStream.Close();

                        context.Response.Write(fileName);
                        context.Response.End();

                    }
                }
                 catch
                {
                    context.Response.Write( " 0 ");
                    context.Response.End();
                }

            } 

 

 

后来换了一种方式,使用.net 3.0生成,目前可以测试正常,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Speech.Synthesis;
using System.IO;
using System.Threading;

namespace DokeeTTS
{
     ///   <summary>
    
///  _default 的摘要说明
    
///   </summary>
     public  class _default : IHttpHandler
    {

         public  void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType =  " text/plain ";

             string txt = context.Request.QueryString[ " txt "];
             string type = context.Request.QueryString[ " type "];
             string fileName =  "";

            fileName = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(type + txt,  " MD5 ");
            Thread t =  new Thread(() =>
                {
                    SpeechSynthesizer syth =  new SpeechSynthesizer();
                     if (type ==  " 2 ")
                    {
                        syth.SelectVoice( " VW Lily ");
                    }
                     else
                    {
                        syth.SelectVoice( " VW Liang ");
                    }
                     string filePath =  " /files/ " + fileName +  " .wav ";
                     if (File.Exists(context.Server.MapPath(filePath)))
                    {
                        context.Response.Write(fileName); ;
                    }
                     else
                    {
                        syth.SetOutputToWaveFile(context.Server.MapPath(filePath));
                        syth.Speak(txt);
                    }
                    syth.Dispose();
                });
            t.Start();
            t.Join();

            context.Response.Write(fileName);
            context.Response.Flush();
            context.Response.End();

        }

         public  bool IsReusable
        {
             get
            {
                 return  false;
            }
        }
    }

你可能感兴趣的:(tts)