1.试用地址

点击链接进行体验

2.开发环境

ASP.NET

3.需求

(1)需要动态将文本转化为语音
(2)实现免费的转换过程
(3)转换成语音后可以直接下载
(4)将语音加载到U3D中试用

3.实现过程

(1)新建ASP.NET空工程

(2)创建TextToSpeechController类,用于生成Wav文件

(3)添加Speech类库

(4)TextToSpeechController类代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SpeechLib;
using System.Speech;
using System.Speech.Synthesis;
using System.IO;
using System.Security.Cryptography;
using System.Text;namespace TextToSpeechServer
{public static class TextToSpeechController{public static string GetSavePath(string speechText){//根据录入内容生成唯一文件名string fileName = Md5(speechText).ToUpper();fileName += ".wav";//获取根目录路径string path = HttpRuntime.AppDomainAppPath.ToString();//文件夹名称string DirectoryName = "音频文件";//音频文件夹路径string DirectoryPath = path + DirectoryName;//判断音频文件夹是否存在if (Directory.Exists(DirectoryPath) == false){Directory.CreateDirectory(path);}//wav文件路径string FilePath = DirectoryPath + "\\" + fileName;//如果已经有该文件,则直接返回文件名if (File.Exists(FilePath)){return fileName;}//否则生成音频文件else{SpeechSynthesizer synth = new SpeechSynthesizer();synth.Volume = 100;//设置文件保存位置synth.SetOutputToWaveFile(FilePath);//电脑中需要安装Huihui的语音包synth.SelectVoice("Microsoft Huihui Desktop");//能读中英文synth.Speak(speechText);synth.SetOutputToNull();}return fileName;}/// <summary>/// MD5加密,主要根据输入的文字来生成唯一的音频文件名/// </summary>/// <param name="value"></param>/// <returns></returns>public static string Md5(string value){var result = string.Empty;if (string.IsNullOrEmpty(value)) return result;using (var md5 = MD5.Create()){result = GetMd5Hash(md5, value);}return result;}/// <summary>/// MD5生成/// </summary>/// <param name="md5Hash"></param>/// <param name="input"></param>/// <returns></returns>static string GetMd5Hash(MD5 md5Hash, string input){byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));var sBuilder = new StringBuilder();foreach (byte t in data){sBuilder.Append(t.ToString("x2"));}return sBuilder.ToString();}}
}

(5)创建一般处理程序GetAudioStreamHandler.ashx
代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;namespace TextToSpeechServer
{/// <summary>/// GetAudioStreamHandler 的摘要说明/// </summary>public class GetAudioStreamHandler : HttpTaskAsyncHandler{/// <summary>/// 获取音频文件所在路径/// </summary>public string ServerRequestAddress = "http://" + HttpContext.Current.Request.Url.Authority + "/音频文件/{0}";/// <summary>/// 异步生成音频文件/// </summary>/// <param name="context"></param>/// <returns></returns>public override async Task ProcessRequestAsync(HttpContext context){await Task.Run(() =>{string speechText = context.Request.Params["SpeechText"].ToString().Trim();//将|!|!|转换为换行符speechText = speechText.Replace("|!|!|", "\n");string fileName = TextToSpeechController.GetSavePath(speechText);string requestURL = string.Format(ServerRequestAddress, fileName);//直接打开生成的音频文件context.Response.Redirect(requestURL);});}}
}

(6)创建前端aspx界面

前端页面代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TextToSpeechPage.aspx.cs" Inherits="TextToSpeechServer.TextToSpeechPage" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><title>文字转语音</title>
</head>
<body background="文字转语音.png" style="background-size:100%,100%;background-repeat:no-repeat;background-attachment:fixed"><form id="form1" runat="server"><table style="width:100%"><tr style="height:100px"><th><label style="font-size:50px;color:white;">文字语音转换器</label></th></tr><tr style="height:600px"><th><%--<textarea style="width:800px;height:500px;margin:100px,100px,100px,100px;font-size:20px"></textarea>--%><asp:TextBox ID="ConvertTextBox" runat="server" Height="500px" Width="800px" Font-Size="20px" TextMode="MultiLine"></asp:TextBox></th></tr><tr style="height:100px"><th><%--<button style="font-size:20px;width:150px;height:50px">开始转换</button>--%><asp:Button ID="ConvertButton" runat="server" OnClick="ConvertButton_Click" Text="开始转换" style="font-size:20px;width:150px;height:50px"/></th></tr></table></form>
</body>
</html>

后端处理程序代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace TextToSpeechServer
{public partial class TextToSpeechPage : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}/// <summary>/// 获取当前部署的服务器IP地址,生成HTTP请求链接/// </summary>public string RequestURL = "http://" + HttpContext.Current.Request.Url.Authority + "//GetAudioStreamHandler.ashx?SpeechText={0}";/// <summary>/// 生成按钮点击事件/// </summary>/// <param name="sender"></param>/// <param name="e"></param>protected void ConvertButton_Click(object sender, EventArgs e){if (string.IsNullOrEmpty(ConvertTextBox.Text)){ConvertTextBox.Text = "请输入信息";}else{//替换换行符,纺织报错string CheckedURL = string.Format(RequestURL, ConvertTextBox.Text).Replace("\n", "|!|!|");Response.Redirect(CheckedURL);}}}
}

(7)测试、发布、部署到IIS

均为常规操作流程,这里不再详细介绍。

(8)注意事项

1)部署到IIS所在的服务器时,需要确保服务器上已经安装了Speech所需的环境

