【AWS】AWS Polly
Aws Polly是一個能將文字轉成語音的功能
並且可以透過特殊的SSML標籤來定義語音的效果。
https://docs.aws.amazon.com/zh_tw/polly/latest/dg/supported-ssml.html
AWS文檔有提供SSML標籤的使用說明。
在開發階段端是使用「5.X」(舊版)的UNITY開發,
C#只能支援到.Net 2.0,所以沒辦法直接使用aws官方提供的SDK。
後來就換個方式。使用AWS CLI 下Command的方式來產生語音檔案。
使用aws polly有個比較需要的注意要點。
就是目前只支援「簡體中文」,也就是如果需要將中文轉語音的話。
需要多做一道繁體轉簡體的動作。
以下是unity 的 aws polly的使用方式:
text 就是需要產生音檔的段落,裏面包含了SSML的標籤效果
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Diagnostics; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System.Text; | |
using System.Net; | |
using UnityEngine.Events; | |
public class AwsPolly : MonoBehaviour { | |
public enum State { NONE, RUNNING }; | |
//執行序 | |
private Process process; | |
//檔案名稱 | |
public string fileName = "helloworld.mp3"; | |
//變量(不可變更) | |
public State currentState = State.NONE; | |
public string currentName = ""; | |
public string fileFullPath = ""; | |
//完成事件 | |
public UnityEvent onCompelete; | |
void Start () { | |
//測試用CODE | |
onCompelete.AddListener(()=> { | |
UnityEngine.Debug.Log("測試監聽!!完成事件"); | |
System.Random rnd = new System.Random(); | |
//CreateTTS("Leeson3"+ rnd.Next()); | |
}); | |
CreateTTS("謎樣般的人物"); | |
} | |
void Update () { | |
} | |
/// <summary> | |
/// TTS產生 | |
/// </summary> | |
/// <param name="name">名稱</param> | |
void CreateTTS(string name) | |
{ | |
if (currentState == State.NONE) | |
{ | |
//繁體轉簡體 | |
currentName = CharSetConverter.ToSimplified(name); | |
//設定檔案絕對路徑 | |
fileFullPath = System.Environment.CurrentDirectory + "/" + fileName; | |
//產生aws指令 | |
UnityEngine.Debug.Log("開始產生聲音檔「"+ currentName + "」"); | |
string text = "<speak><amazon:effect name='whispered'><amazon:breath/><prosody rate='10%'>" + currentName + "</prosody></amazon:effect></speak>"; | |
String commands = "aws polly synthesize-speech --language-code cmn-CN --text-type ssml --output-format mp3 --voice-id Zhiyu --text \"" + text + "\" " + fileFullPath; | |
//執行序info設定 | |
ProcessStartInfo commandsToRun = new ProcessStartInfo("cmd", @"/c " + commands); | |
commandsToRun.CreateNoWindow = true; | |
commandsToRun.UseShellExecute = false; | |
//啟動執行序 | |
if (process != null) | |
{ | |
process.Exited -= CreateTTSCompelete; | |
process.Close(); | |
process = null; | |
} | |
process = new Process(); | |
process.Exited += new EventHandler(CreateTTSCompelete); | |
process.EnableRaisingEvents = true; | |
process.StartInfo = commandsToRun; | |
currentState = State.RUNNING; | |
process.Start(); | |
} | |
else | |
{ | |
UnityEngine.Debug.Log("聲音檔「" + currentName + "」執行序正在處理中.."); | |
} | |
} | |
/// <summary> | |
/// 檔案完成事件 | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
private void CreateTTSCompelete(object sender, System.EventArgs e) | |
{ | |
UnityEngine.Debug.Log("聲音檔「"+ currentName + "」產生完畢,路徑:"+ fileFullPath); | |
currentState = State.NONE; | |
if(onCompelete!=null) | |
{ | |
onCompelete.Invoke(); | |
} | |
} | |
} |
張貼留言