AkAmbientSound.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /*=============================================================================
  16. AkAmbientSound.cpp:
  17. =============================================================================*/
  18. #include "AkAmbientSound.h"
  19. #include "AkAudioDevice.h"
  20. #include "AkComponent.h"
  21. #include "AkAudioEvent.h"
  22. #include "Wwise/WwiseExternalSourceManager.h"
  23. /*------------------------------------------------------------------------------------
  24. AAkAmbientSound
  25. ------------------------------------------------------------------------------------*/
  26. DEFINE_LOG_CATEGORY(LogAkAmbientSound);
  27. AAkAmbientSound::AAkAmbientSound(const class FObjectInitializer& ObjectInitializer) :
  28. Super(ObjectInitializer)
  29. {
  30. // Property initialization
  31. StopWhenOwnerIsDestroyed = true;
  32. CurrentlyPlaying = false;
  33. static const FName ComponentName = TEXT("AkAudioComponent0");
  34. AkComponent = ObjectInitializer.CreateDefaultSubobject<UAkComponent>(this, ComponentName);
  35. AkComponent->StopWhenOwnerDestroyed = StopWhenOwnerIsDestroyed;
  36. RootComponent = AkComponent;
  37. AkComponent->AttenuationScalingFactor = 1.f;
  38. //bNoDelete = true;
  39. SetHidden(true);
  40. AutoPost = false;
  41. }
  42. void AAkAmbientSound::PostLoad()
  43. {
  44. Super::PostLoad();
  45. #if WITH_EDITOR
  46. if( AkAudioEvent_DEPRECATED )
  47. {
  48. AkComponent->AkAudioEvent = AkAudioEvent_DEPRECATED;
  49. }
  50. #endif
  51. }
  52. void AAkAmbientSound::PostInitializeComponents()
  53. {
  54. Super::PostInitializeComponents();
  55. AkComponent->UpdateAkLateReverbComponentList(AkComponent->GetComponentLocation());
  56. }
  57. void AAkAmbientSound::BeginPlay()
  58. {
  59. Super::BeginPlay();
  60. if (AutoPost)
  61. {
  62. StartAmbientSound();
  63. }
  64. }
  65. #if WITH_EDITOR
  66. void AAkAmbientSound::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
  67. {
  68. if( AkComponent )
  69. {
  70. // Reset audio component.
  71. if( IsCurrentlyPlaying() )
  72. {
  73. StartPlaying();
  74. }
  75. }
  76. Super::PostEditChangeProperty(PropertyChangedEvent);
  77. }
  78. #endif
  79. void AAkAmbientSound::StartAmbientSound()
  80. {
  81. StartPlaying();
  82. }
  83. void AAkAmbientSound::StopAmbientSound()
  84. {
  85. StopPlaying();
  86. }
  87. void AAkAmbientSound::StartPlaying()
  88. {
  89. if( !IsCurrentlyPlaying() )
  90. {
  91. FAkAudioDevice* AkAudioDevice = FAkAudioDevice::Get();
  92. if (AkAudioDevice)
  93. {
  94. AkAudioDevice->SetAttenuationScalingFactor(this, AkComponent->AttenuationScalingFactor);
  95. }
  96. if (AkComponent->AkAudioEvent)
  97. {
  98. AkComponent->AkAudioEvent->PostOnActor(this, nullptr, nullptr, nullptr, (AkCallbackType)0, nullptr, StopWhenOwnerIsDestroyed);
  99. }
  100. else
  101. {
  102. UE_LOG(LogAkAmbientSound, Warning, TEXT("Failed to post invalid AkAudioEvent on actor '%s'."), *this->GetName());
  103. }
  104. }
  105. }
  106. void AAkAmbientSound::StopPlaying()
  107. {
  108. if( IsCurrentlyPlaying() )
  109. {
  110. // State of CurrentlyPlaying gets updated in UAkComponent::Stop() through the EndOfEvent callback.
  111. AkComponent->Stop();
  112. }
  113. }
  114. bool AAkAmbientSound::IsCurrentlyPlaying()
  115. {
  116. return AkComponent != nullptr && AkComponent->HasActiveEvents();
  117. }