auxeffectslot.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #ifndef AL_AUXEFFECTSLOT_H
  2. #define AL_AUXEFFECTSLOT_H
  3. #include <array>
  4. #include <atomic>
  5. #include <cstdint>
  6. #include <string_view>
  7. #include <utility>
  8. #include "AL/al.h"
  9. #include "AL/alc.h"
  10. #include "almalloc.h"
  11. #include "alnumeric.h"
  12. #include "core/effects/base.h"
  13. #include "core/effectslot.h"
  14. #include "intrusive_ptr.h"
  15. #ifdef ALSOFT_EAX
  16. #include <memory>
  17. #include "eax/api.h"
  18. #include "eax/call.h"
  19. #include "eax/effect.h"
  20. #include "eax/exception.h"
  21. #include "eax/fx_slot_index.h"
  22. #include "eax/utils.h"
  23. #endif // ALSOFT_EAX
  24. struct ALbuffer;
  25. #ifdef ALSOFT_EAX
  26. class EaxFxSlotException : public EaxException {
  27. public:
  28. explicit EaxFxSlotException(const char* message)
  29. : EaxException{"EAX_FX_SLOT", message}
  30. {}
  31. };
  32. #endif // ALSOFT_EAX
  33. enum class SlotState : bool {
  34. Initial, Playing,
  35. };
  36. struct ALeffectslot {
  37. ALuint EffectId{};
  38. float Gain{1.0f};
  39. bool AuxSendAuto{true};
  40. ALeffectslot *Target{nullptr};
  41. ALbuffer *Buffer{nullptr};
  42. struct EffectData {
  43. EffectSlotType Type{EffectSlotType::None};
  44. EffectProps Props{};
  45. al::intrusive_ptr<EffectState> State;
  46. };
  47. EffectData Effect;
  48. bool mPropsDirty{true};
  49. SlotState mState{SlotState::Initial};
  50. std::atomic<ALuint> ref{0u};
  51. EffectSlot *mSlot{nullptr};
  52. /* Self ID */
  53. ALuint id{};
  54. ALeffectslot(ALCcontext *context);
  55. ALeffectslot(const ALeffectslot&) = delete;
  56. ALeffectslot& operator=(const ALeffectslot&) = delete;
  57. ~ALeffectslot();
  58. ALenum initEffect(ALuint effectId, ALenum effectType, const EffectProps &effectProps,
  59. ALCcontext *context);
  60. void updateProps(ALCcontext *context) const;
  61. static void SetName(ALCcontext *context, ALuint id, std::string_view name);
  62. #ifdef ALSOFT_EAX
  63. public:
  64. void eax_initialize(ALCcontext& al_context, EaxFxSlotIndexValue index);
  65. [[nodiscard]] auto eax_get_index() const noexcept -> EaxFxSlotIndexValue { return eax_fx_slot_index_; }
  66. [[nodiscard]] auto eax_get_eax_fx_slot() const noexcept -> const EAX50FXSLOTPROPERTIES&
  67. { return eax_; }
  68. // Returns `true` if all sources should be updated, or `false` otherwise.
  69. [[nodiscard]] auto eax_dispatch(const EaxCall& call) -> bool
  70. { return call.is_get() ? eax_get(call) : eax_set(call); }
  71. void eax_commit();
  72. private:
  73. static constexpr auto eax_load_effect_dirty_bit = EaxDirtyFlags{1} << 0;
  74. static constexpr auto eax_volume_dirty_bit = EaxDirtyFlags{1} << 1;
  75. static constexpr auto eax_lock_dirty_bit = EaxDirtyFlags{1} << 2;
  76. static constexpr auto eax_flags_dirty_bit = EaxDirtyFlags{1} << 3;
  77. static constexpr auto eax_occlusion_dirty_bit = EaxDirtyFlags{1} << 4;
  78. static constexpr auto eax_occlusion_lf_ratio_dirty_bit = EaxDirtyFlags{1} << 5;
  79. using Exception = EaxFxSlotException;
  80. using Eax4Props = EAX40FXSLOTPROPERTIES;
  81. struct Eax4State {
  82. Eax4Props i; // Immediate.
  83. };
  84. using Eax5Props = EAX50FXSLOTPROPERTIES;
  85. struct Eax5State {
  86. Eax5Props i; // Immediate.
  87. };
  88. struct EaxRangeValidator {
  89. template<typename TValue>
  90. void operator()(
  91. const char* name,
  92. const TValue& value,
  93. const TValue& min_value,
  94. const TValue& max_value) const
  95. {
  96. eax_validate_range<Exception>(name, value, min_value, max_value);
  97. }
  98. };
  99. struct Eax4GuidLoadEffectValidator {
  100. void operator()(const GUID& guidLoadEffect) const
  101. {
  102. if (guidLoadEffect != EAX_NULL_GUID &&
  103. guidLoadEffect != EAX_REVERB_EFFECT &&
  104. guidLoadEffect != EAX_AGCCOMPRESSOR_EFFECT &&
  105. guidLoadEffect != EAX_AUTOWAH_EFFECT &&
  106. guidLoadEffect != EAX_CHORUS_EFFECT &&
  107. guidLoadEffect != EAX_DISTORTION_EFFECT &&
  108. guidLoadEffect != EAX_ECHO_EFFECT &&
  109. guidLoadEffect != EAX_EQUALIZER_EFFECT &&
  110. guidLoadEffect != EAX_FLANGER_EFFECT &&
  111. guidLoadEffect != EAX_FREQUENCYSHIFTER_EFFECT &&
  112. guidLoadEffect != EAX_VOCALMORPHER_EFFECT &&
  113. guidLoadEffect != EAX_PITCHSHIFTER_EFFECT &&
  114. guidLoadEffect != EAX_RINGMODULATOR_EFFECT)
  115. {
  116. eax_fail_unknown_effect_id();
  117. }
  118. }
  119. };
  120. struct Eax4VolumeValidator {
  121. void operator()(long lVolume) const
  122. {
  123. EaxRangeValidator{}(
  124. "Volume",
  125. lVolume,
  126. EAXFXSLOT_MINVOLUME,
  127. EAXFXSLOT_MAXVOLUME);
  128. }
  129. };
  130. struct Eax4LockValidator {
  131. void operator()(long lLock) const
  132. {
  133. EaxRangeValidator{}(
  134. "Lock",
  135. lLock,
  136. EAXFXSLOT_MINLOCK,
  137. EAXFXSLOT_MAXLOCK);
  138. }
  139. };
  140. struct Eax4FlagsValidator {
  141. void operator()(unsigned long ulFlags) const
  142. {
  143. EaxRangeValidator{}(
  144. "Flags",
  145. ulFlags,
  146. 0UL,
  147. ~EAX40FXSLOTFLAGS_RESERVED);
  148. }
  149. };
  150. struct Eax4AllValidator {
  151. void operator()(const EAX40FXSLOTPROPERTIES& all) const
  152. {
  153. Eax4GuidLoadEffectValidator{}(all.guidLoadEffect);
  154. Eax4VolumeValidator{}(all.lVolume);
  155. Eax4LockValidator{}(all.lLock);
  156. Eax4FlagsValidator{}(all.ulFlags);
  157. }
  158. };
  159. struct Eax5FlagsValidator {
  160. void operator()(unsigned long ulFlags) const
  161. {
  162. EaxRangeValidator{}(
  163. "Flags",
  164. ulFlags,
  165. 0UL,
  166. ~EAX50FXSLOTFLAGS_RESERVED);
  167. }
  168. };
  169. struct Eax5OcclusionValidator {
  170. void operator()(long lOcclusion) const
  171. {
  172. EaxRangeValidator{}(
  173. "Occlusion",
  174. lOcclusion,
  175. EAXFXSLOT_MINOCCLUSION,
  176. EAXFXSLOT_MAXOCCLUSION);
  177. }
  178. };
  179. struct Eax5OcclusionLfRatioValidator {
  180. void operator()(float flOcclusionLFRatio) const
  181. {
  182. EaxRangeValidator{}(
  183. "Occlusion LF Ratio",
  184. flOcclusionLFRatio,
  185. EAXFXSLOT_MINOCCLUSIONLFRATIO,
  186. EAXFXSLOT_MAXOCCLUSIONLFRATIO);
  187. }
  188. };
  189. struct Eax5AllValidator {
  190. void operator()(const EAX50FXSLOTPROPERTIES& all) const
  191. {
  192. Eax4GuidLoadEffectValidator{}(all.guidLoadEffect);
  193. Eax4VolumeValidator{}(all.lVolume);
  194. Eax4LockValidator{}(all.lLock);
  195. Eax5FlagsValidator{}(all.ulFlags);
  196. Eax5OcclusionValidator{}(all.lOcclusion);
  197. Eax5OcclusionLfRatioValidator{}(all.flOcclusionLFRatio);
  198. }
  199. };
  200. ALCcontext* eax_al_context_{};
  201. EaxFxSlotIndexValue eax_fx_slot_index_{};
  202. int eax_version_{}; // Current EAX version.
  203. EaxDirtyFlags eax_df_{}; // Dirty flags for the current EAX version.
  204. EaxEffectUPtr eax_effect_{};
  205. Eax5State eax123_{}; // EAX1/EAX2/EAX3 state.
  206. Eax4State eax4_{}; // EAX4 state.
  207. Eax5State eax5_{}; // EAX5 state.
  208. Eax5Props eax_{}; // Current EAX state.
  209. [[noreturn]] static void eax_fail(const char* message);
  210. [[noreturn]] static void eax_fail_unknown_effect_id();
  211. [[noreturn]] static void eax_fail_unknown_property_id();
  212. [[noreturn]] static void eax_fail_unknown_version();
  213. // Gets a new value from EAX call,
  214. // validates it,
  215. // sets a dirty flag only if the new value differs form the old one,
  216. // and assigns the new value.
  217. template<typename TValidator, EaxDirtyFlags TDirtyBit, typename TProperties>
  218. static void eax_fx_slot_set(const EaxCall& call, TProperties& dst, EaxDirtyFlags& dirty_flags)
  219. {
  220. const auto& src = call.get_value<Exception, const TProperties>();
  221. TValidator{}(src);
  222. dirty_flags |= (dst != src ? TDirtyBit : EaxDirtyFlags{});
  223. dst = src;
  224. }
  225. // Gets a new value from EAX call,
  226. // validates it,
  227. // sets a dirty flag without comparing the values,
  228. // and assigns the new value.
  229. template<typename TValidator, EaxDirtyFlags TDirtyBit, typename TProperties>
  230. static void eax_fx_slot_set_dirty(const EaxCall& call, TProperties& dst,
  231. EaxDirtyFlags& dirty_flags)
  232. {
  233. const auto& src = call.get_value<Exception, const TProperties>();
  234. TValidator{}(src);
  235. dirty_flags |= TDirtyBit;
  236. dst = src;
  237. }
  238. [[nodiscard]] constexpr auto eax4_fx_slot_is_legacy() const noexcept -> bool
  239. { return eax_fx_slot_index_ < 2; }
  240. void eax4_fx_slot_ensure_unlocked() const;
  241. [[nodiscard]] static auto eax_get_efx_effect_type(const GUID& guid) -> ALenum;
  242. [[nodiscard]] auto eax_get_eax_default_effect_guid() const noexcept -> const GUID&;
  243. [[nodiscard]] auto eax_get_eax_default_lock() const noexcept -> long;
  244. void eax4_fx_slot_set_defaults(Eax4Props& props) noexcept;
  245. void eax5_fx_slot_set_defaults(Eax5Props& props) noexcept;
  246. void eax4_fx_slot_set_current_defaults(const Eax4Props& props) noexcept;
  247. void eax5_fx_slot_set_current_defaults(const Eax5Props& props) noexcept;
  248. void eax_fx_slot_set_current_defaults();
  249. void eax_fx_slot_set_defaults();
  250. static void eax4_fx_slot_get(const EaxCall& call, const Eax4Props& props);
  251. static void eax5_fx_slot_get(const EaxCall& call, const Eax5Props& props);
  252. void eax_fx_slot_get(const EaxCall& call) const;
  253. // Returns `true` if all sources should be updated, or `false` otherwise.
  254. bool eax_get(const EaxCall& call);
  255. void eax_fx_slot_load_effect(int version, ALenum altype);
  256. void eax_fx_slot_set_volume();
  257. void eax_fx_slot_set_environment_flag();
  258. void eax_fx_slot_set_flags();
  259. void eax4_fx_slot_set_all(const EaxCall& call);
  260. void eax5_fx_slot_set_all(const EaxCall& call);
  261. [[nodiscard]] auto eax_fx_slot_should_update_sources() const noexcept -> bool;
  262. // Returns `true` if all sources should be updated, or `false` otherwise.
  263. bool eax4_fx_slot_set(const EaxCall& call);
  264. // Returns `true` if all sources should be updated, or `false` otherwise.
  265. bool eax5_fx_slot_set(const EaxCall& call);
  266. // Returns `true` if all sources should be updated, or `false` otherwise.
  267. bool eax_fx_slot_set(const EaxCall& call);
  268. // Returns `true` if all sources should be updated, or `false` otherwise.
  269. bool eax_set(const EaxCall& call);
  270. template<
  271. EaxDirtyFlags TDirtyBit,
  272. typename TMemberResult,
  273. typename TProps,
  274. typename TState>
  275. void eax_fx_slot_commit_property(TState& state, EaxDirtyFlags& dst_df,
  276. TMemberResult TProps::*member) noexcept
  277. {
  278. auto& src_i = state.i;
  279. auto& dst_i = eax_;
  280. if((eax_df_ & TDirtyBit) != EaxDirtyFlags{})
  281. {
  282. dst_df |= TDirtyBit;
  283. dst_i.*member = src_i.*member;
  284. }
  285. }
  286. void eax4_fx_slot_commit(EaxDirtyFlags& dst_df);
  287. void eax5_fx_slot_commit(Eax5State& state, EaxDirtyFlags& dst_df);
  288. // `alAuxiliaryEffectSloti(effect_slot, AL_EFFECTSLOT_EFFECT, effect)`
  289. void eax_set_efx_slot_effect(EaxEffect &effect);
  290. // `alAuxiliaryEffectSloti(effect_slot, AL_EFFECTSLOT_AUXILIARY_SEND_AUTO, value)`
  291. void eax_set_efx_slot_send_auto(bool is_send_auto);
  292. // `alAuxiliaryEffectSlotf(effect_slot, AL_EFFECTSLOT_GAIN, gain)`
  293. void eax_set_efx_slot_gain(ALfloat gain);
  294. public:
  295. class EaxDeleter {
  296. public:
  297. void operator()(ALeffectslot *effect_slot);
  298. };
  299. #endif // ALSOFT_EAX
  300. };
  301. void UpdateAllEffectSlotProps(ALCcontext *context);
  302. #ifdef ALSOFT_EAX
  303. using EaxAlEffectSlotUPtr = std::unique_ptr<ALeffectslot, ALeffectslot::EaxDeleter>;
  304. EaxAlEffectSlotUPtr eax_create_al_effect_slot(ALCcontext& context);
  305. void eax_delete_al_effect_slot(ALCcontext& context, ALeffectslot& effect_slot);
  306. #endif // ALSOFT_EAX
  307. struct EffectSlotSubList {
  308. uint64_t FreeMask{~0_u64};
  309. gsl::owner<std::array<ALeffectslot,64>*> EffectSlots{nullptr};
  310. EffectSlotSubList() noexcept = default;
  311. EffectSlotSubList(const EffectSlotSubList&) = delete;
  312. EffectSlotSubList(EffectSlotSubList&& rhs) noexcept
  313. : FreeMask{rhs.FreeMask}, EffectSlots{rhs.EffectSlots}
  314. { rhs.FreeMask = ~0_u64; rhs.EffectSlots = nullptr; }
  315. ~EffectSlotSubList();
  316. EffectSlotSubList& operator=(const EffectSlotSubList&) = delete;
  317. EffectSlotSubList& operator=(EffectSlotSubList&& rhs) noexcept
  318. { std::swap(FreeMask, rhs.FreeMask); std::swap(EffectSlots, rhs.EffectSlots); return *this; }
  319. };
  320. #endif