Ejemplo n.º 1
0
 public void Go()
 {
     if (!isStartPlaying)
     {
         isStartPlaying = true;
         coroutineUtility.StartCoroutine(DoQueue());
     }
 }
Ejemplo n.º 2
0
        // Iterate phrases
        private static void IteratePhrases(TTSService service, TTSPreloadData preloadData, TTSPreloadIterateDelegate onIterate, Action <float> onProgress, Action <string> onComplete)
        {
            // No service
            if (service == null)
            {
                onComplete?.Invoke("\nNo TTSService found in current scene");
                return;
            }
            // No preload data
            if (preloadData == null)
            {
                onComplete?.Invoke("\nTTS Preload Data Not Found");
                return;
            }
            // Ignore if running
            if (Application.isPlaying)
            {
                onComplete?.Invoke("Cannot preload while running");
                return;
            }

            // Unload previous coroutine performer
            if (_performer != null)
            {
                MonoBehaviour.DestroyImmediate(_performer.gameObject);
                _performer = null;
            }

            // Run new coroutine
            _performer = CoroutineUtility.StartCoroutine(PerformIteratePhrases(service, preloadData, onIterate, onProgress, onComplete));
        }
Ejemplo n.º 3
0
        // While active, perform any sent callbacks
        private void WatchMainThreadCallbacks()
        {
            // Ifnore if already performing
            if (_performer != null)
            {
                return;
            }

            // Check callbacks every frame (editor or runtime)
            _performer = CoroutineUtility.StartCoroutine(PerformMainThreadCallbacks());
        }
Ejemplo n.º 4
0
        // Request setup
        public virtual void Setup(UnityWebRequest newRequest, Action <float> newProgress, Action <UnityWebRequest> newComplete)
        {
            // Already setup
            if (_request != null)
            {
                return;
            }

            // Setup
            _request      = newRequest;
            _onProgress   = newProgress;
            _onComplete   = newComplete;
            _transmitting = false;
            _progress     = 0f;

            // Use default timeout
            if (newRequest.timeout <= 0)
            {
                newRequest.timeout = Timeout;
            }

            // Begin
            _coroutine = CoroutineUtility.StartCoroutine(PerformUpdate());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Perform a request for a TTS audio clip
        /// </summary>
        /// <param name="textToSpeak">Text to be spoken in clip</param>
        /// <param name="clipID">Unique clip id</param>
        /// <param name="voiceSettings">Custom voice settings</param>
        /// <param name="diskCacheSettings">Custom cache settings</param>
        /// <returns>Generated TTS clip data</returns>
        public virtual TTSClipData Load(string textToSpeak, string clipID, TTSVoiceSettings voiceSettings,
                                        TTSDiskCacheSettings diskCacheSettings, Action <TTSClipData, string> onStreamReady)
        {
            // Add delegates if needed
            AddDelegates();

            // Get clip data
            TTSClipData clipData = CreateClipData(textToSpeak, clipID, voiceSettings, diskCacheSettings);

            if (clipData == null)
            {
                Log("No clip provided", TTSLogType.Error);
                onStreamReady?.Invoke(clipData, "No clip provided");
                return(null);
            }

            // From Runtime Cache
            if (clipData.loadState != TTSClipLoadState.Unloaded)
            {
                // Add callback
                if (onStreamReady != null)
                {
                    // Call once ready
                    if (clipData.loadState == TTSClipLoadState.Preparing)
                    {
                        clipData.onPlaybackReady += (e) => onStreamReady(clipData, e);
                    }
                    // Call after return
                    else
                    {
                        CoroutineUtility.StartCoroutine(CallAfterAMoment(() => onStreamReady(clipData,
                                                                                             clipData.loadState == TTSClipLoadState.Loaded ? string.Empty : "Error")));
                    }
                }

                // Return clip
                return(clipData);
            }

            // Add to runtime cache if possible
            if (RuntimeCacheHandler != null)
            {
                RuntimeCacheHandler.AddClip(clipData);
            }
            // Load begin
            else
            {
                OnLoadBegin(clipData);
            }

            // Add on ready delegate
            clipData.onPlaybackReady += (error) => onStreamReady(clipData, error);

            // Wait a moment and load
            CoroutineUtility.StartCoroutine(CallAfterAMoment(() =>
            {
                // Check for invalid text
                string invalidError = WebHandler.IsTextValid(clipData.textToSpeak);
                if (!string.IsNullOrEmpty(invalidError))
                {
                    OnWebStreamError(clipData, invalidError);
                    return;
                }

                // If should cache to disk, attempt to do so
                if (ShouldCacheToDisk(clipData))
                {
                    // Download was canceled before starting
                    if (clipData.loadState != TTSClipLoadState.Preparing)
                    {
                        string downloadPath = DiskCacheHandler.GetDiskCachePath(clipData);
                        OnWebDownloadBegin(clipData, downloadPath);
                        OnWebDownloadCancel(clipData, downloadPath);
                        OnWebStreamBegin(clipData);
                        OnWebStreamCancel(clipData);
                        return;
                    }

                    // Download
                    DownloadToDiskCache(clipData, (clipData2, downloadPath, error) =>
                    {
                        // Download was canceled before starting
                        if (string.Equals(error, CANCEL_WARNING))
                        {
                            OnWebStreamBegin(clipData);
                            OnWebStreamCancel(clipData);
                            return;
                        }

                        // Success
                        if (string.IsNullOrEmpty(error))
                        {
                            DiskCacheHandler?.StreamFromDiskCache(clipData);
                        }
                        // Failed
                        else
                        {
                            WebHandler?.RequestStreamFromWeb(clipData);
                        }
                    });
                }
                // Simply stream from the web
                else
                {
                    // Stream was canceled before starting
                    if (clipData.loadState != TTSClipLoadState.Preparing)
                    {
                        OnWebStreamBegin(clipData);
                        OnWebStreamCancel(clipData);
                        return;
                    }

                    // Stream
                    WebHandler?.RequestStreamFromWeb(clipData);
                }
            }));

            // Return data
            return(clipData);
        }