Unity Genshin Text-to-Speech: 语音合成和音频播放
Unity Genshin Text-to-Speech: 语音合成和音频播放
该代码展示了如何使用UnityWebRequest从Genshin Voice API获取语音合成音频,并使用Unity的音频播放器进行播放。
代码示例:
using System;
using System.Collections;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Networking;
public class GenshinTextToSpeech : TTS
{
#region 参数定义
/// <summary>
/// Genshin配置项
/// </summary>
/// <summary>
/// 朗读的角色
/// </summary>
[Header("朗读声音设置")]
public string speaker = "胡桃";
/// <summary>
/// 音频格式
/// </summary>
[Header("音频格式设置")]
public string format = "wav";
/// <summary>
/// 音频长度
/// </summary>
[Header("音频长度设置")]
public float length = 1f;
/// <summary>
/// 噪声
/// </summary>
[Header("噪声设置")]
public float noise = 0.5f;
/// <summary>
/// 噪声权重
/// </summary>
[Header("噪声权重设置")]
public float noisew = 0.9f;
/// <summary>
/// 声调比例
/// </summary>
[Header("声调比例设置")]
public float sdp_ratio = 0.2f;
#endregion
private void Awake()
{
m_PostURL = "https://genshinvoice.top/api";
}
/// <summary>
/// 语音合成
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
public override void Speak(string _msg, Action<AudioClip> _callback)
{
StartCoroutine(GetVoice(_msg, _callback));
}
/// <summary>
/// 语音合成,返回合成文本
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
public override void Speak(string _msg, Action<AudioClip, string> _callback)
{
StartCoroutine(GetVoice(_msg, _callback));
}
/// <summary>
/// restful api语音合成
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
/// <returns></returns>
private IEnumerator GetVoice(string _msg, Action<AudioClip> _callback)
{
stopwatch.Restart();
//发送报文
string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);
using (UnityWebRequest speechRequest = new UnityWebRequest(m_PostURL, "POST"))
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(textToSpeechRequestBody);
speechRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);
speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.uri, AudioType.WAV);
yield return speechRequest.SendWebRequest();
if (speechRequest.responseCode == 200)
{
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);
_callback(audioClip);
}
else
{
Debug.LogError("语音合成失败: " + speechRequest.error);
}
}
stopwatch.Stop();
Debug.Log("Genshin语音合成耗时:" + stopwatch.Elapsed.TotalSeconds);
}
/// <summary>
/// restful api语音合成,返回合成文本
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
/// <returns></returns>
private IEnumerator GetVoice(string _msg, Action<AudioClip, string> _callback)
{
stopwatch.Restart();
//发送报文
string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);
using (UnityWebRequest speechRequest = new UnityWebRequest(m_PostURL, "POST"))
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(textToSpeechRequestBody);
speechRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);
speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.uri, AudioType.WAV);
Debug.Log("传参的值是:" + textToSpeechRequestBody);
yield return speechRequest.SendWebRequest();
if (speechRequest.responseCode == 200)
{
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);
_callback(audioClip, _msg);
}
else
{
Debug.LogError("语音合成失败: " + speechRequest.error);
}
}
stopwatch.Stop();
Debug.Log("Genshin语音合成耗时:" + stopwatch.Elapsed.TotalSeconds);
}
/// <summary>
/// 报文格式转换
/// </summary>
/// <param name="speaker"></param>
/// <param name="msg"></param>
/// <param name="format"></param>
/// <param name="length"></param>
/// <param name="noise"></param>
/// <param name="noisew"></param>
/// <param name="sdp_ratio"></param>
/// <returns></returns>
public string GenerateTextToSpeech(string speaker, string msg, string format, float length, float noise, float noisew, float sdp_ratio)
{
string urlParams = string.Format("?speaker={0}&text={1}&format={2}&length={3}&noise={4}&noisew={5}&sdp_ratio={6}", speaker, msg, format, length, noise, noisew, sdp_ratio);
string url = m_PostURL + urlParams;
return url;
}
}
步骤:
- 修改
GetVoice方法,将返回的音频URL作为回调参数之一传递给回调函数。
private IEnumerator GetVoice(string _msg, Action<AudioClip, string> _callback)
{
// ...
using (UnityWebRequest speechRequest = new UnityWebRequest(m_PostURL, "POST"))
{
// ...
yield return speechRequest.SendWebRequest();
if (speechRequest.responseCode == 200)
{
string audioUrl = speechRequest.GetResponseHeader("Location");
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);
_callback(audioClip, audioUrl);
}
else
{
Debug.LogError("语音合成失败: " + speechRequest.error);
}
}
// ...
}
- 在需要播放音频的地方,使用Unity的音频播放器进行播放。
// 使用 GetVoice 方法获取音频
Speak("要朗读的文本", (audioClip, audioUrl) =>
{
// 播放音频
AudioSource.PlayClipAtPoint(audioClip, Vector3.zero);
});
注意:
- 确保您的代码中包含必要的
using语句。 m_PostURL应替换为您的 Genshin Voice API 的实际 URL。GetResponseHeader("Location")获取返回的音频URL。AudioSource.PlayClipAtPoint使用Unity的音频播放器播放音频。
最终代码:
using System;
using System.Collections;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.Networking;
public class GenshinTextToSpeech : TTS
{
#region 参数定义
/// <summary>
/// Genshin配置项
/// </summary>
/// <summary>
/// 朗读的角色
/// </summary>
[Header("朗读声音设置")]
public string speaker = "胡桃";
/// <summary>
/// 音频格式
/// </summary>
[Header("音频格式设置")]
public string format = "wav";
/// <summary>
/// 音频长度
/// </summary>
[Header("音频长度设置")]
public float length = 1f;
/// <summary>
/// 噪声
/// </summary>
[Header("噪声设置")]
public float noise = 0.5f;
/// <summary>
/// 噪声权重
/// </summary>
[Header("噪声权重设置")]
public float noisew = 0.9f;
/// <summary>
/// 声调比例
/// </summary>
[Header("声调比例设置")]
public float sdp_ratio = 0.2f;
#endregion
private void Awake()
{
m_PostURL = "https://genshinvoice.top/api";
}
/// <summary>
/// 语音合成
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
public override void Speak(string _msg, Action<AudioClip> _callback)
{
StartCoroutine(GetVoice(_msg, _callback));
}
/// <summary>
/// 语音合成,返回合成文本
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
public override void Speak(string _msg, Action<AudioClip, string> _callback)
{
StartCoroutine(GetVoice(_msg, _callback));
}
/// <summary>
/// restful api语音合成
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
/// <returns></returns>
private IEnumerator GetVoice(string _msg, Action<AudioClip> _callback)
{
stopwatch.Restart();
//发送报文
string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);
using (UnityWebRequest speechRequest = new UnityWebRequest(m_PostURL, "POST"))
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(textToSpeechRequestBody);
speechRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);
speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.uri, AudioType.WAV);
yield return speechRequest.SendWebRequest();
if (speechRequest.responseCode == 200)
{
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);
_callback(audioClip);
}
else
{
Debug.LogError("语音合成失败: " + speechRequest.error);
}
}
stopwatch.Stop();
Debug.Log("Genshin语音合成耗时:" + stopwatch.Elapsed.TotalSeconds);
}
/// <summary>
/// restful api语音合成,返回合成文本
/// </summary>
/// <param name="_msg"></param>
/// <param name="_callback"></param>
/// <returns></returns>
private IEnumerator GetVoice(string _msg, Action<AudioClip, string> _callback)
{
stopwatch.Restart();
//发送报文
string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);
using (UnityWebRequest speechRequest = new UnityWebRequest(m_PostURL, "POST"))
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(textToSpeechRequestBody);
speechRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);
speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.uri, AudioType.WAV);
Debug.Log("传参的值是:" + textToSpeechRequestBody);
yield return speechRequest.SendWebRequest();
if (speechRequest.responseCode == 200)
{
string audioUrl = speechRequest.GetResponseHeader("Location");
AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);
_callback(audioClip, audioUrl);
}
else
{
Debug.LogError("语音合成失败: " + speechRequest.error);
}
}
stopwatch.Stop();
Debug.Log("Genshin语音合成耗时:" + stopwatch.Elapsed.TotalSeconds);
}
/// <summary>
/// 报文格式转换
/// </summary>
/// <param name="speaker"></param>
/// <param name="msg"></param>
/// <param name="format"></param>
/// <param name="length"></param>
/// <param name="noise"></param>
/// <param name="noisew"></param>
/// <param name="sdp_ratio"></param>
/// <returns></returns>
public string GenerateTextToSpeech(string speaker, string msg, string format, float length, float noise, float noisew, float sdp_ratio)
{
string urlParams = string.Format("?speaker={0}&text={1}&format={2}&length={3}&noise={4}&noisew={5}&sdp_ratio={6}", speaker, msg, format, length, noise, noisew, sdp_ratio);
string url = m_PostURL + urlParams;
return url;
}
}
现在,您可以在Unity中使用该代码来合成语音并播放音频。
原文地址: https://www.cveoy.top/t/topic/jCJf 著作权归作者所有。请勿转载和采集!