{"title":"Genshin Impact Text-to-Speech: C# Unity Script for Voice Synthesis","description":"This C# Unity script uses a RESTful API to generate Genshin Impact character voices, providing a customizable text-to-speech solution with various voice options and audio settings. ","keywords":"Genshin Impact, TTS, Text-to-Speech, Unity, C#, API, Voice Synthesis, Game Development, Audio, Character Voices","content":"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 /// \n /// \n public override void Speak(string _msg, Action _callback) \n {\n StartCoroutine(GetVoice(_msg, _callback)); \n } \n\n /// \n /// 语音合成,返回合成文本 \n /// \n /// \n /// \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 /// \n /// \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 /// \n /// \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.uri, AudioType.WAV); \n Debug.Log("传参的值是:" + textToSpeechRequestBody); \n yield return speechRequest.SendWebRequest(); \n\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 /// \n /// \n /// \n /// \n /// \n /// \n /// \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 } \n} \n错误信息 \n语音合成失败: HTTP/1.1 500 Internal Server Error \nUnityEngine.Debug:LogError (object) \nGenshinTextToSpeech/d__10:MoveNext () (at Assets/AIChatTookit/Scripts/TTS&&STT/Genshin/GenshinTextToSpeech.cs:135) \nUnityEngine.SetupCoroutine:InvokeMoveNext (System.Collections.IEnumerator,intptr) \n\n\n那么为什么没有下载服务端已经生成的api音频,反而报错500了呢?如何获取到返回页面内的音频,请完整示例一下解析HTML内容获取音频的URL,并使用Unity的音频播放器进行播放。 \n内容:报错500意味着服务器内部出现了错误,可能是由于服务器端的代码问题导致的。您可以尝试联系服务器端的开发人员以获取更多关于500错误的详细信息,并修复服务器端的问题。 \n\n关于获取返回页面内的音频URL并使用Unity的音频播放器进行播放的示例代码,您可以参考以下示例: \n\ncsharp \nprivate IEnumerator GetVoice(string _msg, Action<AudioClip> _callback) \n{ \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 = new DownloadHandlerBuffer(); \n\n yield return speechRequest.SendWebRequest(); \n\n if (speechRequest.responseCode == 200) \n { \n string responseText = speechRequest.downloadHandler.text; \n string audioUrl = ParseAudioUrlFromResponse(responseText); \n\n // 使用Unity的音频播放器进行播放 \n using (UnityWebRequest audioRequest = UnityWebRequestMultimedia.GetAudioClip(audioUrl, AudioType.WAV)) \n { \n yield return audioRequest.SendWebRequest(); \n\n if (audioRequest.result == UnityWebRequest.Result.Success) \n { \n AudioClip audioClip = DownloadHandlerAudioClip.GetContent(audioRequest); \n _callback(audioClip); \n } \n else \n { \n Debug.LogError("音频下载失败: " + audioRequest.error); \n } \n } \n } \n else \n { \n Debug.LogError("语音合成失败: " + speechRequest.error); \n } \n } \n} \n\nprivate string ParseAudioUrlFromResponse(string responseText) \n{ \n // 解析HTML内容获取音频的URL \n string audioUrl = ""; \n\n // 在这里编写解析HTML内容的代码,获取音频的URL \n\n return audioUrl; \n} \n \n\n在上述代码中,ParseAudioUrlFromResponse方法用于解析HTML内容并获取音频的URL。您需要根据实际情况编写解析HTML内容的代码,以从返回的HTML页面中提取出音频的URL。 \n\n然后,使用UnityWebRequestMultimedia.GetAudioClip方法下载音频文件,并使用Unity的音频播放器进行播放。 \n\n请注意,解析HTML内容并提取音频URL可能需要使用一些HTML解析库或正则表达式等技术。具体的解析方法取决于返回的HTML内容的结构和格式。

Genshin Impact Text-to-Speech: C# Unity Script for Voice Synthesis

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

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