< Summary

Class:AudioEvent
Assembly:Audio
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Audio/AudioEvent.cs
Covered lines:43
Uncovered lines:44
Coverable lines:87
Total lines:201
Line coverage:49.4% (43 of 87)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AudioEvent()0%110100%
Initialize(...)0%3.013090%
RandomizeIndex()0%110100%
RandomizeIndex(...)0%330100%
Play(...)0%13.2810068%
PlayScheduled(...)0%30500%
Stop()0%6200%
ResetVolume()0%2100%
SetIndex(...)0%2100%
SetPitch(...)0%110100%
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)]
 757916    public float initialVolume = 1.0f;
 17
 757918    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    {
 886341        if (audioContainer == null)
 042            return;
 43
 886344        pitch = initialPitch;
 886345        lastPlayedTime = 0f;
 886346        nextAvailablePlayTime = 0f;
 886347        lastPlayedIndex = -1;
 886348        RandomizeIndex();
 49
 50        // Add AudioSource component for event
 886351        source = audioContainer.gameObject.AddComponent(typeof(AudioSource)) as AudioSource;
 52
 886353        if (clips.Length == 0)
 54        {
 055            Debug.LogWarning("There are no clips in the audio event '" + name + "' (" + audioContainer.name + ")");
 56        }
 57        else
 58        {
 886359            source.clip = clips[0];
 60        }
 61
 886362        source.volume = initialVolume;
 886363        source.loop = loop;
 886364        source.playOnAwake = false;
 65
 886366        source.outputAudioMixerGroup = audioContainer.audioMixerGroup;
 886367        source.spatialBlend = audioContainer.spatialBlend;
 886368        source.dopplerLevel = audioContainer.dopplerLevel;
 886369        source.minDistance = audioContainer.minDistance;
 886370        source.maxDistance = audioContainer.maxDistance;
 886371    }
 72
 1778873    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        {
 891481            newIndex = Random.Range(from, to);
 891482        } while (clips.Length > 1 && newIndex == lastPlayedIndex);
 83
 889484        clipIndex = newIndex;
 889485    }
 86
 87    public virtual void Play(bool oneShot = false)
 88    {
 46389        if (stopEventOnPlay != null)
 090            stopEventOnPlay.Stop();
 91
 46392        if (source == null)
 93        {
 43294            if (VERBOSE)
 095                Debug.Log($"AudioEvent: Tried to play {name} with source equal to null.");
 43296            return;
 97        }
 98
 3199        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
 31107        if (!source.gameObject.activeSelf)
 108        {
 0109            return;
 110        }
 111
 31112        if (Time.time < nextAvailablePlayTime)
 113        {
 0114            return;
 115        }
 116
 31117        source.clip = clips[clipIndex];
 31118        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 119
 120        // Play
 31121        if (oneShot)
 29122            source.PlayOneShot(source.clip);
 123        else
 2124            source.Play();
 125
 31126        lastPlayedIndex = clipIndex;
 31127        RandomizeIndex();
 128
 31129        lastPlayedTime = Time.time;
 31130        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 131
 31132        OnPlay?.Invoke();
 0133    }
 134
 135    public void PlayScheduled(float delaySeconds)
 136    {
 0137        if (source == null)
 0138            return;
 139
 140        // Check if AudioSource is active and check cooldown time (taking delay into account)
 0141        if (!source.gameObject.activeSelf || Time.time + delaySeconds < nextAvailablePlayTime)
 0142            return;
 143
 0144        source.clip = clips[clipIndex];
 0145        source.pitch = pitch + Random.Range(0f, randomPitch) - (randomPitch * 0.5f);
 0146        source.PlayScheduled(AudioSettings.dspTime + delaySeconds);
 147
 0148        lastPlayedIndex = clipIndex;
 0149        RandomizeIndex();
 150
 0151        lastPlayedTime = Time.time + delaySeconds;
 0152        nextAvailablePlayTime = lastPlayedTime + cooldownSeconds;
 153
 0154        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
 21167    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    {
 0186        float startVolume = source.volume;
 0187        while (source.volume > 0)
 188        {
 0189            source.volume -= startVolume * (Time.unscaledDeltaTime / fadeSeconds);
 0190            yield return null;
 191        }
 192
 0193        if (stopWhenDone)
 194        {
 0195            Stop();
 0196            source.volume = initialVolume;
 197        }
 198
 0199        OnFadedOut?.Invoke();
 0200    }
 201}