< Summary

Class:AudioEvent
Assembly:MordiAudio
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Audio/AudioEvent.cs
Covered lines:54
Uncovered lines:34
Coverable lines:88
Total lines:203
Line coverage:61.3% (54 of 88)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AudioEvent()0%110100%
Initialize(...)0%3.033085.71%
RandomizeIndex()0%110100%
RandomizeIndex(...)0%330100%
Play(...)0%12.210072%
PlayScheduled(...)0%5.095084.62%
Stop()0%6200%
ResetVolume()0%2100%
SetIndex(...)0%2100%
SetPitch(...)0%2100%
FadeIn()0%30500%
FadeOut()0%42600%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Audio/AudioEvent.cs

#LineLine coverage
 1using System.Collections;
 2using UnityEngine;
 3using ReorderableList;
 4
 5[System.Serializable, CreateAssetMenu(fileName = "AudioEvent", menuName = "AudioEvents/AudioEvent")]
 6public class AudioEvent : ScriptableObject
 7{
 8    private static bool VERBOSE = false;
 9
 10    [System.Serializable]
 11    public class AudioClipList : ReorderableArray<AudioClip> { }
 12
 13    public bool loop = false;
 14
 15    [Range(0f, 1f)]
 190416    public float initialVolume = 1.0f;
 17
 190418    public float initialPitch = 1f;
 19
 20    [Range(0f, 1f)]
 21    public float randomPitch = 0.0f;
 22
 23    public float cooldownSeconds = 0.0f;
 24
 25    [Reorderable]
 26    public AudioClipList clips;
 27
 28    [Space(25)]
 29    public AudioEvent stopEventOnPlay;
 30
 31    [HideInInspector]
 32    public AudioSource source;
 33
 34    private int clipIndex, lastPlayedIndex;
 35    protected float pitch;
 36    private float lastPlayedTime, nextAvailablePlayTime; // Used for cooldown
 37    private Coroutine fadeInCoroutine, fadeOutCoroutine;
 38
 39    [HideInInspector] public event System.Action OnPlay, OnStop, OnFadedIn, OnFadedOut;
 40
 41    public virtual void Initialize(AudioContainer audioContainer)
 42    {
 223443        if (audioContainer == null)
 044            return;
 45
 223446        pitch = initialPitch;
 223447        lastPlayedTime = 0f;
 223448        nextAvailablePlayTime = 0f;
 223449        lastPlayedIndex = -1;
 223450        RandomizeIndex();
 51
 52        // Add AudioSource component for event
 223453        source = audioContainer.gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
 54
 223455        if (clips.Length == 0)
 56        {
 057            Debug.LogWarning("There are no clips in the audio event '" + name + "' (" + audioContainer.name + ")");
 058        }
 59        else
 60        {
 223461            source.clip = clips[0];
 62        }
 63
 223464        source.volume = initialVolume;
 223465        source.loop = loop;
 223466        source.playOnAwake = false;
 67
 223468        source.outputAudioMixerGroup = audioContainer.audioMixerGroup;
 223469        source.spatialBlend = audioContainer.spatialBlend;
 223470        source.dopplerLevel = audioContainer.dopplerLevel;
 223471        source.minDistance = audioContainer.minDistance;
 223472        source.maxDistance = audioContainer.maxDistance;
 223473    }
 74
 534675    public void RandomizeIndex() { RandomizeIndex(0, clips.Length); }
 76
 77    // Randomize the index from (inclusive) to y (exclusive)
 78    public void RandomizeIndex(int from, int to)
 79    {
 80        int newIndex;
 81        do
 82        {
 279783            newIndex = Random.Range(from, to);
 279784        } while (clips.Length > 1 && newIndex == lastPlayedIndex);
 85
 267386        clipIndex = newIndex;
 267387    }
 88
 89    public virtual void Play(bool oneShot = false)
 90    {
 40591        if (stopEventOnPlay != null)
 092            stopEventOnPlay.Stop();
 93
 40594        if (source == null)
 95        {
 5496            if (VERBOSE)
 097                Debug.Log($"AudioEvent: Tried to play {name} with source equal to null.");
 5498            return;
 99        }
 100
 351101        if (source.clip == null)
 102        {
 0103            if (VERBOSE)
 0104                Debug.Log($"AudioEvent: Tried to play {name} with audioClip equal to null.");
 0105            return;
 106        }
 107
 108        // Check if AudioSource is active and check cooldown time
 351109        if (!source.gameObject.activeSelf)
 110        {
 0111            return;
 112        }
 113
 351114        if (Time.time < nextAvailablePlayTime)
 115        {
 17116            return;
 117        }
 118
 334119        source.clip = clips[clipIndex];
 334120        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 121
 122        // Play
 334123        if (oneShot)
 332124            source.PlayOneShot(source.clip);
 125        else
 2126            source.Play();
 127
 334128        lastPlayedIndex = clipIndex;
 334129        RandomizeIndex();
 130
 334131        lastPlayedTime = Time.time;
 334132        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 133
 334134        OnPlay?.Invoke();
 0135    }
 136
 137    public void PlayScheduled(float delaySeconds)
 138    {
 117139        if (source == null)
 0140            return;
 141
 142        // Check if AudioSource is active and check cooldown time (taking delay into account)
 117143        if (!source.gameObject.activeSelf || Time.time + delaySeconds < nextAvailablePlayTime)
 12144            return;
 145
 105146        source.clip = clips[clipIndex];
 105147        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 105148        source.PlayScheduled(AudioSettings.dspTime + delaySeconds);
 149
 105150        lastPlayedIndex = clipIndex;
 105151        RandomizeIndex();
 152
 105153        lastPlayedTime = Time.time + delaySeconds;
 105154        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 155
 105156        OnPlay?.Invoke();
 0157    }
 158
 159    public void Stop()
 160    {
 0161        source.Stop();
 0162        OnStop?.Invoke();
 0163    }
 164
 0165    public void ResetVolume() { source.volume = initialVolume; }
 166
 0167    public void SetIndex(int index) { clipIndex = index; }
 168
 0169    public void SetPitch(float pitch) { this.pitch = pitch; }
 170
 171    /// <summary>Use StartCoroutine() on this one.</summary>
 172    public IEnumerator FadeIn(float fadeSeconds)
 173    {
 0174        float startVolume = source.volume;
 0175        while (source.volume < initialVolume)
 176        {
 0177            source.volume += (initialVolume - startVolume) * (Time.unscaledDeltaTime / fadeSeconds);
 0178            yield return null;
 179        }
 180
 0181        source.volume = initialVolume;
 0182        OnFadedIn?.Invoke();
 0183    }
 184
 185    /// <summary>Use StartCoroutine() on this one.</summary>
 186    public IEnumerator FadeOut(float fadeSeconds, bool stopWhenDone = true)
 187    {
 0188        float startVolume = source.volume;
 0189        while (source.volume > 0)
 190        {
 0191            source.volume -= startVolume * (Time.unscaledDeltaTime / fadeSeconds);
 0192            yield return null;
 193        }
 194
 0195        if (stopWhenDone)
 196        {
 0197            Stop();
 0198            source.volume = initialVolume;
 199        }
 200
 0201        OnFadedOut?.Invoke();
 0202    }
 203}