< 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:69
Uncovered lines:20
Coverable lines:89
Total lines:214
Line coverage:77.5% (69 of 89)
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%
OnAudioSettingsChanged(...)0%2100%
OnVirtualAudioMixerChangedValue(...)0%2100%
UpdateAudioSourceVolume()0%6.296080%
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 DCL.SettingsCommon;
 7using AudioSettings = DCL.SettingsCommon.AudioSettings;
 8
 9namespace DCL.Components
 10{
 11    public class DCLAudioSource : BaseComponent, IOutOfSceneBoundariesHandler
 12    {
 13        [System.Serializable]
 14        public class Model : BaseModel
 15        {
 16            public string audioClipId;
 17            public bool playing = false;
 4218            public float volume = 1f;
 19            public bool loop = false;
 4220            public float pitch = 1f;
 21            public long playedAtTimestamp = 0;
 22
 1523            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 24        }
 25
 126        public float playTime => audioSource.time;
 27        internal AudioSource audioSource;
 28        DCLAudioClip lastDCLAudioClip;
 29
 30        private bool isDestroyed = false;
 31        public long playedAtTimestamp = 0;
 32        private bool isOutOfBoundaries = false;
 33
 34        private void Awake()
 35        {
 1236            audioSource = gameObject.GetOrCreateComponent<AudioSource>();
 1237            model = new Model();
 38
 1239            Settings.i.audioSettings.OnChanged += OnAudioSettingsChanged;
 1240            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += OnVirtualAudioMixerChangedValue;
 1241        }
 42
 43        public void InitDCLAudioClip(DCLAudioClip dclAudioClip)
 44        {
 2345            if (lastDCLAudioClip != null)
 46            {
 1147                lastDCLAudioClip.OnLoadingFinished -= DclAudioClip_OnLoadingFinished;
 48            }
 49
 2350            lastDCLAudioClip = dclAudioClip;
 2351        }
 52
 053        public double volume => ((Model) model).volume;
 54
 55        public override IEnumerator ApplyChanges(BaseModel baseModel)
 56        {
 3057            yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get());
 58
 59            //If the scene creates and destroy an audiosource before our renderer has been turned on bad things happen!
 60            //TODO: Analyze if we can catch this upstream and stop the IEnumerator
 1561            if (isDestroyed)
 062                yield break;
 63
 1564            CommonScriptableObjects.sceneID.OnChange -= OnCurrentSceneChanged;
 1565            CommonScriptableObjects.sceneID.OnChange += OnCurrentSceneChanged;
 66
 1567            ApplyCurrentModel();
 68
 1569            yield return null;
 1570        }
 71
 72        private void ApplyCurrentModel()
 73        {
 1574            if (audioSource == null)
 75            {
 076                Debug.LogWarning("AudioSource is null!.");
 077                return;
 78            }
 79
 1580            Model model = (Model) this.model;
 1581            UpdateAudioSourceVolume();
 1582            audioSource.loop = model.loop;
 1583            audioSource.pitch = model.pitch;
 1584            audioSource.spatialBlend = 1;
 1585            audioSource.dopplerLevel = 0.1f;
 86
 1587            if (model.playing)
 88            {
 1189                DCLAudioClip dclAudioClip = scene.GetSharedComponent(model.audioClipId) as DCLAudioClip;
 90
 1191                if (dclAudioClip != null)
 92                {
 1193                    InitDCLAudioClip(dclAudioClip);
 94                    //NOTE(Brian): Play if finished loading, otherwise will wait for the loading to complete (or fail).
 1195                    if (dclAudioClip.loadingState == DCLAudioClip.LoadState.LOADING_COMPLETED)
 96                    {
 1197                        ApplyLoadedAudioClip(dclAudioClip);
 1198                    }
 99                    else
 100                    {
 0101                        dclAudioClip.OnLoadingFinished -= DclAudioClip_OnLoadingFinished;
 0102                        dclAudioClip.OnLoadingFinished += DclAudioClip_OnLoadingFinished;
 103                    }
 0104                }
 105                else
 106                {
 0107                    Debug.LogError("Wrong audio clip type when playing audiosource!!");
 108                }
 0109            }
 110            else
 111            {
 4112                if (audioSource.isPlaying)
 113                {
 2114                    audioSource.Stop();
 115                }
 116            }
 4117        }
 118
 119        private void OnAudioSettingsChanged(AudioSettings settings)
 120        {
 0121            UpdateAudioSourceVolume();
 0122        }
 123
 124        private void OnVirtualAudioMixerChangedValue(float currentValue, float previousValue)
 125        {
 0126            UpdateAudioSourceVolume();
 0127        }
 128
 129        private void UpdateAudioSourceVolume()
 130        {
 19131            AudioSettings audioSettingsData = Settings.i.audioSettings.Data;
 19132            float newVolume = ((Model)model).volume * Utils.ToVolumeCurve(DataStore.i.virtualAudioMixer.sceneSFXVolume.G
 133
 19134            if (scene is GlobalScene globalScene && globalScene.isPortableExperience)
 135            {
 0136                audioSource.volume = newVolume;
 0137                return;
 138            }
 139
 19140            if (isOutOfBoundaries)
 141            {
 6142                audioSource.volume = 0;
 6143                return;
 144            }
 145
 13146            audioSource.volume = scene.sceneData.id == CommonScriptableObjects.sceneID.Get() ? newVolume : 0f;
 13147        }
 148
 149        private void OnCurrentSceneChanged(string currentSceneId, string previousSceneId)
 150        {
 2151            if (audioSource != null)
 152            {
 2153                Model model = (Model) this.model;
 2154                float volume = 0;
 2155                if ((scene.sceneData.id == currentSceneId) || (scene is GlobalScene globalScene && globalScene.isPortabl
 156                {
 1157                    volume = model.volume;
 158                }
 159
 2160                audioSource.volume = volume;
 161            }
 2162        }
 163
 164        private void OnDestroy()
 165        {
 12166            isDestroyed = true;
 12167            CommonScriptableObjects.sceneID.OnChange -= OnCurrentSceneChanged;
 168
 169            //NOTE(Brian): Unsuscribe events.
 12170            InitDCLAudioClip(null);
 171
 12172            Settings.i.audioSettings.OnChanged -= OnAudioSettingsChanged;
 12173            DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= OnVirtualAudioMixerChangedValue;
 12174        }
 175
 176        public void UpdateOutOfBoundariesState(bool isEnabled)
 177        {
 4178            isOutOfBoundaries = !isEnabled;
 4179            UpdateAudioSourceVolume();
 4180        }
 181
 182        private void DclAudioClip_OnLoadingFinished(DCLAudioClip obj)
 183        {
 0184            if (obj.loadingState == DCLAudioClip.LoadState.LOADING_COMPLETED && audioSource != null)
 185            {
 0186                ApplyLoadedAudioClip(obj);
 187            }
 188
 0189            obj.OnLoadingFinished -= DclAudioClip_OnLoadingFinished;
 0190        }
 191
 192        private void ApplyLoadedAudioClip(DCLAudioClip clip)
 193        {
 11194            if (audioSource.clip != clip.audioClip)
 195            {
 11196                audioSource.clip = clip.audioClip;
 197            }
 198
 11199            Model model = (Model) this.model;
 11200            bool shouldPlay = playedAtTimestamp != model.playedAtTimestamp ||
 201                              (model.playing && !audioSource.isPlaying);
 202
 11203            if (audioSource.enabled && model.playing && shouldPlay)
 204            {
 205                //To remove a pesky and quite unlikely warning when the audiosource is out of scenebounds
 11206                audioSource.Play();
 207            }
 208
 11209            playedAtTimestamp = model.playedAtTimestamp;
 11210        }
 211
 0212        public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AUDIO_SOURCE; }
 213    }
 214}