Example #1
0
    //////////////////////////////////////////////////////////////////////////

    private void InvalidateActiveEntry()
    {
        if (m_ActiveEntry != null && m_ActiveEntry.m_CallbackOnDone != null)
        {
            m_ActiveEntry.m_CallbackOnDone();
        }
        m_ActiveEntry = null;
    }
Example #2
0
    public void QueuePause(float durationInSecs)
    {
        // Queue Pause
        SAudioEntry newEntry = new SAudioEntry();

        newEntry.m_AudioType     = EAudioType.Pause;
        newEntry.m_PauseDuration = durationInSecs;
        QueueAudio(newEntry, EInterrupt.None);
    }
Example #3
0
    public void QueueAudio(AudioClip audioFile, EAudioType type, EInterrupt interruptsAudioTypes = EInterrupt.None, bool isInterruptible = true)
    {
        //Debug.Log("playing " + audioFile.name);
        // Build struct and call internal add function
        SAudioEntry newEntry = new SAudioEntry();

        newEntry.m_Audio           = audioFile;
        newEntry.m_AudioType       = type;
        newEntry.m_IsInterruptible = isInterruptible;
        QueueAudio(newEntry, interruptsAudioTypes);
    }
Example #4
0
    //////////////////////////////////////////////////////////////////////////

    public void QueueAudio(string textForTTS, EAudioType type, bool allowVoiceOver, EInterrupt interruptsAudioTypes = EInterrupt.None, bool isInterruptible = true)
    {
        //Debug.Log("speaking " + textForTTS);
        // Build struct and call internal add function
        SAudioEntry newEntry = new SAudioEntry();

        newEntry.m_TTS_Text        = textForTTS;
        newEntry.m_AllowVoiceOver  = allowVoiceOver;
        newEntry.m_AudioType       = type;
        newEntry.m_IsInterruptible = isInterruptible;
        QueueAudio(newEntry, interruptsAudioTypes);
    }
    //////////////////////////////////////////////////////////////////////////

    public void QueueAudio(string textForTTS, EAudioType type, bool allowVoiceOver, UAP_GenericCallback callbackOnDone = null, EInterrupt interruptsAudioTypes = EInterrupt.None, bool isInterruptible = true)
    {
        //Debug.Log("speaking " + textForTTS);
        // Build struct and call internal add function
        SAudioEntry newEntry = new SAudioEntry();

        newEntry.m_TTS_Text        = Regex.Replace(textForTTS, "(<.[^(><.)]+>)", " ");
        newEntry.m_AllowVoiceOver  = allowVoiceOver;
        newEntry.m_AudioType       = type;
        newEntry.m_IsInterruptible = isInterruptible;
        newEntry.m_CallbackOnDone  = callbackOnDone;
        QueueAudio(newEntry, interruptsAudioTypes);
    }
Example #6
0
    //////////////////////////////////////////////////////////////////////////

    private void QueueAudio(SAudioEntry newEntry, EInterrupt interrupts)
    {
        // check for interrupts and cancel/queue accordingly
        if (interrupts != EInterrupt.None)
        {
            // Stop current element (if any)
            if (m_ActiveEntry != null && m_ActiveEntry.m_IsInterruptible)
            {
                if (((int)m_ActiveEntry.m_AudioType & (int)interrupts) > 0)
                {
                    //Debug.Log("Current audio type is " + m_ActiveEntry.m_AudioType + " and it is interruptible");
                    StopAudio();
                    m_ActiveEntry = null;
                }
            }

            // Go through the queue and remove all entries that match the type
            int entryCount = m_AudioQueue.Count;
            Queue <SAudioEntry> tempQueue = new Queue <SAudioEntry>();
            for (int i = 0; i < entryCount; ++i)
            {
                SAudioEntry entry = m_AudioQueue.Dequeue();
                if (!entry.m_IsInterruptible)
                {
                    tempQueue.Enqueue(entry);
                }
                else if (((int)entry.m_AudioType & (int)interrupts) == 0)
                {
                    tempQueue.Enqueue(entry);
                }
            }
            m_AudioQueue = tempQueue;
        }

        // Sanity Check - don't queue up empty entries
        if (newEntry.m_AudioType == EAudioType.None)
        {
            return;
        }
        if (newEntry.m_AudioType != EAudioType.Pause && (newEntry.m_Audio == null && newEntry.m_TTS_Text.Length == 0))
        {
            return;
        }

        m_AudioQueue.Enqueue(newEntry);
    }
Example #7
0
    //////////////////////////////////////////////////////////////////////////

    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.
                    m_ActiveEntry = null;
                }
            }
            else
            {
                m_CurrentElement = "Voice";

                // Is the voice still playing/speaking?
                bool stillPlaying = IsPlaying();
                m_IsSpeaking = stillPlaying;

                if (!stillPlaying)
                {
                    m_ActiveEntry = null;
                }
            }
        }
        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)
        {
            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);
                }
            }
        }
    }
Example #8
0
    //////////////////////////////////////////////////////////////////////////

    public void Stop()
    {
        StopAudio();
        m_AudioQueue.Clear();
        m_ActiveEntry = null;
    }
Example #9
0
    //////////////////////////////////////////////////////////////////////////

    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
        }
    }