コード例 #1
0
        public IEnumerator Synthesis(string text, Action <TtsResponse> callback, int speed = 6, int pit = 5, int vol = 5,
                                     Pronouncer per = Pronouncer.Duyaya)
        {
            yield return(PreAction());

            if (tokenFetchStatus == Base.TokenFetchStatus.Failed)
            {
                Debug.LogError("Token was fetched failed. Please check your APIKey and SecretKey");
                callback(new TtsResponse()
                {
                    err_no  = -1,
                    err_msg = "Token was fetched failed. Please check your APIKey and SecretKey"
                });
                yield break;
            }

            var param = new Dictionary <string, string>();

            param.Add("tex", text);
            param.Add("tok", Token);
            param.Add("cuid", SystemInfo.deviceUniqueIdentifier);
            param.Add("ctp", "1");
            param.Add("lan", "zh");
            param.Add("spd", Mathf.Clamp(speed, 0, 9).ToString());
            param.Add("pit", Mathf.Clamp(pit, 0, 9).ToString());
            param.Add("vol", Mathf.Clamp(vol, 0, 15).ToString());
            param.Add("per", ((int)per).ToString());

            string url = UrlTts;
            int    i   = 0;

            foreach (var p in param)
            {
                url += i != 0 ? "&" : "?";
                url += p.Key + "=" + p.Value;
                i++;
            }

#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP
            var www = UnityWebRequest.Get(url);
#else
            var www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
#endif
            yield return(www.SendWebRequest());


            if (string.IsNullOrEmpty(www.error))
            {
                var type = www.GetResponseHeader("Content-Type");
                Debug.Log("response type: " + type);

                if (type == "audio/mp3")
                {
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP
                    var clip     = GetAudioClipFromMP3ByteArray(www.downloadHandler.data);
                    var response = new TtsResponse {
                        clip = clip
                    };
#else
                    var response = new TtsResponse {
                        clip = DownloadHandlerAudioClip.GetContent(www)
                    };
#endif
                    callback(response);
                }
                else
                {
                    Debug.LogError(www.downloadHandler.text);
                    callback(JsonUtility.FromJson <TtsResponse>(www.downloadHandler.text));
                }
            }
            else
            {
                Debug.LogError(www.error);
            }
        }
コード例 #2
0
ファイル: Tts.cs プロジェクト: Crates9/DoSomeThing-ByUnity
        public IEnumerator Synthesis(string text, Action <TtsResponse> callback, int speed = 5, int pit = 5, int vol = 5,
                                     Pronouncer per = Pronouncer.Duxiaoyao)
        {
            yield return(PreAction());

            if (tokenFetchStatus == Base.TokenFetchStatus.Failed)
            {
                Debug.LogError("Token不对");
                callback(new TtsResponse()
                {
                    err_no  = -1,
                    err_msg = "Token不对"
                });
                yield break;
            }

            var param = new Dictionary <string, string>();

            param.Add("tex", text);
            param.Add("tok", Token);
            param.Add("cuid", SystemInfo.deviceUniqueIdentifier);
            param.Add("ctp", "1");
            param.Add("lan", "zh");
            param.Add("spd", Mathf.Clamp(speed, 0, 9).ToString());
            param.Add("pit", Mathf.Clamp(pit, 0, 9).ToString());
            param.Add("vol", Mathf.Clamp(vol, 0, 15).ToString());
            param.Add("per", ((int)per).ToString());
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP
            param.Add("aue", "6"); // set to wav, default is mp3
#endif

            string url = UrlTts;
            int    i   = 0;
            foreach (var p in param)
            {
                url += i != 0 ? "&" : "?";
                url += p.Key + "=" + p.Value;
                i++;
            }

#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP
            var www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV);
#else
            var www = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.MPEG);
#endif
            Debug.Log("[WitBaiduAip]" + www.url);
            yield return(www.SendWebRequest());


            if (string.IsNullOrEmpty(www.error))
            {
                var type = www.GetResponseHeader("Content-Type");
                Debug.Log("[WitBaiduAip]response type: " + type);

                if (type.Contains("audio"))
                {
#if UNITY_STANDALONE || UNITY_EDITOR || UNITY_UWP
                    var clip     = DownloadHandlerAudioClip.GetContent(www);
                    var response = new TtsResponse {
                        clip = clip
                    };
#else
                    var response = new TtsResponse {
                        clip = DownloadHandlerAudioClip.GetContent(www)
                    };
#endif
                    callback(response);
                }
                else
                {
                    var textBytes = www.downloadHandler.data;
                    var errorText = Encoding.UTF8.GetString(textBytes);
                    Debug.LogError("[WitBaiduAip]" + errorText);
                    callback(JsonUtility.FromJson <TtsResponse>(errorText));
                }
            }
            else
            {
                Debug.LogError(www.error);
            }
        }
コード例 #3
0
        public IEnumerator Synthesis(string text, Action <TtsResponse> callback, int speed = 5, int pit = 5, int vol = 5,
                                     Pronouncer per = Pronouncer.Female)
        {
            yield return(PreAction());

            if (tokenFetchStatus == Base.TokenFetchStatus.Failed)
            {
                Debug.LogError("Token was fetched failed. Please check your APIKey and SecretKey");
                callback(new TtsResponse()
                {
                    err_no  = -1,
                    err_msg = "Token was fetched failed. Please check your APIKey and SecretKey"
                });
                yield break;
            }

            var param = new Dictionary <string, string>();

            param.Add("tex", text);
            param.Add("tok", Token);
            param.Add("cuid", SystemInfo.deviceUniqueIdentifier);
            param.Add("ctp", "1");
            param.Add("lan", "zh");
            param.Add("spd", Mathf.Clamp(speed, 0, 9).ToString());
            param.Add("pit", Mathf.Clamp(pit, 0, 9).ToString());
            param.Add("vol", Mathf.Clamp(vol, 0, 15).ToString());
            param.Add("per", ((int)per).ToString());

            string url = UrlTts;
            int    i   = 0;

            foreach (var p in param)
            {
                url += i != 0 ? "&" : "?";
                url += p.Key + "=" + p.Value;
                i++;
            }

            WWW www = new WWW(url);

            yield return(www);


            if (string.IsNullOrEmpty(www.error))
            {
                var type = www.responseHeaders["Content-Type"];
                Debug.Log("response type: " + type);

                if (type == "audio/mp3")
                {
                    var response = new TtsResponse {
                        clip = FromMp3Data(www.bytes)
                    };
                    callback(response);
                }
                else
                {
                    Debug.LogError(www.text);
                    callback(JsonUtility.FromJson <TtsResponse>(www.text));
                }
            }
            else
            {
                Debug.LogError(www.error);
            }
        }