AkAmbientSound.cpp 3.8 KB

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