< Summary

Class:AudioEvent
Assembly:Audio
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Audio/AudioEvent.cs
Covered lines:59
Uncovered lines:29
Coverable lines:88
Total lines:201
Line coverage:67% (59 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%11.3810076%
PlayScheduled(...)0%5.095084.62%
Stop()0%6200%
ResetVolume()0%2100%
SetIndex(...)0%2100%
SetPitch(...)0%2100%
FadeIn()0%30500%
FadeOut()0%12.176044.44%

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)]
 278316    public float initialVolume = 1.0f;
 17
 278318    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    [HideInInspector] public event System.Action OnPlay, OnStop, OnFadedIn, OnFadedOut;
 38
 39    public virtual void Initialize(AudioContainer audioContainer)
 40    {
 331841        if (audioContainer == null)
 042            return;
 43
 331844        pitch = initialPitch;
 331845        lastPlayedTime = 0f;
 331846        nextAvailablePlayTime = 0f;
 331847        lastPlayedIndex = -1;
 331848        RandomizeIndex();
 49
 50        // Add AudioSource component for event
 331851        source = audioContainer.gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
 52
 331853        if (clips.Length == 0)
 54        {
 055            Debug.LogWarning("There are no clips in the audio event '" + name + "' (" + audioContainer.name + ")");
 056        }
 57        else
 58        {
 331859            source.clip = clips[0];
 60        }
 61
 331862        source.volume = initialVolume;
 331863        source.loop = loop;
 331864        source.playOnAwake = false;
 65
 331866        source.outputAudioMixerGroup = audioContainer.audioMixerGroup;
 331867        source.spatialBlend = audioContainer.spatialBlend;
 331868        source.dopplerLevel = audioContainer.dopplerLevel;
 331869        source.minDistance = audioContainer.minDistance;
 331870        source.maxDistance = audioContainer.maxDistance;
 331871    }
 72
 756673    public void RandomizeIndex() { RandomizeIndex(0, clips.Length); }
 74
 75    // Randomize the index from (inclusive) to y (exclusive)
 76    public void RandomizeIndex(int from, int to)
 77    {
 78        int newIndex;
 79        do
 80        {
 392981            newIndex = Random.Range(from, to);
 392982        } while (clips.Length > 1 && newIndex == lastPlayedIndex);
 83
 378384        clipIndex = newIndex;
 378385    }
 86
 87    public virtual void Play(bool oneShot = false)
 88    {
 47789        if (stopEventOnPlay != null)
 090            stopEventOnPlay.Stop();
 91
 47792        if (source == null)
 93        {
 9894            if (VERBOSE)
 095                Debug.Log($"AudioEvent: Tried to play {name} with source equal to null.");
 9896            return;
 97        }
 98
 37999        if (source.clip == null)
 100        {
 0101            if (VERBOSE)
 0102                Debug.Log($"AudioEvent: Tried to play {name} with audioClip equal to null.");
 0103            return;
 104        }
 105
 106        // Check if AudioSource is active and check cooldown time
 379107        if (!source.gameObject.activeSelf)
 108        {
 1109            return;
 110        }
 111
 378112        if (Time.time < nextAvailablePlayTime)
 113        {
 19114            return;
 115        }
 116
 359117        source.clip = clips[clipIndex];
 359118        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 119
 120        // Play
 359121        if (oneShot)
 357122            source.PlayOneShot(source.clip);
 123        else
 2124            source.Play();
 125
 359126        lastPlayedIndex = clipIndex;
 359127        RandomizeIndex();
 128
 359129        lastPlayedTime = Time.time;
 359130        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 131
 359132        OnPlay?.Invoke();
 0133    }
 134
 135    public void PlayScheduled(float delaySeconds)
 136    {
 121137        if (source == null)
 0138            return;
 139
 140        // Check if AudioSource is active and check cooldown time (taking delay into account)
 121141        if (!source.gameObject.activeSelf || Time.time + delaySeconds < nextAvailablePlayTime)
 15142            return;
 143
 106144        source.clip = clips[clipIndex];
 106145        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 106146        source.PlayScheduled(AudioSettings.dspTime + delaySeconds);
 147
 106148        lastPlayedIndex = clipIndex;
 106149        RandomizeIndex();
 150
 106151        lastPlayedTime = Time.time + delaySeconds;
 106152        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 153
 106154        OnPlay?.Invoke();
 0155    }
 156
 157    public void Stop()
 158    {
 0159        source.Stop();
 0160        OnStop?.Invoke();
 0161    }
 162
 0163    public void ResetVolume() { source.volume = initialVolume; }
 164
 0165    public void SetIndex(int index) { clipIndex = index; }
 166
 0167    public void SetPitch(float pitch) { this.pitch = pitch; }
 168
 169    /// <summary>Use StartCoroutine() on this one.</summary>
 170    public IEnumerator FadeIn(float fadeSeconds)
 171    {
 0172        float startVolume = source.volume;
 0173        while (source.volume < initialVolume)
 174        {
 0175            source.volume += (initialVolume - startVolume) * (Time.unscaledDeltaTime / fadeSeconds);
 0176            yield return null;
 177        }
 178
 0179        source.volume = initialVolume;
 0180        OnFadedIn?.Invoke();
 0181    }
 182
 183    /// <summary>Use StartCoroutine() on this one.</summary>
 184    public IEnumerator FadeOut(float fadeSeconds, bool stopWhenDone = true)
 185    {
 5186        float startVolume = source.volume;
 5187        while (source.volume > 0)
 188        {
 5189            source.volume -= startVolume * (Time.unscaledDeltaTime / fadeSeconds);
 5190            yield return null;
 191        }
 192
 0193        if (stopWhenDone)
 194        {
 0195            Stop();
 0196            source.volume = initialVolume;
 197        }
 198
 0199        OnFadedOut?.Invoke();
 0200    }
 201}