文本转声音,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;
            }
        }
    }

转载于:https://www.cnblogs.com/tim190/archive/2012/08/21/2648867.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值