AudioSystemGemSystemComponent.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "AudioSystemGemSystemComponent.h"
  9. #include <AzCore/Console/IConsole.h>
  10. #include <AzCore/Console/ILogger.h>
  11. #include <AzCore/Interface/Interface.h>
  12. #include <AzCore/Module/Environment.h>
  13. #include <AzCore/Serialization/SerializeContext.h>
  14. #include <AzCore/Serialization/EditContext.h>
  15. #include <AzCore/Serialization/EditContextConstants.inl>
  16. #include <AudioAllocators.h>
  17. #include <AudioSystem.h>
  18. #include <NullAudioSystem.h>
  19. #if defined(AUDIO_SYSTEM_EDITOR)
  20. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  21. #include <AudioControlsEditorPlugin.h>
  22. struct IEditor;
  23. #include <Atom/RPI.Public/View.h>
  24. #include <Atom/RPI.Public/ViewportContext.h>
  25. #endif // AUDIO_SYSTEM_EDITOR
  26. namespace Audio
  27. {
  28. namespace Platform
  29. {
  30. void InitializeAudioAllocators();
  31. void ShutdownAudioAllocators();
  32. }
  33. } // namespace Audio
  34. namespace AudioSystemGem
  35. {
  36. #if defined(AUDIO_SYSTEM_EDITOR)
  37. IEditor* g_editor = nullptr;
  38. #endif // AUDIO_SYSTEM_EDITOR
  39. void AudioSystemGemSystemComponent::Reflect(AZ::ReflectContext* context)
  40. {
  41. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  42. {
  43. serialize->Class<AudioSystemGemSystemComponent, AZ::Component>()
  44. ->Version(0)
  45. ;
  46. if (AZ::EditContext* ec = serialize->GetEditContext())
  47. {
  48. ec->Class<AudioSystemGemSystemComponent>("Audio System Gem", "Audio System handles requests and managages data related to the audio sub-system")
  49. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  50. ->Attribute(AZ::Edit::Attributes::AutoExpand, true)
  51. ;
  52. }
  53. }
  54. }
  55. void AudioSystemGemSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  56. {
  57. provided.push_back(AZ_CRC("AudioSystemGemService"));
  58. }
  59. void AudioSystemGemSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  60. {
  61. incompatible.push_back(AZ_CRC("AudioSystemGemService"));
  62. }
  63. void AudioSystemGemSystemComponent::GetRequiredServices(AZ::ComponentDescriptor::DependencyArrayType& required)
  64. {
  65. AZ_UNUSED(required);
  66. }
  67. void AudioSystemGemSystemComponent::GetDependentServices(AZ::ComponentDescriptor::DependencyArrayType& dependent)
  68. {
  69. AZ_UNUSED(dependent);
  70. }
  71. AudioSystemGemSystemComponent::AudioSystemGemSystemComponent()
  72. {
  73. Audio::Platform::InitializeAudioAllocators();
  74. CreateAudioSystem();
  75. }
  76. AudioSystemGemSystemComponent::~AudioSystemGemSystemComponent()
  77. {
  78. // The audio system uses the Audio::AudioSystemAllocator
  79. // so it needs to be deleted before the allocator is shutdown
  80. m_audioSystem.reset();
  81. Audio::Platform::ShutdownAudioAllocators();
  82. }
  83. void AudioSystemGemSystemComponent::Init()
  84. {
  85. #if defined(AUDIO_SYSTEM_EDITOR)
  86. m_cameraTransformHandler = AZ::RPI::MatrixChangedEvent::Handler(
  87. []([[maybe_unused]] const AZ::Matrix4x4& matrix)
  88. {
  89. auto atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  90. auto viewportContext = atomViewportRequests->GetDefaultViewportContext();
  91. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get();
  92. audioSystem != nullptr && viewportContext != nullptr)
  93. {
  94. Audio::ListenerRequest::SetWorldTransform setWorldTM;
  95. setWorldTM.m_transform = viewportContext->GetCameraTransform();
  96. audioSystem->PushRequest(AZStd::move(setWorldTM));
  97. }
  98. });
  99. #endif // AUDIO_SYSTEM_EDITOR
  100. }
  101. void AudioSystemGemSystemComponent::Activate()
  102. {
  103. Audio::Gem::SystemRequestBus::Handler::BusConnect();
  104. AzFramework::ApplicationLifecycleEvents::Bus::Handler::BusConnect();
  105. AzFramework::LevelSystemLifecycleNotificationBus::Handler::BusConnect();
  106. #if defined(AUDIO_SYSTEM_EDITOR)
  107. AzToolsFramework::EditorEvents::Bus::Handler::BusConnect();
  108. auto atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  109. auto defaultContextName = atomViewportRequests->GetDefaultViewportContextName();
  110. AZ::RPI::ViewportContextNotificationBus::Handler::BusConnect(defaultContextName);
  111. #endif // AUDIO_SYSTEM_EDITOR
  112. }
  113. void AudioSystemGemSystemComponent::Deactivate()
  114. {
  115. Audio::Gem::SystemRequestBus::Handler::BusDisconnect();
  116. AzFramework::ApplicationLifecycleEvents::Bus::Handler::BusDisconnect();
  117. AzFramework::LevelSystemLifecycleNotificationBus::Handler::BusDisconnect();
  118. #if defined(AUDIO_SYSTEM_EDITOR)
  119. AzToolsFramework::EditorEvents::Bus::Handler::BusDisconnect();
  120. AZ::RPI::ViewportContextNotificationBus::Handler::BusDisconnect();
  121. m_cameraTransformHandler.Disconnect();
  122. #endif // AUDIO_SYSTEM_EDITOR
  123. }
  124. bool AudioSystemGemSystemComponent::Initialize()
  125. {
  126. using namespace Audio;
  127. bool success = AZ::Interface<Audio::IAudioSystem>::Get()->Initialize();
  128. if (success)
  129. {
  130. // Initialize the implementation module...
  131. bool initImplSuccess = false;
  132. Gem::EngineRequestBus::BroadcastResult(initImplSuccess, &Gem::EngineRequestBus::Events::Initialize);
  133. if (initImplSuccess)
  134. {
  135. PrepareAudioSystem();
  136. }
  137. else
  138. {
  139. if (Gem::EngineRequestBus::HasHandlers())
  140. {
  141. AZLOG_ERROR("%s", "The Audio Engine did not initialize correctly!");
  142. }
  143. else
  144. {
  145. AZLOG_NOTICE("%s", "Running without any Audio Engine!");
  146. }
  147. }
  148. success = true;
  149. }
  150. return success;
  151. }
  152. void AudioSystemGemSystemComponent::Release()
  153. {
  154. AZ_Assert(AZ::Interface<Audio::IAudioSystem>::Get() != nullptr, "The IAudioSystem interface has already been unregistered!");
  155. AZ::Interface<Audio::IAudioSystem>::Get()->Release();
  156. Audio::Gem::EngineRequestBus::Broadcast(&Audio::Gem::EngineRequestBus::Events::Release);
  157. }
  158. void AudioSystemGemSystemComponent::OnLoadingStart([[maybe_unused]] const char* levelName)
  159. {
  160. AZ::AllocatorInstance<Audio::AudioSystemAllocator>::Get().GarbageCollect();
  161. }
  162. void AudioSystemGemSystemComponent::OnUnloadComplete([[maybe_unused]] const char* levelName)
  163. {
  164. AZ::AllocatorInstance<Audio::AudioSystemAllocator>::Get().GarbageCollect();
  165. }
  166. void AudioSystemGemSystemComponent::OnApplicationConstrained(Event)
  167. {
  168. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  169. {
  170. Audio::SystemRequest::LoseFocus loseFocus;
  171. audioSystem->PushRequest(AZStd::move(loseFocus));
  172. }
  173. }
  174. void AudioSystemGemSystemComponent::OnApplicationUnconstrained(Event)
  175. {
  176. if (auto audioSystem = AZ::Interface<Audio::IAudioSystem>::Get(); audioSystem != nullptr)
  177. {
  178. Audio::SystemRequest::GetFocus getFocus;
  179. audioSystem->PushRequest(AZStd::move(getFocus));
  180. }
  181. }
  182. #if defined(AUDIO_SYSTEM_EDITOR)
  183. void AudioSystemGemSystemComponent::NotifyIEditorAvailable(IEditor* editor)
  184. {
  185. g_editor = editor;
  186. }
  187. void AudioSystemGemSystemComponent::NotifyRegisterViews()
  188. {
  189. if (g_editor)
  190. {
  191. if (m_editorPlugin)
  192. {
  193. m_editorPlugin->Release();
  194. }
  195. m_editorPlugin.reset(new CAudioControlsEditorPlugin(g_editor));
  196. }
  197. }
  198. void AudioSystemGemSystemComponent::OnViewportDefaultViewChanged([[maybe_unused]] AZ::RPI::ViewPtr view)
  199. {
  200. auto atomViewportRequests = AZ::Interface<AZ::RPI::ViewportContextRequestsInterface>::Get();
  201. auto viewportContext = atomViewportRequests->GetDefaultViewportContext();
  202. if (viewportContext != nullptr)
  203. {
  204. m_cameraTransformHandler.Disconnect();
  205. viewportContext->ConnectViewMatrixChangedHandler(m_cameraTransformHandler);
  206. }
  207. }
  208. #endif // AUDIO_SYSTEM_EDITOR
  209. void AudioSystemGemSystemComponent::CreateAudioSystem()
  210. {
  211. int audioIsDisabled = 0;
  212. if (auto console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
  213. {
  214. [[maybe_unused]] auto result = console->GetCvarValue("sys_audio_disable", audioIsDisabled);
  215. AZ_Warning("AudioSystem", result == AZ::GetValueResult::Success,
  216. "Failed to get the 's_AudioDisable' Cvar, result is %d\n", static_cast<int>(result));
  217. }
  218. if (audioIsDisabled)
  219. {
  220. m_audioSystem = AZStd::make_unique<Audio::NullAudioSystem>();
  221. AZLOG_INFO("%s", "Null AudioSystem created!");
  222. }
  223. else
  224. {
  225. m_audioSystem = AZStd::make_unique<Audio::CAudioSystem>();
  226. AZLOG_INFO("%s", "AudioSystem created!");
  227. }
  228. }
  229. void AudioSystemGemSystemComponent::PrepareAudioSystem()
  230. {
  231. using namespace Audio;
  232. if (auto audioSystem = AZ::Interface<IAudioSystem>::Get(); audioSystem != nullptr)
  233. {
  234. // This is called when a new audio implementation has been set,
  235. // so update the controls path before we start loading data...
  236. audioSystem->UpdateControlsPath();
  237. const char* controlsPath = audioSystem->GetControlsPath();
  238. Audio::SystemRequest::LoadControls loadControls;
  239. loadControls.m_controlsPath = (controlsPath ? controlsPath : "");
  240. loadControls.m_scope = eADS_GLOBAL;
  241. audioSystem->PushRequestBlocking(AZStd::move(loadControls));
  242. Audio::SystemRequest::LoadBank loadBank;
  243. loadBank.m_asyncLoad = false;
  244. loadBank.m_preloadRequestId = ATLInternalControlIDs::GlobalPreloadRequestID;
  245. audioSystem->PushRequestBlocking(AZStd::move(loadBank));
  246. }
  247. }
  248. } // namespace AudioSystemGem