using System;/nusing System.Collections;/nusing System.Xml.Linq;/nusing UnityEngine;/nusing UnityEngine.Networking;/n/npublic class GenshinTextToSpeech : TTS/n{/n/n #region 参数定义/n ///

/n /// Genshin配置项/n /// /n /// /n /// 朗读的角色/n /// /n [Header(/'朗读声音设置/')]/n public string speaker = /'胡桃/';/n /// /n /// 音频格式/n /// /n [Header(/'音频格式设置/')]/n public string format = /'wav/';/n /// /n /// 音频长度/n /// /n [Header(/'音频长度设置/')]/n public float length = 1f;/n /// /n /// 噪声/n /// /n [Header(/'噪声设置/')]/n public float noise = 0.5f;/n /// /n /// 噪声权重/n /// /n [Header(/'噪声权重设置/')]/n public float noisew = 0.9f;/n /// /n /// 声调比例/n /// /n [Header(/'声调比例设置/')]/n public float sdp_ratio = 0.2f;/n/n #endregion/n/n private void Awake()/n {/n m_PostURL = /'https://genshinvoice.top/api/'; /n }/n/n /// /n /// 语音合成/n /// /n /// <param name=/'_msg/'>/n /// <param name=/'_callback/'>/n public override void Speak(string _msg, Action _callback)/n {/n StartCoroutine(GetVoice(_msg, _callback));/n }/n/n /// /n /// 语音合成,返回合成文本/n /// /n /// <param name=/'_msg/'>/n /// <param name=/'_callback/'>/n public override void Speak(string _msg, Action<AudioClip, string> _callback)/n {/n StartCoroutine(GetVoice(_msg, _callback));/n }/n/n /// /n /// restful api语音合成/n /// /n /// <param name=/'_msg/'>/n /// <param name=/'_callback/'>/n /// /n private IEnumerator GetVoice(string _msg, Action _callback)/n {/n stopwatch.Restart();/n //发送报文/n string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);/n/n using (UnityWebRequest speechRequest = new UnityWebRequest(m_PostURL, /'POST/'))/n {/n byte[] data = System.Text.Encoding.UTF8.GetBytes(textToSpeechRequestBody);/n speechRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);/n speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.uri, AudioType.WAV);/n/n yield return speechRequest.SendWebRequest();/n/n if (speechRequest.responseCode == 200)/n {/n AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);/n _callback(audioClip);/n }/n else/n {/n Debug.LogError(/'语音合成失败: /' + speechRequest.error);/n }/n }/n/n stopwatch.Stop();/n Debug.Log(/'Genshin语音合成耗时:/' + stopwatch.Elapsed.TotalSeconds);/n }/n/n /// /n /// restful api语音合成,返回合成文本/n /// /n /// <param name=/'_msg/'>/n /// <param name=/'_callback/'>/n /// /n private IEnumerator GetVoice(string _msg, Action<AudioClip, string> _callback)/n {/n stopwatch.Restart();/n //发送报文/n string textToSpeechRequestBody = GenerateTextToSpeech(speaker, _msg, format, length, noise, noisew, sdp_ratio);/n/n using (UnityWebRequest speechRequest = new UnityWebRequest(m_PostURL, /'POST/'))/n {/n byte[] data = System.Text.Encoding.UTF8.GetBytes(textToSpeechRequestBody);/n speechRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(data);/n speechRequest.downloadHandler = (DownloadHandler)new DownloadHandlerAudioClip(speechRequest.url, AudioType.WAV);/n // Force cast to DownloadHandlerAudioClip and get the url/n speechRequest.SendWebRequest().completed += (operation) =>/n {/n DownloadHandlerAudioClip downloadHandler = (DownloadHandlerAudioClip)speechRequest.downloadHandler;/n Debug.Log(/'传参的值是:/' + downloadHandler.url);/n };/n yield return speechRequest.SendWebRequest();/n if (speechRequest.responseCode == 200)/n {/n AudioClip audioClip = DownloadHandlerAudioClip.GetContent(speechRequest);/n _callback(audioClip, _msg);/n }/n else/n {/n /n Debug.LogError(/'语音合成失败: /' + speechRequest.error);/n }/n }/n/n stopwatch.Stop();/n Debug.Log(/'Genshin语音合成耗时:/' + stopwatch.Elapsed.TotalSeconds);/n }/n/n /// /n /// 报文格式转换/n /// /n /// <param name=/'speaker/'>/n /// <param name=/'msg/'>/n /// <param name=/'format/'>/n /// <param name=/'length/'>/n /// <param name=/'noise/'>/n /// <param name=/'noisew/'>/n /// <param name=/'sdp_ratio/'>/n /// /n public string GenerateTextToSpeech(string speaker, string msg, string format, float length, float noise, float noisew, float sdp_ratio)/n {/n 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);/n string url = m_PostURL + urlParams;/n/n return url;/n }/

Genshin Impact Text-to-Speech Unity Plugin

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

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