effect.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #ifndef EAX_EFFECT_INCLUDED
  2. #define EAX_EFFECT_INCLUDED
  3. #include <cassert>
  4. #include <memory>
  5. #include <variant>
  6. #include "alnumeric.h"
  7. #include "AL/al.h"
  8. #include "AL/alext.h"
  9. #include "core/effects/base.h"
  10. #include "call.h"
  11. struct EaxEffectErrorMessages {
  12. static constexpr auto unknown_property_id() noexcept { return "Unknown property id."; }
  13. static constexpr auto unknown_version() noexcept { return "Unknown version."; }
  14. }; // EaxEffectErrorMessages
  15. using EaxEffectProps = std::variant<std::monostate,
  16. EAXREVERBPROPERTIES,
  17. EAXCHORUSPROPERTIES,
  18. EAXAUTOWAHPROPERTIES,
  19. EAXAGCCOMPRESSORPROPERTIES,
  20. EAXDISTORTIONPROPERTIES,
  21. EAXECHOPROPERTIES,
  22. EAXEQUALIZERPROPERTIES,
  23. EAXFLANGERPROPERTIES,
  24. EAXFREQUENCYSHIFTERPROPERTIES,
  25. EAXRINGMODULATORPROPERTIES,
  26. EAXPITCHSHIFTERPROPERTIES,
  27. EAXVOCALMORPHERPROPERTIES>;
  28. template<typename... Ts>
  29. struct overloaded : Ts... { using Ts::operator()...; };
  30. template<typename... Ts>
  31. overloaded(Ts...) -> overloaded<Ts...>;
  32. constexpr ALenum EnumFromEaxEffectType(const EaxEffectProps &props)
  33. {
  34. return std::visit(overloaded{
  35. [](const std::monostate&) noexcept { return AL_EFFECT_NULL; },
  36. [](const EAXREVERBPROPERTIES&) noexcept { return AL_EFFECT_EAXREVERB; },
  37. [](const EAXCHORUSPROPERTIES&) noexcept { return AL_EFFECT_CHORUS; },
  38. [](const EAXAUTOWAHPROPERTIES&) noexcept { return AL_EFFECT_AUTOWAH; },
  39. [](const EAXAGCCOMPRESSORPROPERTIES&) noexcept { return AL_EFFECT_COMPRESSOR; },
  40. [](const EAXDISTORTIONPROPERTIES&) noexcept { return AL_EFFECT_DISTORTION; },
  41. [](const EAXECHOPROPERTIES&) noexcept { return AL_EFFECT_ECHO; },
  42. [](const EAXEQUALIZERPROPERTIES&) noexcept { return AL_EFFECT_EQUALIZER; },
  43. [](const EAXFLANGERPROPERTIES&) noexcept { return AL_EFFECT_FLANGER; },
  44. [](const EAXFREQUENCYSHIFTERPROPERTIES&) noexcept { return AL_EFFECT_FREQUENCY_SHIFTER; },
  45. [](const EAXRINGMODULATORPROPERTIES&) noexcept { return AL_EFFECT_RING_MODULATOR; },
  46. [](const EAXPITCHSHIFTERPROPERTIES&) noexcept { return AL_EFFECT_PITCH_SHIFTER; },
  47. [](const EAXVOCALMORPHERPROPERTIES&) noexcept { return AL_EFFECT_VOCAL_MORPHER; }
  48. }, props);
  49. }
  50. struct EaxReverbCommitter {
  51. struct Exception;
  52. EaxReverbCommitter(EaxEffectProps &eaxprops, EffectProps &alprops)
  53. : mEaxProps{eaxprops}, mAlProps{alprops}
  54. { }
  55. EaxEffectProps &mEaxProps;
  56. EffectProps &mAlProps;
  57. [[noreturn]] static void fail(const char* message);
  58. [[noreturn]] static void fail_unknown_property_id()
  59. { fail(EaxEffectErrorMessages::unknown_property_id()); }
  60. template<typename TValidator, typename TProperty>
  61. static void defer(const EaxCall& call, TProperty& property)
  62. {
  63. const auto& value = call.get_value<Exception, const TProperty>();
  64. TValidator{}(value);
  65. property = value;
  66. }
  67. template<typename TValidator, typename TDeferrer, typename TProperties, typename TProperty>
  68. static void defer(const EaxCall& call, TProperties& properties, TProperty&)
  69. {
  70. const auto& value = call.get_value<Exception, const TProperty>();
  71. TValidator{}(value);
  72. TDeferrer{}(properties, value);
  73. }
  74. template<typename TValidator, typename TProperty>
  75. static void defer3(const EaxCall& call, EAXREVERBPROPERTIES& properties, TProperty& property)
  76. {
  77. const auto& value = call.get_value<Exception, const TProperty>();
  78. TValidator{}(value);
  79. if (value == property)
  80. return;
  81. property = value;
  82. properties.ulEnvironment = EAX_ENVIRONMENT_UNDEFINED;
  83. }
  84. bool commit(const EAX_REVERBPROPERTIES &props);
  85. bool commit(const EAX20LISTENERPROPERTIES &props);
  86. bool commit(const EAXREVERBPROPERTIES &props);
  87. static void SetDefaults(EAX_REVERBPROPERTIES &props);
  88. static void SetDefaults(EAX20LISTENERPROPERTIES &props);
  89. static void SetDefaults(EAXREVERBPROPERTIES &props);
  90. static void SetDefaults(EaxEffectProps &props);
  91. static void Get(const EaxCall &call, const EAX_REVERBPROPERTIES &props);
  92. static void Get(const EaxCall &call, const EAX20LISTENERPROPERTIES &props);
  93. static void Get(const EaxCall &call, const EAXREVERBPROPERTIES &props);
  94. static void Set(const EaxCall &call, EAX_REVERBPROPERTIES &props);
  95. static void Set(const EaxCall &call, EAX20LISTENERPROPERTIES &props);
  96. static void Set(const EaxCall &call, EAXREVERBPROPERTIES &props);
  97. static void translate(const EAX_REVERBPROPERTIES& src, EAXREVERBPROPERTIES& dst) noexcept;
  98. static void translate(const EAX20LISTENERPROPERTIES& src, EAXREVERBPROPERTIES& dst) noexcept;
  99. };
  100. template<typename T>
  101. struct EaxCommitter {
  102. struct Exception;
  103. EaxCommitter(EaxEffectProps &eaxprops, EffectProps &alprops)
  104. : mEaxProps{eaxprops}, mAlProps{alprops}
  105. { }
  106. EaxEffectProps &mEaxProps;
  107. EffectProps &mAlProps;
  108. template<typename TValidator, typename TProperty>
  109. static void defer(const EaxCall& call, TProperty& property)
  110. {
  111. const auto& value = call.get_value<Exception, const TProperty>();
  112. TValidator{}(value);
  113. property = value;
  114. }
  115. [[noreturn]] static void fail(const char *message);
  116. [[noreturn]] static void fail_unknown_property_id()
  117. { fail(EaxEffectErrorMessages::unknown_property_id()); }
  118. };
  119. struct EaxAutowahCommitter : public EaxCommitter<EaxAutowahCommitter> {
  120. using EaxCommitter<EaxAutowahCommitter>::EaxCommitter;
  121. bool commit(const EAXAUTOWAHPROPERTIES &props);
  122. static void SetDefaults(EaxEffectProps &props);
  123. static void Get(const EaxCall &call, const EAXAUTOWAHPROPERTIES &props);
  124. static void Set(const EaxCall &call, EAXAUTOWAHPROPERTIES &props);
  125. };
  126. struct EaxChorusCommitter : public EaxCommitter<EaxChorusCommitter> {
  127. using EaxCommitter<EaxChorusCommitter>::EaxCommitter;
  128. bool commit(const EAXCHORUSPROPERTIES &props);
  129. static void SetDefaults(EaxEffectProps &props);
  130. static void Get(const EaxCall &call, const EAXCHORUSPROPERTIES &props);
  131. static void Set(const EaxCall &call, EAXCHORUSPROPERTIES &props);
  132. };
  133. struct EaxCompressorCommitter : public EaxCommitter<EaxCompressorCommitter> {
  134. using EaxCommitter<EaxCompressorCommitter>::EaxCommitter;
  135. bool commit(const EAXAGCCOMPRESSORPROPERTIES &props);
  136. static void SetDefaults(EaxEffectProps &props);
  137. static void Get(const EaxCall &call, const EAXAGCCOMPRESSORPROPERTIES &props);
  138. static void Set(const EaxCall &call, EAXAGCCOMPRESSORPROPERTIES &props);
  139. };
  140. struct EaxDistortionCommitter : public EaxCommitter<EaxDistortionCommitter> {
  141. using EaxCommitter<EaxDistortionCommitter>::EaxCommitter;
  142. bool commit(const EAXDISTORTIONPROPERTIES &props);
  143. static void SetDefaults(EaxEffectProps &props);
  144. static void Get(const EaxCall &call, const EAXDISTORTIONPROPERTIES &props);
  145. static void Set(const EaxCall &call, EAXDISTORTIONPROPERTIES &props);
  146. };
  147. struct EaxEchoCommitter : public EaxCommitter<EaxEchoCommitter> {
  148. using EaxCommitter<EaxEchoCommitter>::EaxCommitter;
  149. bool commit(const EAXECHOPROPERTIES &props);
  150. static void SetDefaults(EaxEffectProps &props);
  151. static void Get(const EaxCall &call, const EAXECHOPROPERTIES &props);
  152. static void Set(const EaxCall &call, EAXECHOPROPERTIES &props);
  153. };
  154. struct EaxEqualizerCommitter : public EaxCommitter<EaxEqualizerCommitter> {
  155. using EaxCommitter<EaxEqualizerCommitter>::EaxCommitter;
  156. bool commit(const EAXEQUALIZERPROPERTIES &props);
  157. static void SetDefaults(EaxEffectProps &props);
  158. static void Get(const EaxCall &call, const EAXEQUALIZERPROPERTIES &props);
  159. static void Set(const EaxCall &call, EAXEQUALIZERPROPERTIES &props);
  160. };
  161. struct EaxFlangerCommitter : public EaxCommitter<EaxFlangerCommitter> {
  162. using EaxCommitter<EaxFlangerCommitter>::EaxCommitter;
  163. bool commit(const EAXFLANGERPROPERTIES &props);
  164. static void SetDefaults(EaxEffectProps &props);
  165. static void Get(const EaxCall &call, const EAXFLANGERPROPERTIES &props);
  166. static void Set(const EaxCall &call, EAXFLANGERPROPERTIES &props);
  167. };
  168. struct EaxFrequencyShifterCommitter : public EaxCommitter<EaxFrequencyShifterCommitter> {
  169. using EaxCommitter<EaxFrequencyShifterCommitter>::EaxCommitter;
  170. bool commit(const EAXFREQUENCYSHIFTERPROPERTIES &props);
  171. static void SetDefaults(EaxEffectProps &props);
  172. static void Get(const EaxCall &call, const EAXFREQUENCYSHIFTERPROPERTIES &props);
  173. static void Set(const EaxCall &call, EAXFREQUENCYSHIFTERPROPERTIES &props);
  174. };
  175. struct EaxModulatorCommitter : public EaxCommitter<EaxModulatorCommitter> {
  176. using EaxCommitter<EaxModulatorCommitter>::EaxCommitter;
  177. bool commit(const EAXRINGMODULATORPROPERTIES &props);
  178. static void SetDefaults(EaxEffectProps &props);
  179. static void Get(const EaxCall &call, const EAXRINGMODULATORPROPERTIES &props);
  180. static void Set(const EaxCall &call, EAXRINGMODULATORPROPERTIES &props);
  181. };
  182. struct EaxPitchShifterCommitter : public EaxCommitter<EaxPitchShifterCommitter> {
  183. using EaxCommitter<EaxPitchShifterCommitter>::EaxCommitter;
  184. bool commit(const EAXPITCHSHIFTERPROPERTIES &props);
  185. static void SetDefaults(EaxEffectProps &props);
  186. static void Get(const EaxCall &call, const EAXPITCHSHIFTERPROPERTIES &props);
  187. static void Set(const EaxCall &call, EAXPITCHSHIFTERPROPERTIES &props);
  188. };
  189. struct EaxVocalMorpherCommitter : public EaxCommitter<EaxVocalMorpherCommitter> {
  190. using EaxCommitter<EaxVocalMorpherCommitter>::EaxCommitter;
  191. bool commit(const EAXVOCALMORPHERPROPERTIES &props);
  192. static void SetDefaults(EaxEffectProps &props);
  193. static void Get(const EaxCall &call, const EAXVOCALMORPHERPROPERTIES &props);
  194. static void Set(const EaxCall &call, EAXVOCALMORPHERPROPERTIES &props);
  195. };
  196. struct EaxNullCommitter : public EaxCommitter<EaxNullCommitter> {
  197. using EaxCommitter<EaxNullCommitter>::EaxCommitter;
  198. bool commit(const std::monostate &props);
  199. static void SetDefaults(EaxEffectProps &props);
  200. static void Get(const EaxCall &call, const std::monostate &props);
  201. static void Set(const EaxCall &call, std::monostate &props);
  202. };
  203. template<typename T>
  204. struct CommitterFromProps { };
  205. template<> struct CommitterFromProps<std::monostate> { using type = EaxNullCommitter; };
  206. template<> struct CommitterFromProps<EAXREVERBPROPERTIES> { using type = EaxReverbCommitter; };
  207. template<> struct CommitterFromProps<EAXCHORUSPROPERTIES> { using type = EaxChorusCommitter; };
  208. template<> struct CommitterFromProps<EAXAGCCOMPRESSORPROPERTIES> { using type = EaxCompressorCommitter; };
  209. template<> struct CommitterFromProps<EAXAUTOWAHPROPERTIES> { using type = EaxAutowahCommitter; };
  210. template<> struct CommitterFromProps<EAXDISTORTIONPROPERTIES> { using type = EaxDistortionCommitter; };
  211. template<> struct CommitterFromProps<EAXECHOPROPERTIES> { using type = EaxEchoCommitter; };
  212. template<> struct CommitterFromProps<EAXEQUALIZERPROPERTIES> { using type = EaxEqualizerCommitter; };
  213. template<> struct CommitterFromProps<EAXFLANGERPROPERTIES> { using type = EaxFlangerCommitter; };
  214. template<> struct CommitterFromProps<EAXFREQUENCYSHIFTERPROPERTIES> { using type = EaxFrequencyShifterCommitter; };
  215. template<> struct CommitterFromProps<EAXRINGMODULATORPROPERTIES> { using type = EaxModulatorCommitter; };
  216. template<> struct CommitterFromProps<EAXPITCHSHIFTERPROPERTIES> { using type = EaxPitchShifterCommitter; };
  217. template<> struct CommitterFromProps<EAXVOCALMORPHERPROPERTIES> { using type = EaxVocalMorpherCommitter; };
  218. template<typename T>
  219. using CommitterFor = typename CommitterFromProps<std::remove_cv_t<std::remove_reference_t<T>>>::type;
  220. class EaxEffect {
  221. public:
  222. EaxEffect() noexcept = default;
  223. ~EaxEffect() = default;
  224. ALenum al_effect_type_{AL_EFFECT_NULL};
  225. EffectProps al_effect_props_{};
  226. using Props1 = EAX_REVERBPROPERTIES;
  227. using Props2 = EAX20LISTENERPROPERTIES;
  228. using Props3 = EAXREVERBPROPERTIES;
  229. using Props4 = EaxEffectProps;
  230. struct State1 {
  231. Props1 i; // Immediate.
  232. Props1 d; // Deferred.
  233. };
  234. struct State2 {
  235. Props2 i; // Immediate.
  236. Props2 d; // Deferred.
  237. };
  238. struct State3 {
  239. Props3 i; // Immediate.
  240. Props3 d; // Deferred.
  241. };
  242. struct State4 {
  243. Props4 i; // Immediate.
  244. Props4 d; // Deferred.
  245. };
  246. int version_{};
  247. bool changed_{};
  248. Props4 props_{};
  249. State1 state1_{};
  250. State2 state2_{};
  251. State3 state3_{};
  252. State4 state4_{};
  253. State4 state5_{};
  254. static void call_set_defaults(const ALenum altype, EaxEffectProps &props)
  255. {
  256. switch(altype)
  257. {
  258. case AL_EFFECT_EAXREVERB: return EaxReverbCommitter::SetDefaults(props);
  259. case AL_EFFECT_CHORUS: return EaxChorusCommitter::SetDefaults(props);
  260. case AL_EFFECT_AUTOWAH: return EaxAutowahCommitter::SetDefaults(props);
  261. case AL_EFFECT_COMPRESSOR: return EaxCompressorCommitter::SetDefaults(props);
  262. case AL_EFFECT_DISTORTION: return EaxDistortionCommitter::SetDefaults(props);
  263. case AL_EFFECT_ECHO: return EaxEchoCommitter::SetDefaults(props);
  264. case AL_EFFECT_EQUALIZER: return EaxEqualizerCommitter::SetDefaults(props);
  265. case AL_EFFECT_FLANGER: return EaxFlangerCommitter::SetDefaults(props);
  266. case AL_EFFECT_FREQUENCY_SHIFTER: return EaxFrequencyShifterCommitter::SetDefaults(props);
  267. case AL_EFFECT_RING_MODULATOR: return EaxModulatorCommitter::SetDefaults(props);
  268. case AL_EFFECT_PITCH_SHIFTER: return EaxPitchShifterCommitter::SetDefaults(props);
  269. case AL_EFFECT_VOCAL_MORPHER: return EaxVocalMorpherCommitter::SetDefaults(props);
  270. case AL_EFFECT_NULL: break;
  271. }
  272. return EaxNullCommitter::SetDefaults(props);
  273. }
  274. template<typename T>
  275. void init()
  276. {
  277. EaxReverbCommitter::SetDefaults(state1_.d);
  278. state1_.i = state1_.d;
  279. EaxReverbCommitter::SetDefaults(state2_.d);
  280. state2_.i = state2_.d;
  281. EaxReverbCommitter::SetDefaults(state3_.d);
  282. state3_.i = state3_.d;
  283. T::SetDefaults(state4_.d);
  284. state4_.i = state4_.d;
  285. T::SetDefaults(state5_.d);
  286. state5_.i = state5_.d;
  287. }
  288. void set_defaults(int eax_version, ALenum altype)
  289. {
  290. switch(eax_version)
  291. {
  292. case 1: EaxReverbCommitter::SetDefaults(state1_.d); break;
  293. case 2: EaxReverbCommitter::SetDefaults(state2_.d); break;
  294. case 3: EaxReverbCommitter::SetDefaults(state3_.d); break;
  295. case 4: call_set_defaults(altype, state4_.d); break;
  296. case 5: call_set_defaults(altype, state5_.d); break;
  297. }
  298. changed_ = true;
  299. }
  300. static void call_set(const EaxCall &call, EaxEffectProps &props)
  301. {
  302. return std::visit([&](auto &arg)
  303. { return CommitterFor<decltype(arg)>::Set(call, arg); },
  304. props);
  305. }
  306. void set(const EaxCall &call)
  307. {
  308. switch(call.get_version())
  309. {
  310. case 1: EaxReverbCommitter::Set(call, state1_.d); break;
  311. case 2: EaxReverbCommitter::Set(call, state2_.d); break;
  312. case 3: EaxReverbCommitter::Set(call, state3_.d); break;
  313. case 4: call_set(call, state4_.d); break;
  314. case 5: call_set(call, state5_.d); break;
  315. }
  316. changed_ = true;
  317. }
  318. static void call_get(const EaxCall &call, const EaxEffectProps &props)
  319. {
  320. return std::visit([&](auto &arg)
  321. { return CommitterFor<decltype(arg)>::Get(call, arg); },
  322. props);
  323. }
  324. void get(const EaxCall &call) const
  325. {
  326. switch(call.get_version())
  327. {
  328. case 1: EaxReverbCommitter::Get(call, state1_.d); break;
  329. case 2: EaxReverbCommitter::Get(call, state2_.d); break;
  330. case 3: EaxReverbCommitter::Get(call, state3_.d); break;
  331. case 4: call_get(call, state4_.d); break;
  332. case 5: call_get(call, state5_.d); break;
  333. }
  334. }
  335. bool call_commit(const EaxEffectProps &props)
  336. {
  337. return std::visit([&](auto &arg)
  338. { return CommitterFor<decltype(arg)>{props_, al_effect_props_}.commit(arg); },
  339. props);
  340. }
  341. bool commit(int eax_version)
  342. {
  343. changed_ |= version_ != eax_version;
  344. if(!changed_) return false;
  345. bool ret{version_ != eax_version};
  346. version_ = eax_version;
  347. changed_ = false;
  348. switch(eax_version)
  349. {
  350. case 1:
  351. state1_.i = state1_.d;
  352. ret |= EaxReverbCommitter{props_, al_effect_props_}.commit(state1_.d);
  353. break;
  354. case 2:
  355. state2_.i = state2_.d;
  356. ret |= EaxReverbCommitter{props_, al_effect_props_}.commit(state2_.d);
  357. break;
  358. case 3:
  359. state3_.i = state3_.d;
  360. ret |= EaxReverbCommitter{props_, al_effect_props_}.commit(state3_.d);
  361. break;
  362. case 4:
  363. state4_.i = state4_.d;
  364. ret |= call_commit(state4_.d);
  365. break;
  366. case 5:
  367. state5_.i = state5_.d;
  368. ret |= call_commit(state5_.d);
  369. break;
  370. }
  371. al_effect_type_ = EnumFromEaxEffectType(props_);
  372. return ret;
  373. }
  374. #undef EAXCALL
  375. }; // EaxEffect
  376. using EaxEffectUPtr = std::unique_ptr<EaxEffect>;
  377. #endif // !EAX_EFFECT_INCLUDED