| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using UnityEngine; |
| | 5 | | using DCL.Models; |
| | 6 | | using System.Collections.Generic; |
| | 7 | |
|
| | 8 | | namespace 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; |
| 39 | 17 | | public float volume = 1f; |
| | 18 | | public bool loop = false; |
| 39 | 19 | | public float pitch = 1f; |
| | 20 | | public long playedAtTimestamp = 0; |
| | 21 | |
|
| 14 | 22 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 23 | | } |
| | 24 | |
|
| 1 | 25 | | 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 | | { |
| 11 | 35 | | audioSource = gameObject.GetOrCreateComponent<AudioSource>(); |
| 11 | 36 | | model = new Model(); |
| | 37 | |
|
| 11 | 38 | | Settings.i.OnAudioSettingsChanged += OnAudioSettingsChanged; |
| 11 | 39 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange += OnVirtualAudioMixerChangedValue; |
| 11 | 40 | | } |
| | 41 | |
|
| | 42 | | public void InitDCLAudioClip(DCLAudioClip dclAudioClip) |
| | 43 | | { |
| 21 | 44 | | if (lastDCLAudioClip != null) |
| | 45 | | { |
| 10 | 46 | | lastDCLAudioClip.OnLoadingFinished -= DclAudioClip_OnLoadingFinished; |
| | 47 | | } |
| | 48 | |
|
| 21 | 49 | | lastDCLAudioClip = dclAudioClip; |
| 21 | 50 | | } |
| | 51 | |
|
| 0 | 52 | | public double volume => ((Model) model).volume; |
| | 53 | |
|
| | 54 | | public override IEnumerator ApplyChanges(BaseModel baseModel) |
| | 55 | | { |
| 28 | 56 | | yield return new WaitUntil(() => CommonScriptableObjects.rendererState.Get()); |
| | 57 | |
|
| | 58 | | //If the scene creates and destroy an audiosource before our renderer has been turned on bad things happen! |
| | 59 | | //TODO: Analyze if we can catch this upstream and stop the IEnumerator |
| 14 | 60 | | if (isDestroyed) |
| 0 | 61 | | yield break; |
| | 62 | |
|
| 14 | 63 | | CommonScriptableObjects.sceneID.OnChange -= OnCurrentSceneChanged; |
| 14 | 64 | | CommonScriptableObjects.sceneID.OnChange += OnCurrentSceneChanged; |
| | 65 | |
|
| 14 | 66 | | ApplyCurrentModel(); |
| | 67 | |
|
| 14 | 68 | | yield return null; |
| 14 | 69 | | } |
| | 70 | |
|
| | 71 | | private void ApplyCurrentModel() |
| | 72 | | { |
| 14 | 73 | | if (audioSource == null) |
| | 74 | | { |
| 0 | 75 | | Debug.LogWarning("AudioSource is null!."); |
| 0 | 76 | | return; |
| | 77 | | } |
| | 78 | |
|
| 14 | 79 | | Model model = (Model) this.model; |
| 14 | 80 | | UpdateAudioSourceVolume(); |
| 14 | 81 | | audioSource.loop = model.loop; |
| 14 | 82 | | audioSource.pitch = model.pitch; |
| 14 | 83 | | audioSource.spatialBlend = 1; |
| 14 | 84 | | audioSource.dopplerLevel = 0.1f; |
| | 85 | |
|
| 14 | 86 | | if (model.playing) |
| | 87 | | { |
| 10 | 88 | | DCLAudioClip dclAudioClip = scene.GetSharedComponent(model.audioClipId) as DCLAudioClip; |
| | 89 | |
|
| 10 | 90 | | if (dclAudioClip != null) |
| | 91 | | { |
| 10 | 92 | | InitDCLAudioClip(dclAudioClip); |
| | 93 | | //NOTE(Brian): Play if finished loading, otherwise will wait for the loading to complete (or fail). |
| 10 | 94 | | if (dclAudioClip.loadingState == DCLAudioClip.LoadState.LOADING_COMPLETED) |
| | 95 | | { |
| 10 | 96 | | ApplyLoadedAudioClip(dclAudioClip); |
| 10 | 97 | | } |
| | 98 | | else |
| | 99 | | { |
| 0 | 100 | | dclAudioClip.OnLoadingFinished -= DclAudioClip_OnLoadingFinished; |
| 0 | 101 | | dclAudioClip.OnLoadingFinished += DclAudioClip_OnLoadingFinished; |
| | 102 | | } |
| 0 | 103 | | } |
| | 104 | | else |
| | 105 | | { |
| 0 | 106 | | Debug.LogError("Wrong audio clip type when playing audiosource!!"); |
| | 107 | | } |
| 0 | 108 | | } |
| | 109 | | else |
| | 110 | | { |
| 4 | 111 | | if (audioSource.isPlaying) |
| | 112 | | { |
| 2 | 113 | | audioSource.Stop(); |
| | 114 | | } |
| | 115 | | } |
| 4 | 116 | | } |
| | 117 | |
|
| | 118 | | private void OnAudioSettingsChanged(SettingsData.AudioSettings settings) |
| | 119 | | { |
| 0 | 120 | | UpdateAudioSourceVolume(); |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | private void OnVirtualAudioMixerChangedValue(float currentValue, float previousValue) |
| | 124 | | { |
| 0 | 125 | | UpdateAudioSourceVolume(); |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private void UpdateAudioSourceVolume() |
| | 129 | | { |
| 18 | 130 | | float newVolume = ((Model)model).volume * Utils.ToVolumeCurve(DataStore.i.virtualAudioMixer.sceneSFXVolume.G |
| | 131 | |
|
| 18 | 132 | | if (scene is GlobalScene globalScene && globalScene.isPortableExperience) |
| | 133 | | { |
| 0 | 134 | | audioSource.volume = newVolume; |
| 0 | 135 | | return; |
| | 136 | | } |
| | 137 | |
|
| 18 | 138 | | if (isOutOfBoundaries) |
| | 139 | | { |
| 6 | 140 | | audioSource.volume = 0; |
| 6 | 141 | | return; |
| | 142 | | } |
| | 143 | |
|
| 12 | 144 | | audioSource.volume = scene.sceneData.id == CommonScriptableObjects.sceneID.Get() ? newVolume : 0f; |
| 12 | 145 | | } |
| | 146 | |
|
| | 147 | | private void OnCurrentSceneChanged(string currentSceneId, string previousSceneId) |
| | 148 | | { |
| 2 | 149 | | if (audioSource != null) |
| | 150 | | { |
| 2 | 151 | | Model model = (Model) this.model; |
| 2 | 152 | | float volume = 0; |
| 2 | 153 | | if ((scene.sceneData.id == currentSceneId) || (scene is GlobalScene globalScene && globalScene.isPortabl |
| | 154 | | { |
| 1 | 155 | | volume = model.volume; |
| | 156 | | } |
| | 157 | |
|
| 2 | 158 | | audioSource.volume = volume; |
| | 159 | | } |
| 2 | 160 | | } |
| | 161 | |
|
| | 162 | | private void OnDestroy() |
| | 163 | | { |
| 11 | 164 | | isDestroyed = true; |
| 11 | 165 | | CommonScriptableObjects.sceneID.OnChange -= OnCurrentSceneChanged; |
| | 166 | |
|
| | 167 | | //NOTE(Brian): Unsuscribe events. |
| 11 | 168 | | InitDCLAudioClip(null); |
| | 169 | |
|
| 11 | 170 | | Settings.i.OnAudioSettingsChanged -= OnAudioSettingsChanged; |
| 11 | 171 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.OnChange -= OnVirtualAudioMixerChangedValue; |
| 11 | 172 | | } |
| | 173 | |
|
| | 174 | | public void UpdateOutOfBoundariesState(bool isEnabled) |
| | 175 | | { |
| 4 | 176 | | isOutOfBoundaries = !isEnabled; |
| 4 | 177 | | UpdateAudioSourceVolume(); |
| 4 | 178 | | } |
| | 179 | |
|
| | 180 | | private void DclAudioClip_OnLoadingFinished(DCLAudioClip obj) |
| | 181 | | { |
| 0 | 182 | | if (obj.loadingState == DCLAudioClip.LoadState.LOADING_COMPLETED && audioSource != null) |
| | 183 | | { |
| 0 | 184 | | ApplyLoadedAudioClip(obj); |
| | 185 | | } |
| | 186 | |
|
| 0 | 187 | | obj.OnLoadingFinished -= DclAudioClip_OnLoadingFinished; |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | private void ApplyLoadedAudioClip(DCLAudioClip clip) |
| | 191 | | { |
| 10 | 192 | | if (audioSource.clip != clip.audioClip) |
| | 193 | | { |
| 10 | 194 | | audioSource.clip = clip.audioClip; |
| | 195 | | } |
| | 196 | |
|
| 10 | 197 | | Model model = (Model) this.model; |
| 10 | 198 | | bool shouldPlay = playedAtTimestamp != model.playedAtTimestamp || |
| | 199 | | (model.playing && !audioSource.isPlaying); |
| | 200 | |
|
| 10 | 201 | | if (audioSource.enabled && model.playing && shouldPlay) |
| | 202 | | { |
| | 203 | | //To remove a pesky and quite unlikely warning when the audiosource is out of scenebounds |
| 10 | 204 | | audioSource.Play(); |
| | 205 | | } |
| | 206 | |
|
| 10 | 207 | | playedAtTimestamp = model.playedAtTimestamp; |
| 10 | 208 | | } |
| | 209 | |
|
| 0 | 210 | | public override int GetClassId() { return (int) CLASS_ID_COMPONENT.AUDIO_SOURCE; } |
| | 211 | | } |
| | 212 | | } |