AkLateReverbComponent.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*******************************************************************************
  2. The content of this file includes portions of the proprietary AUDIOKINETIC Wwise
  3. Technology released in source code form as part of the game integration package.
  4. The content of this file may not be used without valid licenses to the
  5. AUDIOKINETIC Wwise Technology.
  6. Note that the use of the game engine is subject to the Unreal(R) Engine End User
  7. License Agreement at https://www.unrealengine.com/en-US/eula/unreal
  8. License Usage
  9. Licensees holding valid licenses to the AUDIOKINETIC Wwise Technology may use
  10. this file in accordance with the end user license agreement provided with the
  11. software or, alternatively, in accordance with the terms contained
  12. in a written agreement between you and Audiokinetic Inc.
  13. Copyright (c) 2023 Audiokinetic Inc.
  14. *******************************************************************************/
  15. #pragma once
  16. #include "Components/SceneComponent.h"
  17. #include "AkReverbDescriptor.h"
  18. #if WITH_EDITOR
  19. #include "Components/TextRenderComponent.h"
  20. #endif
  21. #include "AkLateReverbComponent.generated.h"
  22. class UAkRoomComponent;
  23. class UAkAcousticTextureSetComponent;
  24. UCLASS(ClassGroup = Audiokinetic, BlueprintType, hidecategories = (Transform, Rendering, Mobility, LOD, Component, Activation, Tags), meta = (BlueprintSpawnableComponent))
  25. class AKAUDIO_API UAkLateReverbComponent : public USceneComponent
  26. {
  27. GENERATED_BODY()
  28. public:
  29. UAkLateReverbComponent(const class FObjectInitializer& ObjectInitializer);
  30. /**
  31. * Enable the Late Reverb Component to apply a late reverb to sounds emitted in this volume. Additional properties are available in the Late Reverb category.
  32. * The number of simultaneous reverb volumes is configurable in the Unreal Editor Project Settings under Plugins > Wwise
  33. * If this Late Reverb is applied to a Spatial Audio Room, it is active even if the maximum number of simultaneous reverb volumes is reached.
  34. */
  35. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "EnableComponent", meta = (DisplayName = "Enable Late Reverb"))
  36. bool bEnable = false;
  37. /** Maximum send level to the Wwise Auxiliary Bus associated to this AkReverbVolume */
  38. UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = "Late Reverb", meta = (ClampMin = 0.0f, ClampMax = 1.0f, UIMin = 0.0f, UIMax = 1.0f))
  39. float SendLevel = .0f;
  40. /** Rate at which to fade in/out the SendLevel of the current Reverb Volume when entering/exiting it, in percentage per second (0.2 will make the fade time 5 seconds) */
  41. UPROPERTY(EditAnywhere,BlueprintReadWrite, Category = "Late Reverb" ,meta = (ClampMin = 0.0f, UIMin = 0.0f))
  42. float FadeRate = .0f;
  43. /**
  44. * The precedence in which the AkReverbVolumes will be applied. In the case of overlapping volumes, only the ones
  45. * with the highest priority are chosen. If two or more overlapping AkReverbVolumes have the same
  46. * priority, the chosen AkReverbVolume is unpredictable.
  47. * If this Late Reverb is applied to a Spatial Audio room, the room's priority will be used instead.
  48. * Sound emitted by game objects in a room will always be sent to the room late reverb independently of other late reverbs in the scene.
  49. */
  50. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Late Reverb")
  51. float Priority = .0f;
  52. /**
  53. * When enabled, the aux bus for this reverb component will be assigned automatically. This is done by estimating the decay time of the reverb produced by the parent Primitive Component, given its volume and surface area.
  54. * This decay value is used to select an aux bus from the reverb aux bus assignment map in the integration settings.
  55. */
  56. UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Late Reverb")
  57. bool AutoAssignAuxBus = true;
  58. UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition = "!AutoAssignAuxBus"), Category = "Late Reverb")
  59. class UAkAuxBus* AuxBus = nullptr;
  60. /** Wwise Auxiliary Bus associated to this AkReverbVolume */
  61. UPROPERTY(EditAnywhere, BlueprintReadWrite, AdvancedDisplay, meta = (EditCondition = "!AutoAssignAuxBus"), Category = "Late Reverb")
  62. FString AuxBusName;
  63. /** Get the AkAuxBusId associated to AuxBusName */
  64. uint32 GetAuxBusId() const;
  65. bool HasEffectOnLocation(const FVector& Location) const;
  66. bool LateReverbIsActive() const { return Parent.IsValid() && bEnable && !IsRunningCommandlet(); }
  67. virtual void BeginPlay() override;
  68. virtual void BeginDestroy() override;
  69. virtual void OnRegister() override;
  70. virtual void OnUnregister() override;
  71. virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
  72. virtual bool MoveComponentImpl(
  73. const FVector & Delta,
  74. const FQuat & NewRotation,
  75. bool bSweep,
  76. FHitResult * Hit,
  77. EMoveComponentFlags MoveFlags,
  78. ETeleportType Teleport) override;
  79. virtual void OnUpdateTransform(EUpdateTransformFlags UpdateTransformFlags, ETeleportType Teleport) override;
  80. virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
  81. virtual void PostLoad() override;
  82. virtual void Serialize(FArchive& Ar) override;
  83. #if WITH_EDITOR
  84. virtual void PreEditChange(FProperty* PropertyAboutToChange) override;
  85. virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
  86. virtual void OnAttachmentChanged() override;
  87. void UpdateHFDampingEstimation(float hfDamping);
  88. void UpdatePredelayEstimation(float predelay);
  89. virtual void OnComponentDestroyed(bool bDestroyingHierarchy) override;
  90. virtual void InitializeComponent() override;
  91. virtual void OnComponentCreated() override;
  92. void RegisterReverbInfoEnabledCallback();
  93. FDelegateHandle ShowReverbInfoChangedHandle;
  94. #endif
  95. void UpdateDecayEstimation(float decay, float volume, float surfaceArea);
  96. void UpdateRTPCs(const UAkRoomComponent* room) const;
  97. /** Set the component that will be used to estimate the HFDamping. For example, in a Blueprint that has a static mesh component with an AkGeometry child component, this function can be called in BeginPlay to associate that AkGeometry component with this reverb component.
  98. * If this late reverb component has a sibling geometry component (or surface reflector set component), they will be associated automatically and there is no need to call this function.
  99. */
  100. UFUNCTION(BlueprintCallable, Category = "Audiokinetic|LateReverb|ReverbParameterEstimation")
  101. void AssociateAkTextureSetComponent(UAkAcousticTextureSetComponent* textureSetComponent);
  102. UAkAcousticTextureSetComponent* GetAttachedTextureSetComponent();
  103. /* public function to get notified when the texture set changed */
  104. void TextureSetUpdated();
  105. private:
  106. friend class FAkAudioDevice;
  107. TWeakObjectPtr<class UPrimitiveComponent> Parent;
  108. /** Save the manually assigned aux bus so we can recall it if auto-assign is disabled. */
  109. UPROPERTY()
  110. class UAkAuxBus* AuxBusManual = nullptr;
  111. /** The component that will be used to estimate the HFDamping value. This will usually be an AkGeometryComponent.
  112. * When the owning Actor is a Volume (as is the case for SpatialAudioVolume) this will be an AkSurfaceReflectorSetComponent.
  113. */
  114. UAkAcousticTextureSetComponent* TextureSetComponent = nullptr;
  115. // Used to estimate the reverb parameters from the Primitive parent.
  116. FAkReverbDescriptor ReverbDescriptor;
  117. // Used to track when the reverb parameters need updated (on register, and when the size changes).
  118. float SecondsSinceDecayUpdate = 0.0f;
  119. bool DecayEstimationNeedsUpdate = false;
  120. float SecondsSincePredelayUpdate = 0.0f;
  121. bool PredelayEstimationNeedsUpdate = false;
  122. bool ReverbAssignmentNeedsUpdate = false;
  123. // Indicates that the component was added to the spatial index in AkAudioDevice.
  124. bool IsIndexed = false;
  125. bool TextureSetHasChanged = false;
  126. bool ReverbParamsChanged = false;
  127. void OnReverbParamsChanged();
  128. void RecalculateDecay();
  129. void RecalculatePredelay();
  130. void InitializeParent();
  131. void ParentChanged();
  132. bool EncompassesPoint(FVector Point, float SphereRadius = 0.f, float* OutDistanceToPoint = nullptr) const;
  133. // Used to track when the parameters of an Acoustic Texture asset change.
  134. FDelegateHandle TextureParamChangedHandle;
  135. #if WITH_EDITOR
  136. void HandleObjectsReplaced(const TMap<UObject*, UObject*>& ReplacementMap);
  137. #endif
  138. #if WITH_EDITORONLY_DATA
  139. static float TextVisualizerHeightOffset;
  140. bool bTextStatusNeedsUpdate = false;
  141. // The text visualizers display the values of the parameter estimations directly in the level (or blueprint editor).
  142. UPROPERTY(SkipSerialization, NonTransactional)
  143. UTextRenderComponent* TextVisualizerLabels = nullptr;
  144. UPROPERTY(SkipSerialization, NonTransactional)
  145. UTextRenderComponent* TextVisualizerValues = nullptr;
  146. void UpdateTextVisualizerStatus();
  147. bool TextVisualizersInitialized() const;
  148. FText GetValuesLabels() const;
  149. void DestroyTextVisualizers();
  150. void InitTextVisualizers();
  151. void UpdateValuesLabels();
  152. bool WasSelected = false;
  153. FVector GetTextVisualizersLocation();
  154. // Used to track when the Reverb Assignment in the integration settings changes.
  155. void RegisterReverbAssignmentChangedCallback();
  156. FDelegateHandle ReverbAssignmentChangedHandle;
  157. // Used to track when the Global Decay Absorption value in the integration settings changes.
  158. void RegisterGlobalDecayAbsorptionChangedCallback();
  159. FDelegateHandle GlobalDecayAbsorptionChangedHandle;
  160. // Used to track when the global RTPCs in the integration settings changes.
  161. void RegisterReverbRTPCChangedCallback();
  162. FDelegateHandle RTPCChangedHandle;
  163. UPROPERTY(VisibleAnywhere, Category = "Late Reverb|Reverb Parameter Estimation|Primitive Geometry")
  164. float EnvironmentVolume = 0.0f;
  165. UPROPERTY(VisibleAnywhere, Category = "Late Reverb|Reverb Parameter Estimation|Primitive Geometry")
  166. float SurfaceArea = 0.0f;
  167. /** An estimation of the T60 (the time taken for the sound pressure level to reduce by 60dB) for the reverb's environment, based on the primitive component to which the late reverb is attached.
  168. * This T60 value can be used to automatically assign an aux bus using the Reverb Assignment Map in the integration settings, and/or to drive the Decay Estimate RTPC, also found in the integration settings.
  169. * In order to use the global reverb RTPCs, the reverb component must have a sibling AkRoomComponent (in other words, a room component attached to the same Primitive parent).
  170. */
  171. UPROPERTY(VisibleAnywhere, Category = "Late Reverb|Reverb Parameter Estimation")
  172. float EnvironmentDecayEstimate = 0.0f;
  173. /** This value is driven by the acoustic textures used in an associated AkGeometryComponent or AkSurfaceReflectorSetComponent. It measures the average frequency bias of the damping. In other words, whether there is more high-frequency damping, more low-frequency damping, or uniform damping across frequencies.
  174. * A value of 0.0 indicates uniform damping across all frequencies. A value > 0.0 indicates more damping for higher frequencies than lower frequencies. A value < 0.0 indicates more damping for lower frequencies than high frequencies. Average absorption values are calculated using each of the textures in the collection, weighted by their corresponding surface area.
  175. * This value can be used to drive the HFDamping RTPC, found in the integration settings.
  176. * In order to use the global reverb RTPCs, the reverb component must have a sibling AkRoomComponent (in other words, a room component attached to the same Primitive parent).
  177. */
  178. UPROPERTY(VisibleAnywhere, Category = "Late Reverb|Reverb Parameter Estimation")
  179. float HFDamping = 0.0f;
  180. /** An estimation of the time taken for the first reflection to reach the listener, based on the primitive component to which the late reverb is attached.
  181. * This is estimated based on an emitter and listener being positioned at the centre of the parent primitive component. This value can be used to drive the Time To First Reflection RTPC, found in the integration settings.
  182. * In order to use the global reverb RTPCs, the reverb component must have a sibling AkRoomComponent (in other words, a room component attached to the same Primitive parent).
  183. */
  184. UPROPERTY(VisibleAnywhere, Category = "Late Reverb|Reverb Parameter Estimation")
  185. float TimeToFirstReflection = 0.0f;
  186. #endif
  187. };