ATLComponents.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #pragma once
  9. #include <AzCore/EBus/EBus.h>
  10. #include <AzCore/XML/rapidxml.h>
  11. #include <AzFramework/Physics/Common/PhysicsEvents.h>
  12. #include <AzFramework/Physics/Common/PhysicsTypes.h>
  13. #include <IAudioSystem.h>
  14. #include <ATLAudioObject.h>
  15. #include <ATLEntities.h>
  16. #include <ATLUtils.h>
  17. #include <FileCacheManager.h>
  18. namespace AzFramework
  19. {
  20. class DebugDisplayRequests;
  21. }
  22. namespace Audio
  23. {
  24. ///////////////////////////////////////////////////////////////////////////////////////////////////
  25. class AudioObjectIDFactory
  26. {
  27. public:
  28. static TAudioObjectID GetNextID();
  29. static const TAudioObjectID s_invalidAudioObjectID;
  30. static const TAudioObjectID s_globalAudioObjectID;
  31. static const TAudioObjectID s_minValidAudioObjectID;
  32. static const TAudioObjectID s_maxValidAudioObjectID;
  33. };
  34. ///////////////////////////////////////////////////////////////////////////////////////////////////
  35. class CAudioEventManager
  36. {
  37. public:
  38. CAudioEventManager();
  39. ~CAudioEventManager();
  40. AZ_DISABLE_COPY_MOVE(CAudioEventManager);
  41. void Initialize();
  42. void Release();
  43. CATLEvent* GetEvent(const EATLSubsystem eSender);
  44. CATLEvent* LookupID(const TAudioEventID nID) const;
  45. void ReleaseEvent(CATLEvent* const pEvent);
  46. size_t GetNumActive() const;
  47. private:
  48. CATLEvent* GetImplInstance();
  49. void ReleaseImplInstance(CATLEvent* const pOldEvent);
  50. CATLEvent* GetInternalInstance();
  51. void ReleaseInternalInstance(CATLEvent* const pOldEvent);
  52. using TActiveEventMap = ATLMapLookupType<TAudioEventID, CATLEvent*>;
  53. TActiveEventMap m_cActiveAudioEvents;
  54. CInstanceManager<CATLEvent, TAudioEventID> m_oAudioEventPool;
  55. #if !defined(AUDIO_RELEASE)
  56. public:
  57. void SetDebugNameStore(const CATLDebugNameStore* const pDebugNameStore);
  58. void DrawDebugInfo(AzFramework::DebugDisplayRequests& debugDisplay, float posX, float posY) const;
  59. private:
  60. const CATLDebugNameStore* m_pDebugNameStore;
  61. #endif // !AUDIO_RELEASE
  62. };
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////
  64. class AudioRaycastManager
  65. : public AudioRaycastRequestBus::Handler
  66. {
  67. public:
  68. AudioRaycastManager();
  69. ~AudioRaycastManager() override;
  70. // AudioRaycastRequestBus::Handler interface
  71. void PushAudioRaycastRequest(const AudioRaycastRequest& request) override;
  72. void OnPhysicsSubtickFinished();
  73. // Additional functionality related to processing raycasts...
  74. void ProcessRaycastResults(float updateIntervalMs);
  75. using AudioRaycastRequestQueueType = AZStd::vector<AudioRaycastRequest>;
  76. using AudioRaycastResultQueueType = AZStd::vector<AudioRaycastResult>;
  77. protected:
  78. AZStd::mutex m_raycastRequestsMutex;
  79. AZStd::mutex m_raycastResultsMutex;
  80. AudioRaycastRequestQueueType m_raycastRequests;
  81. AudioRaycastResultQueueType m_raycastResults;
  82. AzPhysics::SceneEvents::OnSceneSimulationFinishHandler m_sceneFinishSimHandler;
  83. };
  84. ///////////////////////////////////////////////////////////////////////////////////////////////////
  85. class CAudioObjectManager
  86. {
  87. public:
  88. CAudioObjectManager(CAudioEventManager& refAudioEventManager);
  89. ~CAudioObjectManager();
  90. AZ_DISABLE_COPY_MOVE(CAudioObjectManager);
  91. void Initialize();
  92. void Release();
  93. void Update(const float fUpdateIntervalMS, const SATLWorldPosition& rListenerPosition);
  94. bool ReserveID(TAudioObjectID& rAudioObjectID, const char* const sAudioObjectName);
  95. bool ReleaseID(const TAudioObjectID nAudioObjectID);
  96. CATLAudioObject* LookupID(const TAudioObjectID nAudioObjectID) const;
  97. //void ReportEventStarted(const CATLEvent* atlEvent); // needed?
  98. void ReportEventFinished(const CATLEvent* atlEvent);
  99. using TActiveObjectMap = ATLMapLookupType<TAudioObjectID, CATLAudioObject*>;
  100. private:
  101. static float s_fVelocityUpdateIntervalMS;
  102. CATLAudioObject* GetInstance();
  103. bool ReleaseInstance(CATLAudioObject* const pOldObject);
  104. TActiveObjectMap m_cAudioObjects;
  105. CInstanceManager<CATLAudioObject, TAudioObjectID> m_cObjectPool;
  106. float m_fTimeSinceLastVelocityUpdateMS;
  107. AudioRaycastManager m_raycastManager;
  108. #if !defined(AUDIO_RELEASE)
  109. public:
  110. void SetDebugNameStore(CATLDebugNameStore* const pDebugNameStore);
  111. size_t GetNumAudioObjects() const;
  112. size_t GetNumActiveAudioObjects() const;
  113. const TActiveObjectMap& GetActiveAudioObjects() const
  114. {
  115. return m_cAudioObjects;
  116. }
  117. void DrawPerObjectDebugInfo(AzFramework::DebugDisplayRequests& debugDisplay, const AZ::Vector3& rListenerPos) const;
  118. void DrawDebugInfo(AzFramework::DebugDisplayRequests& debugDisplay, float posX, float posY) const;
  119. private:
  120. CATLDebugNameStore* m_pDebugNameStore;
  121. #endif // !AUDIO_RELEASE
  122. };
  123. ///////////////////////////////////////////////////////////////////////////////////////////////////
  124. class CAudioListenerManager
  125. {
  126. public:
  127. CAudioListenerManager();
  128. ~CAudioListenerManager();
  129. AZ_DISABLE_COPY_MOVE(CAudioListenerManager);
  130. void Initialize();
  131. void Release();
  132. bool ReserveID(TAudioObjectID& rAudioObjectID);
  133. bool ReleaseID(const TAudioObjectID nAudioObjectID);
  134. CATLListenerObject* LookupID(const TAudioObjectID nAudioObjectID) const;
  135. size_t GetNumActive() const;
  136. void GetDefaultListenerPosition(SATLWorldPosition& oPosition);
  137. TAudioObjectID GetDefaultListenerID() const
  138. {
  139. return m_nDefaultListenerID;
  140. }
  141. bool SetOverrideListenerID(const TAudioObjectID nAudioObjectID);
  142. TAudioObjectID GetOverrideListenerID() const
  143. {
  144. return m_listenerOverrideID;
  145. }
  146. private:
  147. using TListenerPtrContainer = AZStd::vector<CATLListenerObject*, Audio::AudioSystemStdAllocator>;
  148. using TActiveListenerMap = ATLMapLookupType<TAudioObjectID, CATLListenerObject*>;
  149. TActiveListenerMap m_cActiveListeners;
  150. TListenerPtrContainer m_cListenerPool;
  151. CATLListenerObject* m_pDefaultListenerObject;
  152. const TAudioObjectID m_nDefaultListenerID;
  153. TAudioObjectID m_listenerOverrideID;
  154. // No longer have a maximum, but we can create a number of additional listeners at startup.
  155. // TODO: Control this by a cvar
  156. const size_t m_numReservedListeners = 8;
  157. #if !defined(AUDIO_RELEASE)
  158. public:
  159. void DrawDebugInfo(AzFramework::DebugDisplayRequests& debugDisplay) const;
  160. #endif // !AUDIO_RELEASE
  161. };
  162. ///////////////////////////////////////////////////////////////////////////////////////////////////
  163. class CATLXmlProcessor
  164. {
  165. public:
  166. CATLXmlProcessor(
  167. TATLTriggerLookup& rTriggers,
  168. TATLRtpcLookup& rRtpcs,
  169. TATLSwitchLookup& rSwitches,
  170. TATLEnvironmentLookup& rEnvironments,
  171. TATLPreloadRequestLookup& rPreloadRequests,
  172. CFileCacheManager& rFileCacheMgr);
  173. void ParseControlsData(const char* const sFolderPath, const EATLDataScope eDataScope);
  174. void ClearControlsData(const EATLDataScope eDataScope);
  175. void ParsePreloadsData(const char* const sFolderPath, const EATLDataScope eDataScope);
  176. void ClearPreloadsData(const EATLDataScope eDataScope);
  177. private:
  178. void ParseAudioTriggers(const AZ::rapidxml::xml_node<char>* triggersXmlRoot, const EATLDataScope dataScope);
  179. void ParseAudioSwitches(const AZ::rapidxml::xml_node<char>* switchesXmlRoot, const EATLDataScope dataScope);
  180. void ParseAudioRtpcs(const AZ::rapidxml::xml_node<char>* rtpcsXmlRoot, const EATLDataScope dataScope);
  181. void ParseAudioPreloads(const AZ::rapidxml::xml_node<char>* preloadsXmlRoot, const EATLDataScope dataScope, const char* const folderName);
  182. void ParseAudioEnvironments(const AZ::rapidxml::xml_node<char>* environmentsXmlRoot, const EATLDataScope dataScope);
  183. CATLPreloadRequest::TFileEntryIDs LegacyParseFileEntries(const AZ::rapidxml::xml_node<char>* preloadNode, const EATLDataScope dataScope, bool autoLoad);
  184. CATLPreloadRequest::TFileEntryIDs ParseFileEntries(const AZ::rapidxml::xml_node<char>* preloadNode, const EATLDataScope dataScope, bool autoLoad);
  185. IATLTriggerImplData* NewAudioTriggerImplDataInternal(const AZ::rapidxml::xml_node<char>* triggerXmlRoot);
  186. IATLRtpcImplData* NewAudioRtpcImplDataInternal(const AZ::rapidxml::xml_node<char>* rtpcXmlRoot);
  187. IATLSwitchStateImplData* NewAudioSwitchStateImplDataInternal(const AZ::rapidxml::xml_node<char>* switchXmlRoot);
  188. IATLEnvironmentImplData* NewAudioEnvironmentImplDataInternal(const AZ::rapidxml::xml_node<char>* environmentXmlRoot);
  189. void DeleteAudioTrigger(CATLTrigger* const pOldTrigger);
  190. void DeleteAudioRtpc(CATLRtpc* const pOldRtpc);
  191. void DeleteAudioSwitch(CATLSwitch* const pOldSwitch);
  192. void DeleteAudioPreloadRequest(CATLPreloadRequest* const pOldPreloadRequest);
  193. void DeleteAudioEnvironment(CATLAudioEnvironment* const pOldEnvironment);
  194. TATLTriggerLookup& m_rTriggers;
  195. TATLRtpcLookup& m_rRtpcs;
  196. TATLSwitchLookup& m_rSwitches;
  197. TATLEnvironmentLookup& m_rEnvironments;
  198. TATLPreloadRequestLookup& m_rPreloadRequests;
  199. CFileCacheManager& m_rFileCacheMgr;
  200. TAudioTriggerImplID m_nTriggerImplIDCounter;
  201. AZStd::string m_rootPath;
  202. #if !defined(AUDIO_RELEASE)
  203. public:
  204. void SetDebugNameStore(CATLDebugNameStore* const pDebugNameStore);
  205. void SetRootPath(const char* path);
  206. private:
  207. CATLDebugNameStore* m_pDebugNameStore;
  208. #endif // !AUDIO_RELEASE
  209. };
  210. ///////////////////////////////////////////////////////////////////////////////////////////////////
  211. struct SATLSharedData
  212. {
  213. SATLWorldPosition m_oActiveListenerPosition;
  214. };
  215. } // namespace Audio