// Either:
        // - returns an existing non playing source if available
        // - creates a new source if all existing are busy and maxSources allows it
        // - return NULL if none of the previous options work
        // Also sets the src targetVolume to 1
        DeAudioSource GetAvailableSource()
        {
            int len = sources.Count;

            for (int i = 0; i < len; ++i)
            {
                DeAudioSource src = sources[i];
                if (src.isFree)
                {
                    src.Reset();
                    return(src);
                }
            }
            // No free sources...
            if (maxSources < 0 || len < maxSources)
            {
                // Create new source
                DeAudioSource src = new DeAudioSource(this, _sourcesContainer);
                src.Reset();
                sources.Add(src);
                return(src);
            }
            else if (recycle)
            {
                // Recycle oldest source
                DeAudioSource src = GetOldestSource();
                src.Reset();
                return(src);
            }
            else
            {
                // All sources busy and can't be recycled
                return(null);
            }
        }
        /// <summary>
        /// Plays the given <see cref="DeAudioClipData"/> with the stored volume, pitch and loop settings (unless set otherwise).
        /// <para>Returns the <see cref="DeAudioSource"/> instance used to play, or NULL if the clip couldn't be played</para>
        /// </summary>
        public DeAudioSource Play(DeAudioClipData clipData, float?volume = null, float?pitch = null, bool?loop = null)
        {
            DeAudioSource src = PlayFrom(clipData.clip, 0,
                                         volume == null ? clipData.volume : (float)volume,
                                         pitch == null ? clipData.pitch : (float)pitch,
                                         loop == null ? clipData.loop : (bool)loop
                                         );

            return(src);
        }
        /// <summary>
        /// Fades out then stops all sources in this group, while starting the given clip with a fade-in effect.
        /// <para>Returns the <see cref="DeAudioSource"/> instance used to play, or NULL if the clip couldn't be played</para>
        /// </summary>
        public DeAudioSource Crossfade(AudioClip clip, float volume = 1, float pitch = 1, bool loop = false, float fadeDuration = 1.5f, bool ignoreTimeScale = true, FadeBehaviour onfadeOutBehaviour = FadeBehaviour.Stop, TweenCallback onComplete = null)
        {
            FadeSourcesTo(0, fadeDuration, ignoreTimeScale, onfadeOutBehaviour, null);
            DeAudioSource s = Play(clip, volume, pitch, loop);

            if (s != null)
            {
                s.FadeFrom(0, fadeDuration, ignoreTimeScale, onComplete);
            }
            return(s);
        }
        internal void FadeSourcesTo(float to, float duration, bool ignoreTimeScale, FadeBehaviour onCompleteBehaviour, TweenCallback onComplete)
        {
            int len = sources.Count;

            for (int i = 0; i < len; ++i)
            {
                DeAudioSource src = sources[i];
                if (!src.isPlaying || src.isFadingOut)
                {
                    continue;                                    // Ignore sources that are fading to 0 or that are not playing at all
                }
                sources[i].FadeTo(to, duration, ignoreTimeScale, onCompleteBehaviour, onComplete);
            }
        }
        /// <summary>
        /// Plays the given sound with the given options from the given time.
        /// <para>Returns the <see cref="DeAudioSource"/> instance used to play, or NULL if the clip couldn't be played</para>
        /// </summary>
        public DeAudioSource PlayFrom(AudioClip clip, float fromTime, float volume = 1, float pitch = 1, bool loop = false)
        {
            DeAudioSource src = GetAvailableSource();

            if (src == null)
            {
                if (DeAudioManager.I.logInfo)
                {
                    Debug.Log(DeAudioManager.LogPrefix + "Clip can't be played because all sources are busy");
                }
                return(null);
            }
            src.PlayFrom(clip, fromTime, volume, pitch, loop);
            return(src);
        }
        DeAudioSource GetOldestSource()
        {
            int           len  = sources.Count;
            float         time = float.MaxValue;
            DeAudioSource res  = null;

            for (int i = 0; i < len; ++i)
            {
                DeAudioSource s = sources[i];
                if (s.playTime >= time)
                {
                    continue;
                }
                res  = s;
                time = s.playTime;
            }
            return(res);
        }
Exemple #7
0
        static void FadeTo(AudioClip clip, float to, float duration, bool ignoreTimeScale, FadeBehaviour onCompleteBehaviour, TweenCallback onComplete)
        {
            int len = audioGroups.Length;

            for (int i = 0; i < len; ++i)
            {
                DeAudioGroup group = audioGroups[i];
                int          slen  = group.sources.Count;
                for (int c = 0; c < slen; c++)
                {
                    DeAudioSource s = group.sources[c];
                    if (s.clip == clip)
                    {
                        s.FadeTo(to, duration, ignoreTimeScale, onCompleteBehaviour, onComplete);
                    }
                }
            }
        }
 // Returns the eventual existing source for the given clip, or NULL if there is none
 internal DeAudioSource GetExistingAudioSourceFor(AudioClip clip, bool ignorePaused = false, bool ignoreFadingOut = true)
 {
     if (clip == null)
     {
         return(null);
     }
     for (int i = 0; i < sources.Count; ++i)
     {
         DeAudioSource src = sources[i];
         if (src.clip != clip)
         {
             continue;
         }
         if (ignorePaused && !src.isPlaying || ignoreFadingOut && src.isFadingOut)
         {
             continue;
         }
         return(src);
     }
     return(null);
 }
        void IterateOnAllSources(OperationType operationType, AudioClip clip = null, float floatValue = 0)
        {
            int len = sources.Count;

            for (int i = 0; i < len; ++i)
            {
                DeAudioSource s = sources[i];
                switch (operationType)
                {
                case OperationType.Stop:
                    s.Stop();
                    break;

                case OperationType.StopByClip:
                    if (s.clip == clip)
                    {
                        s.Stop();
                    }
                    break;

                case OperationType.StopIfPaused:
                    if (s.isPaused)
                    {
                        s.Stop();
                    }
                    break;

                case OperationType.Pause:
                    s.Pause();
                    break;

                case OperationType.PauseByClip:
                    if (s.clip == clip)
                    {
                        s.Pause();
                    }
                    break;

                case OperationType.Resume:
                    s.Resume();
                    break;

                case OperationType.ResumeByClip:
                    if (s.clip == clip)
                    {
                        if (floatValue >= 0)
                        {
                            s.volume = floatValue;
                        }
                        s.Resume();
                    }
                    break;

                case OperationType.SetVolumeByClip:
                    if (s.clip == clip)
                    {
                        s.volume = floatValue;
                    }
                    break;

                case OperationType.Unlock:
                    s.locked = false;
                    break;

                case OperationType.UnlockByClip:
                    if (s.clip == clip)
                    {
                        s.locked = false;
                    }
                    break;
                }
            }
        }
 public static void DispatchDeAudioEvent(DeAudioEventType type, DeAudioGroup audioGroup = null, DeAudioSource source = null)
 {
     if (DeAudioEvent != null)
     {
         DeAudioEvent(new DeAudioEventArgs(type, audioGroup, source));
     }
 }