GenshinTextToSpeech: 基于原神语音 API 的 Unity 文本转语音工具
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);
Debug.Log('传参的值是:' + textToSpeechRequestBody);
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);
Debug.Log('传参的值是:' + textToSpeechRequestBody);
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, _msg);
}
else
{
Debug.Log('传参的值是:' + ?);
Debug.LogError('语音合成失败: ' + speechRequest.error);
}
}
stopwatch.Stop();
Debug.Log('Genshin语音合成耗时:' + stopwatch.Elapsed.TotalSeconds);
}
}
原文地址: https://www.cveoy.top/t/topic/jJHi 著作权归作者所有。请勿转载和采集!