ATLEntities.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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/IO/IStreamer.h>
  10. #include <AzCore/std/containers/unordered_map.h>
  11. #include <AzCore/std/containers/unordered_set.h>
  12. #include <AzCore/std/smart_ptr/unique_ptr.h>
  13. #if !defined(AUDIO_RELEASE)
  14. #include <AzCore/std/chrono/chrono.h>
  15. #endif // !AUDIO_RELEASE
  16. #include <IAudioSystem.h>
  17. #include <ATLCommon.h>
  18. #include <ATLEntityData.h>
  19. #include <ATLUtils.h>
  20. #include <AudioAllocators.h>
  21. namespace Audio
  22. {
  23. template <typename KeyType, typename ValueType>
  24. using ATLMapLookupType = AZStd::unordered_map<KeyType, ValueType, AZStd::hash<KeyType>, AZStd::equal_to<KeyType>, AudioSystemStdAllocator>;
  25. template <typename KeyType>
  26. using ATLSetLookupType = AZStd::unordered_set<KeyType, AZStd::hash<KeyType>, AZStd::equal_to<KeyType>, AudioSystemStdAllocator>;
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. enum EATLObjectFlags : TATLEnumFlagsType
  29. {
  30. eAOF_NONE = 0,
  31. eAOF_TRACK_VELOCITY = AUDIO_BIT(0),
  32. };
  33. ///////////////////////////////////////////////////////////////////////////////////////////////////
  34. enum EATLSubsystem : TATLEnumFlagsType
  35. {
  36. eAS_NONE = 0,
  37. eAS_AUDIO_SYSTEM_IMPLEMENTATION,
  38. eAS_ATL_INTERNAL,
  39. };
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////
  41. template <typename IDType>
  42. class CATLEntity
  43. {
  44. public:
  45. explicit CATLEntity(const IDType nID, const EATLDataScope eDataScope)
  46. : m_nID(nID)
  47. , m_eDataScope(eDataScope)
  48. {}
  49. virtual ~CATLEntity() = default;
  50. virtual IDType GetID() const
  51. {
  52. return m_nID;
  53. }
  54. virtual EATLDataScope GetDataScope() const
  55. {
  56. return m_eDataScope;
  57. }
  58. protected:
  59. EATLDataScope m_eDataScope;
  60. private:
  61. const IDType m_nID;
  62. };
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////
  64. struct SATLSoundPropagationData
  65. {
  66. SATLSoundPropagationData()
  67. : fObstruction(0.0f)
  68. , fOcclusion(0.0f)
  69. {}
  70. ~SATLSoundPropagationData() {}
  71. float fObstruction;
  72. float fOcclusion;
  73. };
  74. ///////////////////////////////////////////////////////////////////////////////////////////////////
  75. class CATLListenerObject
  76. : public CATLEntity<TAudioObjectID>
  77. {
  78. public:
  79. explicit CATLListenerObject(const TAudioObjectID nID, IATLListenerData* const pImplData = nullptr)
  80. : CATLEntity<TAudioObjectID>(nID, eADS_NONE)
  81. , m_pImplData(pImplData)
  82. {}
  83. ~CATLListenerObject() override {}
  84. SATLWorldPosition oPosition;
  85. IATLListenerData* const m_pImplData;
  86. };
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////
  88. class CATLControlImpl
  89. {
  90. public:
  91. CATLControlImpl()
  92. : m_eReceiver(eAS_NONE)
  93. {}
  94. explicit CATLControlImpl(const EATLSubsystem receiver)
  95. : m_eReceiver(receiver)
  96. {}
  97. virtual ~CATLControlImpl() {}
  98. EATLSubsystem GetReceiver() const
  99. {
  100. return m_eReceiver;
  101. }
  102. protected:
  103. EATLSubsystem m_eReceiver;
  104. };
  105. ///////////////////////////////////////////////////////////////////////////////////////////////////
  106. class CATLTriggerImpl
  107. : public CATLControlImpl
  108. {
  109. public:
  110. explicit CATLTriggerImpl(
  111. const TAudioTriggerImplID nID,
  112. const TAudioControlID nTriggerID,
  113. const EATLSubsystem eReceiver,
  114. IATLTriggerImplData* const pImplData = nullptr)
  115. : CATLControlImpl(eReceiver)
  116. , m_nATLID(nID)
  117. , m_nATLTriggerID(nTriggerID)
  118. , m_pImplData(pImplData)
  119. {}
  120. ~CATLTriggerImpl() override {}
  121. const TAudioTriggerImplID m_nATLID;
  122. const TAudioControlID m_nATLTriggerID;
  123. IATLTriggerImplData* const m_pImplData;
  124. };
  125. ///////////////////////////////////////////////////////////////////////////////////////////////////
  126. class CATLTrigger
  127. : public CATLEntity<TAudioControlID>
  128. {
  129. public:
  130. using TImplPtrVec = AZStd::vector<CATLTriggerImpl*, Audio::AudioSystemStdAllocator>;
  131. CATLTrigger(const TAudioControlID nID, const EATLDataScope eDataScope, const TImplPtrVec& rImplPtrs)
  132. : CATLEntity<TAudioControlID>(nID, eDataScope)
  133. , m_cImplPtrs(rImplPtrs)
  134. {}
  135. ~CATLTrigger() override {}
  136. TImplPtrVec m_cImplPtrs;
  137. };
  138. ///////////////////////////////////////////////////////////////////////////////////////////////////
  139. class CATLRtpcImpl
  140. : public CATLControlImpl
  141. {
  142. public:
  143. CATLRtpcImpl(const EATLSubsystem eReceiver, IATLRtpcImplData* const pImplData = nullptr)
  144. : CATLControlImpl(eReceiver)
  145. , m_pImplData(pImplData)
  146. {}
  147. ~CATLRtpcImpl() override {}
  148. IATLRtpcImplData* const m_pImplData;
  149. };
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////
  151. class CATLRtpc
  152. : public CATLEntity<TAudioControlID>
  153. {
  154. public:
  155. using TImplPtrVec = AZStd::vector<CATLRtpcImpl*, Audio::AudioSystemStdAllocator>;
  156. CATLRtpc(const TAudioControlID nID, const EATLDataScope eDataScope, const TImplPtrVec& cImplPtrs)
  157. : CATLEntity<TAudioControlID>(nID, eDataScope)
  158. , m_cImplPtrs(cImplPtrs)
  159. {}
  160. ~CATLRtpc() override {}
  161. TImplPtrVec m_cImplPtrs;
  162. };
  163. ///////////////////////////////////////////////////////////////////////////////////////////////////
  164. class CATLSwitchStateImpl
  165. : public CATLControlImpl
  166. {
  167. public:
  168. explicit CATLSwitchStateImpl(const EATLSubsystem eReceiver, IATLSwitchStateImplData* const pImplData = nullptr)
  169. : CATLControlImpl(eReceiver)
  170. , m_pImplData(pImplData)
  171. {}
  172. ~CATLSwitchStateImpl() override {}
  173. IATLSwitchStateImplData* const m_pImplData;
  174. };
  175. ///////////////////////////////////////////////////////////////////////////////////////////////////
  176. class CATLSwitchState
  177. {
  178. public:
  179. using TImplPtrVec = AZStd::vector<CATLSwitchStateImpl*, Audio::AudioSystemStdAllocator>;
  180. CATLSwitchState(
  181. const TAudioControlID nSwitchID,
  182. const TAudioSwitchStateID nStateID,
  183. const TImplPtrVec& cImplPtrs)
  184. : m_nID(nStateID)
  185. , m_nSwitchID(nSwitchID)
  186. , m_cImplPtrs(cImplPtrs)
  187. {}
  188. ~CATLSwitchState() {}
  189. TAudioSwitchStateID GetID() const
  190. {
  191. return m_nID;
  192. }
  193. TAudioSwitchStateID GetParentID() const
  194. {
  195. return m_nSwitchID;
  196. }
  197. TImplPtrVec m_cImplPtrs;
  198. private:
  199. const TAudioSwitchStateID m_nID;
  200. const TAudioControlID m_nSwitchID;
  201. };
  202. ///////////////////////////////////////////////////////////////////////////////////////////////////
  203. class CATLSwitch
  204. : public CATLEntity<TAudioControlID>
  205. {
  206. public:
  207. explicit CATLSwitch(const TAudioControlID nID, const EATLDataScope eDataScope)
  208. : CATLEntity<TAudioControlID>(nID, eDataScope)
  209. {}
  210. ~CATLSwitch() override {}
  211. using TStateMap = ATLMapLookupType<TAudioSwitchStateID, CATLSwitchState*>;
  212. TStateMap cStates;
  213. };
  214. ///////////////////////////////////////////////////////////////////////////////////////////////////
  215. class CATLEnvironmentImpl
  216. : public CATLControlImpl
  217. {
  218. public:
  219. explicit CATLEnvironmentImpl(const EATLSubsystem eReceiver, IATLEnvironmentImplData* const pImplData = nullptr)
  220. : CATLControlImpl(eReceiver)
  221. , m_pImplData(pImplData)
  222. {}
  223. ~CATLEnvironmentImpl() override {}
  224. IATLEnvironmentImplData* const m_pImplData;
  225. };
  226. ///////////////////////////////////////////////////////////////////////////////////////////////////
  227. class CATLAudioEnvironment
  228. : public CATLEntity<TAudioEnvironmentID>
  229. {
  230. public:
  231. using TImplPtrVec = AZStd::vector<CATLEnvironmentImpl*, Audio::AudioSystemStdAllocator>;
  232. explicit CATLAudioEnvironment(const TAudioEnvironmentID nID, const EATLDataScope eDataScope, const TImplPtrVec& rImplPtrs)
  233. : CATLEntity<TAudioEnvironmentID>(nID, eDataScope)
  234. , m_cImplPtrs(rImplPtrs)
  235. {}
  236. ~CATLAudioEnvironment() override {}
  237. TImplPtrVec m_cImplPtrs;
  238. };
  239. ///////////////////////////////////////////////////////////////////////////////////////////////////
  240. class CATLEvent
  241. : public CATLEntity<TAudioEventID>
  242. {
  243. public:
  244. explicit CATLEvent(const TAudioEventID nID, const EATLSubsystem eSender, IATLEventData* const pImplData)
  245. : CATLEntity<TAudioEventID>(nID, eADS_NONE)
  246. , m_nObjectID(INVALID_AUDIO_OBJECT_ID)
  247. , m_nTriggerID(INVALID_AUDIO_CONTROL_ID)
  248. , m_nTriggerImplID(INVALID_AUDIO_TRIGGER_IMPL_ID)
  249. , m_nTriggerInstanceID(INVALID_AUDIO_TRIGGER_INSTANCE_ID)
  250. , m_audioEventState(eAES_NONE)
  251. , m_eSender(eSender)
  252. , m_pImplData(pImplData)
  253. {}
  254. ~CATLEvent() override = default;
  255. CATLEvent(const CATLEvent&) = delete; // copy protection!
  256. CATLEvent& operator=(const CATLEvent&) = delete; // copy protection!
  257. void SetDataScope(const EATLDataScope eDataScope)
  258. {
  259. m_eDataScope = eDataScope;
  260. }
  261. bool IsPlaying() const
  262. {
  263. return m_audioEventState == eAES_PLAYING;
  264. }
  265. void Clear()
  266. {
  267. m_eDataScope = eADS_NONE;
  268. m_nObjectID = INVALID_AUDIO_OBJECT_ID;
  269. m_nTriggerID = INVALID_AUDIO_CONTROL_ID;
  270. m_nTriggerImplID = INVALID_AUDIO_TRIGGER_IMPL_ID;
  271. m_nTriggerInstanceID = INVALID_AUDIO_TRIGGER_INSTANCE_ID;
  272. m_audioEventState = eAES_NONE;
  273. }
  274. TAudioObjectID m_nObjectID;
  275. TAudioControlID m_nTriggerID;
  276. TAudioTriggerImplID m_nTriggerImplID;
  277. TAudioTriggerInstanceID m_nTriggerInstanceID;
  278. EAudioEventState m_audioEventState;
  279. const EATLSubsystem m_eSender;
  280. IATLEventData* m_pImplData;
  281. };
  282. ///////////////////////////////////////////////////////////////////////////////////////////////////
  283. class CATLAudioFileEntry
  284. {
  285. public:
  286. explicit CATLAudioFileEntry(const char* const filePath = nullptr, IATLAudioFileEntryData* const implData = nullptr);
  287. ~CATLAudioFileEntry();
  288. AZStd::string m_filePath;
  289. AZ::IO::FileRequestPtr m_asyncStreamRequest;
  290. void* m_memoryBlock;
  291. IATLAudioFileEntryData* m_implData;
  292. size_t m_fileSize;
  293. size_t m_memoryBlockAlignment;
  294. AZ::u32 m_useCount;
  295. Flags<TATLEnumFlagsType> m_flags;
  296. EATLDataScope m_dataScope;
  297. #if !defined(AUDIO_RELEASE)
  298. AZStd::chrono::steady_clock::time_point m_timeCached;
  299. #endif // !AUDIO_RELEASE
  300. };
  301. ///////////////////////////////////////////////////////////////////////////////////////////////////
  302. class CATLPreloadRequest
  303. : public CATLEntity<TAudioPreloadRequestID>
  304. {
  305. public:
  306. using TFileEntryIDs = AZStd::vector<TAudioFileEntryID, Audio::AudioSystemStdAllocator>;
  307. explicit CATLPreloadRequest(
  308. const TAudioPreloadRequestID nID,
  309. const EATLDataScope eDataScope,
  310. const bool bAutoLoad,
  311. const TFileEntryIDs& cFileEntryIDs)
  312. : CATLEntity<TAudioPreloadRequestID>(nID, eDataScope)
  313. , m_bAutoLoad(bAutoLoad)
  314. , m_cFileEntryIDs(cFileEntryIDs)
  315. , m_allLoaded(false)
  316. {}
  317. ~CATLPreloadRequest() override {}
  318. const bool m_bAutoLoad;
  319. TFileEntryIDs m_cFileEntryIDs;
  320. bool m_allLoaded;
  321. };
  322. ///////////////////////////////////////////////////////////////////////////////////////////////////
  323. using TATLTriggerLookup = ATLMapLookupType<TAudioControlID, CATLTrigger*>;
  324. using TATLRtpcLookup = ATLMapLookupType<TAudioControlID, CATLRtpc*>;
  325. using TATLSwitchLookup = ATLMapLookupType<TAudioControlID, CATLSwitch*>;
  326. using TATLPreloadRequestLookup = ATLMapLookupType<TAudioPreloadRequestID, CATLPreloadRequest*>;
  327. using TATLEnvironmentLookup = ATLMapLookupType<TAudioEnvironmentID, CATLAudioEnvironment*>;
  328. ///////////////////////////////////////////////////////////////////////////////////////////////////
  329. struct SATLSwitchStateImplData_internal
  330. : public IATLSwitchStateImplData
  331. {
  332. SATLSwitchStateImplData_internal(const TAudioControlID nSwitchID, const TAudioSwitchStateID nSwitchStateID)
  333. : nATLInternalSwitchID(nSwitchID)
  334. , nATLInternalStateID(nSwitchStateID)
  335. {}
  336. ~SATLSwitchStateImplData_internal() override {}
  337. const TAudioControlID nATLInternalSwitchID;
  338. const TAudioSwitchStateID nATLInternalStateID;
  339. };
  340. #if !defined(AUDIO_RELEASE)
  341. ///////////////////////////////////////////////////////////////////////////////////////////////////
  342. class CATLDebugNameStore
  343. {
  344. public:
  345. CATLDebugNameStore() = default;
  346. ~CATLDebugNameStore() = default;
  347. // The Add* and Remove* functions return true if the storage changed, false otherwise
  348. bool AddAudioObject(const TAudioObjectID nObjectID, const char* const sName);
  349. bool AddAudioTrigger(const TAudioControlID nTriggerID, const char* const sName);
  350. bool AddAudioRtpc(const TAudioControlID nRtpcID, const char* const sName);
  351. bool AddAudioSwitch(const TAudioControlID nSwitchID, const char* const sName);
  352. bool AddAudioSwitchState(const TAudioControlID nSwitchID, const TAudioSwitchStateID nStateID, const char* const sName);
  353. bool AddAudioPreloadRequest(const TAudioPreloadRequestID nRequestID, const char* const sName);
  354. bool AddAudioEnvironment(const TAudioEnvironmentID nEnvironmentID, const char* const sName);
  355. bool RemoveAudioObject(const TAudioObjectID nObjectID);
  356. bool RemoveAudioTrigger(const TAudioControlID nTriggerID);
  357. bool RemoveAudioRtpc(const TAudioControlID nRtpcID);
  358. bool RemoveAudioSwitch(const TAudioControlID nSwitchID);
  359. bool RemoveAudioSwitchState(const TAudioControlID nSwitchID, const TAudioSwitchStateID nStateID);
  360. bool RemoveAudioPreloadRequest(const TAudioPreloadRequestID nRequestID);
  361. bool RemoveAudioEnvironment(const TAudioEnvironmentID nEnvironmentID);
  362. // The Lookup* functions return nullptr if the content is not found.
  363. const char* LookupAudioObjectName(const TAudioObjectID nObjectID) const;
  364. const char* LookupAudioTriggerName(const TAudioControlID nTriggerID) const;
  365. const char* LookupAudioRtpcName(const TAudioControlID nRtpcID) const;
  366. const char* LookupAudioSwitchName(const TAudioControlID nSwitchID) const;
  367. const char* LookupAudioSwitchStateName(const TAudioControlID nSwitchID, const TAudioSwitchStateID nStateID) const;
  368. const char* LookupAudioPreloadRequestName(const TAudioPreloadRequestID nRequestID) const;
  369. const char* LookupAudioEnvironmentName(const TAudioEnvironmentID nEnvironmentID) const;
  370. private:
  371. using TAudioObjectMap = ATLMapLookupType<TAudioObjectID, AZStd::string>;
  372. using TAudioControlMap = ATLMapLookupType<TAudioControlID, AZStd::string>;
  373. using TAudioSwitchStateMap = ATLMapLookupType<TAudioSwitchStateID, AZStd::string>;
  374. using TAudioSwitchMap = ATLMapLookupType<TAudioControlID, AZStd::pair<AZStd::string, TAudioSwitchStateMap>>;
  375. using TAudioPreloadRequestsMap = ATLMapLookupType<TAudioPreloadRequestID, AZStd::string>;
  376. using TAudioEnvironmentMap = ATLMapLookupType<TAudioEnvironmentID, AZStd::string>;
  377. TAudioObjectMap m_cATLObjectNames;
  378. TAudioControlMap m_cATLTriggerNames;
  379. TAudioControlMap m_cATLRtpcNames;
  380. TAudioSwitchMap m_cATLSwitchNames;
  381. TAudioPreloadRequestsMap m_cATLPreloadRequestNames;
  382. TAudioEnvironmentMap m_cATLEnvironmentNames;
  383. };
  384. #endif // !AUDIO_RELEASE
  385. } // namespace Audio