AkAcousticTextureSetComponent.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #include "AkAcousticTextureSetComponent.h"
  16. #include "AkAudioDevice.h"
  17. #include "AkRoomComponent.h"
  18. #include "AkReverbDescriptor.h"
  19. #include "AkComponentHelpers.h"
  20. #include "AkLateReverbComponent.h"
  21. #include "AkSpatialAudioHelper.h"
  22. UAkAcousticTextureSetComponent::UAkAcousticTextureSetComponent(const class FObjectInitializer& ObjectInitializer) :
  23. Super(ObjectInitializer)
  24. {
  25. PrimaryComponentTick.bCanEverTick = true;
  26. bTickInEditor = true;
  27. #if WITH_EDITOR
  28. if (AkSpatialAudioHelper::GetObjectReplacedEvent())
  29. {
  30. AkSpatialAudioHelper::GetObjectReplacedEvent()->AddUObject(this, &UAkAcousticTextureSetComponent::HandleObjectsReplaced);
  31. }
  32. #endif
  33. }
  34. void UAkAcousticTextureSetComponent::OnRegister()
  35. {
  36. Super::OnRegister();
  37. #if WITH_EDITOR
  38. RegisterAllTextureParamCallbacks();
  39. RegisterReverbRTPCChangedCallback();
  40. #endif
  41. // In the case where a blueprint class has a texture set component and a late reverb component as siblings, We can't know which will be registered first.
  42. // We need to check for the sibling in each OnRegister function and associate the texture set component to the late reverb when they are both registered.
  43. if (USceneComponent* parent = GetAttachParent())
  44. {
  45. if (UAkLateReverbComponent* reverbComp = AkComponentHelpers::GetChildComponentOfType<UAkLateReverbComponent>(*parent))
  46. {
  47. reverbComp->AssociateAkTextureSetComponent(this);
  48. }
  49. }
  50. DampingEstimationNeedsUpdate = true;
  51. }
  52. void UAkAcousticTextureSetComponent::OnUnregister()
  53. {
  54. #if WITH_EDITOR
  55. UnregisterTextureParamChangeCallbacks();
  56. UAkSettings* AkSettings = GetMutableDefault<UAkSettings>();
  57. if (AkSettings != nullptr)
  58. {
  59. if (RTPCChangedHandle.IsValid())
  60. AkSettings->OnReverbRTPCChanged.Remove(RTPCChangedHandle);
  61. }
  62. #endif
  63. Super::OnUnregister();
  64. }
  65. void UAkAcousticTextureSetComponent::BeginPlay()
  66. {
  67. Super::BeginPlay();
  68. DampingEstimationNeedsUpdate = true;
  69. }
  70. void UAkAcousticTextureSetComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  71. {
  72. Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  73. if (SecondsSinceDampingUpdate < PARAM_ESTIMATION_UPDATE_PERIOD)
  74. {
  75. SecondsSinceDampingUpdate += DeltaTime;
  76. }
  77. if (DampingEstimationNeedsUpdate && SecondsSinceDampingUpdate >= PARAM_ESTIMATION_UPDATE_PERIOD)
  78. {
  79. RecalculateHFDamping();
  80. DampingEstimationNeedsUpdate = false;
  81. }
  82. }
  83. void UAkAcousticTextureSetComponent::SetReverbDescriptor(FAkReverbDescriptor* reverbDescriptor)
  84. {
  85. ReverbDescriptor = reverbDescriptor;
  86. #if WITH_EDITOR
  87. UnregisterTextureParamChangeCallbacks();
  88. if (reverbDescriptor != nullptr)
  89. RegisterAllTextureParamCallbacks();
  90. #endif
  91. if (reverbDescriptor != nullptr)
  92. DampingEstimationNeedsUpdate = true;
  93. }
  94. void UAkAcousticTextureSetComponent::RecalculateHFDamping()
  95. {
  96. if (ReverbDescriptor != nullptr && ReverbDescriptor->ShouldEstimateDamping())
  97. {
  98. ReverbDescriptor->CalculateHFDamping(this);
  99. SecondsSinceDampingUpdate = 0.0f;
  100. }
  101. }
  102. #if WITH_EDITOR
  103. void UAkAcousticTextureSetComponent::BeginDestroy()
  104. {
  105. Super::BeginDestroy();
  106. if (AkSpatialAudioHelper::GetObjectReplacedEvent())
  107. {
  108. AkSpatialAudioHelper::GetObjectReplacedEvent()->RemoveAll(this);
  109. }
  110. }
  111. void UAkAcousticTextureSetComponent::HandleObjectsReplaced(const TMap<UObject*, UObject*>& ReplacementMap)
  112. {
  113. if (ReplacementMap.Contains(this))
  114. {
  115. UAkAcousticTextureSetComponent* NewTextureSetComponent = Cast<UAkAcousticTextureSetComponent>(ReplacementMap[this]);
  116. if (USceneComponent* Parent = NewTextureSetComponent->GetAttachParent())
  117. {
  118. if (UAkLateReverbComponent* ReverbComp = AkComponentHelpers::GetChildComponentOfType<UAkLateReverbComponent>(*Parent))
  119. {
  120. ReverbComp->AssociateAkTextureSetComponent(NewTextureSetComponent);
  121. }
  122. }
  123. }
  124. }
  125. void UAkAcousticTextureSetComponent::RegisterReverbRTPCChangedCallback()
  126. {
  127. UAkSettings* AkSettings = GetMutableDefault<UAkSettings>();
  128. if (AkSettings != nullptr)
  129. {
  130. if (RTPCChangedHandle.IsValid())
  131. AkSettings->OnReverbRTPCChanged.Remove(RTPCChangedHandle);
  132. RTPCChangedHandle = AkSettings->OnReverbRTPCChanged.AddLambda([this]()
  133. {
  134. DampingEstimationNeedsUpdate = true;
  135. });
  136. }
  137. }
  138. void UAkAcousticTextureSetComponent::RegisterTextureParamChangeCallback(FGuid textureID)
  139. {
  140. UAkSettings* AkSettings = GetMutableDefault<UAkSettings>();
  141. if (AkSettings != nullptr)
  142. {
  143. if (TextureDelegateHandles.Find(textureID) != nullptr)
  144. {
  145. if (TextureDelegateHandles[textureID].IsValid())
  146. {
  147. AkSettings->OnTextureParamsChanged.Remove(TextureDelegateHandles[textureID]);
  148. }
  149. TextureDelegateHandles.Remove(textureID);
  150. }
  151. TextureDelegateHandles.Add(textureID, AkSettings->OnTextureParamsChanged.AddLambda([&](const FGuid& textureID)
  152. {
  153. if (ContainsTexture(textureID) && ReverbDescriptor != nullptr)
  154. DampingEstimationNeedsUpdate = ReverbDescriptor->ShouldEstimateDamping();
  155. }));
  156. }
  157. }
  158. void UAkAcousticTextureSetComponent::UnregisterTextureParamChangeCallbacks()
  159. {
  160. UAkSettings* AkSettings = GetMutableDefault<UAkSettings>();
  161. if (AkSettings != nullptr)
  162. {
  163. for (auto it = TextureDelegateHandles.CreateIterator(); it; ++it)
  164. {
  165. if (it->Value.IsValid())
  166. AkSettings->OnTextureParamsChanged.Remove(it->Value);
  167. }
  168. TextureDelegateHandles.Empty();
  169. }
  170. }
  171. #endif
  172. bool UAkAcousticTextureSetComponent::ShouldSendGeometry() const
  173. {
  174. UWorld* CurrentWorld = GetWorld();
  175. if (CurrentWorld && !IsRunningCommandlet())
  176. {
  177. return CurrentWorld->WorldType == EWorldType::Game || CurrentWorld->WorldType == EWorldType::PIE;
  178. }
  179. return false;
  180. }
  181. void UAkAcousticTextureSetComponent::SendGeometryToWwise(const AkGeometryParams& params)
  182. {
  183. if (ShouldSendGeometry())
  184. {
  185. FAkAudioDevice* AkAudioDevice = FAkAudioDevice::Get();
  186. if (AkAudioDevice != nullptr && AkAudioDevice->SetGeometry(GetGeometrySetID(), params) == AK_Success)
  187. GeometryHasBeenSent = true;
  188. }
  189. }
  190. void UAkAcousticTextureSetComponent::SendGeometryInstanceToWwise(const FRotator& rotation, const FVector& location, const FVector& scale, const AkRoomID roomID)
  191. {
  192. if (ShouldSendGeometry() && GeometryHasBeenSent)
  193. {
  194. AkVector front, up;
  195. AkVector64 position;
  196. FAkAudioDevice::FVectorToAKVector(rotation.RotateVector(FVector::ForwardVector), front);
  197. FAkAudioDevice::FVectorToAKVector(rotation.RotateVector(FVector::UpVector), up);
  198. FAkAudioDevice::FVectorToAKVector64(location, position);
  199. AkGeometryInstanceParams params;
  200. params.PositionAndOrientation.Set(position, front, up);
  201. FAkAudioDevice::FVectorToAKVector(scale, params.Scale);
  202. params.GeometrySetID = GetGeometrySetID();
  203. params.RoomID = roomID;
  204. FAkAudioDevice* AkAudioDevice = FAkAudioDevice::Get();
  205. if (AkAudioDevice != nullptr && AkAudioDevice->SetGeometryInstance(GetGeometrySetID(), params) == AK_Success)
  206. GeometryInstanceHasBeenSent = true;
  207. }
  208. }
  209. void UAkAcousticTextureSetComponent::RemoveGeometryFromWwise()
  210. {
  211. if (ShouldSendGeometry() && GeometryHasBeenSent)
  212. {
  213. FAkAudioDevice* AkAudioDevice = FAkAudioDevice::Get();
  214. if (AkAudioDevice != nullptr && AkAudioDevice->RemoveGeometrySet(GetGeometrySetID()) == AK_Success)
  215. {
  216. GeometryHasBeenSent = false;
  217. GeometryInstanceHasBeenSent = false;
  218. }
  219. }
  220. }
  221. void UAkAcousticTextureSetComponent::RemoveGeometryInstanceFromWwise()
  222. {
  223. if (ShouldSendGeometry() && GeometryInstanceHasBeenSent)
  224. {
  225. FAkAudioDevice* AkAudioDevice = FAkAudioDevice::Get();
  226. if (AkAudioDevice != nullptr && AkAudioDevice->RemoveGeometrySet(GetGeometrySetID()) == AK_Success)
  227. GeometryInstanceHasBeenSent = false;
  228. }
  229. }