·安装 Microsoft Speech Platform - Server Runtime
·安装Microsoft Speech Platform - Software Development Kit (SDK)
·安装Microsoft Speech Platform - Server Runtime Languages【语言包,其中包含文本转语音功能包(TTS)和语音识别功能包(SR),这里我们主要用TTS包】

2)部署后无法访问

·使用云产品的需要在控制台配置安全组规则,允许相关端口被其他电脑访问

·在部署的IIS所在文件夹进行权限设置,可参考这里。除了IIS_IUSRS和IUSRS外,还可以设置Everyone用户。

·可能上述设置了还会报错,如下图

这个与IIS中的应用程序池内权限有关

在高级设置中,将“标识”从默认的ApplicationPoolIdentity改为LocalSystem即可。

欲知详情,可点击这里。
【该部分设置private key部分,打开方式是Win+R运行,输入MMC,打开Microsoft管理控制台,但是该方法笔者没试成功~~】

(9)源码

Github链接

(10)参考文章

·IIS ApplicationPoolIdentity
·利用C#开发web应用程序时,对注册表进行操作提示没有权限的解决办法
·Microsoft Speech Platform 自学笔记2 环境要求与安装过程

(11)补充U3D中下载并播放语音

创建PlayAudioController脚本

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using System.Security.Cryptography;
using System.Text;public class PlayAudioController : MonoBehaviour
{/// <summary>/// 转换文字输入框/// </summary>public InputField TextToAudioInputField;/// <summary>/// 音频/// </summary>public AudioSource audioSource;/// <summary>/// 请求地址/// </summary>private string RequestURL = "http://39.104.127.80:8082/GetAudioStreamHandler.ashx?SpeechText={0}";/// <summary>/// 文件名/// </summary>string FileName = "";/// <summary>/// 是否在StreamingAssets/AudioFile中存在/// </summary>bool alreadyExit = false;/// <summary>/// 将Text转化为Speech/// </summary>public void ConvertTextToSpeech(){if (string.IsNullOrEmpty(TextToAudioInputField.text)){return;}FileName = Md5(TextToAudioInputField.text).ToUpper() + ".wav";string FilePath = Application.streamingAssetsPath + "/AudioFile/" + FileName;alreadyExit = false;StartCoroutine(GetWavFileAndPlay(FilePath));}/// <summary>/// 从StreamingAssets/AudioFile文件中加载音频文件并播放/// </summary>/// <param name="FilePath"></param>/// <returns></returns>IEnumerator GetWavFileAndPlay(string FilePath){UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(FilePath, AudioType.WAV);yield return uwr.SendWebRequest();if (uwr.isDone){if (uwr.isNetworkError || uwr.isHttpError){alreadyExit = false;Debug.Log(uwr.error);}else{AudioClip audioClip = DownloadHandlerAudioClip.GetContent(uwr);audioSource.clip = audioClip;audioSource.Play();alreadyExit = true;}}//判断当前音频文件是否存在if (!alreadyExit){string text = TextToAudioInputField.text.Replace("\n", "|!|!|");StartCoroutine(SaveWavFile(text));}}/// <summary>/// 当StreamingAssets/AudioFile文件夹下不存在指定Wav文件时,下载该音频文件并播放/// </summary>/// <param name="text"></param>/// <returns></returns>IEnumerator SaveWavFile(string text){UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(string.Format(RequestURL, text),AudioType.WAV);yield return uwr.SendWebRequest();if (uwr.isNetworkError || uwr.isHttpError){Debug.Log(uwr.error);}else{//播放下载的Wav音频AudioClip audioClip = DownloadHandlerAudioClip.GetContent(uwr);audioSource.clip = audioClip;audioSource.Play();//保存下载的Wav音频文件byte[] data = uwr.downloadHandler.data;//这里的FileMode.create是创建这个文件,如果文件名存在则覆盖重新创建FileStream fs = new FileStream(Application.streamingAssetsPath + "/AudioFile/" + FileName, FileMode.Create);fs.Write(data, 0, data.Length);//每次读取文件后都要记得关闭文件fs.Close();}}/// <summary>/// MD5加密,主要根据输入的文字来生成唯一的音频文件名/// </summary>/// <param name="value"></param>/// <returns></returns>public static string Md5(string value){var result = string.Empty;if (string.IsNullOrEmpty(value)) return result;using (var md5 = MD5.Create()){result = GetMd5Hash(md5, value);}return result;}/// <summary>/// MD5生成/// </summary>/// <param name="md5Hash"></param>/// <param name="input"></param>/// <returns></returns>static string GetMd5Hash(MD5 md5Hash, string input){byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));var sBuilder = new StringBuilder();foreach (byte t in data){sBuilder.Append(t.ToString("x2"));}return sBuilder.ToString();}
}

