文本转声音,TTS语音实现
生活随笔
收集整理的這篇文章主要介紹了
文本转声音,TTS语音实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近公司做的棋牌游戲,領導說客戶端的聊天內(nèi)容要能夠?qū)崿F(xiàn)發(fā)音,也就是說玩家發(fā)的文本還要自動讀出來,如果把語音包集成到客戶端勢必會造成客戶端安裝文件大增,經(jīng)商量得出此方案:B/s端實現(xiàn),大致過程這樣客戶端請求B/S端,B/S端生成語音文件,客戶端再下載。
在做文本轉(zhuǎn)語音,之前用的是匿名類型+反射,不過生成的語音文件有時沒聲音,文件大小也只有幾個字節(jié),生成不成功,原因未知?
!代碼如下:
????????????{
????????????????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;
????????????}
????????}
????}
}?
轉(zhuǎn)載于:https://www.cnblogs.com/tim190/archive/2012/08/21/2648867.html
總結(jié)
以上是生活随笔為你收集整理的文本转声音,TTS语音实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每日英语:Relationship Re
- 下一篇: 枚举和位域