Пример #1
0
        private static IEnumerator SynthesisByKEYCoroutine(string text, HTFAction <AudioClip> handler, int timeout, Speaker speaker, int volume, int speed, int pitch)
        {
            Tts tts = GetTts();

            tts.Timeout       = timeout;
            TtsOptions["spd"] = Mathf.Clamp(speed, 0, 9);
            TtsOptions["pit"] = Mathf.Clamp(pitch, 0, 9);
            TtsOptions["vol"] = Mathf.Clamp(volume, 0, 15);
            TtsOptions["per"] = (int)speaker;
            TtsOptions["aue"] = 6;
            TtsResponse response = tts.Synthesis(text, TtsOptions);

            yield return(response);

            if (response.Success)
            {
                AudioClip audioClip = SpeechUtility.ToAudioClip(response.Data);

                handler?.Invoke(audioClip);
            }
            else
            {
                GlobalTools.LogError("合成语音失败:" + response.ErrorCode + " " + response.ErrorMsg);
            }
            RecycleTts(tts);
        }
Пример #2
0
        /// <summary>
        /// 语音识别
        /// </summary>
        /// <param name="clip">语音音频</param>
        /// <param name="handler">识别成功处理者</param>
        /// <param name="failHandler">识别失败处理者</param>
        /// <returns>语音识别的协程</returns>
        public static Coroutine Recognition(AudioClip clip, HTFAction <string> handler, HTFAction failHandler)
        {
            if (clip == null)
            {
                Log.Error("语音识别失败:语音内容为空!");
                return(null);
            }

            return(Main.Current.StartCoroutine(RecognitionCoroutine(SpeechUtility.FromAudioClip(clip), handler, failHandler)));
        }
Пример #3
0
        private static IEnumerator SynthesisByTOKENCoroutine(string text, HTFAction <AudioClip> handler, int timeout, Speaker speaker, int volume, int speed, int pitch)
        {
            string url = string.Format("http://tsn.baidu.com/text2audio?tex='{0}'&tok={1}&cuid={2}&ctp={3}&lan={4}&spd={5}&pit={6}&vol={7}&per={8}&aue={9}",
                                       text, TOKEN, SystemInfo.deviceUniqueIdentifier, 1, "zh", speed, pitch, volume, (int)speaker, 6);

            UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(url, AudioType.WAV);

            yield return(request.SendWebRequest());

            if (!request.isNetworkError && !request.isHttpError)
            {
                AudioClip audioClip = SpeechUtility.ToAudioClip(request.downloadHandler.data);

                handler?.Invoke(audioClip);
            }
            else
            {
                GlobalTools.LogError("合成语音失败:" + request.responseCode + " " + request.error);
            }
        }