AudioSystemComponent.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <Audio/AudioSystemComponent.h>
  9. #include <AzCore/RTTI/BehaviorContext.h>
  10. #include <AzCore/Serialization/SerializeContext.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <LmbrCentral/Audio/AudioTriggerComponentBus.h>
  13. #include <AzCore/IO/Path/Path.h>
  14. #include <IAudioSystem.h>
  15. using namespace Audio;
  16. namespace LmbrCentral
  17. {
  18. ////////////////////////////////////////////////////////////////////////
  19. class BehaviorAudioSystemComponentNotificationBusHandler
  20. : public AudioSystemComponentNotificationBus::Handler
  21. , public AZ::BehaviorEBusHandler
  22. {
  23. public:
  24. AZ_EBUS_BEHAVIOR_BINDER(BehaviorAudioSystemComponentNotificationBusHandler, "{2644951B-AB87-4D4D-BBB6-310E0ED2A3C9}", AZ::SystemAllocator,
  25. OnGamePaused,
  26. OnGameUnpaused
  27. );
  28. void OnGamePaused() override
  29. {
  30. Call(FN_OnGamePaused);
  31. }
  32. void OnGameUnpaused() override
  33. {
  34. Call(FN_OnGameUnpaused);
  35. }
  36. };
  37. ////////////////////////////////////////////////////////////////////////
  38. // static
  39. void AudioSystemComponent::Reflect(AZ::ReflectContext* context)
  40. {
  41. if (auto serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  42. {
  43. serializeContext->Class<AudioSystemComponent, AZ::Component>()
  44. ->Version(1)
  45. ;
  46. if (auto editContext = serializeContext->GetEditContext())
  47. {
  48. editContext->Class<AudioSystemComponent>("Audio System", "Provides access to audio system features without the need for an Entity")
  49. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  50. ->Attribute(AZ::Edit::Attributes::Category, "Audio")
  51. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  52. ;
  53. }
  54. }
  55. if (auto behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context))
  56. {
  57. behaviorContext->EBus<AudioSystemComponentRequestBus>("AudioSystemComponentRequestBus")
  58. ->Event("GlobalStopAllSounds", &AudioSystemComponentRequestBus::Events::GlobalStopAllSounds)
  59. ->Event("GlobalMuteAudio", &AudioSystemComponentRequestBus::Events::GlobalMuteAudio)
  60. ->Event("GlobalUnmuteAudio", &AudioSystemComponentRequestBus::Events::GlobalUnmuteAudio)
  61. ->Event("GlobalRefreshAudio", &AudioSystemComponentRequestBus::Events::GlobalRefreshAudio)
  62. ->Event("GlobalExecuteAudioTrigger", &AudioSystemComponentRequestBus::Events::GlobalExecuteAudioTrigger)
  63. ->Event("GlobalKillAudioTrigger", &AudioSystemComponentRequestBus::Events::GlobalKillAudioTrigger)
  64. ->Event("GlobalSetAudioRtpc", &AudioSystemComponentRequestBus::Events::GlobalSetAudioRtpc)
  65. ->Event("GlobalResetAudioRtpcs", &AudioSystemComponentRequestBus::Events::GlobalResetAudioRtpcs)
  66. ->Event("GlobalSetAudioSwitchState", &AudioSystemComponentRequestBus::Events::GlobalSetAudioSwitchState)
  67. ->Event("LevelLoadAudio", &AudioSystemComponentRequestBus::Events::LevelLoadAudio)
  68. ->Event("LevelUnloadAudio", &AudioSystemComponentRequestBus::Events::LevelUnloadAudio)
  69. ;
  70. behaviorContext->EBus<AudioSystemComponentNotificationBus>("Audio System Component Notifications", "AudioSystemComponentNotificationBus")
  71. ->Handler<BehaviorAudioSystemComponentNotificationBusHandler>()
  72. ;
  73. }
  74. }
  75. ////////////////////////////////////////////////////////////////////////
  76. // static
  77. void AudioSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  78. {
  79. provided.push_back(AZ_CRC_CE("AudioSystemService"));
  80. }
  81. ////////////////////////////////////////////////////////////////////////
  82. // static
  83. void AudioSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  84. {
  85. incompatible.push_back(AZ_CRC_CE("AudioSystemService"));
  86. }
  87. ////////////////////////////////////////////////////////////////////////
  88. void AudioSystemComponent::Init()
  89. {
  90. }
  91. ////////////////////////////////////////////////////////////////////////
  92. void AudioSystemComponent::Activate()
  93. {
  94. AzFramework::LevelSystemLifecycleNotificationBus::Handler::BusConnect();
  95. if (IsAudioSystemInitialized())
  96. {
  97. AudioSystemComponentRequestBus::Handler::BusConnect();
  98. }
  99. }
  100. ////////////////////////////////////////////////////////////////////////
  101. void AudioSystemComponent::Deactivate()
  102. {
  103. AudioSystemComponentRequestBus::Handler::BusDisconnect();
  104. AzFramework::LevelSystemLifecycleNotificationBus::Handler::BusDisconnect();
  105. }
  106. ////////////////////////////////////////////////////////////////////////
  107. bool AudioSystemComponent::IsAudioSystemInitialized()
  108. {
  109. return (AZ::Interface<Audio::IAudioSystem>::Get() != nullptr);
  110. }
  111. ////////////////////////////////////////////////////////////////////////
  112. void AudioSystemComponent::GlobalStopAllSounds()
  113. {
  114. Audio::SystemRequest::StopAllAudio stopAll;
  115. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(stopAll));
  116. }
  117. ////////////////////////////////////////////////////////////////////////
  118. void AudioSystemComponent::GlobalMuteAudio()
  119. {
  120. Audio::SystemRequest::MuteAll muteAll;
  121. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(muteAll));
  122. }
  123. ////////////////////////////////////////////////////////////////////////
  124. void AudioSystemComponent::GlobalUnmuteAudio()
  125. {
  126. Audio::SystemRequest::UnmuteAll unmuteAll;
  127. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(unmuteAll));
  128. }
  129. ////////////////////////////////////////////////////////////////////////
  130. void AudioSystemComponent::GlobalRefreshAudio(AZStd::string_view levelName)
  131. {
  132. AZ::Interface<Audio::IAudioSystem>::Get()->RefreshAudioSystem(levelName.data());
  133. }
  134. ////////////////////////////////////////////////////////////////////////
  135. void AudioSystemComponent::GlobalExecuteAudioTrigger(const char* triggerName, AZ::EntityId callbackOwnerEntityId)
  136. {
  137. if (triggerName && triggerName[0] != '\0')
  138. {
  139. TAudioControlID triggerId = AZ::Interface<Audio::IAudioSystem>::Get()->GetAudioTriggerID(triggerName);
  140. if (triggerId != INVALID_AUDIO_CONTROL_ID)
  141. {
  142. Audio::ObjectRequest::ExecuteTrigger execTrigger;
  143. execTrigger.m_triggerId = triggerId;
  144. execTrigger.m_owner = (callbackOwnerEntityId.IsValid()
  145. ? reinterpret_cast<void*>(static_cast<uintptr_t>(static_cast<AZ::u64>(callbackOwnerEntityId)))
  146. : this);
  147. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(execTrigger));
  148. }
  149. }
  150. }
  151. ////////////////////////////////////////////////////////////////////////
  152. void AudioSystemComponent::GlobalKillAudioTrigger(const char* triggerName, AZ::EntityId callbackOwnerEntityId)
  153. {
  154. if (triggerName && triggerName[0] != '\0')
  155. {
  156. TAudioControlID triggerId = AZ::Interface<Audio::IAudioSystem>::Get()->GetAudioTriggerID(triggerName);
  157. if (triggerId != INVALID_AUDIO_CONTROL_ID)
  158. {
  159. Audio::ObjectRequest::StopTrigger stopTrigger;
  160. stopTrigger.m_triggerId = triggerId;
  161. stopTrigger.m_owner = (callbackOwnerEntityId.IsValid()
  162. ? reinterpret_cast<void*>(static_cast<uintptr_t>(static_cast<AZ::u64>(callbackOwnerEntityId)))
  163. : this);
  164. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(stopTrigger));
  165. }
  166. }
  167. }
  168. ////////////////////////////////////////////////////////////////////////
  169. void AudioSystemComponent::GlobalSetAudioRtpc(const char* rtpcName, float value)
  170. {
  171. if (rtpcName && rtpcName[0] != '\0')
  172. {
  173. TAudioControlID rtpcId = AZ::Interface<Audio::IAudioSystem>::Get()->GetAudioRtpcID(rtpcName);
  174. if (rtpcId != INVALID_AUDIO_CONTROL_ID)
  175. {
  176. Audio::ObjectRequest::SetParameterValue setParameter;
  177. setParameter.m_parameterId = rtpcId;
  178. setParameter.m_value = value;
  179. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(setParameter));
  180. }
  181. }
  182. }
  183. ////////////////////////////////////////////////////////////////////////
  184. void AudioSystemComponent::GlobalResetAudioRtpcs()
  185. {
  186. Audio::ObjectRequest::ResetParameters resetParameters;
  187. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(resetParameters));
  188. }
  189. ////////////////////////////////////////////////////////////////////////
  190. void AudioSystemComponent::GlobalSetAudioSwitchState(const char* switchName, const char* stateName)
  191. {
  192. if (switchName && switchName[0] != '\0' && stateName && stateName[0] != '\0')
  193. {
  194. TAudioControlID switchId = AZ::Interface<Audio::IAudioSystem>::Get()->GetAudioSwitchID(switchName);
  195. TAudioSwitchStateID stateId = INVALID_AUDIO_SWITCH_STATE_ID;
  196. if (switchId != INVALID_AUDIO_CONTROL_ID)
  197. {
  198. AZ::Interface<Audio::IAudioSystem>::Get()->GetAudioSwitchStateID(switchId, stateName);
  199. }
  200. if (stateId != INVALID_AUDIO_SWITCH_STATE_ID)
  201. {
  202. Audio::ObjectRequest::SetSwitchValue setSwitch;
  203. setSwitch.m_switchId = switchId;
  204. setSwitch.m_stateId = stateId;
  205. AZ::Interface<Audio::IAudioSystem>::Get()->PushRequest(AZStd::move(setSwitch));
  206. }
  207. }
  208. }
  209. ////////////////////////////////////////////////////////////////////////
  210. void AudioSystemComponent::LevelLoadAudio(AZStd::string_view levelName)
  211. {
  212. auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
  213. if (!audioSystem || levelName.empty())
  214. {
  215. return;
  216. }
  217. AZ::IO::FixedMaxPath levelControlsPath{ audioSystem->GetControlsPath() };
  218. levelControlsPath /= "levels";
  219. levelControlsPath /= levelName;
  220. Audio::SystemRequest::LoadControls loadControls;
  221. loadControls.m_controlsPath = levelControlsPath.c_str();
  222. loadControls.m_scope = eADS_LEVEL_SPECIFIC;
  223. audioSystem->PushRequestBlocking(AZStd::move(loadControls));
  224. TAudioPreloadRequestID preloadRequestId = INVALID_AUDIO_PRELOAD_REQUEST_ID;
  225. audioSystem->GetAudioPreloadRequestID(levelName.data());
  226. if (preloadRequestId != INVALID_AUDIO_PRELOAD_REQUEST_ID)
  227. {
  228. Audio::SystemRequest::LoadBank loadBank;
  229. loadBank.m_preloadRequestId = preloadRequestId;
  230. loadBank.m_asyncLoad = false;
  231. audioSystem->PushRequestBlocking(AZStd::move(loadBank));
  232. }
  233. }
  234. ////////////////////////////////////////////////////////////////////////
  235. void AudioSystemComponent::LevelUnloadAudio()
  236. {
  237. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
  238. audioSystem != nullptr)
  239. {
  240. // Unload level-specific banks...
  241. Audio::SystemRequest::UnloadBanksByScope unloadBanks;
  242. unloadBanks.m_scope = eADS_LEVEL_SPECIFIC;
  243. audioSystem->PushRequestBlocking(AZStd::move(unloadBanks));
  244. // Now unload level-specific audio config data (controls then preloads)...
  245. Audio::SystemRequest::UnloadControls unloadControls;
  246. unloadControls.m_scope = eADS_LEVEL_SPECIFIC;
  247. // same flags as above
  248. audioSystem->PushRequestBlocking(AZStd::move(unloadControls));
  249. }
  250. }
  251. ////////////////////////////////////////////////////////////////////////
  252. void AudioSystemComponent::OnLoadingStart(const char* levelName)
  253. {
  254. if (levelName && levelName[0] != '\0')
  255. {
  256. LevelLoadAudio(AZStd::string_view{ levelName });
  257. }
  258. }
  259. ////////////////////////////////////////////////////////////////////////
  260. void AudioSystemComponent::OnUnloadComplete([[maybe_unused]] const char* levelName)
  261. {
  262. LevelUnloadAudio();
  263. }
  264. } // namespace LmbrCentral