源码

搭建文字转语音(TTS)服务器相关推荐

  1. 汉字转拼音,文字转语音tts (语音技术、语音识别),Asr/tts,变声

    语音识别,语音合成.语音技术主要分两块:一块是语音转文字,即语音识别:另一块是文字转语音,即语音合成.   语音相关技术研发 语音合成技术整体解决方案.一系列语音技术的相关专利,包括文本处理.韵律预测 ...

  2. 基于ROS2和科大讯飞的文字转语音TTS入门教程

    基于ROS2和科大讯飞的语音转文字入门教程 基于ROS2和科大讯飞的文字转语音TTS入门教程 1.环境搭建 2.创建工程 3.编译和执行 基于ROS2和科大讯飞的文字转语音TTS入门教程 本文将展示, ...

  3. SwiftUI 文字转语音TTS 开发朗读器 AVSpeechSynthesizer(教程含源码)

    实战需求 SwiftUI 文字转语音TTS 开发朗读器 本文价值与收获 看完本文后,您将能够作出下面的界面 看完本文您将掌握的技能 AVFoundation AVSpeechSynthesizer A ...

  4. teamspeak语音服务器价格,语音聊天社交很热门,带你搭建自己的语音聊天服务器...

    在近段时间,在国外,一款主打语音聊天社交的软件Clubhouse火爆了全球,Clubhouse是一款主打即时性的音频社交软件,诞生于2020年3月,由Paul Davison和前谷歌员工Rohan S ...

  5. PHP使用阿里云(语音合成)实现文字转语音“TTS“

    在做前,我发现阿里云竟然没有PHP文字转语音的SDK包,有点尴尬啊,没办法我选择了RESTful API 2.0的方式请求: 1:第一步:打开阿里云-->产品分类-->人工智能--> ...

  6. vue 实现文字转语音tts

    调用本地方法实现文字转语音 .缺点:win7系统部分版本不发声音,优点:不需要外网支持 const synth = window.speechSynthesis const msg = new Spe ...

  7. Android离线文字转语音(TTS)原生实现

    目前文字转语音用的最多的是第三方厂商科大讯飞,不过需要收费.google也有离线文字转语音sdk,支持中文,发音也很好,使用免费的它不更香么!下边介绍具体使用步骤: 一.下载并设置Google文字转语 ...

  8. TTS 文字转语音研究,效果原来这么好。

    自己做了个在线文字转语音服务,没想到效果竟然这么好 有时候我们需要文字转语音服务,在网上找了好多免费的,但效果都不太好,偶然间发现了微软的Edge浏览器有个大声朗读功能,试了一下,效果竟然出奇的好, ...

  9. 前端实现tts(文字转语音)

    <!--* @Author: xss 995550359@qq.com* @Date: 2022-09-23 15:16:20* @LastEditors: xss 995550359@qq.c ...

最新文章

  1. 怎么用css控制border成为三角形
  2. 数据结构实践——队列数组
  3. struct.error: 'h' format requires -32768 number 32767
  4. 搞网络都应该知道的12条基本命令
  5. java gc信息_JVM之GC回收信息详解
  6. php 自动验证 正则表达,使用正则表达式验证登录页面的输入内容
  7. Python Demo 02 蒙特卡罗方法输出e
  8. python easygui进度条_Python _easygui详细版
  9. python写入excel表格数据绘制图表_(原创)xlsxwriter,python excel 写入数据\图表等操作_图表操作(二)...
  10. Windows 7 - 使用批处理脚本模拟Windows XP中的msbackup备份程序
  11. java发送带附件的电子邮件
  12. 关于 u-nas 报警声音
  13. 《Java学习笔记1》
  14. 【雕爷学编程】Arduino动手做(100)---MAX30102手腕心率
  15. 数分-理论-大数据2-Hadoop
  16. 炉石传说 android,炉石传说安卓版
  17. android 钉钉考勤日历,Flutter仿钉钉考勤日历
  18. 最大化偏差问题与Double Q-Learning(一)——最大化偏差问题介绍
  19. 使用自开发的代理服务器解决 SAP UI5 FileUploader 上传文件时遇到的跨域访问错误试读版
  20. 智慧指间丨生态环境网格化监管系统——编织生态环保“绿网”

热门文章

  1. Git 出错error: Pulling is not possible because you have unmerged files
  2. 全球重磁异常以及水深数据分享
  3. AV1的CDEF过程介绍
  4. 示波器分析IIC波形图
  5. 前端-Excel在线预览
  6. c语言程序冒号的作用是什么,C语言里面的冒号
  7. Revit 参数说明
  8. python numpy.arry, pytorch.Tensor及原生list相互转换
  9. 强烈推荐!程序员必备的15 款工具(含阿里内部工具)
  10. Qt5实现可配置截图及基于百度OCR自动识别标题保存文件