////////////////////////////////////////////////////////////////////////// void OnDestroy() { if (Instance == this) { Instance = null; } }
bool TTS_IsSpeaking() { if (UAP_CustomTTS.IsInitialized() == UAP_CustomTTS.TTSInitializationState.NotInitialized) { #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN if (UAP_AccessibilityManager.UseWindowsTTS() && WindowsTTS.instance != null) { return(WindowsTTS.IsSpeaking()); } else { return(false); } #elif (UNITY_STANDALONE_OSX || UNITY_EDITOR) && !UNITY_WEBPLAYER if (UAP_AccessibilityManager.UseMacOSTTS()) { return(MacOSTTS.instance.IsSpeaking()); } else { return(false); } #elif UNITY_ANDROID return(AndroidTTS.IsSpeaking()); //bool isTTSSpeaking = AndroidTTS.IsSpeaking(); //if (!isTTSSpeaking) // return false; //return m_TTS_SpeakingTimer > 0.0f; #elif UNITY_IOS // VoiceOver sometimes times out without notification if (!m_LastEntryUsedVoiceOver) { return(iOSTTS.IsSpeaking()); } // VoiceOver is not 100% reliable unfortunately bool VOSpeaking = iOSTTS.IsSpeaking(); if (!VOSpeaking) { return(false); } return(m_TTS_SpeakingTimer > 0.0f); #else return(false); #endif } else { return(UAP_CustomTTS.IsSpeaking()); } }
void StopAudio(bool includingAndroid = false) { if (m_AudioPlayer.isPlaying) { m_AudioPlayer.Stop(); m_AudioPlayer.clip = null; } if (UAP_CustomTTS.IsInitialized() == UAP_CustomTTS.TTSInitializationState.NotInitialized) { #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN if (UAP_AccessibilityManager.UseWindowsTTS() && WindowsTTS.instance != null) { WindowsTTS.Stop(); } #elif (UNITY_STANDALONE_OSX || UNITY_EDITOR) && !UNITY_WEBPLAYER if (UAP_AccessibilityManager.UseMacOSTTS()) { MacOSTTS.instance.Stop(); } #elif UNITY_ANDROID if (UAP_AccessibilityManager.UseAndroidTTS()) { m_TTS_SpeakingTimer = 0.0f; if (includingAndroid) { AndroidTTS.StopSpeaking(); } } #elif UNITY_IOS if (UAP_AccessibilityManager.UseiOSTTS()) { m_TTS_SpeakingTimer = 0.0f; iOSTTS.StopSpeaking(); } #endif } else { UAP_CustomTTS.Stop(); } }
////////////////////////////////////////////////////////////////////////// public static void InitializeCustomTTS <T>() { if (Instance != null) { return; } GameObject newTTSGO = new GameObject("Custom TTS"); newTTSGO.AddComponent(typeof(T)); Instance = newTTSGO.GetComponent <UAP_CustomTTS>(); if (Instance == null) { Debug.LogError("[TTS] Error creating custom TTS system. " + typeof(T).ToString() + " is not derived from UAP_CustomTTS"); return; } Debug.Log("[TTS] Initializing Custom TTS"); Instance.Initialize(); DontDestroyOnLoad(newTTSGO); }
////////////////////////////////////////////////////////////////////////// void TTS_Speak(string text, bool allowVoiceOver = true) { if (UAP_CustomTTS.IsInitialized() == UAP_CustomTTS.TTSInitializationState.NotInitialized) { #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN if (UAP_AccessibilityManager.UseWindowsTTS()) { InitializeWindowsTTS(); //Debug.Log("Speaking: " + text); WindowsTTS.Speak(text); } #elif (UNITY_STANDALONE_OSX || UNITY_EDITOR) && !UNITY_WEBPLAYER if (UAP_AccessibilityManager.UseMacOSTTS()) { //Debug.Log("Speaking: " + text); MacOSTTS.instance.Speak(text); } #elif UNITY_ANDROID if (UAP_AccessibilityManager.UseAndroidTTS()) { m_TTS_SpeakingTimer += (text.Length * 3.0f / 16.0f); AndroidTTS.SetSpeechRate(m_SpeechRate); AndroidTTS.Speak(text); } #elif UNITY_IOS if (UAP_AccessibilityManager.UseiOSTTS()) { m_TTS_SpeakingTimer += (text.Length * 3.0f / 16.0f); iOSTTS.StartSpeaking(text, allowVoiceOver && UAP_AccessibilityManager.IsVoiceOverAllowed(), m_SpeechRate); } #endif } else { UAP_CustomTTS.Speak(text, (m_SpeechRate + 50) / 100.0f); } }
////////////////////////////////////////////////////////////////////////// void Update() { m_CurrentQueueLength = m_AudioQueue.Count; m_CurrentPauseDuration = m_PauseTimer; // Check active entry (if any) and check whether audio is still playing (or pause still in effect) if (m_ActiveEntry != null) { if (m_ActiveEntry.m_AudioType == EAudioType.Pause) { m_CurrentElement = "Pause"; m_IsSpeaking = false; m_PauseTimer -= Time.unscaledDeltaTime; if (m_PauseTimer <= 0.0f) { // Pause is done. InvalidateActiveEntry(); } } else { m_CurrentElement = "Voice"; // Is the voice still playing/speaking? bool stillPlaying = IsPlaying(); m_IsSpeaking = stillPlaying; if (!stillPlaying) { InvalidateActiveEntry(); } } } else { m_CurrentElement = "none"; m_IsSpeaking = false; } if (m_TTS_SpeakingTimer > 0.0f) { m_TTS_SpeakingTimer -= Time.unscaledDeltaTime; } // Update audio queue // If the current entry is finished, get the next one from the queue if (m_ActiveEntry == null && m_AudioQueue.Count > 0) { bool dequeue = true; // Check whether it's a TTS entry AND we're using a custom TTS AND the TTS is not done initializing if (UAP_CustomTTS.IsInitialized() == UAP_CustomTTS.TTSInitializationState.InProgress) { if (m_AudioQueue.Peek().m_Audio == null) { // Next up is a TTS Entry, but custom TTS is still busy initializing. Waiting instead. dequeue = false; } } if (dequeue) { m_ActiveEntry = m_AudioQueue.Dequeue(); #if UNITY_IOS && !UNITY_EDITOR_WIN m_LastEntryUsedVoiceOver = false; #endif if (m_ActiveEntry.m_AudioType == EAudioType.Pause) { m_PauseTimer = m_ActiveEntry.m_PauseDuration; } else { if (m_ActiveEntry.m_Audio != null) { m_AudioPlayer.clip = m_ActiveEntry.m_Audio; m_AudioPlayer.Play(); } else if (m_ActiveEntry.m_TTS_Text.Length > 0) { #if UNITY_IOS && !UNITY_EDITOR_WIN m_LastEntryUsedVoiceOver = m_ActiveEntry.m_AllowVoiceOver; #endif TTS_Speak(m_ActiveEntry.m_TTS_Text, m_ActiveEntry.m_AllowVoiceOver); } } } // Done with dequeuing next entry } }
////////////////////////////////////////////////////////////////////////// public void Initialize() { m_SpeechRate = PlayerPrefs.GetInt("Accessibility_Speech_Rate", 50); if (m_AudioPlayer == null) { m_AudioPlayer = GetComponent <AudioSource>(); } // Initialize which custom TTS to use, if any InitializeCustomTTS(); ////////////////////////////////////////////////////////////////////////// // For WebGL, we need a custom TTS solution // The following code makes sure that there is either a custom TTS already set up, or it tries to // initialize the included Google Cloud TTS API. This can only be done if an API key is provided. #if UNITY_WEBGL if (Application.platform == RuntimePlatform.WebGLPlayer && UAP_AccessibilityManager.UseWebGLTTS()) { if (UAP_CustomTTS.IsInitialized() == UAP_CustomTTS.TTSInitializationState.NotInitialized) { // Use Google TTS if set up correctly, or internal TTS otherwise if (!string.IsNullOrEmpty(UAP_AccessibilityManager.GoogleTTSAPIKey)) { UAP_CustomTTS.InitializeCustomTTS <Google_TTS>(); } else { UAP_CustomTTS.InitializeCustomTTS <WebSpeechAPI_TTS>(); } } } #endif ////////////////////////////////////////////////////////////////////////// if (UAP_CustomTTS.IsInitialized() == UAP_CustomTTS.TTSInitializationState.NotInitialized) { #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN // Initialize Text To Speech for Windows platforms - if so desired /* * No more auto-initialization * if (UAP_AccessibilityManager.UseWindowsTTS()) * { * InitializeWindowsTTS(); * } */ #elif (UNITY_STANDALONE_OSX || UNITY_EDITOR) && !UNITY_WEBPLAYER // Initialize Text To Speech for Mac platform - if so desired if (UAP_AccessibilityManager.UseMacOSTTS()) { if (MacOSTTS.instance == null) { GameObject MacOSTTSObj = new GameObject("MacOS TTS"); MacOSTTSObj.AddComponent <MacOSTTS>(); MacOSTTSObj.transform.SetParent(this.transform, false); } } #elif UNITY_ANDROID && !UNITY_EDITOR if (UAP_AccessibilityManager.UseAndroidTTS()) { AndroidTTS.Initialize(); } #elif UNITY_IOS && !UNITY_EDITOR if (UAP_AccessibilityManager.UseiOSTTS()) { iOSTTS.Init(); } #endif } }