using System; using System.Collections; using System.Xml.Linq; using UnityEngine; using UnityEngine.Networking;

public class GenshinTextToSpeech : TTS { #region 参数定义 ///

/// Genshin配置项 /// /// /// 朗读的角色 /// [Header("朗读声音设置")] public string speaker = "胡桃"; /// /// 音频格式 /// [Header("音频格式设置")] public string format = "wav"; /// /// 音频长度 /// [Header("音频长度设置")] public float length = 1f; /// /// 噪声 /// [Header("噪声设置")] public float noise = 0.5f; /// /// 噪声权重 /// [Header("噪声权重设置")] public float noisew = 0.9f; /// /// 声调比例 /// [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 = UnityWebRequest.Get(textToSpeechRequestBody))
    {
        speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.uri, AudioType.WAV);

        // 修改X-Requested-With请求头为XMLHttpRequest
        speechRequest.SetRequestHeader("X-Requested-With", "XMLHttpRequest");

        yield return speechRequest.SendWebRequest();

        if (speechRequest.result == UnityWebRequest.Result.Success)
        {
            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 = UnityWebRequest.Get(textToSpeechRequestBody))
    {
        speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.uri, AudioType.WAV);

        // 修改X-Requested-With请求头为XMLHttpRequest
        speechRequest.SetRequestHeader("X-Requested-With", "XMLHttpRequest");

        yield return speechRequest.SendWebRequest();

        if (speechRequest.result == UnityWebRequest.Result.Success)
        {
            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;
}

}

将XHR类型改为JSONP类型发送完整示例修改后的代码:using System;using SystemCollections;using SystemXmlLinq;using UnityEngine;using UnityEngineNetworking;public class GenshinTextToSpeech TTS #region 参数定义 summary

原文地址: http://www.cveoy.top/t/topic/jaub 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录