AudioRtpcComponent.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "AudioRtpcComponent.h"
  9. #include <ISystem.h>
  10. #include <LmbrCentral/Audio/AudioProxyComponentBus.h>
  11. #include <AzCore/Component/Entity.h>
  12. #include <AzCore/Debug/Trace.h>
  13. #include <AzCore/RTTI/BehaviorContext.h>
  14. #include <AzCore/Serialization/SerializeContext.h>
  15. namespace LmbrCentral
  16. {
  17. void AudioRtpcComponent::Reflect(AZ::ReflectContext* context)
  18. {
  19. auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context);
  20. if (serializeContext)
  21. {
  22. serializeContext->Class<AudioRtpcComponent, AZ::Component>()
  23. ->Version(1)
  24. ->Field("Rtpc Name", &AudioRtpcComponent::m_defaultRtpcName)
  25. ;
  26. }
  27. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  28. {
  29. behaviorContext->EBus<AudioRtpcComponentRequestBus>("AudioRtpcComponentRequestBus")
  30. ->Event("SetValue", &AudioRtpcComponentRequestBus::Events::SetValue)
  31. ->Event("SetRtpcValue", &AudioRtpcComponentRequestBus::Events::SetRtpcValue)
  32. ;
  33. }
  34. }
  35. //=========================================================================
  36. void AudioRtpcComponent::Activate()
  37. {
  38. OnRtpcNameChanged();
  39. AudioRtpcComponentRequestBus::Handler::BusConnect(GetEntityId());
  40. }
  41. //=========================================================================
  42. void AudioRtpcComponent::Deactivate()
  43. {
  44. AudioRtpcComponentRequestBus::Handler::BusDisconnect(GetEntityId());
  45. }
  46. //=========================================================================
  47. void AudioRtpcComponent::SetValue(float value)
  48. {
  49. if (m_defaultRtpcID != INVALID_AUDIO_CONTROL_ID)
  50. {
  51. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetRtpcValue, m_defaultRtpcID, value);
  52. }
  53. }
  54. //=========================================================================
  55. AudioRtpcComponent::AudioRtpcComponent(const AZStd::string& rtpcName)
  56. : m_defaultRtpcName(rtpcName)
  57. {
  58. }
  59. //=========================================================================
  60. void AudioRtpcComponent::SetRtpcValue(const char* rtpcName, float value)
  61. {
  62. if (rtpcName && rtpcName[0] != '\0')
  63. {
  64. Audio::TAudioControlID rtpcID = INVALID_AUDIO_CONTROL_ID;
  65. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  66. {
  67. rtpcID = audioSystem->GetAudioRtpcID(rtpcName);
  68. }
  69. if (rtpcID != INVALID_AUDIO_CONTROL_ID)
  70. {
  71. AudioProxyComponentRequestBus::Event(GetEntityId(), &AudioProxyComponentRequestBus::Events::SetRtpcValue, rtpcID, value);
  72. }
  73. }
  74. }
  75. //=========================================================================
  76. void AudioRtpcComponent::OnRtpcNameChanged()
  77. {
  78. m_defaultRtpcID = INVALID_AUDIO_CONTROL_ID;
  79. if (!m_defaultRtpcName.empty())
  80. {
  81. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  82. {
  83. m_defaultRtpcID = audioSystem->GetAudioRtpcID(m_defaultRtpcName.c_str());
  84. }
  85. }
  86. }
  87. } // namespace LmbrCentral