Ejemplo n.º 1
0
        public static byte[] GenerateTTS(string serverUrl, ParamsTTS tts, string serverToken = null)
        {
            var request = GenerateTTSRequest(tts);
            var json    = JsonConvert.SerializeObject(request, new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            Uri baseUri = new Uri(serverUrl);
            Uri reqUri  = new Uri(baseUri, "/dh/api/v1/tts");

            var webRequest = UnityWebRequest.Put(reqUri, json);

            if (!string.IsNullOrEmpty(serverToken))
            {
                webRequest.SetRequestHeader("Authorization", "Bearer " + serverToken);
            }
            webRequest.SetRequestHeader("Content-Type", "application/json");
            webRequest.method = "POST";

            var webRequestHandler = webRequest.SendWebRequest();

            while (!webRequestHandler.isDone)
            {
            }

            if (webRequest.isNetworkError)
            {
                Debug.LogError("TTS failed with network error: " + webRequest.error);
                return(null);
            }

            if (webRequest.isHttpError)
            {
                var text    = webRequest.downloadHandler.text;
                var errCode = webRequest.responseCode;

                Debug.LogError($"TTS failed with response code {errCode}. " +
                               $"Server returned message: {text}");
                return(null);
            }

            var data = webRequest.downloadHandler.data;

            return(data);
        }
Ejemplo n.º 2
0
        public static Request <AttributesTTS> GenerateTTSRequest(ParamsTTS paramsTTS)
        {
            var request = new Request <AttributesTTS>
            {
                data = new Data <AttributesTTS>
                {
                    id         = Guid.NewGuid().ToString(),
                    type       = "textToSpeech",
                    attributes = new AttributesTTS()
                    {
                        tts = paramsTTS
                    }
                }
            };

            return(request);
        }