using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking;

public class WhisperTextToSpeech : TTS { private void Awake() { m_TextToSpeechURL = GetPostUrl(); }

/// <summary>
/// 服务地址
/// </summary>
[SerializeField] private string m_ServerSetting = "https://genshinvoice.top/api";
/// <summary>
/// 语音合成接口路径
/// </summary>
[SerializeField] private string m_APIPath = "synthesize";
/// <summary>
/// 语音合成接口参数
/// </summary>
[SerializeField] private string m_Parameters = "speaker=胡桃&format=wav&length=1&noise=0.5&noisew=0.9&sdp_ratio=0.2";

/// <summary>
/// 合成语音的URL地址
/// </summary>
private string m_TextToSpeechURL;

/// <summary>
/// 获取请求的URL
/// </summary>
/// <returns></returns>
private string GetPostUrl()
{
    string _url = string.Format("{0}/{1}?{2}", m_ServerSetting, m_APIPath, m_Parameters);
    return _url;
}

/// <summary>
/// 文字转语音
/// </summary>
/// <param name="_text"></param>
/// <param name="_callback"></param>
public override void TextToSpeech(string _text, Action<AudioClip> _callback)
{
    StartCoroutine(SendTextData(_text, _callback));
}

/// <summary>
/// 发送文字数据到API
/// </summary>
/// <param name="_text"></param>
/// <param name="_callback"></param>
/// <returns></returns>
private IEnumerator SendTextData(string _text, Action<AudioClip> _callback)
{
    stopwatch.Restart();
    string _url = m_TextToSpeechURL + "&msg=" + UnityWebRequest.EscapeURL(_text);
    UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(_url, AudioType.WAV);
    yield return www.SendWebRequest();

    if (www.result != UnityWebRequest.Result.Success)
    {
        Debug.LogError("Error sending text data: " + www.error);
    }
    else
    {
        AudioClip _audioClip = DownloadHandlerAudioClip.GetContent(www);
        _callback(_audioClip);
    }

    stopwatch.Stop();
    Debug.Log("Whisper语音合成耗时:" + stopwatch.Elapsed.TotalSeconds);
}

}

写一个unity的文字合成语音的API对应_msg为下面参考示例中参数这里是API的接口地址和参数: httpsgenshinvoicetopapispeaker=胡桃&msg=你好&format=wav&length=1&noise=05&noisew=09&sdp_ratio=02 下方是示例请求的样式请改为前面genshinvoice的请求地址和参数方式 using System;using

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

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