AkAuxBus.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "AkAuxBus.h"
  16. #include "AkAudioBank.h"
  17. #include "Wwise/WwiseResourceLoader.h"
  18. #include "AkInclude.h"
  19. #include "AkAudioDevice.h"
  20. #include "Wwise/Stats/AkAudio.h"
  21. #if WITH_EDITORONLY_DATA
  22. #include "Wwise/WwiseProjectDatabase.h"
  23. #include "Wwise/WwiseResourceCooker.h"
  24. #endif
  25. void UAkAuxBus::Serialize(FArchive& Ar)
  26. {
  27. Super::Serialize(Ar);
  28. if (HasAnyFlags(RF_ClassDefaultObject))
  29. {
  30. return;
  31. }
  32. #if !UE_SERVER
  33. #if WITH_EDITORONLY_DATA
  34. if (Ar.IsCooking() && Ar.IsSaving() && !Ar.CookingTarget()->IsServerOnly())
  35. {
  36. FWwiseLocalizedAuxBusCookedData CookedDataToArchive;
  37. if (auto* ResourceCooker = FWwiseResourceCooker::GetForArchive(Ar))
  38. {
  39. ResourceCooker->PrepareCookedData(CookedDataToArchive, GetValidatedInfo(AuxBusInfo));
  40. FillMetadata(ResourceCooker->GetProjectDatabase());
  41. }
  42. CookedDataToArchive.Serialize(Ar);
  43. Ar << MaxAttenuationRadius;
  44. }
  45. #else
  46. AuxBusCookedData.Serialize(Ar);
  47. Ar << MaxAttenuationRadius;
  48. #endif
  49. #endif
  50. }
  51. void UAkAuxBus::LoadAuxBus()
  52. {
  53. SCOPED_AKAUDIO_EVENT_2(TEXT("LoadAuxBus"));
  54. auto* ResourceLoader = FWwiseResourceLoader::Get();
  55. if (UNLIKELY(!ResourceLoader))
  56. {
  57. return;
  58. }
  59. UnloadAuxBus(false);
  60. #if WITH_EDITORONLY_DATA
  61. if (!IWwiseProjectDatabaseModule::ShouldInitializeProjectDatabase())
  62. {
  63. return;
  64. }
  65. auto* ProjectDatabase = FWwiseProjectDatabase::Get();
  66. if (!ProjectDatabase || !ProjectDatabase->IsProjectDatabaseParsed())
  67. {
  68. UE_LOG(LogAkAudio, VeryVerbose, TEXT("UAkAuxBus::LoadAuxBus: Not loading '%s' because project database is not parsed."), *GetName())
  69. return;
  70. }
  71. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  72. if (UNLIKELY(!ResourceCooker))
  73. {
  74. return;
  75. }
  76. if (UNLIKELY(!ResourceCooker->PrepareCookedData(AuxBusCookedData, GetValidatedInfo(AuxBusInfo))))
  77. {
  78. return;
  79. }
  80. FillMetadata(ResourceCooker->GetProjectDatabase());
  81. #endif
  82. const auto NewlyLoadedAuxBus = ResourceLoader->LoadAuxBus(AuxBusCookedData);
  83. auto PreviouslyLoadedAuxBus = LoadedAuxBus.exchange(NewlyLoadedAuxBus);
  84. if (UNLIKELY(PreviouslyLoadedAuxBus))
  85. {
  86. ResourceLoader->UnloadAuxBus(MoveTemp(PreviouslyLoadedAuxBus));
  87. }
  88. }
  89. void UAkAuxBus::UnloadAuxBus(bool bAsync)
  90. {
  91. auto PreviouslyLoadedAuxBus = LoadedAuxBus.exchange(nullptr);
  92. if (PreviouslyLoadedAuxBus)
  93. {
  94. auto* ResourceLoader = FWwiseResourceLoader::Get();
  95. if (UNLIKELY(!ResourceLoader))
  96. {
  97. return;
  98. }
  99. if (bAsync)
  100. {
  101. FWwiseLoadedAuxBusPromise Promise;
  102. Promise.EmplaceValue(MoveTemp(PreviouslyLoadedAuxBus));
  103. ResourceUnload = ResourceLoader->UnloadAuxBusAsync(Promise.GetFuture());
  104. }
  105. else
  106. {
  107. ResourceLoader->UnloadAuxBus(MoveTemp(PreviouslyLoadedAuxBus));
  108. }
  109. }
  110. }
  111. #if WITH_EDITORONLY_DATA
  112. void UAkAuxBus::CookAdditionalFilesOverride(const TCHAR* PackageFilename, const ITargetPlatform* TargetPlatform,
  113. TFunctionRef<void(const TCHAR* Filename, void* Data, int64 Size)> WriteAdditionalFile)
  114. {
  115. if (HasAnyFlags(RF_ClassDefaultObject))
  116. {
  117. return;
  118. }
  119. FWwiseResourceCooker* ResourceCooker = FWwiseResourceCooker::GetForPlatform(TargetPlatform);
  120. if (!ResourceCooker)
  121. {
  122. return;
  123. }
  124. ResourceCooker->SetSandboxRootPath(PackageFilename);
  125. ResourceCooker->CookAuxBus(GetValidatedInfo(AuxBusInfo), WriteAdditionalFile);
  126. }
  127. void UAkAuxBus::FillMetadata(FWwiseProjectDatabase* ProjectDatabase)
  128. {
  129. Super::FillMetadata(ProjectDatabase);
  130. const auto AuxBusRef = FWwiseDataStructureScopeLock(*ProjectDatabase).GetAuxBus(GetValidatedInfo(AuxBusInfo));
  131. if (UNLIKELY(!AuxBusRef.IsValid()))
  132. {
  133. UE_LOG(LogAkAudio, Log, TEXT("UAkAuxBus::FillMetadata (%s): Cannot fill Metadata - Aux Bus not found in Project Database"), *GetName());
  134. return;
  135. }
  136. const FWwiseMetadataBus* AuxBusMetadata = AuxBusRef.GetAuxBus();
  137. if (AuxBusMetadata->Name.ToString().IsEmpty() || !AuxBusMetadata->GUID.IsValid() || AuxBusMetadata->Id == AK_INVALID_UNIQUE_ID)
  138. {
  139. UE_LOG(LogAkAudio, Warning, TEXT("UAkAuxBus::FillMetadata: Valid object not found in Project Database"));
  140. return;
  141. }
  142. MaxAttenuationRadius = AuxBusMetadata->MaxAttenuation;
  143. }
  144. bool UAkAuxBus::ObjectIsInSoundBanks()
  145. {
  146. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  147. if (UNLIKELY(!ResourceCooker))
  148. {
  149. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::GetWwiseRef: ResourceCooker not initialized"));
  150. return false;
  151. }
  152. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  153. if (UNLIKELY(!ProjectDatabase))
  154. {
  155. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::GetWwiseRef: ProjectDatabase not initialized"));
  156. return false;
  157. }
  158. FWwiseObjectInfo* AudioTypeInfo = &AuxBusInfo;
  159. const FWwiseRefAuxBus AudioTypeRef = FWwiseDataStructureScopeLock(*ProjectDatabase).GetAuxBus(
  160. GetValidatedInfo(AuxBusInfo));
  161. return AudioTypeRef.IsValid();
  162. }
  163. void UAkAuxBus::FillInfo()
  164. {
  165. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  166. if (UNLIKELY(!ResourceCooker))
  167. {
  168. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::FillInfo: ResourceCooker not initialized"));
  169. return;
  170. }
  171. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  172. if (UNLIKELY(!ProjectDatabase))
  173. {
  174. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::FillInfo: ProjectDatabase not initialized"));
  175. return;
  176. }
  177. FWwiseObjectInfo* AudioTypeInfo = &AuxBusInfo;
  178. const FWwiseRefAuxBus AudioTypeRef = FWwiseDataStructureScopeLock(*ProjectDatabase).GetAuxBus(
  179. GetValidatedInfo(AuxBusInfo));
  180. if (AudioTypeRef.AuxBusName().ToString().IsEmpty() || !AudioTypeRef.AuxBusGuid().IsValid() || AudioTypeRef.AuxBusId() == AK_INVALID_UNIQUE_ID)
  181. {
  182. UE_LOG(LogAkAudio, Warning, TEXT("UAkAuxBus::FillInfo: Valid object not found in Project Database"));
  183. return;
  184. }
  185. AudioTypeInfo->WwiseName = AudioTypeRef.AuxBusName();
  186. AudioTypeInfo->WwiseGuid = AudioTypeRef.AuxBusGuid();
  187. AudioTypeInfo->WwiseShortId = AudioTypeRef.AuxBusId();
  188. }
  189. #endif