< Summary

Class:AudioEvent
Assembly:Audio
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Audio/AudioEvent.cs
Covered lines:58
Uncovered lines:30
Coverable lines:88
Total lines:201
Line coverage:65.9% (58 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%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)]
 938316    public float initialVolume = 1.0f;
 17
 938318    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    {
 943541        if (audioContainer == null)
 042            return;
 43
 943544        pitch = initialPitch;
 943545        lastPlayedTime = 0f;
 943546        nextAvailablePlayTime = 0f;
 943547        lastPlayedIndex = -1;
 943548        RandomizeIndex();
 49
 50        // Add AudioSource component for event
 943551        source = audioContainer.gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
 52
 943553        if (clips.Length == 0)
 54        {
 055            Debug.LogWarning("There are no clips in the audio event '" + name + "' (" + audioContainer.name + ")");
 056        }
 57        else
 58        {
 943559            source.clip = clips[0];
 60        }
 61
 943562        source.volume = initialVolume;
 943563        source.loop = loop;
 943564        source.playOnAwake = false;
 65
 943566        source.outputAudioMixerGroup = audioContainer.audioMixerGroup;
 943567        source.spatialBlend = audioContainer.spatialBlend;
 943568        source.dopplerLevel = audioContainer.dopplerLevel;
 943569        source.minDistance = audioContainer.minDistance;
 943570        source.maxDistance = audioContainer.maxDistance;
 943571    }
 72
 1971473    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        {
 998581            newIndex = Random.Range(from, to);
 998582        } while (clips.Length > 1 && newIndex == lastPlayedIndex);
 83
 985784        clipIndex = newIndex;
 985785    }
 86
 87    public virtual void Play(bool oneShot = false)
 88    {
 44089        if (stopEventOnPlay != null)
 090            stopEventOnPlay.Stop();
 91
 44092        if (source == null)
 93        {
 8694            if (VERBOSE)
 095                Debug.Log($"AudioEvent: Tried to play {name} with source equal to null.");
 8696            return;
 97        }
 98
 35499        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
 354107        if (!source.gameObject.activeSelf)
 108        {
 0109            return;
 110        }
 111
 354112        if (Time.time < nextAvailablePlayTime)
 113        {
 19114            return;
 115        }
 116
 335117        source.clip = clips[clipIndex];
 335118        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 119
 120        // Play
 335121        if (oneShot)
 333122            source.PlayOneShot(source.clip);
 123        else
 2124            source.Play();
 125
 335126        lastPlayedIndex = clipIndex;
 335127        RandomizeIndex();
 128
 335129        lastPlayedTime = Time.time;
 335130        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 131
 335132        OnPlay?.Invoke();
 0133    }
 134
 135    public void PlayScheduled(float delaySeconds)
 136    {
 102137        if (source == null)
 0138            return;
 139
 140        // Check if AudioSource is active and check cooldown time (taking delay into account)
 102141        if (!source.gameObject.activeSelf || Time.time + delaySeconds < nextAvailablePlayTime)
 15142            return;
 143
 87144        source.clip = clips[clipIndex];
 87145        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 87146        source.PlayScheduled(AudioSettings.dspTime + delaySeconds);
 147
 87148        lastPlayedIndex = clipIndex;
 87149        RandomizeIndex();
 150
 87151        lastPlayedTime = Time.time + delaySeconds;
 87152        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 153
 87154        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    {
 3186        float startVolume = source.volume;
 3187        while (source.volume > 0)
 188        {
 3189            source.volume -= startVolume * (Time.unscaledDeltaTime / fadeSeconds);
 3190            yield return null;
 191        }
 192
 0193        if (stopWhenDone)
 194        {
 0195            Stop();
 0196            source.volume = initialVolume;
 197        }
 198
 0199        OnFadedOut?.Invoke();
 0200    }
 201}