AkAuxBus.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. }
  41. CookedDataToArchive.Serialize(Ar);
  42. }
  43. #else
  44. AuxBusCookedData.Serialize(Ar);
  45. #endif
  46. #endif
  47. }
  48. void UAkAuxBus::LoadAuxBus()
  49. {
  50. SCOPED_AKAUDIO_EVENT_2(TEXT("LoadAuxBus"));
  51. auto* ResourceLoader = FWwiseResourceLoader::Get();
  52. if (UNLIKELY(!ResourceLoader))
  53. {
  54. return;
  55. }
  56. if (LoadedAuxBus)
  57. {
  58. UnloadAuxBus(false);
  59. }
  60. #if WITH_EDITORONLY_DATA
  61. if (IWwiseProjectDatabaseModule::IsInACookingCommandlet())
  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. #endif
  81. LoadedAuxBus = ResourceLoader->LoadAuxBus(AuxBusCookedData);
  82. }
  83. void UAkAuxBus::UnloadAuxBus(bool bAsync)
  84. {
  85. if (LoadedAuxBus)
  86. {
  87. auto* ResourceLoader = FWwiseResourceLoader::Get();
  88. if (UNLIKELY(!ResourceLoader))
  89. {
  90. return;
  91. }
  92. if (bAsync)
  93. {
  94. FWwiseLoadedAuxBusPromise Promise;
  95. Promise.EmplaceValue(MoveTemp(LoadedAuxBus));
  96. ResourceUnload = ResourceLoader->UnloadAuxBusAsync(Promise.GetFuture());
  97. }
  98. else
  99. {
  100. ResourceLoader->UnloadAuxBus(MoveTemp(LoadedAuxBus));
  101. }
  102. LoadedAuxBus = nullptr;
  103. }
  104. }
  105. #if WITH_EDITORONLY_DATA
  106. void UAkAuxBus::CookAdditionalFilesOverride(const TCHAR* PackageFilename, const ITargetPlatform* TargetPlatform,
  107. TFunctionRef<void(const TCHAR* Filename, void* Data, int64 Size)> WriteAdditionalFile)
  108. {
  109. if (HasAnyFlags(RF_ClassDefaultObject))
  110. {
  111. return;
  112. }
  113. FWwiseResourceCooker* ResourceCooker = FWwiseResourceCooker::GetForPlatform(TargetPlatform);
  114. if (!ResourceCooker)
  115. {
  116. return;
  117. }
  118. ResourceCooker->SetSandboxRootPath(PackageFilename);
  119. ResourceCooker->CookAuxBus(GetValidatedInfo(AuxBusInfo), WriteAdditionalFile);
  120. }
  121. bool UAkAuxBus::ObjectIsInSoundBanks()
  122. {
  123. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  124. if (UNLIKELY(!ResourceCooker))
  125. {
  126. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::GetWwiseRef: ResourceCooker not initialized"));
  127. return false;
  128. }
  129. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  130. if (UNLIKELY(!ProjectDatabase))
  131. {
  132. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::GetWwiseRef: ProjectDatabase not initialized"));
  133. return false;
  134. }
  135. FWwiseObjectInfo* AudioTypeInfo = &AuxBusInfo;
  136. const FWwiseRefAuxBus AudioTypeRef = FWwiseDataStructureScopeLock(*ProjectDatabase).GetAuxBus(
  137. GetValidatedInfo(AuxBusInfo));
  138. return AudioTypeRef.IsValid();
  139. }
  140. void UAkAuxBus::FillInfo()
  141. {
  142. auto* ResourceCooker = FWwiseResourceCooker::GetDefault();
  143. if (UNLIKELY(!ResourceCooker))
  144. {
  145. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::FillInfo: ResourceCooker not initialized"));
  146. return;
  147. }
  148. auto ProjectDatabase = ResourceCooker->GetProjectDatabase();
  149. if (UNLIKELY(!ProjectDatabase))
  150. {
  151. UE_LOG(LogAkAudio, Error, TEXT("UAkAuxBus::FillInfo: ProjectDatabase not initialized"));
  152. return;
  153. }
  154. FWwiseObjectInfo* AudioTypeInfo = &AuxBusInfo;
  155. const FWwiseRefAuxBus AudioTypeRef = FWwiseDataStructureScopeLock(*ProjectDatabase).GetAuxBus(
  156. GetValidatedInfo(AuxBusInfo));
  157. if (AudioTypeRef.AuxBusName().IsNone() || !AudioTypeRef.AuxBusGuid().IsValid() || AudioTypeRef.AuxBusId() == AK_INVALID_UNIQUE_ID)
  158. {
  159. UE_LOG(LogAkAudio, Warning, TEXT("UAkAuxBus::FillInfo: Valid object not found in Project Database"));
  160. return;
  161. }
  162. AudioTypeInfo->WwiseName = AudioTypeRef.AuxBusName();
  163. AudioTypeInfo->WwiseGuid = AudioTypeRef.AuxBusGuid();
  164. AudioTypeInfo->WwiseShortId = AudioTypeRef.AuxBusId();
  165. }
  166. #endif