AudioProxyComponent.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "AudioProxyComponent.h"
  9. #include <ISystem.h>
  10. #include <MathConversion.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/Debug/Trace.h>
  15. namespace LmbrCentral
  16. {
  17. //=========================================================================
  18. void AudioProxyComponent::Reflect(AZ::ReflectContext* context)
  19. {
  20. auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  21. if (serializeContext)
  22. {
  23. serializeContext->Class<AudioProxyComponent, AZ::Component>()
  24. ->Version(1)
  25. ;
  26. auto editContext = serializeContext->GetEditContext();
  27. if (editContext)
  28. {
  29. editContext->Class<AudioProxyComponent>("Audio Proxy", "The Audio Proxy component is a required dependency when you add other audio components to an entity")
  30. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  31. ->Attribute(AZ::Edit::Attributes::Category, "Audio")
  32. ->Attribute(AZ::Edit::Attributes::Icon, "Icons/Components/AudioProxy.svg")
  33. ->Attribute(AZ::Edit::Attributes::ViewportIcon, "Icons/Components/Viewport/AudioProxy.svg")
  34. ->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC_CE("Game"))
  35. ->Attribute(AZ::Edit::Attributes::AddableByUser, true)
  36. ->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/audio/proxy/")
  37. ;
  38. }
  39. }
  40. }
  41. //=========================================================================
  42. void AudioProxyComponent::Activate()
  43. {
  44. AZ_Assert(!m_audioProxy, "AudioProxyComponent::Activate - Audio Proxy has been set already!");
  45. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  46. {
  47. m_audioProxy = audioSystem->GetAudioProxy();
  48. }
  49. if (m_audioProxy)
  50. {
  51. AZStd::string proxyName = AZStd::string::format("%s_audioproxy", GetEntity()->GetName().c_str());
  52. m_audioProxy->Initialize(proxyName.c_str(), reinterpret_cast<void*>(static_cast<uintptr_t>(static_cast<AZ::u64>(GetEntityId()))));
  53. m_audioProxy->SetObstructionCalcType(Audio::ObstructionType::Ignore);
  54. // don't need to set position on the proxy now, but initialize the transform from the entity.
  55. AZ::TransformBus::EventResult(m_transform, GetEntityId(), &AZ::TransformBus::Events::GetWorldTM);
  56. AudioProxyComponentRequestBus::Handler::BusConnect(GetEntityId());
  57. AZ::TransformNotificationBus::Handler::BusConnect(GetEntityId());
  58. }
  59. }
  60. //=========================================================================
  61. void AudioProxyComponent::Deactivate()
  62. {
  63. if (m_audioProxy)
  64. {
  65. AZ::TransformNotificationBus::Handler::BusDisconnect(GetEntityId());
  66. AudioProxyComponentRequestBus::Handler::BusDisconnect(GetEntityId());
  67. m_audioProxy->StopAllTriggers();
  68. m_audioProxy->Release();
  69. m_audioProxy = nullptr;
  70. }
  71. }
  72. //=========================================================================
  73. void AudioProxyComponent::OnTransformChanged(const AZ::Transform& /*local*/, const AZ::Transform& world)
  74. {
  75. m_transform = world;
  76. if (m_tracksEntityPosition)
  77. {
  78. m_audioProxy->SetPosition(m_transform);
  79. }
  80. }
  81. //=========================================================================
  82. bool AudioProxyComponent::ExecuteTrigger(const Audio::TAudioControlID triggerID)
  83. {
  84. if (triggerID != INVALID_AUDIO_CONTROL_ID)
  85. {
  86. // set the position at the Entity's current location.
  87. // need to poll in case we haven't hit a transform update yet.
  88. m_audioProxy->SetPosition(m_transform);
  89. // ...and kick it off...
  90. m_audioProxy->ExecuteTrigger(triggerID);
  91. return true;
  92. }
  93. return false;
  94. }
  95. //=========================================================================
  96. bool AudioProxyComponent::ExecuteSourceTrigger(const Audio::TAudioControlID triggerID, const Audio::SAudioSourceInfo& sourceInfo)
  97. {
  98. if (triggerID != INVALID_AUDIO_CONTROL_ID)
  99. {
  100. // set the position at the Entity's current location.
  101. // need to poll in case we haven't hit a transform update yet.
  102. m_audioProxy->SetPosition(m_transform);
  103. // ...and kick it off...
  104. m_audioProxy->ExecuteSourceTrigger(triggerID, sourceInfo);
  105. return true;
  106. }
  107. return false;
  108. }
  109. //=========================================================================
  110. void AudioProxyComponent::KillTrigger(const Audio::TAudioControlID triggerID)
  111. {
  112. m_audioProxy->StopTrigger(triggerID);
  113. }
  114. //=========================================================================
  115. void AudioProxyComponent::KillAllTriggers()
  116. {
  117. m_audioProxy->StopAllTriggers();
  118. }
  119. //=========================================================================
  120. void AudioProxyComponent::SetRtpcValue(const Audio::TAudioControlID rtpcID, float value)
  121. {
  122. m_audioProxy->SetRtpcValue(rtpcID, value);
  123. }
  124. //=========================================================================
  125. void AudioProxyComponent::SetSwitchState(const Audio::TAudioControlID switchID, const Audio::TAudioSwitchStateID stateID)
  126. {
  127. m_audioProxy->SetSwitchState(switchID, stateID);
  128. }
  129. //=========================================================================
  130. void AudioProxyComponent::SetEnvironmentAmount(const Audio::TAudioEnvironmentID environmentID, float amount)
  131. {
  132. m_audioProxy->SetEnvironmentAmount(environmentID, amount);
  133. }
  134. //=========================================================================
  135. void AudioProxyComponent::SetObstructionCalcType(const Audio::ObstructionType type)
  136. {
  137. m_audioProxy->SetObstructionCalcType(type);
  138. }
  139. //=========================================================================
  140. void AudioProxyComponent::SetPosition(const Audio::SATLWorldPosition& position)
  141. {
  142. m_audioProxy->SetPosition(position);
  143. }
  144. //=========================================================================
  145. void AudioProxyComponent::SetMultiplePositions(const Audio::MultiPositionParams& params)
  146. {
  147. m_audioProxy->SetMultiplePositions(params);
  148. }
  149. } // namespace LmbrCentral