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