IAudioInterfacesCommonData.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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/base.h>
  10. #include <AzCore/Component/EntityId.h>
  11. #include <AzCore/Math/Matrix3x4.h>
  12. #include <AzCore/Math/Transform.h>
  13. #include <AzCore/RTTI/TypeInfo.h>
  14. #include <AzCore/std/containers/vector.h>
  15. #include <AzCore/std/string/string.h>
  16. #define AUDIO_BIT(x) (1 << (x))
  17. #define AUDIO_TRIGGER_IMPL_ID_NUM_RESERVED 100// IDs below that value are used for the CATLTriggerImpl_Internal
  18. namespace Audio
  19. {
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////
  21. typedef AZ::u64 TATLIDType;
  22. typedef AZ::u32 TATLEnumFlagsType;
  23. typedef TATLIDType TAudioObjectID;
  24. typedef TATLIDType TAudioControlID;
  25. typedef TATLIDType TAudioSwitchStateID;
  26. typedef TATLIDType TAudioEnvironmentID;
  27. typedef TATLIDType TAudioPreloadRequestID;
  28. typedef TATLIDType TAudioEventID;
  29. typedef TATLIDType TAudioFileEntryID;
  30. typedef TATLIDType TAudioTriggerImplID;
  31. typedef TATLIDType TAudioTriggerInstanceID;
  32. typedef TATLIDType TAudioProxyID;
  33. typedef TATLIDType TAudioSourceId;
  34. typedef TATLIDType TAudioFileId;
  35. typedef TATLIDType TAudioFileCollectionId;
  36. typedef TATLIDType TAudioFileLanguageId;
  37. #define INVALID_AUDIO_OBJECT_ID (static_cast<Audio::TAudioObjectID>(0))
  38. #define GLOBAL_AUDIO_OBJECT_ID (static_cast<Audio::TAudioObjectID>(1))
  39. #define INVALID_AUDIO_CONTROL_ID (static_cast<Audio::TAudioControlID>(0))
  40. #define INVALID_AUDIO_SWITCH_STATE_ID (static_cast<Audio::TAudioSwitchStateID>(0))
  41. #define INVALID_AUDIO_ENVIRONMENT_ID (static_cast<Audio::TAudioEnvironmentID>(0))
  42. #define INVALID_AUDIO_PRELOAD_REQUEST_ID (static_cast<Audio::TAudioPreloadRequestID>(0))
  43. #define INVALID_AUDIO_EVENT_ID (static_cast<Audio::TAudioEventID>(0))
  44. #define INVALID_AUDIO_FILE_ENTRY_ID (static_cast<Audio::TAudioFileEntryID>(0))
  45. #define INVALID_AUDIO_TRIGGER_IMPL_ID (static_cast<Audio::TAudioTriggerImplID>(0))
  46. #define INVALID_AUDIO_TRIGGER_INSTANCE_ID (static_cast<Audio::TAudioTriggerInstanceID>(0))
  47. #define INVALID_AUDIO_ENUM_FLAG_TYPE (static_cast<Audio::TATLEnumFlagsType>(0))
  48. #define ALL_AUDIO_REQUEST_SPECIFIC_TYPE_FLAGS (static_cast<Audio::TATLEnumFlagsType>(-1))
  49. #define INVALID_AUDIO_PROXY_ID (static_cast<Audio::TAudioProxyID>(0))
  50. #define DEFAULT_AUDIO_PROXY_ID (static_cast<Audio::TAudioProxyID>(1))
  51. #define INVALID_AUDIO_SOURCE_ID (static_cast<Audio::TAudioSourceId>(0))
  52. #define INVALID_AUDIO_FILE_ID (static_cast<Audio::TAudioFileId>(0))
  53. #define INVALID_AUDIO_FILE_COLLECTION_ID (static_cast<Audio::TAudioFileCollectionId>(0))
  54. #define INVALID_AUDIO_FILE_LANGUAGE_ID (static_cast<Audio::TAudioFileLanguageId>(0))
  55. ///////////////////////////////////////////////////////////////////////////////////////////////////
  56. // <title EAudioRequestStatus>
  57. // Summary:
  58. // An enum that lists possible statuses of an in-progress AudioRequest.
  59. // Used as a return type for many function used by the AudioSystem/ATL internally,
  60. // and also for most of the IAudioSystemImplementation calls.
  61. ///////////////////////////////////////////////////////////////////////////////////////////////////
  62. enum class EAudioRequestStatus
  63. {
  64. None,
  65. Success,
  66. Failure,
  67. PartialSuccess,
  68. Pending,
  69. FailureInvalidObjectId,
  70. FailureInvalidControlId,
  71. FailureInvalidRequest,
  72. };
  73. ///////////////////////////////////////////////////////////////////////////////////////////////////
  74. enum class EAudioRequestResult
  75. {
  76. Success,
  77. Failure,
  78. };
  79. ///////////////////////////////////////////////////////////////////////////////////////////////////
  80. //! Converts a boolean value to an EAudioRequestStatus.
  81. //! @param result The boolean value to convert.
  82. //! @return Success if result is true, Failure otherwise.
  83. inline EAudioRequestStatus BoolToARS(bool result)
  84. {
  85. return result ? EAudioRequestStatus::Success : EAudioRequestStatus::Failure;
  86. }
  87. ///////////////////////////////////////////////////////////////////////////////////////////////////
  88. struct SATLWorldPosition
  89. {
  90. SATLWorldPosition()
  91. : m_transform(AZ::Matrix3x4::CreateIdentity())
  92. {}
  93. SATLWorldPosition(const AZ::Vector3& rPos)
  94. : m_transform(AZ::Matrix3x4::CreateIdentity())
  95. {
  96. m_transform.SetTranslation(rPos);
  97. }
  98. SATLWorldPosition(const AZ::Transform& rTransform)
  99. : m_transform(AZ::Matrix3x4::CreateFromTransform(rTransform))
  100. {}
  101. SATLWorldPosition(const AZ::Matrix3x4& rTransform)
  102. : m_transform(rTransform)
  103. {
  104. }
  105. inline AZ::Vector3 GetPositionVec() const
  106. {
  107. return m_transform.GetTranslation();
  108. }
  109. inline AZ::Vector3 GetUpVec() const
  110. {
  111. return m_transform.GetBasisZ();
  112. }
  113. inline AZ::Vector3 GetForwardVec() const
  114. {
  115. return m_transform.GetBasisY();
  116. }
  117. inline AZ::Vector3 GetRightVec() const
  118. {
  119. return m_transform.GetBasisX();
  120. }
  121. inline void NormalizeForwardVec()
  122. {
  123. auto forward = GetForwardVec();
  124. if (forward.IsZero())
  125. {
  126. m_transform.SetBasisY(AZ::Vector3::CreateAxisY());
  127. }
  128. else
  129. {
  130. forward.Normalize();
  131. m_transform.SetBasisY(forward);
  132. }
  133. }
  134. inline void NormalizeUpVec()
  135. {
  136. auto up = GetUpVec();
  137. if (up.IsZero())
  138. {
  139. m_transform.SetBasisZ(AZ::Vector3::CreateAxisZ());
  140. }
  141. else
  142. {
  143. up.Normalize();
  144. m_transform.SetBasisZ(up);
  145. }
  146. }
  147. private:
  148. AZ::Matrix3x4 m_transform;
  149. };
  150. ///////////////////////////////////////////////////////////////////////////////////////////////////
  151. enum EAudioAssetType : TATLEnumFlagsType
  152. {
  153. eAAT_STREAM = 1,
  154. eAAT_SOURCE = 2,
  155. eAAT_NONE = 3,
  156. };
  157. ///////////////////////////////////////////////////////////////////////////////////////////////////
  158. enum EAudioCodecType : TATLEnumFlagsType
  159. {
  160. eACT_PCM = 1,
  161. eACT_ADPCM = 2,
  162. eACT_XMA = 3,
  163. eACT_VORBIS = 4,
  164. eACT_XWMA = 5,
  165. eACT_AAC = 6,
  166. eACT_STREAM_PCM = 7,
  167. };
  168. ///////////////////////////////////////////////////////////////////////////////////////////////////
  169. enum EAudioRequestFlags : TATLEnumFlagsType
  170. {
  171. eARF_NONE = 0,
  172. eARF_SYNC_CALLBACK = AUDIO_BIT(0), // Indicates the callback will be called on the audio
  173. // thread immediately after the request has been handled.
  174. // If it's a blocking request, this means the callback is
  175. // executed before main thread is unblocked.
  176. // Care should be taken to avoid any data races.
  177. };
  178. ///////////////////////////////////////////////////////////////////////////////////////////////////
  179. enum EAudioEventState : TATLEnumFlagsType
  180. {
  181. eAES_NONE = 0,
  182. eAES_PLAYING = 1,
  183. eAES_LOADING = 3,
  184. eAES_UNLOADING = 4,
  185. };
  186. ///////////////////////////////////////////////////////////////////////////////////////////////////
  187. enum EATLTriggerStatus : TATLEnumFlagsType
  188. {
  189. eATS_NONE = 0,
  190. eATS_PLAYING = AUDIO_BIT(0),
  191. eATS_PREPARED = AUDIO_BIT(1),
  192. eATS_LOADING = AUDIO_BIT(2),
  193. eATS_UNLOADING = AUDIO_BIT(3),
  194. eATS_STARTING = AUDIO_BIT(4),
  195. };
  196. ///////////////////////////////////////////////////////////////////////////////////////////////////
  197. enum class ObstructionType : TATLEnumFlagsType
  198. {
  199. Ignore = 0,
  200. SingleRay,
  201. MultiRay,
  202. Count,
  203. };
  204. ///////////////////////////////////////////////////////////////////////////////////////////////////
  205. enum class PanningMode
  206. {
  207. Speakers,
  208. Headphones,
  209. };
  210. ///////////////////////////////////////////////////////////////////////////////////////////////////
  211. enum class AudioInputSourceType
  212. {
  213. Unsupported, // Unsupported type
  214. //OggFile, // Audio Input from an Ogg file
  215. //OpusFile, // Audio Input from an Opus file
  216. PcmFile, // Audio Input from a raw PCM file
  217. WavFile, // Audio Input from a Wav file
  218. Microphone, // Audio Input from a Microphone
  219. Synthesis, // Audio Input that is synthesized (user-provided systhesis function)
  220. ExternalStream, // Audio Input from a stream source (video stream, network stream, etc)
  221. };
  222. ///////////////////////////////////////////////////////////////////////////////////////////////////
  223. enum class AudioInputSampleType
  224. {
  225. Unsupported, // Unsupported type
  226. Int, // Integer type, probably don't need to differentiate signed vs unsigned
  227. Float, // Floating poitn type
  228. };
  229. ///////////////////////////////////////////////////////////////////////////////////////////////////
  230. enum class MultiPositionBehaviorType
  231. {
  232. Separate, // Sound positions are treated separately as individual point sources, i.e. like torches along a wall.
  233. Blended, // Sound positions are blended together as a 'spread out' sound, i.e. like a river.
  234. };
  235. ///////////////////////////////////////////////////////////////////////////////////////////////////
  236. using MultiPositionVec = AZStd::vector<AZ::Vector3>;
  237. ///////////////////////////////////////////////////////////////////////////////////////////////////
  238. struct MultiPositionParams
  239. {
  240. MultiPositionVec m_positions;
  241. MultiPositionBehaviorType m_type;
  242. };
  243. ///////////////////////////////////////////////////////////////////////////////////////////////////
  244. struct SAudioInputConfig
  245. {
  246. SAudioInputConfig() = default;
  247. SAudioInputConfig(AudioInputSourceType sourceType, const char* filename, bool autoUnloadFile = true)
  248. : m_sourceType(sourceType)
  249. , m_sourceFilename(filename)
  250. , m_autoUnloadFile(autoUnloadFile)
  251. {}
  252. SAudioInputConfig(
  253. AudioInputSourceType sourceType,
  254. AZ::u32 sampleRate,
  255. AZ::u32 numChannels,
  256. AZ::u32 bitsPerSample,
  257. AudioInputSampleType sampleType)
  258. : m_sampleRate(sampleRate)
  259. , m_numChannels(numChannels)
  260. , m_bitsPerSample(bitsPerSample)
  261. , m_sourceType(sourceType)
  262. , m_sampleType(sampleType)
  263. {}
  264. void SetBufferSizeFromFrameCount(AZ::u32 frameCount)
  265. {
  266. m_bufferSize = (m_numChannels * frameCount * (m_bitsPerSample >> 3));
  267. }
  268. AZ::u32 GetSampleCountFromBufferSize() const
  269. {
  270. AZ_Assert(m_bitsPerSample >= 8, "Bits Per Sample is set too low!\n");
  271. return m_bufferSize / (m_bitsPerSample >> 3);
  272. }
  273. //! Source Id, this is set after the source is created with the manager
  274. TAudioSourceId m_sourceId{ INVALID_AUDIO_SOURCE_ID };
  275. //! Sample rate of the source, e.g. 44100, 48000
  276. AZ::u32 m_sampleRate{ 0 };
  277. //! Number of channels, e.g. 1 = Mono, 2 = Stereo
  278. AZ::u32 m_numChannels{ 0 };
  279. //! Number of bits per sample, e.g. 16, 32
  280. AZ::u32 m_bitsPerSample{ 0 };
  281. //! Size of the buffer in bytes
  282. AZ::u32 m_bufferSize{ 0 };
  283. //! The type of the source, e.g. File, Synthesis, Microphone
  284. AudioInputSourceType m_sourceType{ AudioInputSourceType::Unsupported };
  285. //! The sample format, e.g. Int, Float
  286. AudioInputSampleType m_sampleType{ AudioInputSampleType::Unsupported };
  287. //! The filename of the source (if any)
  288. AZStd::string m_sourceFilename{};
  289. //! For files, whether the file should unload after playback completes
  290. bool m_autoUnloadFile{ false };
  291. };
  292. ///////////////////////////////////////////////////////////////////////////////////////////////////
  293. struct AudioStreamData
  294. {
  295. AudioStreamData()
  296. {}
  297. AudioStreamData(AZ::u8* buffer, AZStd::size_t dataSize)
  298. : m_data(buffer)
  299. , m_sizeBytes(dataSize)
  300. {}
  301. AZ::u8* m_data = nullptr; // points to start of raw data
  302. union {
  303. AZStd::size_t m_sizeBytes = 0; // in bytes
  304. AZStd::size_t m_offsetBytes; // if using this structure as a read/write bookmark, use this alias
  305. };
  306. };
  307. ///////////////////////////////////////////////////////////////////////////////////////////////////
  308. struct AudioStreamMultiTrackData
  309. {
  310. AudioStreamMultiTrackData()
  311. {
  312. m_data[0] = m_data[1] = m_data[2] = m_data[3] = m_data[4] = m_data[5] = nullptr;
  313. }
  314. const void* m_data[6]; // 6 channels max
  315. union {
  316. AZStd::size_t m_sizeBytes = 0; // size in bytes of each track
  317. AZStd::size_t m_offsetBytes; // if using this structure as a read/write bookmark, use this alias
  318. };
  319. };
  320. ///////////////////////////////////////////////////////////////////////////////////////////////////
  321. struct SAudioSourceInfo
  322. {
  323. TAudioSourceId m_sourceId{ INVALID_AUDIO_SOURCE_ID };
  324. TAudioFileId m_fileId{ INVALID_AUDIO_FILE_ID };
  325. TAudioFileCollectionId m_languageId{ INVALID_AUDIO_FILE_LANGUAGE_ID };
  326. TAudioFileLanguageId m_collectionId{ INVALID_AUDIO_FILE_COLLECTION_ID };
  327. EAudioCodecType m_codecType{ eACT_STREAM_PCM };
  328. SAudioSourceInfo() = default;
  329. SAudioSourceInfo(TAudioSourceId sourceId)
  330. : m_sourceId(sourceId)
  331. {}
  332. SAudioSourceInfo(
  333. TAudioSourceId sourceId,
  334. TAudioFileId fileId,
  335. TAudioFileLanguageId languageId,
  336. TAudioFileCollectionId collectionId,
  337. EAudioCodecType codecType)
  338. : m_sourceId(sourceId)
  339. , m_fileId(fileId)
  340. , m_languageId(languageId)
  341. , m_collectionId(collectionId)
  342. , m_codecType(codecType)
  343. {}
  344. };
  345. ///////////////////////////////////////////////////////////////////////////////////////////////////
  346. struct TriggerNotificationIdType
  347. {
  348. AZ_TYPE_INFO(TriggerNotificationIdType, "{E355AC15-8C88-4BDD-8CCE-9999EC32F970}");
  349. uintptr_t m_owner{ 0 };
  350. TriggerNotificationIdType() = default;
  351. ~TriggerNotificationIdType() = default;
  352. TriggerNotificationIdType(void* owner)
  353. : m_owner(reinterpret_cast<uintptr_t>(owner))
  354. {
  355. }
  356. TriggerNotificationIdType(AZ::EntityId owner)
  357. : m_owner(static_cast<uintptr_t>(static_cast<AZ::u64>(owner)))
  358. {
  359. }
  360. inline bool operator==(const TriggerNotificationIdType& rhs) const
  361. {
  362. return (m_owner == rhs.m_owner);
  363. }
  364. inline bool operator!=(const TriggerNotificationIdType& rhs) const
  365. {
  366. return !(*this == rhs);
  367. }
  368. };
  369. } // namespace Audio
  370. namespace AZStd
  371. {
  372. template<>
  373. struct hash<Audio::TriggerNotificationIdType>
  374. {
  375. constexpr size_t operator()(const Audio::TriggerNotificationIdType& id) const
  376. {
  377. AZStd::hash<uintptr_t> hasher;
  378. return hasher(id.m_owner);
  379. }
  380. };
  381. } // namespace AZStd
  382. namespace AZ
  383. {
  384. AZ_TYPE_INFO_SPECIALIZE(Audio::MultiPositionBehaviorType, "{96851568-74F9-4EEC-9195-82DCF701EEEF}");
  385. AZ_TYPE_INFO_SPECIALIZE(Audio::ObstructionType, "{8C056768-40E2-4B2D-AF01-9F7A6817BAAA}");
  386. } // namespace AZ