< Summary

Class:DCL.Components.DCLAudioSource
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs
Covered lines:63
Uncovered lines:16
Coverable lines:79
Total lines:194
Line coverage:79.7% (63 of 79)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
Awake()0%110100%
InitDCLAudioClip(...)0%220100%
ApplyChanges()0%6.076087.5%
ApplyCurrentModel()0%6.896070.83%
UpdateAudioSourceVolume()0%6.566075%
OnCurrentSceneChanged(...)0%550100%
OnDestroy()0%110100%
UpdateOutOfBoundariesState(...)0%110100%
DclAudioClip_OnLoadingFinished(...)0%12300%
ApplyLoadedAudioClip(...)0%880100%
GetClassId()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Audio/DCLAudioSource.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections;
 3using DCL.Controllers;
 4using UnityEngine;
 5using DCL.Models;
 6using System.Collections.Generic;
 7
 8namespace DCL.Components
 9{
 10    public class DCLAudioSource : BaseComponent, IOutOfSceneBoundariesHandler
 11    {
 12        [System.Serializable]
 13        public class Model : BaseModel
 14        {
 15            public string audioClipId;
 16            public bool playing = false;
 4217            public float volume = 1f;
 18            public bool loop = false;
 4219            public float pitch = 1f;
 20            public long playedAtTimestamp = 0;
 21
 1522            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 23        }
 24
 325        public float playTime => audioSource.time;
 26        internal AudioSource audioSource;
 27        DCLAudioClip lastDCLAudioClip;
 28
 29        private bool isDestroyed = false;
 30        public long playedAtTimestamp = 0;
 31        private bool isOutOfBoundaries = false;
 32
 33        private void Awake()
 34        {
 1235            audioSource = gameObject.GetOrCreateComponent<AudioSource>();
 1236            model = new Model();
 1237        }
 38
 39        public void InitDCLAudioClip(DCLAudioClip dclAudioClip)
 40        {
 2341            if (lastDCLAudioClip != null)
 42            {
 1143                lastDCLAudioClip.OnLoadingFinished -= DclAudioClip_OnLoadingFinished;
 44            }
 45
 2346            lastDCLAudioClip = dclAudioClip;
 2347        }
 48
 049        public double volume => ((Model) model).volume;
 50
 51        public override IEnumerator ApplyChanges(BaseModel baseModel)
 52        {
 3053            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 54
 55            //If the scene creates and destroy an audiosource before our renderer has been turned on bad things happen!
 56            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 1557            if (isDestroyed)
 058                yield break;
 59
 1560            CommonScriptableObjects.sceneID.OnChange -= OnCurrentSceneChanged;
 1561            CommonScriptableObjects.sceneID.OnChange += OnCurrentSceneChanged;
 62
 1563            ApplyCurrentModel();
 64
 1565            yield return null;
 1566        }
 67
 68        private void ApplyCurrentModel()
 69        {
 1570            if (audioSource == null)
 71            {
 072                Debug.LogWarning("AudioSource is null!.");
 073                return;
 74            }
 75
 1576            Model model = (Model) this.model;
 1577            UpdateAudioSourceVolume();
 1578            audioSource.loop = model.loop;
 1579            audioSource.pitch = model.pitch;
 1580            audioSource.spatialBlend = 1;
 1581            audioSource.dopplerLevel = 0.1f;
 82
 1583            if (model.playing)
 84            {
 1185                DCLAudioClip dclAudioClip = scene.GetSharedComponent(model.audioClipId) as DCLAudioClip;
 86
 1187                if (dclAudioClip != null)
 88                {
 1189                    InitDCLAudioClip(dclAudioClip);
 90                    //NOTE(Brian): Play if finished loading, otherwise will wait for the loading to complete (or fail).
 1191                    if (dclAudioClip.loadingState == DCLAudioClip.LoadState.LOADING_COMPLETED)
 92                    {
 1193                        ApplyLoadedAudioClip(dclAudioClip);
 1194                    }
 95                    else
 96                    {
 097                        dclAudioClip.OnLoadingFinished -= DclAudioClip_OnLoadingFinished;
 098                        dclAudioClip.OnLoadingFinished += DclAudioClip_OnLoadingFinished;
 99                    }
 0100                }
 101                else
 102                {
 0103                    Debug.LogError("Wrong audio clip type when playing audiosource!!");
 104                }
 0105            }
 106            else
 107            {
 4108                if (audioSource.isPlaying)
 109                {
 2110                    audioSource.Stop();
 111                }
 112            }
 4113        }
 114
 115        private void UpdateAudioSourceVolume()
 116        {
 19117            if (scene is GlobalScene globalScene && globalScene.isPortableExperience)
 118            {
 0119                audioSource.volume = ((Model)model).volume;
 0120                return;
 121            }
 122
 19123            if (isOutOfBoundaries)
 124            {
 6125                audioSource.volume = 0;
 6126                return;
 127            }
 128
 13129            audioSource.volume = scene.sceneData.id == CommonScriptableObjects.sceneID.Get() ? ((Model)model).volume : 0
 13130        }
 131
 132        private void OnCurrentSceneChanged(string currentSceneId, string previousSceneId)
 133        {
 2134            if (audioSource != null)
 135            {
 2136                Model model = (Model) this.model;
 2137                float volume = 0;
 2138                if ((scene.sceneData.id == currentSceneId) || (scene is GlobalScene globalScene && globalScene.isPortabl
 139                {
 1140                    volume = model.volume;
 141                }
 142
 2143                audioSource.volume = volume;
 144            }
 2145        }
 146
 147        private void OnDestroy()
 148        {
 12149            isDestroyed = true;
 12150            CommonScriptableObjects.sceneID.OnChange -= OnCurrentSceneChanged;
 151
 152            //NOTE(Brian): Unsuscribe events.
 12153            InitDCLAudioClip(null);
 12154        }
 155
 156        public void UpdateOutOfBoundariesState(bool isEnabled)
 157        {
 4158            isOutOfBoundaries = !isEnabled;
 4159            UpdateAudioSourceVolume();
 4160        }
 161
 162        private void DclAudioClip_OnLoadingFinished(DCLAudioClip obj)
 163        {
 0164            if (obj.loadingState == DCLAudioClip.LoadState.LOADING_COMPLETED && audioSource != null)
 165            {
 0166                ApplyLoadedAudioClip(obj);
 167            }
 168
 0169            obj.OnLoadingFinished -= DclAudioClip_OnLoadingFinished;
 0170        }
 171
 172        private void ApplyLoadedAudioClip(DCLAudioClip clip)
 173        {
 11174            if (audioSource.clip != clip.audioClip)
 175            {
 11176                audioSource.clip = clip.audioClip;
 177            }
 178
 11179            Model model = (Model) this.model;
 11180            bool shouldPlay = playedAtTimestamp != model.playedAtTimestamp ||
 181                              (model.playing && !audioSource.isPlaying);
 182
 11183            if (audioSource.enabled && model.playing && shouldPlay)
 184            {
 185                //To remove a pesky and quite unlikely warning when the audiosource is out of scenebounds
 11186                audioSource.Play();
 187            }
 188
 11189            playedAtTimestamp = model.playedAtTimestamp;
 11190        }
 191
 0192        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AUDIO_SOURCE; }
 193    }
 194}