日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

文本转声音,TTS语音实现

發布時間:2025/5/22 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文本转声音,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

總結

以上是生活随笔為你收集整理的文本转声音,TTS语音实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。