source.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. #ifndef AL_SOURCE_H
  2. #define AL_SOURCE_H
  3. #include "config.h"
  4. #include <array>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <deque>
  8. #include <limits>
  9. #include <string_view>
  10. #include <utility>
  11. #include "AL/al.h"
  12. #include "AL/alc.h"
  13. #include "AL/alext.h"
  14. #include "almalloc.h"
  15. #include "alnumbers.h"
  16. #include "alnumeric.h"
  17. #include "alspan.h"
  18. #include "core/context.h"
  19. #include "core/voice.h"
  20. #if ALSOFT_EAX
  21. #include "eax/api.h"
  22. #include "eax/call.h"
  23. #include "eax/exception.h"
  24. #include "eax/fx_slot_index.h"
  25. #include "eax/utils.h"
  26. #endif // ALSOFT_EAX
  27. struct ALbuffer;
  28. struct ALeffectslot;
  29. enum class Resampler : uint8_t;
  30. enum class SourceStereo : bool {
  31. Normal = AL_NORMAL_SOFT,
  32. Enhanced = AL_SUPER_STEREO_SOFT
  33. };
  34. inline constexpr size_t DefaultSendCount{2};
  35. inline constexpr ALuint InvalidVoiceIndex{std::numeric_limits<ALuint>::max()};
  36. inline bool sBufferSubDataCompat{false};
  37. struct ALbufferQueueItem : public VoiceBufferItem {
  38. ALbuffer *mBuffer{nullptr};
  39. };
  40. #if ALSOFT_EAX
  41. class EaxSourceException : public EaxException {
  42. public:
  43. explicit EaxSourceException(const char* message)
  44. : EaxException{"EAX_SOURCE", message}
  45. {}
  46. };
  47. #endif // ALSOFT_EAX
  48. struct ALsource {
  49. /** Source properties. */
  50. float Pitch{1.0f};
  51. float Gain{1.0f};
  52. float OuterGain{0.0f};
  53. float MinGain{0.0f};
  54. float MaxGain{1.0f};
  55. float InnerAngle{360.0f};
  56. float OuterAngle{360.0f};
  57. float RefDistance{1.0f};
  58. float MaxDistance{std::numeric_limits<float>::max()};
  59. float RolloffFactor{1.0f};
  60. #if ALSOFT_EAX
  61. // For EAXSOURCE_ROLLOFFFACTOR, which is distinct from and added to
  62. // AL_ROLLOFF_FACTOR
  63. float RolloffFactor2{0.0f};
  64. #endif
  65. std::array<float,3> Position{{0.0f, 0.0f, 0.0f}};
  66. std::array<float,3> Velocity{{0.0f, 0.0f, 0.0f}};
  67. std::array<float,3> Direction{{0.0f, 0.0f, 0.0f}};
  68. std::array<float,3> OrientAt{{0.0f, 0.0f, -1.0f}};
  69. std::array<float,3> OrientUp{{0.0f, 1.0f, 0.0f}};
  70. bool HeadRelative{false};
  71. bool Looping{false};
  72. DistanceModel mDistanceModel{DistanceModel::Default};
  73. Resampler mResampler{ResamplerDefault};
  74. DirectMode DirectChannels{DirectMode::Off};
  75. SpatializeMode mSpatialize{SpatializeMode::Auto};
  76. SourceStereo mStereoMode{SourceStereo::Normal};
  77. bool mPanningEnabled{false};
  78. bool DryGainHFAuto{true};
  79. bool WetGainAuto{true};
  80. bool WetGainHFAuto{true};
  81. float OuterGainHF{1.0f};
  82. float AirAbsorptionFactor{0.0f};
  83. float RoomRolloffFactor{0.0f};
  84. float DopplerFactor{1.0f};
  85. /* NOTE: Stereo pan angles are specified in radians, counter-clockwise
  86. * rather than clockwise.
  87. */
  88. std::array<float,2> StereoPan{{al::numbers::pi_v<float>/6.0f, -al::numbers::pi_v<float>/6.0f}};
  89. float Radius{0.0f};
  90. float EnhWidth{0.593f};
  91. float mPan{0.0f};
  92. /** Direct filter and auxiliary send info. */
  93. struct DirectData {
  94. float Gain{};
  95. float GainHF{};
  96. float HFReference{};
  97. float GainLF{};
  98. float LFReference{};
  99. };
  100. DirectData Direct;
  101. struct SendData {
  102. ALeffectslot *Slot{};
  103. float Gain{};
  104. float GainHF{};
  105. float HFReference{};
  106. float GainLF{};
  107. float LFReference{};
  108. };
  109. std::array<SendData,MaxSendCount> Send;
  110. /**
  111. * Last user-specified offset, and the offset type (bytes, samples, or
  112. * seconds).
  113. */
  114. double Offset{0.0};
  115. ALenum OffsetType{AL_NONE};
  116. /** Source type (static, streaming, or undetermined) */
  117. ALenum SourceType{AL_UNDETERMINED};
  118. /** Source state (initial, playing, paused, or stopped) */
  119. ALenum state{AL_INITIAL};
  120. /** Source Buffer Queue head. */
  121. std::deque<ALbufferQueueItem> mQueue;
  122. bool mPropsDirty{true};
  123. /* Index into the context's Voices array. Lazily updated, only checked and
  124. * reset when looking up the voice.
  125. */
  126. ALuint VoiceIdx{InvalidVoiceIndex};
  127. /** Self ID */
  128. ALuint id{0};
  129. ALsource() noexcept;
  130. ~ALsource();
  131. ALsource(const ALsource&) = delete;
  132. ALsource& operator=(const ALsource&) = delete;
  133. static void SetName(ALCcontext *context, ALuint id, std::string_view name);
  134. DISABLE_ALLOC
  135. #if ALSOFT_EAX
  136. public:
  137. void eaxInitialize(ALCcontext *context) noexcept;
  138. void eaxDispatch(const EaxCall& call) { call.is_get() ? eax_get(call) : eax_set(call); }
  139. void eaxCommit();
  140. void eaxMarkAsChanged() noexcept { mEaxChanged = true; }
  141. static ALsource* EaxLookupSource(ALCcontext& al_context, ALuint source_id) noexcept;
  142. private:
  143. using Exception = EaxSourceException;
  144. static constexpr auto eax_max_speakers{9u};
  145. using EaxFxSlotIds = std::array<const GUID*,EAX_MAX_FXSLOTS>;
  146. static constexpr const EaxFxSlotIds eax4_fx_slot_ids{
  147. &EAXPROPERTYID_EAX40_FXSlot0,
  148. &EAXPROPERTYID_EAX40_FXSlot1,
  149. &EAXPROPERTYID_EAX40_FXSlot2,
  150. &EAXPROPERTYID_EAX40_FXSlot3,
  151. };
  152. static constexpr const EaxFxSlotIds eax5_fx_slot_ids{
  153. &EAXPROPERTYID_EAX50_FXSlot0,
  154. &EAXPROPERTYID_EAX50_FXSlot1,
  155. &EAXPROPERTYID_EAX50_FXSlot2,
  156. &EAXPROPERTYID_EAX50_FXSlot3,
  157. };
  158. using EaxActiveFxSlots = std::array<bool, EAX_MAX_FXSLOTS>;
  159. using EaxSpeakerLevels = std::array<EAXSPEAKERLEVELPROPERTIES, eax_max_speakers>;
  160. using EaxSends = std::array<EAXSOURCEALLSENDPROPERTIES, EAX_MAX_FXSLOTS>;
  161. struct Eax1State {
  162. EAXBUFFER_REVERBPROPERTIES i; // Immediate.
  163. EAXBUFFER_REVERBPROPERTIES d; // Deferred.
  164. };
  165. struct Eax2State {
  166. EAX20BUFFERPROPERTIES i; // Immediate.
  167. EAX20BUFFERPROPERTIES d; // Deferred.
  168. };
  169. struct Eax3State {
  170. EAX30SOURCEPROPERTIES i; // Immediate.
  171. EAX30SOURCEPROPERTIES d; // Deferred.
  172. };
  173. struct Eax4Props {
  174. EAX30SOURCEPROPERTIES source;
  175. EaxSends sends;
  176. EAX40ACTIVEFXSLOTS active_fx_slots;
  177. };
  178. struct Eax4State {
  179. Eax4Props i; // Immediate.
  180. Eax4Props d; // Deferred.
  181. };
  182. struct Eax5Props {
  183. EAX50SOURCEPROPERTIES source;
  184. EaxSends sends;
  185. EAX50ACTIVEFXSLOTS active_fx_slots;
  186. EaxSpeakerLevels speaker_levels;
  187. };
  188. struct Eax5State {
  189. Eax5Props i; // Immediate.
  190. Eax5Props d; // Deferred.
  191. };
  192. ALCcontext* mEaxAlContext{};
  193. EaxFxSlotIndex mEaxPrimaryFxSlotId{};
  194. EaxActiveFxSlots mEaxActiveFxSlots{};
  195. int mEaxVersion{};
  196. bool mEaxChanged{};
  197. Eax1State mEax1{};
  198. Eax2State mEax2{};
  199. Eax3State mEax3{};
  200. Eax4State mEax4{};
  201. Eax5State mEax5{};
  202. Eax5Props mEax{};
  203. // ----------------------------------------------------------------------
  204. // Source validators
  205. struct Eax1SourceReverbMixValidator {
  206. void operator()(float reverb_mix) const
  207. {
  208. if (reverb_mix == EAX_REVERBMIX_USEDISTANCE)
  209. return;
  210. eax_validate_range<Exception>(
  211. "Reverb Mix",
  212. reverb_mix,
  213. EAX_BUFFER_MINREVERBMIX,
  214. EAX_BUFFER_MAXREVERBMIX);
  215. }
  216. };
  217. struct Eax2SourceDirectValidator {
  218. void operator()(long lDirect) const
  219. {
  220. eax_validate_range<Exception>(
  221. "Direct",
  222. lDirect,
  223. EAXSOURCE_MINDIRECT,
  224. EAXSOURCE_MAXDIRECT);
  225. }
  226. };
  227. struct Eax2SourceDirectHfValidator {
  228. void operator()(long lDirectHF) const
  229. {
  230. eax_validate_range<Exception>(
  231. "Direct HF",
  232. lDirectHF,
  233. EAXSOURCE_MINDIRECTHF,
  234. EAXSOURCE_MAXDIRECTHF);
  235. }
  236. };
  237. struct Eax2SourceRoomValidator {
  238. void operator()(long lRoom) const
  239. {
  240. eax_validate_range<Exception>(
  241. "Room",
  242. lRoom,
  243. EAXSOURCE_MINROOM,
  244. EAXSOURCE_MAXROOM);
  245. }
  246. };
  247. struct Eax2SourceRoomHfValidator {
  248. void operator()(long lRoomHF) const
  249. {
  250. eax_validate_range<Exception>(
  251. "Room HF",
  252. lRoomHF,
  253. EAXSOURCE_MINROOMHF,
  254. EAXSOURCE_MAXROOMHF);
  255. }
  256. };
  257. struct Eax2SourceRoomRolloffFactorValidator {
  258. void operator()(float flRoomRolloffFactor) const
  259. {
  260. eax_validate_range<Exception>(
  261. "Room Rolloff Factor",
  262. flRoomRolloffFactor,
  263. EAXSOURCE_MINROOMROLLOFFFACTOR,
  264. EAXSOURCE_MAXROOMROLLOFFFACTOR);
  265. }
  266. };
  267. struct Eax2SourceObstructionValidator {
  268. void operator()(long lObstruction) const
  269. {
  270. eax_validate_range<Exception>(
  271. "Obstruction",
  272. lObstruction,
  273. EAXSOURCE_MINOBSTRUCTION,
  274. EAXSOURCE_MAXOBSTRUCTION);
  275. }
  276. };
  277. struct Eax2SourceObstructionLfRatioValidator {
  278. void operator()(float flObstructionLFRatio) const
  279. {
  280. eax_validate_range<Exception>(
  281. "Obstruction LF Ratio",
  282. flObstructionLFRatio,
  283. EAXSOURCE_MINOBSTRUCTIONLFRATIO,
  284. EAXSOURCE_MAXOBSTRUCTIONLFRATIO);
  285. }
  286. };
  287. struct Eax2SourceOcclusionValidator {
  288. void operator()(long lOcclusion) const
  289. {
  290. eax_validate_range<Exception>(
  291. "Occlusion",
  292. lOcclusion,
  293. EAXSOURCE_MINOCCLUSION,
  294. EAXSOURCE_MAXOCCLUSION);
  295. }
  296. };
  297. struct Eax2SourceOcclusionLfRatioValidator {
  298. void operator()(float flOcclusionLFRatio) const
  299. {
  300. eax_validate_range<Exception>(
  301. "Occlusion LF Ratio",
  302. flOcclusionLFRatio,
  303. EAXSOURCE_MINOCCLUSIONLFRATIO,
  304. EAXSOURCE_MAXOCCLUSIONLFRATIO);
  305. }
  306. };
  307. struct Eax2SourceOcclusionRoomRatioValidator {
  308. void operator()(float flOcclusionRoomRatio) const
  309. {
  310. eax_validate_range<Exception>(
  311. "Occlusion Room Ratio",
  312. flOcclusionRoomRatio,
  313. EAXSOURCE_MINOCCLUSIONROOMRATIO,
  314. EAXSOURCE_MAXOCCLUSIONROOMRATIO);
  315. }
  316. };
  317. struct Eax2SourceOutsideVolumeHfValidator {
  318. void operator()(long lOutsideVolumeHF) const
  319. {
  320. eax_validate_range<Exception>(
  321. "Outside Volume HF",
  322. lOutsideVolumeHF,
  323. EAXSOURCE_MINOUTSIDEVOLUMEHF,
  324. EAXSOURCE_MAXOUTSIDEVOLUMEHF);
  325. }
  326. };
  327. struct Eax2SourceAirAbsorptionFactorValidator {
  328. void operator()(float flAirAbsorptionFactor) const
  329. {
  330. eax_validate_range<Exception>(
  331. "Air Absorption Factor",
  332. flAirAbsorptionFactor,
  333. EAXSOURCE_MINAIRABSORPTIONFACTOR,
  334. EAXSOURCE_MAXAIRABSORPTIONFACTOR);
  335. }
  336. };
  337. struct Eax2SourceFlagsValidator {
  338. void operator()(unsigned long dwFlags) const
  339. {
  340. eax_validate_range<Exception>(
  341. "Flags",
  342. dwFlags,
  343. 0UL,
  344. ~EAX20SOURCEFLAGS_RESERVED);
  345. }
  346. };
  347. struct Eax3SourceOcclusionDirectRatioValidator {
  348. void operator()(float flOcclusionDirectRatio) const
  349. {
  350. eax_validate_range<Exception>(
  351. "Occlusion Direct Ratio",
  352. flOcclusionDirectRatio,
  353. EAXSOURCE_MINOCCLUSIONDIRECTRATIO,
  354. EAXSOURCE_MAXOCCLUSIONDIRECTRATIO);
  355. }
  356. };
  357. struct Eax3SourceExclusionValidator {
  358. void operator()(long lExclusion) const
  359. {
  360. eax_validate_range<Exception>(
  361. "Exclusion",
  362. lExclusion,
  363. EAXSOURCE_MINEXCLUSION,
  364. EAXSOURCE_MAXEXCLUSION);
  365. }
  366. };
  367. struct Eax3SourceExclusionLfRatioValidator {
  368. void operator()(float flExclusionLFRatio) const
  369. {
  370. eax_validate_range<Exception>(
  371. "Exclusion LF Ratio",
  372. flExclusionLFRatio,
  373. EAXSOURCE_MINEXCLUSIONLFRATIO,
  374. EAXSOURCE_MAXEXCLUSIONLFRATIO);
  375. }
  376. };
  377. struct Eax3SourceDopplerFactorValidator {
  378. void operator()(float flDopplerFactor) const
  379. {
  380. eax_validate_range<Exception>(
  381. "Doppler Factor",
  382. flDopplerFactor,
  383. EAXSOURCE_MINDOPPLERFACTOR,
  384. EAXSOURCE_MAXDOPPLERFACTOR);
  385. }
  386. };
  387. struct Eax3SourceRolloffFactorValidator {
  388. void operator()(float flRolloffFactor) const
  389. {
  390. eax_validate_range<Exception>(
  391. "Rolloff Factor",
  392. flRolloffFactor,
  393. EAXSOURCE_MINROLLOFFFACTOR,
  394. EAXSOURCE_MAXROLLOFFFACTOR);
  395. }
  396. };
  397. struct Eax5SourceMacroFXFactorValidator {
  398. void operator()(float flMacroFXFactor) const
  399. {
  400. eax_validate_range<Exception>(
  401. "Macro FX Factor",
  402. flMacroFXFactor,
  403. EAXSOURCE_MINMACROFXFACTOR,
  404. EAXSOURCE_MAXMACROFXFACTOR);
  405. }
  406. };
  407. struct Eax5SourceFlagsValidator {
  408. void operator()(unsigned long dwFlags) const
  409. {
  410. eax_validate_range<Exception>(
  411. "Flags",
  412. dwFlags,
  413. 0UL,
  414. ~EAX50SOURCEFLAGS_RESERVED);
  415. }
  416. };
  417. struct Eax1SourceAllValidator {
  418. void operator()(const EAXBUFFER_REVERBPROPERTIES& props) const
  419. {
  420. Eax1SourceReverbMixValidator{}(props.fMix);
  421. }
  422. };
  423. struct Eax2SourceAllValidator {
  424. void operator()(const EAX20BUFFERPROPERTIES& props) const
  425. {
  426. Eax2SourceDirectValidator{}(props.lDirect);
  427. Eax2SourceDirectHfValidator{}(props.lDirectHF);
  428. Eax2SourceRoomValidator{}(props.lRoom);
  429. Eax2SourceRoomHfValidator{}(props.lRoomHF);
  430. Eax2SourceRoomRolloffFactorValidator{}(props.flRoomRolloffFactor);
  431. Eax2SourceObstructionValidator{}(props.lObstruction);
  432. Eax2SourceObstructionLfRatioValidator{}(props.flObstructionLFRatio);
  433. Eax2SourceOcclusionValidator{}(props.lOcclusion);
  434. Eax2SourceOcclusionLfRatioValidator{}(props.flOcclusionLFRatio);
  435. Eax2SourceOcclusionRoomRatioValidator{}(props.flOcclusionRoomRatio);
  436. Eax2SourceOutsideVolumeHfValidator{}(props.lOutsideVolumeHF);
  437. Eax2SourceAirAbsorptionFactorValidator{}(props.flAirAbsorptionFactor);
  438. Eax2SourceFlagsValidator{}(props.dwFlags);
  439. }
  440. };
  441. struct Eax3SourceAllValidator {
  442. void operator()(const EAX30SOURCEPROPERTIES& props) const
  443. {
  444. Eax2SourceDirectValidator{}(props.lDirect);
  445. Eax2SourceDirectHfValidator{}(props.lDirectHF);
  446. Eax2SourceRoomValidator{}(props.lRoom);
  447. Eax2SourceRoomHfValidator{}(props.lRoomHF);
  448. Eax2SourceObstructionValidator{}(props.lObstruction);
  449. Eax2SourceObstructionLfRatioValidator{}(props.flObstructionLFRatio);
  450. Eax2SourceOcclusionValidator{}(props.lOcclusion);
  451. Eax2SourceOcclusionLfRatioValidator{}(props.flOcclusionLFRatio);
  452. Eax2SourceOcclusionRoomRatioValidator{}(props.flOcclusionRoomRatio);
  453. Eax3SourceOcclusionDirectRatioValidator{}(props.flOcclusionDirectRatio);
  454. Eax3SourceExclusionValidator{}(props.lExclusion);
  455. Eax3SourceExclusionLfRatioValidator{}(props.flExclusionLFRatio);
  456. Eax2SourceOutsideVolumeHfValidator{}(props.lOutsideVolumeHF);
  457. Eax3SourceDopplerFactorValidator{}(props.flDopplerFactor);
  458. Eax3SourceRolloffFactorValidator{}(props.flRolloffFactor);
  459. Eax2SourceRoomRolloffFactorValidator{}(props.flRoomRolloffFactor);
  460. Eax2SourceAirAbsorptionFactorValidator{}(props.flAirAbsorptionFactor);
  461. Eax2SourceFlagsValidator{}(props.ulFlags);
  462. }
  463. };
  464. struct Eax5SourceAllValidator {
  465. void operator()(const EAX50SOURCEPROPERTIES& props) const
  466. {
  467. Eax2SourceDirectValidator{}(props.lDirect);
  468. Eax2SourceDirectHfValidator{}(props.lDirectHF);
  469. Eax2SourceRoomValidator{}(props.lRoom);
  470. Eax2SourceRoomHfValidator{}(props.lRoomHF);
  471. Eax2SourceObstructionValidator{}(props.lObstruction);
  472. Eax2SourceObstructionLfRatioValidator{}(props.flObstructionLFRatio);
  473. Eax2SourceOcclusionValidator{}(props.lOcclusion);
  474. Eax2SourceOcclusionLfRatioValidator{}(props.flOcclusionLFRatio);
  475. Eax2SourceOcclusionRoomRatioValidator{}(props.flOcclusionRoomRatio);
  476. Eax3SourceOcclusionDirectRatioValidator{}(props.flOcclusionDirectRatio);
  477. Eax3SourceExclusionValidator{}(props.lExclusion);
  478. Eax3SourceExclusionLfRatioValidator{}(props.flExclusionLFRatio);
  479. Eax2SourceOutsideVolumeHfValidator{}(props.lOutsideVolumeHF);
  480. Eax3SourceDopplerFactorValidator{}(props.flDopplerFactor);
  481. Eax3SourceRolloffFactorValidator{}(props.flRolloffFactor);
  482. Eax2SourceRoomRolloffFactorValidator{}(props.flRoomRolloffFactor);
  483. Eax2SourceAirAbsorptionFactorValidator{}(props.flAirAbsorptionFactor);
  484. Eax5SourceFlagsValidator{}(props.ulFlags);
  485. Eax5SourceMacroFXFactorValidator{}(props.flMacroFXFactor);
  486. }
  487. };
  488. struct Eax5SourceAll2dValidator {
  489. void operator()(const EAXSOURCE2DPROPERTIES& props) const
  490. {
  491. Eax2SourceDirectValidator{}(props.lDirect);
  492. Eax2SourceDirectHfValidator{}(props.lDirectHF);
  493. Eax2SourceRoomValidator{}(props.lRoom);
  494. Eax2SourceRoomHfValidator{}(props.lRoomHF);
  495. Eax5SourceFlagsValidator{}(props.ulFlags);
  496. }
  497. };
  498. struct Eax4ObstructionValidator {
  499. void operator()(const EAXOBSTRUCTIONPROPERTIES& props) const
  500. {
  501. Eax2SourceObstructionValidator{}(props.lObstruction);
  502. Eax2SourceObstructionLfRatioValidator{}(props.flObstructionLFRatio);
  503. }
  504. };
  505. struct Eax4OcclusionValidator {
  506. void operator()(const EAXOCCLUSIONPROPERTIES& props) const
  507. {
  508. Eax2SourceOcclusionValidator{}(props.lOcclusion);
  509. Eax2SourceOcclusionLfRatioValidator{}(props.flOcclusionLFRatio);
  510. Eax2SourceOcclusionRoomRatioValidator{}(props.flOcclusionRoomRatio);
  511. Eax3SourceOcclusionDirectRatioValidator{}(props.flOcclusionDirectRatio);
  512. }
  513. };
  514. struct Eax4ExclusionValidator {
  515. void operator()(const EAXEXCLUSIONPROPERTIES& props) const
  516. {
  517. Eax3SourceExclusionValidator{}(props.lExclusion);
  518. Eax3SourceExclusionLfRatioValidator{}(props.flExclusionLFRatio);
  519. }
  520. };
  521. // Source validators
  522. // ----------------------------------------------------------------------
  523. // Send validators
  524. struct Eax4SendReceivingFxSlotIdValidator {
  525. void operator()(const GUID& guidReceivingFXSlotID) const
  526. {
  527. if (guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot0 &&
  528. guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot1 &&
  529. guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot2 &&
  530. guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot3)
  531. {
  532. eax_fail_unknown_receiving_fx_slot_id();
  533. }
  534. }
  535. };
  536. struct Eax5SendReceivingFxSlotIdValidator {
  537. void operator()(const GUID& guidReceivingFXSlotID) const
  538. {
  539. if (guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot0 &&
  540. guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot1 &&
  541. guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot2 &&
  542. guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot3)
  543. {
  544. eax_fail_unknown_receiving_fx_slot_id();
  545. }
  546. }
  547. };
  548. struct Eax4SendSendValidator {
  549. void operator()(long lSend) const
  550. {
  551. eax_validate_range<Exception>(
  552. "Send",
  553. lSend,
  554. EAXSOURCE_MINSEND,
  555. EAXSOURCE_MAXSEND);
  556. }
  557. };
  558. struct Eax4SendSendHfValidator {
  559. void operator()(long lSendHF) const
  560. {
  561. eax_validate_range<Exception>(
  562. "Send HF",
  563. lSendHF,
  564. EAXSOURCE_MINSENDHF,
  565. EAXSOURCE_MAXSENDHF);
  566. }
  567. };
  568. template<typename TIdValidator>
  569. struct EaxSendValidator {
  570. void operator()(const EAXSOURCESENDPROPERTIES& props) const
  571. {
  572. TIdValidator{}(props.guidReceivingFXSlotID);
  573. Eax4SendSendValidator{}(props.lSend);
  574. Eax4SendSendHfValidator{}(props.lSendHF);
  575. }
  576. };
  577. struct Eax4SendValidator : EaxSendValidator<Eax4SendReceivingFxSlotIdValidator> {};
  578. struct Eax5SendValidator : EaxSendValidator<Eax5SendReceivingFxSlotIdValidator> {};
  579. template<typename TIdValidator>
  580. struct EaxOcclusionSendValidator {
  581. void operator()(const EAXSOURCEOCCLUSIONSENDPROPERTIES& props) const
  582. {
  583. TIdValidator{}(props.guidReceivingFXSlotID);
  584. Eax2SourceOcclusionValidator{}(props.lOcclusion);
  585. Eax2SourceOcclusionLfRatioValidator{}(props.flOcclusionLFRatio);
  586. Eax2SourceOcclusionRoomRatioValidator{}(props.flOcclusionRoomRatio);
  587. Eax3SourceOcclusionDirectRatioValidator{}(props.flOcclusionDirectRatio);
  588. }
  589. };
  590. struct Eax4OcclusionSendValidator : EaxOcclusionSendValidator<Eax4SendReceivingFxSlotIdValidator> {};
  591. struct Eax5OcclusionSendValidator : EaxOcclusionSendValidator<Eax5SendReceivingFxSlotIdValidator> {};
  592. template<typename TIdValidator>
  593. struct EaxExclusionSendValidator {
  594. void operator()(const EAXSOURCEEXCLUSIONSENDPROPERTIES& props) const
  595. {
  596. TIdValidator{}(props.guidReceivingFXSlotID);
  597. Eax3SourceExclusionValidator{}(props.lExclusion);
  598. Eax3SourceExclusionLfRatioValidator{}(props.flExclusionLFRatio);
  599. }
  600. };
  601. struct Eax4ExclusionSendValidator : EaxExclusionSendValidator<Eax4SendReceivingFxSlotIdValidator> {};
  602. struct Eax5ExclusionSendValidator : EaxExclusionSendValidator<Eax5SendReceivingFxSlotIdValidator> {};
  603. template<typename TIdValidator>
  604. struct EaxAllSendValidator {
  605. void operator()(const EAXSOURCEALLSENDPROPERTIES& props) const
  606. {
  607. TIdValidator{}(props.guidReceivingFXSlotID);
  608. Eax4SendSendValidator{}(props.lSend);
  609. Eax4SendSendHfValidator{}(props.lSendHF);
  610. Eax2SourceOcclusionValidator{}(props.lOcclusion);
  611. Eax2SourceOcclusionLfRatioValidator{}(props.flOcclusionLFRatio);
  612. Eax2SourceOcclusionRoomRatioValidator{}(props.flOcclusionRoomRatio);
  613. Eax3SourceOcclusionDirectRatioValidator{}(props.flOcclusionDirectRatio);
  614. Eax3SourceExclusionValidator{}(props.lExclusion);
  615. Eax3SourceExclusionLfRatioValidator{}(props.flExclusionLFRatio);
  616. }
  617. };
  618. struct Eax4AllSendValidator : EaxAllSendValidator<Eax4SendReceivingFxSlotIdValidator> {};
  619. struct Eax5AllSendValidator : EaxAllSendValidator<Eax5SendReceivingFxSlotIdValidator> {};
  620. // Send validators
  621. // ----------------------------------------------------------------------
  622. // Active FX slot ID validators
  623. struct Eax4ActiveFxSlotIdValidator {
  624. void operator()(const GUID &guid) const
  625. {
  626. if(guid != EAX_NULL_GUID && guid != EAX_PrimaryFXSlotID
  627. && guid != EAXPROPERTYID_EAX40_FXSlot0 && guid != EAXPROPERTYID_EAX40_FXSlot1
  628. && guid != EAXPROPERTYID_EAX40_FXSlot2 && guid != EAXPROPERTYID_EAX40_FXSlot3)
  629. {
  630. eax_fail_unknown_active_fx_slot_id();
  631. }
  632. }
  633. };
  634. struct Eax5ActiveFxSlotIdValidator {
  635. void operator()(const GUID &guid) const
  636. {
  637. if(guid != EAX_NULL_GUID && guid != EAX_PrimaryFXSlotID
  638. && guid != EAXPROPERTYID_EAX50_FXSlot0 && guid != EAXPROPERTYID_EAX50_FXSlot1
  639. && guid != EAXPROPERTYID_EAX50_FXSlot2 && guid != EAXPROPERTYID_EAX50_FXSlot3)
  640. {
  641. eax_fail_unknown_active_fx_slot_id();
  642. }
  643. }
  644. };
  645. // Active FX slot ID validators
  646. // ----------------------------------------------------------------------
  647. // Speaker level validators.
  648. struct Eax5SpeakerIdValidator {
  649. void operator()(long lSpeakerID) const
  650. {
  651. switch (lSpeakerID) {
  652. case EAXSPEAKER_FRONT_LEFT:
  653. case EAXSPEAKER_FRONT_CENTER:
  654. case EAXSPEAKER_FRONT_RIGHT:
  655. case EAXSPEAKER_SIDE_RIGHT:
  656. case EAXSPEAKER_REAR_RIGHT:
  657. case EAXSPEAKER_REAR_CENTER:
  658. case EAXSPEAKER_REAR_LEFT:
  659. case EAXSPEAKER_SIDE_LEFT:
  660. case EAXSPEAKER_LOW_FREQUENCY:
  661. break;
  662. default:
  663. eax_fail("Unknown speaker ID.");
  664. }
  665. }
  666. };
  667. struct Eax5SpeakerLevelValidator {
  668. void operator()(long lLevel) const
  669. {
  670. // TODO Use a range when the feature will be implemented.
  671. if (lLevel != EAXSOURCE_DEFAULTSPEAKERLEVEL)
  672. eax_fail("Speaker level out of range.");
  673. }
  674. };
  675. struct Eax5SpeakerAllValidator {
  676. void operator()(const EAXSPEAKERLEVELPROPERTIES& all) const
  677. {
  678. Eax5SpeakerIdValidator{}(all.lSpeakerID);
  679. Eax5SpeakerLevelValidator{}(all.lLevel);
  680. }
  681. };
  682. // Speaker level validators.
  683. // ----------------------------------------------------------------------
  684. struct Eax4SendIndexGetter {
  685. EaxFxSlotIndexValue operator()(const GUID &guid) const
  686. {
  687. if(guid == EAXPROPERTYID_EAX40_FXSlot0)
  688. return 0;
  689. if(guid == EAXPROPERTYID_EAX40_FXSlot1)
  690. return 1;
  691. if(guid == EAXPROPERTYID_EAX40_FXSlot2)
  692. return 2;
  693. if(guid == EAXPROPERTYID_EAX40_FXSlot3)
  694. return 3;
  695. eax_fail_unknown_receiving_fx_slot_id();
  696. }
  697. };
  698. struct Eax5SendIndexGetter {
  699. EaxFxSlotIndexValue operator()(const GUID &guid) const
  700. {
  701. if(guid == EAXPROPERTYID_EAX50_FXSlot0)
  702. return 0;
  703. if(guid == EAXPROPERTYID_EAX50_FXSlot1)
  704. return 1;
  705. if(guid == EAXPROPERTYID_EAX50_FXSlot2)
  706. return 2;
  707. if(guid == EAXPROPERTYID_EAX50_FXSlot3)
  708. return 3;
  709. eax_fail_unknown_receiving_fx_slot_id();
  710. }
  711. };
  712. [[noreturn]] static void eax_fail(const char* message);
  713. [[noreturn]] static void eax_fail_unknown_property_id();
  714. [[noreturn]] static void eax_fail_unknown_version();
  715. [[noreturn]] static void eax_fail_unknown_active_fx_slot_id();
  716. [[noreturn]] static void eax_fail_unknown_receiving_fx_slot_id();
  717. static void eax_set_sends_defaults(EaxSends& sends, const EaxFxSlotIds& ids) noexcept;
  718. static void eax1_set_defaults(EAXBUFFER_REVERBPROPERTIES& props) noexcept;
  719. void eax1_set_defaults() noexcept;
  720. static void eax2_set_defaults(EAX20BUFFERPROPERTIES& props) noexcept;
  721. void eax2_set_defaults() noexcept;
  722. static void eax3_set_defaults(EAX30SOURCEPROPERTIES& props) noexcept;
  723. void eax3_set_defaults() noexcept;
  724. static void eax4_set_sends_defaults(EaxSends& sends) noexcept;
  725. static void eax4_set_active_fx_slots_defaults(EAX40ACTIVEFXSLOTS& slots) noexcept;
  726. void eax4_set_defaults() noexcept;
  727. static void eax5_set_source_defaults(EAX50SOURCEPROPERTIES& props) noexcept;
  728. static void eax5_set_sends_defaults(EaxSends& sends) noexcept;
  729. static void eax5_set_active_fx_slots_defaults(EAX50ACTIVEFXSLOTS& slots) noexcept;
  730. static void eax5_set_speaker_levels_defaults(EaxSpeakerLevels& speaker_levels) noexcept;
  731. static void eax5_set_defaults(Eax5Props& props) noexcept;
  732. void eax5_set_defaults() noexcept;
  733. void eax_set_defaults() noexcept;
  734. static void eax1_translate(const EAXBUFFER_REVERBPROPERTIES& src, Eax5Props& dst) noexcept;
  735. static void eax2_translate(const EAX20BUFFERPROPERTIES& src, Eax5Props& dst) noexcept;
  736. static void eax3_translate(const EAX30SOURCEPROPERTIES& src, Eax5Props& dst) noexcept;
  737. static void eax4_translate(const Eax4Props& src, Eax5Props& dst) noexcept;
  738. static float eax_calculate_dst_occlusion_mb(
  739. long src_occlusion_mb,
  740. float path_ratio,
  741. float lf_ratio) noexcept;
  742. [[nodiscard]] auto eax_create_direct_filter_param() const noexcept -> EaxAlLowPassParam;
  743. [[nodiscard]] auto eax_create_room_filter_param(const ALeffectslot& fx_slot,
  744. const EAXSOURCEALLSENDPROPERTIES& send) const noexcept -> EaxAlLowPassParam;
  745. void eax_update_direct_filter();
  746. void eax_update_room_filters();
  747. void eax_commit_filters();
  748. static void eax_copy_send_for_get(
  749. const EAXSOURCEALLSENDPROPERTIES& src,
  750. EAXSOURCESENDPROPERTIES& dst) noexcept
  751. {
  752. dst = reinterpret_cast<const EAXSOURCESENDPROPERTIES&>(src);
  753. }
  754. static void eax_copy_send_for_get(
  755. const EAXSOURCEALLSENDPROPERTIES& src,
  756. EAXSOURCEALLSENDPROPERTIES& dst) noexcept
  757. {
  758. dst = src;
  759. }
  760. static void eax_copy_send_for_get(
  761. const EAXSOURCEALLSENDPROPERTIES& src,
  762. EAXSOURCEOCCLUSIONSENDPROPERTIES& dst) noexcept
  763. {
  764. dst.guidReceivingFXSlotID = src.guidReceivingFXSlotID;
  765. dst.lOcclusion = src.lOcclusion;
  766. dst.flOcclusionLFRatio = src.flOcclusionLFRatio;
  767. dst.flOcclusionRoomRatio = src.flOcclusionRoomRatio;
  768. dst.flOcclusionDirectRatio = src.flOcclusionDirectRatio;
  769. }
  770. static void eax_copy_send_for_get(
  771. const EAXSOURCEALLSENDPROPERTIES& src,
  772. EAXSOURCEEXCLUSIONSENDPROPERTIES& dst) noexcept
  773. {
  774. dst.guidReceivingFXSlotID = src.guidReceivingFXSlotID;
  775. dst.lExclusion = src.lExclusion;
  776. dst.flExclusionLFRatio = src.flExclusionLFRatio;
  777. }
  778. template<typename TDstSend>
  779. void eax_get_sends(const EaxCall& call, const EaxSends& src_sends)
  780. {
  781. const auto dst_sends = call.get_values<TDstSend>(EAX_MAX_FXSLOTS);
  782. const auto count = dst_sends.size();
  783. for (auto i = decltype(count){}; i < count; ++i) {
  784. const auto& src_send = src_sends[i];
  785. auto& dst_send = dst_sends[i];
  786. eax_copy_send_for_get(src_send, dst_send);
  787. }
  788. }
  789. static void eax_get_active_fx_slot_id(const EaxCall& call, const al::span<const GUID> src_ids);
  790. static void eax1_get(const EaxCall& call, const EAXBUFFER_REVERBPROPERTIES& props);
  791. static void eax2_get(const EaxCall& call, const EAX20BUFFERPROPERTIES& props);
  792. static void eax3_get_obstruction(const EaxCall& call, const EAX30SOURCEPROPERTIES& props);
  793. static void eax3_get_occlusion(const EaxCall& call, const EAX30SOURCEPROPERTIES& props);
  794. static void eax3_get_exclusion(const EaxCall& call, const EAX30SOURCEPROPERTIES& props);
  795. static void eax3_get(const EaxCall& call, const EAX30SOURCEPROPERTIES& props);
  796. void eax4_get(const EaxCall& call, const Eax4Props& props);
  797. static void eax5_get_all_2d(const EaxCall& call, const EAX50SOURCEPROPERTIES& props);
  798. static void eax5_get_speaker_levels(const EaxCall& call, const EaxSpeakerLevels& props);
  799. void eax5_get(const EaxCall& call, const Eax5Props& props);
  800. void eax_get(const EaxCall& call);
  801. static void eax_copy_send_for_set(
  802. const EAXSOURCESENDPROPERTIES& src,
  803. EAXSOURCEALLSENDPROPERTIES& dst) noexcept
  804. {
  805. dst.lSend = src.lSend;
  806. dst.lSendHF = src.lSendHF;
  807. }
  808. static void eax_copy_send_for_set(
  809. const EAXSOURCEALLSENDPROPERTIES& src,
  810. EAXSOURCEALLSENDPROPERTIES& dst) noexcept
  811. {
  812. dst.lSend = src.lSend;
  813. dst.lSendHF = src.lSendHF;
  814. dst.lOcclusion = src.lOcclusion;
  815. dst.flOcclusionLFRatio = src.flOcclusionLFRatio;
  816. dst.flOcclusionRoomRatio = src.flOcclusionRoomRatio;
  817. dst.flOcclusionDirectRatio = src.flOcclusionDirectRatio;
  818. dst.lExclusion = src.lExclusion;
  819. dst.flExclusionLFRatio = src.flExclusionLFRatio;
  820. }
  821. static void eax_copy_send_for_set(
  822. const EAXSOURCEOCCLUSIONSENDPROPERTIES& src,
  823. EAXSOURCEALLSENDPROPERTIES& dst) noexcept
  824. {
  825. dst.lOcclusion = src.lOcclusion;
  826. dst.flOcclusionLFRatio = src.flOcclusionLFRatio;
  827. dst.flOcclusionRoomRatio = src.flOcclusionRoomRatio;
  828. dst.flOcclusionDirectRatio = src.flOcclusionDirectRatio;
  829. }
  830. static void eax_copy_send_for_set(
  831. const EAXSOURCEEXCLUSIONSENDPROPERTIES& src,
  832. EAXSOURCEALLSENDPROPERTIES& dst) noexcept
  833. {
  834. dst.lExclusion = src.lExclusion;
  835. dst.flExclusionLFRatio = src.flExclusionLFRatio;
  836. }
  837. template<typename TValidator, typename TIndexGetter, typename TSrcSend>
  838. void eax_defer_sends(const EaxCall& call, EaxSends& dst_sends)
  839. {
  840. const auto src_sends = call.get_values<const TSrcSend>(EAX_MAX_FXSLOTS);
  841. std::for_each(src_sends.cbegin(), src_sends.cend(), TValidator{});
  842. const auto count = src_sends.size();
  843. const auto index_getter = TIndexGetter{};
  844. for (auto i = decltype(count){}; i < count; ++i) {
  845. const auto& src_send = src_sends[i];
  846. const auto dst_index = index_getter(src_send.guidReceivingFXSlotID);
  847. auto& dst_send = dst_sends[dst_index];
  848. eax_copy_send_for_set(src_send, dst_send);
  849. }
  850. }
  851. template<typename TValidator, typename TSrcSend>
  852. void eax4_defer_sends(const EaxCall& call, EaxSends& dst_sends)
  853. {
  854. eax_defer_sends<TValidator, Eax4SendIndexGetter, TSrcSend>(call, dst_sends);
  855. }
  856. template<typename TValidator, typename TSrcSend>
  857. void eax5_defer_sends(const EaxCall& call, EaxSends& dst_sends)
  858. {
  859. eax_defer_sends<TValidator, Eax5SendIndexGetter, TSrcSend>(call, dst_sends);
  860. }
  861. template<typename TValidator, size_t TIdCount>
  862. void eax_defer_active_fx_slot_id(const EaxCall& call, const al::span<GUID,TIdCount> dst_ids)
  863. {
  864. const auto src_ids = call.get_values<const GUID>(TIdCount);
  865. std::for_each(src_ids.cbegin(), src_ids.cend(), TValidator{});
  866. std::uninitialized_copy(src_ids.cbegin(), src_ids.cend(), dst_ids.begin());
  867. }
  868. template<size_t TIdCount>
  869. void eax4_defer_active_fx_slot_id(const EaxCall& call, const al::span<GUID,TIdCount> dst_ids)
  870. {
  871. eax_defer_active_fx_slot_id<Eax4ActiveFxSlotIdValidator>(call, dst_ids);
  872. }
  873. template<size_t TIdCount>
  874. void eax5_defer_active_fx_slot_id(const EaxCall& call, const al::span<GUID,TIdCount> dst_ids)
  875. {
  876. eax_defer_active_fx_slot_id<Eax5ActiveFxSlotIdValidator>(call, dst_ids);
  877. }
  878. template<typename TValidator, typename TProperty>
  879. static void eax_defer(const EaxCall& call, TProperty& property)
  880. {
  881. const auto& value = call.get_value<Exception, const TProperty>();
  882. TValidator{}(value);
  883. property = value;
  884. }
  885. // Defers source's sub-properties (obstruction, occlusion, exclusion).
  886. template<typename TValidator, typename TSubproperty, typename TProperty>
  887. void eax_defer_sub(const EaxCall& call, TProperty& property)
  888. {
  889. const auto& src_props = call.get_value<Exception, const TSubproperty>();
  890. TValidator{}(src_props);
  891. auto& dst_props = reinterpret_cast<TSubproperty&>(property);
  892. dst_props = src_props;
  893. }
  894. void eax_set_efx_outer_gain_hf();
  895. void eax_set_efx_doppler_factor();
  896. void eax_set_efx_rolloff_factor();
  897. void eax_set_efx_room_rolloff_factor();
  898. void eax_set_efx_air_absorption_factor();
  899. void eax_set_efx_dry_gain_hf_auto();
  900. void eax_set_efx_wet_gain_auto();
  901. void eax_set_efx_wet_gain_hf_auto();
  902. static void eax1_set(const EaxCall& call, EAXBUFFER_REVERBPROPERTIES& props);
  903. static void eax2_set(const EaxCall& call, EAX20BUFFERPROPERTIES& props);
  904. void eax3_set(const EaxCall& call, EAX30SOURCEPROPERTIES& props);
  905. void eax4_set(const EaxCall& call, Eax4Props& props);
  906. static void eax5_defer_all_2d(const EaxCall& call, EAX50SOURCEPROPERTIES& props);
  907. static void eax5_defer_speaker_levels(const EaxCall& call, EaxSpeakerLevels& props);
  908. void eax5_set(const EaxCall& call, Eax5Props& props);
  909. void eax_set(const EaxCall& call);
  910. // `alSource3i(source, AL_AUXILIARY_SEND_FILTER, ...)`
  911. void eax_set_al_source_send(ALeffectslot *slot, size_t sendidx,
  912. const EaxAlLowPassParam &filter);
  913. void eax_commit_active_fx_slots();
  914. #endif // ALSOFT_EAX
  915. };
  916. void UpdateAllSourceProps(ALCcontext *context);
  917. struct SourceSubList {
  918. uint64_t FreeMask{~0_u64};
  919. gsl::owner<std::array<ALsource,64>*> Sources{nullptr};
  920. SourceSubList() noexcept = default;
  921. SourceSubList(const SourceSubList&) = delete;
  922. SourceSubList(SourceSubList&& rhs) noexcept : FreeMask{rhs.FreeMask}, Sources{rhs.Sources}
  923. { rhs.FreeMask = ~0_u64; rhs.Sources = nullptr; }
  924. ~SourceSubList();
  925. SourceSubList& operator=(const SourceSubList&) = delete;
  926. SourceSubList& operator=(SourceSubList&& rhs) noexcept
  927. { std::swap(FreeMask, rhs.FreeMask); std::swap(Sources, rhs.Sources); return *this; }
  928. };
  929. #endif