effect.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include "effect.h"
  22. #include <algorithm>
  23. #include <cstdint>
  24. #include <cstring>
  25. #include <iterator>
  26. #include <memory>
  27. #include <mutex>
  28. #include <numeric>
  29. #include <string>
  30. #include <type_traits>
  31. #include <unordered_map>
  32. #include <utility>
  33. #include <variant>
  34. #include <vector>
  35. #include "AL/al.h"
  36. #include "AL/alc.h"
  37. #include "AL/alext.h"
  38. #include "AL/efx-presets.h"
  39. #include "AL/efx.h"
  40. #include "al/effects/effects.h"
  41. #include "albit.h"
  42. #include "alc/context.h"
  43. #include "alc/device.h"
  44. #include "alc/inprogext.h"
  45. #include "almalloc.h"
  46. #include "alnumeric.h"
  47. #include "alspan.h"
  48. #include "alstring.h"
  49. #include "core/except.h"
  50. #include "core/logging.h"
  51. #include "direct_defs.h"
  52. #include "intrusive_ptr.h"
  53. #include "opthelpers.h"
  54. const std::array<EffectList,16> gEffectList{{
  55. { "eaxreverb", EAXREVERB_EFFECT, AL_EFFECT_EAXREVERB },
  56. { "reverb", REVERB_EFFECT, AL_EFFECT_REVERB },
  57. { "autowah", AUTOWAH_EFFECT, AL_EFFECT_AUTOWAH },
  58. { "chorus", CHORUS_EFFECT, AL_EFFECT_CHORUS },
  59. { "compressor", COMPRESSOR_EFFECT, AL_EFFECT_COMPRESSOR },
  60. { "distortion", DISTORTION_EFFECT, AL_EFFECT_DISTORTION },
  61. { "echo", ECHO_EFFECT, AL_EFFECT_ECHO },
  62. { "equalizer", EQUALIZER_EFFECT, AL_EFFECT_EQUALIZER },
  63. { "flanger", FLANGER_EFFECT, AL_EFFECT_FLANGER },
  64. { "fshifter", FSHIFTER_EFFECT, AL_EFFECT_FREQUENCY_SHIFTER },
  65. { "modulator", MODULATOR_EFFECT, AL_EFFECT_RING_MODULATOR },
  66. { "pshifter", PSHIFTER_EFFECT, AL_EFFECT_PITCH_SHIFTER },
  67. { "vmorpher", VMORPHER_EFFECT, AL_EFFECT_VOCAL_MORPHER },
  68. { "dedicated", DEDICATED_EFFECT, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT },
  69. { "dedicated", DEDICATED_EFFECT, AL_EFFECT_DEDICATED_DIALOGUE },
  70. { "convolution", CONVOLUTION_EFFECT, AL_EFFECT_CONVOLUTION_SOFT },
  71. }};
  72. namespace {
  73. using SubListAllocator = al::allocator<std::array<ALeffect,64>>;
  74. constexpr auto GetDefaultProps(ALenum type) noexcept -> const EffectProps&
  75. {
  76. switch(type)
  77. {
  78. case AL_EFFECT_NULL: return NullEffectProps;
  79. case AL_EFFECT_EAXREVERB: return ReverbEffectProps;
  80. case AL_EFFECT_REVERB: return StdReverbEffectProps;
  81. case AL_EFFECT_AUTOWAH: return AutowahEffectProps;
  82. case AL_EFFECT_CHORUS: return ChorusEffectProps;
  83. case AL_EFFECT_COMPRESSOR: return CompressorEffectProps;
  84. case AL_EFFECT_DISTORTION: return DistortionEffectProps;
  85. case AL_EFFECT_ECHO: return EchoEffectProps;
  86. case AL_EFFECT_EQUALIZER: return EqualizerEffectProps;
  87. case AL_EFFECT_FLANGER: return FlangerEffectProps;
  88. case AL_EFFECT_FREQUENCY_SHIFTER: return FshifterEffectProps;
  89. case AL_EFFECT_RING_MODULATOR: return ModulatorEffectProps;
  90. case AL_EFFECT_PITCH_SHIFTER: return PshifterEffectProps;
  91. case AL_EFFECT_VOCAL_MORPHER: return VmorpherEffectProps;
  92. case AL_EFFECT_DEDICATED_DIALOGUE: return DedicatedDialogEffectProps;
  93. case AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT: return DedicatedLfeEffectProps;
  94. case AL_EFFECT_CONVOLUTION_SOFT: return ConvolutionEffectProps;
  95. }
  96. return NullEffectProps;
  97. }
  98. void InitEffectParams(ALeffect *effect, ALenum type) noexcept
  99. {
  100. switch(type)
  101. {
  102. case AL_EFFECT_NULL: effect->PropsVariant.emplace<NullEffectHandler>(); break;
  103. case AL_EFFECT_EAXREVERB: effect->PropsVariant.emplace<ReverbEffectHandler>(); break;
  104. case AL_EFFECT_REVERB: effect->PropsVariant.emplace<StdReverbEffectHandler>(); break;
  105. case AL_EFFECT_AUTOWAH: effect->PropsVariant.emplace<AutowahEffectHandler>(); break;
  106. case AL_EFFECT_CHORUS: effect->PropsVariant.emplace<ChorusEffectHandler>(); break;
  107. case AL_EFFECT_COMPRESSOR: effect->PropsVariant.emplace<CompressorEffectHandler>(); break;
  108. case AL_EFFECT_DISTORTION: effect->PropsVariant.emplace<DistortionEffectHandler>(); break;
  109. case AL_EFFECT_ECHO: effect->PropsVariant.emplace<EchoEffectHandler>(); break;
  110. case AL_EFFECT_EQUALIZER: effect->PropsVariant.emplace<EqualizerEffectHandler>(); break;
  111. case AL_EFFECT_FLANGER: effect->PropsVariant.emplace<ChorusEffectHandler>(); break;
  112. case AL_EFFECT_FREQUENCY_SHIFTER: effect->PropsVariant.emplace<FshifterEffectHandler>(); break;
  113. case AL_EFFECT_RING_MODULATOR: effect->PropsVariant.emplace<ModulatorEffectHandler>(); break;
  114. case AL_EFFECT_PITCH_SHIFTER: effect->PropsVariant.emplace<PshifterEffectHandler>(); break;
  115. case AL_EFFECT_VOCAL_MORPHER: effect->PropsVariant.emplace<VmorpherEffectHandler>(); break;
  116. case AL_EFFECT_DEDICATED_DIALOGUE:
  117. effect->PropsVariant.emplace<DedicatedDialogEffectHandler>();
  118. break;
  119. case AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT:
  120. effect->PropsVariant.emplace<DedicatedLfeEffectHandler>();
  121. break;
  122. case AL_EFFECT_CONVOLUTION_SOFT:
  123. effect->PropsVariant.emplace<ConvolutionEffectHandler>();
  124. break;
  125. }
  126. effect->Props = GetDefaultProps(type);
  127. effect->type = type;
  128. }
  129. [[nodiscard]]
  130. auto EnsureEffects(al::Device *device, size_t needed) noexcept -> bool
  131. try {
  132. size_t count{std::accumulate(device->EffectList.cbegin(), device->EffectList.cend(), 0_uz,
  133. [](size_t cur, const EffectSubList &sublist) noexcept -> size_t
  134. { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
  135. while(needed > count)
  136. {
  137. if(device->EffectList.size() >= 1<<25) UNLIKELY
  138. return false;
  139. EffectSubList sublist{};
  140. sublist.FreeMask = ~0_u64;
  141. sublist.Effects = SubListAllocator{}.allocate(1);
  142. device->EffectList.emplace_back(std::move(sublist));
  143. count += std::tuple_size_v<SubListAllocator::value_type>;
  144. }
  145. return true;
  146. }
  147. catch(...) {
  148. return false;
  149. }
  150. [[nodiscard]]
  151. auto AllocEffect(al::Device *device) noexcept -> ALeffect*
  152. {
  153. auto sublist = std::find_if(device->EffectList.begin(), device->EffectList.end(),
  154. [](const EffectSubList &entry) noexcept -> bool
  155. { return entry.FreeMask != 0; });
  156. auto lidx = static_cast<ALuint>(std::distance(device->EffectList.begin(), sublist));
  157. auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
  158. ASSUME(slidx < 64);
  159. ALeffect *effect{al::construct_at(al::to_address(sublist->Effects->begin() + slidx))};
  160. InitEffectParams(effect, AL_EFFECT_NULL);
  161. /* Add 1 to avoid effect ID 0. */
  162. effect->id = ((lidx<<6) | slidx) + 1;
  163. sublist->FreeMask &= ~(1_u64 << slidx);
  164. return effect;
  165. }
  166. void FreeEffect(al::Device *device, ALeffect *effect)
  167. {
  168. device->mEffectNames.erase(effect->id);
  169. const ALuint id{effect->id - 1};
  170. const size_t lidx{id >> 6};
  171. const ALuint slidx{id & 0x3f};
  172. std::destroy_at(effect);
  173. device->EffectList[lidx].FreeMask |= 1_u64 << slidx;
  174. }
  175. [[nodiscard]] inline
  176. auto LookupEffect(al::Device *device, ALuint id) noexcept -> ALeffect*
  177. {
  178. const size_t lidx{(id-1) >> 6};
  179. const ALuint slidx{(id-1) & 0x3f};
  180. if(lidx >= device->EffectList.size()) UNLIKELY
  181. return nullptr;
  182. EffectSubList &sublist = device->EffectList[lidx];
  183. if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
  184. return nullptr;
  185. return al::to_address(sublist.Effects->begin() + slidx);
  186. }
  187. } // namespace
  188. AL_API DECL_FUNC2(void, alGenEffects, ALsizei,n, ALuint*,effects)
  189. FORCE_ALIGN void AL_APIENTRY alGenEffectsDirect(ALCcontext *context, ALsizei n, ALuint *effects) noexcept
  190. try {
  191. if(n < 0)
  192. context->throw_error(AL_INVALID_VALUE, "Generating {} effects", n);
  193. if(n <= 0) UNLIKELY return;
  194. auto *device = context->mALDevice.get();
  195. auto effectlock = std::lock_guard{device->EffectLock};
  196. const al::span eids{effects, static_cast<ALuint>(n)};
  197. if(!EnsureEffects(device, eids.size()))
  198. context->throw_error(AL_OUT_OF_MEMORY, "Failed to allocate {} effect{}", n,
  199. (n==1) ? "" : "s");
  200. std::generate(eids.begin(), eids.end(), [device]{ return AllocEffect(device)->id; });
  201. }
  202. catch(al::base_exception&) {
  203. }
  204. catch(std::exception &e) {
  205. ERR("Caught exception: {}", e.what());
  206. }
  207. AL_API DECL_FUNC2(void, alDeleteEffects, ALsizei,n, const ALuint*,effects)
  208. FORCE_ALIGN void AL_APIENTRY alDeleteEffectsDirect(ALCcontext *context, ALsizei n,
  209. const ALuint *effects) noexcept
  210. try {
  211. if(n < 0)
  212. context->throw_error(AL_INVALID_VALUE, "Deleting {} effects", n);
  213. if(n <= 0) UNLIKELY return;
  214. auto *device = context->mALDevice.get();
  215. auto effectlock = std::lock_guard{device->EffectLock};
  216. /* First try to find any effects that are invalid. */
  217. auto validate_effect = [device](const ALuint eid) -> bool
  218. { return !eid || LookupEffect(device, eid) != nullptr; };
  219. const al::span eids{effects, static_cast<ALuint>(n)};
  220. auto inveffect = std::find_if_not(eids.begin(), eids.end(), validate_effect);
  221. if(inveffect != eids.end())
  222. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", *inveffect);
  223. /* All good. Delete non-0 effect IDs. */
  224. auto delete_effect = [device](ALuint eid) -> void
  225. {
  226. if(ALeffect *effect{eid ? LookupEffect(device, eid) : nullptr})
  227. FreeEffect(device, effect);
  228. };
  229. std::for_each(eids.begin(), eids.end(), delete_effect);
  230. }
  231. catch(al::base_exception&) {
  232. }
  233. catch(std::exception &e) {
  234. ERR("Caught exception: {}", e.what());
  235. }
  236. AL_API DECL_FUNC1(ALboolean, alIsEffect, ALuint,effect)
  237. FORCE_ALIGN ALboolean AL_APIENTRY alIsEffectDirect(ALCcontext *context, ALuint effect) noexcept
  238. {
  239. auto *device = context->mALDevice.get();
  240. auto effectlock = std::lock_guard{device->EffectLock};
  241. if(!effect || LookupEffect(device, effect))
  242. return AL_TRUE;
  243. return AL_FALSE;
  244. }
  245. AL_API DECL_FUNC3(void, alEffecti, ALuint,effect, ALenum,param, ALint,value)
  246. FORCE_ALIGN void AL_APIENTRY alEffectiDirect(ALCcontext *context, ALuint effect, ALenum param,
  247. ALint value) noexcept
  248. try {
  249. auto *device = context->mALDevice.get();
  250. auto effectlock = std::lock_guard{device->EffectLock};
  251. ALeffect *aleffect{LookupEffect(device, effect)};
  252. if(!aleffect)
  253. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  254. switch(param)
  255. {
  256. case AL_EFFECT_TYPE:
  257. if(value != AL_EFFECT_NULL)
  258. {
  259. auto check_effect = [value](const EffectList &item) -> bool
  260. { return value == item.val && !DisabledEffects.test(item.type); };
  261. if(!std::any_of(gEffectList.cbegin(), gEffectList.cend(), check_effect))
  262. context->throw_error(AL_INVALID_VALUE, "Effect type {:#04x} not supported",
  263. as_unsigned(value));
  264. }
  265. InitEffectParams(aleffect, value);
  266. return;
  267. }
  268. /* Call the appropriate handler */
  269. std::visit([context,aleffect,param,value](auto &arg)
  270. {
  271. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  272. using PropType = typename Type::prop_type;
  273. return arg.SetParami(context, std::get<PropType>(aleffect->Props), param, value);
  274. }, aleffect->PropsVariant);
  275. }
  276. catch(al::base_exception&) {
  277. }
  278. catch(std::exception &e) {
  279. ERR("Caught exception: {}", e.what());
  280. }
  281. AL_API DECL_FUNC3(void, alEffectiv, ALuint,effect, ALenum,param, const ALint*,values)
  282. FORCE_ALIGN void AL_APIENTRY alEffectivDirect(ALCcontext *context, ALuint effect, ALenum param,
  283. const ALint *values) noexcept
  284. try {
  285. switch(param)
  286. {
  287. case AL_EFFECT_TYPE:
  288. alEffectiDirect(context, effect, param, *values);
  289. return;
  290. }
  291. auto *device = context->mALDevice.get();
  292. auto effectlock = std::lock_guard{device->EffectLock};
  293. ALeffect *aleffect{LookupEffect(device, effect)};
  294. if(!aleffect)
  295. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  296. /* Call the appropriate handler */
  297. std::visit([context,aleffect,param,values](auto &arg)
  298. {
  299. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  300. using PropType = typename Type::prop_type;
  301. return arg.SetParamiv(context, std::get<PropType>(aleffect->Props), param, values);
  302. }, aleffect->PropsVariant);
  303. }
  304. catch(al::base_exception&) {
  305. }
  306. catch(std::exception &e) {
  307. ERR("Caught exception: {}", e.what());
  308. }
  309. AL_API DECL_FUNC3(void, alEffectf, ALuint,effect, ALenum,param, ALfloat,value)
  310. FORCE_ALIGN void AL_APIENTRY alEffectfDirect(ALCcontext *context, ALuint effect, ALenum param,
  311. ALfloat value) noexcept
  312. try {
  313. auto *device = context->mALDevice.get();
  314. auto effectlock = std::lock_guard{device->EffectLock};
  315. ALeffect *aleffect{LookupEffect(device, effect)};
  316. if(!aleffect)
  317. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  318. /* Call the appropriate handler */
  319. std::visit([context,aleffect,param,value](auto &arg)
  320. {
  321. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  322. using PropType = typename Type::prop_type;
  323. return arg.SetParamf(context, std::get<PropType>(aleffect->Props), param, value);
  324. }, aleffect->PropsVariant);
  325. }
  326. catch(al::base_exception&) {
  327. }
  328. catch(std::exception &e) {
  329. ERR("Caught exception: {}", e.what());
  330. }
  331. AL_API DECL_FUNC3(void, alEffectfv, ALuint,effect, ALenum,param, const ALfloat*,values)
  332. FORCE_ALIGN void AL_APIENTRY alEffectfvDirect(ALCcontext *context, ALuint effect, ALenum param,
  333. const ALfloat *values) noexcept
  334. try {
  335. auto *device = context->mALDevice.get();
  336. auto effectlock = std::lock_guard{device->EffectLock};
  337. ALeffect *aleffect{LookupEffect(device, effect)};
  338. if(!aleffect)
  339. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  340. /* Call the appropriate handler */
  341. std::visit([context,aleffect,param,values](auto &arg)
  342. {
  343. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  344. using PropType = typename Type::prop_type;
  345. return arg.SetParamfv(context, std::get<PropType>(aleffect->Props), param, values);
  346. }, aleffect->PropsVariant);
  347. }
  348. catch(al::base_exception&) {
  349. }
  350. catch(std::exception &e) {
  351. ERR("Caught exception: {}", e.what());
  352. }
  353. AL_API DECL_FUNC3(void, alGetEffecti, ALuint,effect, ALenum,param, ALint*,value)
  354. FORCE_ALIGN void AL_APIENTRY alGetEffectiDirect(ALCcontext *context, ALuint effect, ALenum param,
  355. ALint *value) noexcept
  356. try {
  357. auto *device = context->mALDevice.get();
  358. auto effectlock = std::lock_guard{device->EffectLock};
  359. const ALeffect *aleffect{LookupEffect(device, effect)};
  360. if(!aleffect)
  361. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  362. switch(param)
  363. {
  364. case AL_EFFECT_TYPE:
  365. *value = aleffect->type;
  366. return;
  367. }
  368. /* Call the appropriate handler */
  369. std::visit([context,aleffect,param,value](auto &arg)
  370. {
  371. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  372. using PropType = typename Type::prop_type;
  373. return arg.GetParami(context, std::get<PropType>(aleffect->Props), param, value);
  374. }, aleffect->PropsVariant);
  375. }
  376. catch(al::base_exception&) {
  377. }
  378. catch(std::exception &e) {
  379. ERR("Caught exception: {}", e.what());
  380. }
  381. AL_API DECL_FUNC3(void, alGetEffectiv, ALuint,effect, ALenum,param, ALint*,values)
  382. FORCE_ALIGN void AL_APIENTRY alGetEffectivDirect(ALCcontext *context, ALuint effect, ALenum param,
  383. ALint *values) noexcept
  384. try {
  385. switch(param)
  386. {
  387. case AL_EFFECT_TYPE:
  388. alGetEffectiDirect(context, effect, param, values);
  389. return;
  390. }
  391. auto *device = context->mALDevice.get();
  392. auto effectlock = std::lock_guard{device->EffectLock};
  393. const ALeffect *aleffect{LookupEffect(device, effect)};
  394. if(!aleffect)
  395. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  396. /* Call the appropriate handler */
  397. std::visit([context,aleffect,param,values](auto &arg)
  398. {
  399. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  400. using PropType = typename Type::prop_type;
  401. return arg.GetParamiv(context, std::get<PropType>(aleffect->Props), param, values);
  402. }, aleffect->PropsVariant);
  403. }
  404. catch(al::base_exception&) {
  405. }
  406. catch(std::exception &e) {
  407. ERR("Caught exception: {}", e.what());
  408. }
  409. AL_API DECL_FUNC3(void, alGetEffectf, ALuint,effect, ALenum,param, ALfloat*,value)
  410. FORCE_ALIGN void AL_APIENTRY alGetEffectfDirect(ALCcontext *context, ALuint effect, ALenum param,
  411. ALfloat *value) noexcept
  412. try {
  413. auto *device = context->mALDevice.get();
  414. auto effectlock = std::lock_guard{device->EffectLock};
  415. const ALeffect *aleffect{LookupEffect(device, effect)};
  416. if(!aleffect)
  417. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  418. /* Call the appropriate handler */
  419. std::visit([context,aleffect,param,value](auto &arg)
  420. {
  421. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  422. using PropType = typename Type::prop_type;
  423. return arg.GetParamf(context, std::get<PropType>(aleffect->Props), param, value);
  424. }, aleffect->PropsVariant);
  425. }
  426. catch(al::base_exception&) {
  427. }
  428. catch(std::exception &e) {
  429. ERR("Caught exception: {}", e.what());
  430. }
  431. AL_API DECL_FUNC3(void, alGetEffectfv, ALuint,effect, ALenum,param, ALfloat*,values)
  432. FORCE_ALIGN void AL_APIENTRY alGetEffectfvDirect(ALCcontext *context, ALuint effect, ALenum param,
  433. ALfloat *values) noexcept
  434. try {
  435. auto *device = context->mALDevice.get();
  436. auto effectlock = std::lock_guard{device->EffectLock};
  437. const ALeffect *aleffect{LookupEffect(device, effect)};
  438. if(!aleffect)
  439. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", effect);
  440. /* Call the appropriate handler */
  441. std::visit([context,aleffect,param,values](auto &arg)
  442. {
  443. using Type = std::remove_cv_t<std::remove_reference_t<decltype(arg)>>;
  444. using PropType = typename Type::prop_type;
  445. return arg.GetParamfv(context, std::get<PropType>(aleffect->Props), param, values);
  446. }, aleffect->PropsVariant);
  447. }
  448. catch(al::base_exception&) {
  449. }
  450. catch(std::exception &e) {
  451. ERR("Caught exception: {}", e.what());
  452. }
  453. void InitEffect(ALeffect *effect)
  454. {
  455. InitEffectParams(effect, AL_EFFECT_NULL);
  456. }
  457. void ALeffect::SetName(ALCcontext* context, ALuint id, std::string_view name)
  458. {
  459. auto *device = context->mALDevice.get();
  460. auto effectlock = std::lock_guard{device->EffectLock};
  461. auto effect = LookupEffect(device, id);
  462. if(!effect)
  463. context->throw_error(AL_INVALID_NAME, "Invalid effect ID {}", id);
  464. device->mEffectNames.insert_or_assign(id, name);
  465. }
  466. EffectSubList::~EffectSubList()
  467. {
  468. if(!Effects)
  469. return;
  470. uint64_t usemask{~FreeMask};
  471. while(usemask)
  472. {
  473. const int idx{al::countr_zero(usemask)};
  474. std::destroy_at(al::to_address(Effects->begin()+idx));
  475. usemask &= ~(1_u64 << idx);
  476. }
  477. FreeMask = ~usemask;
  478. SubListAllocator{}.deallocate(Effects, 1);
  479. Effects = nullptr;
  480. }
  481. struct EffectPreset {
  482. const char name[32]; /* NOLINT(*-avoid-c-arrays) */
  483. EFXEAXREVERBPROPERTIES props;
  484. };
  485. #define DECL(x) EffectPreset{#x, EFX_REVERB_PRESET_##x}
  486. static constexpr std::array reverblist{
  487. DECL(GENERIC),
  488. DECL(PADDEDCELL),
  489. DECL(ROOM),
  490. DECL(BATHROOM),
  491. DECL(LIVINGROOM),
  492. DECL(STONEROOM),
  493. DECL(AUDITORIUM),
  494. DECL(CONCERTHALL),
  495. DECL(CAVE),
  496. DECL(ARENA),
  497. DECL(HANGAR),
  498. DECL(CARPETEDHALLWAY),
  499. DECL(HALLWAY),
  500. DECL(STONECORRIDOR),
  501. DECL(ALLEY),
  502. DECL(FOREST),
  503. DECL(CITY),
  504. DECL(MOUNTAINS),
  505. DECL(QUARRY),
  506. DECL(PLAIN),
  507. DECL(PARKINGLOT),
  508. DECL(SEWERPIPE),
  509. DECL(UNDERWATER),
  510. DECL(DRUGGED),
  511. DECL(DIZZY),
  512. DECL(PSYCHOTIC),
  513. DECL(CASTLE_SMALLROOM),
  514. DECL(CASTLE_SHORTPASSAGE),
  515. DECL(CASTLE_MEDIUMROOM),
  516. DECL(CASTLE_LARGEROOM),
  517. DECL(CASTLE_LONGPASSAGE),
  518. DECL(CASTLE_HALL),
  519. DECL(CASTLE_CUPBOARD),
  520. DECL(CASTLE_COURTYARD),
  521. DECL(CASTLE_ALCOVE),
  522. DECL(FACTORY_SMALLROOM),
  523. DECL(FACTORY_SHORTPASSAGE),
  524. DECL(FACTORY_MEDIUMROOM),
  525. DECL(FACTORY_LARGEROOM),
  526. DECL(FACTORY_LONGPASSAGE),
  527. DECL(FACTORY_HALL),
  528. DECL(FACTORY_CUPBOARD),
  529. DECL(FACTORY_COURTYARD),
  530. DECL(FACTORY_ALCOVE),
  531. DECL(ICEPALACE_SMALLROOM),
  532. DECL(ICEPALACE_SHORTPASSAGE),
  533. DECL(ICEPALACE_MEDIUMROOM),
  534. DECL(ICEPALACE_LARGEROOM),
  535. DECL(ICEPALACE_LONGPASSAGE),
  536. DECL(ICEPALACE_HALL),
  537. DECL(ICEPALACE_CUPBOARD),
  538. DECL(ICEPALACE_COURTYARD),
  539. DECL(ICEPALACE_ALCOVE),
  540. DECL(SPACESTATION_SMALLROOM),
  541. DECL(SPACESTATION_SHORTPASSAGE),
  542. DECL(SPACESTATION_MEDIUMROOM),
  543. DECL(SPACESTATION_LARGEROOM),
  544. DECL(SPACESTATION_LONGPASSAGE),
  545. DECL(SPACESTATION_HALL),
  546. DECL(SPACESTATION_CUPBOARD),
  547. DECL(SPACESTATION_ALCOVE),
  548. DECL(WOODEN_SMALLROOM),
  549. DECL(WOODEN_SHORTPASSAGE),
  550. DECL(WOODEN_MEDIUMROOM),
  551. DECL(WOODEN_LARGEROOM),
  552. DECL(WOODEN_LONGPASSAGE),
  553. DECL(WOODEN_HALL),
  554. DECL(WOODEN_CUPBOARD),
  555. DECL(WOODEN_COURTYARD),
  556. DECL(WOODEN_ALCOVE),
  557. DECL(SPORT_EMPTYSTADIUM),
  558. DECL(SPORT_SQUASHCOURT),
  559. DECL(SPORT_SMALLSWIMMINGPOOL),
  560. DECL(SPORT_LARGESWIMMINGPOOL),
  561. DECL(SPORT_GYMNASIUM),
  562. DECL(SPORT_FULLSTADIUM),
  563. DECL(SPORT_STADIUMTANNOY),
  564. DECL(PREFAB_WORKSHOP),
  565. DECL(PREFAB_SCHOOLROOM),
  566. DECL(PREFAB_PRACTISEROOM),
  567. DECL(PREFAB_OUTHOUSE),
  568. DECL(PREFAB_CARAVAN),
  569. DECL(DOME_TOMB),
  570. DECL(PIPE_SMALL),
  571. DECL(DOME_SAINTPAULS),
  572. DECL(PIPE_LONGTHIN),
  573. DECL(PIPE_LARGE),
  574. DECL(PIPE_RESONANT),
  575. DECL(OUTDOORS_BACKYARD),
  576. DECL(OUTDOORS_ROLLINGPLAINS),
  577. DECL(OUTDOORS_DEEPCANYON),
  578. DECL(OUTDOORS_CREEK),
  579. DECL(OUTDOORS_VALLEY),
  580. DECL(MOOD_HEAVEN),
  581. DECL(MOOD_HELL),
  582. DECL(MOOD_MEMORY),
  583. DECL(DRIVING_COMMENTATOR),
  584. DECL(DRIVING_PITGARAGE),
  585. DECL(DRIVING_INCAR_RACER),
  586. DECL(DRIVING_INCAR_SPORTS),
  587. DECL(DRIVING_INCAR_LUXURY),
  588. DECL(DRIVING_FULLGRANDSTAND),
  589. DECL(DRIVING_EMPTYGRANDSTAND),
  590. DECL(DRIVING_TUNNEL),
  591. DECL(CITY_STREETS),
  592. DECL(CITY_SUBWAY),
  593. DECL(CITY_MUSEUM),
  594. DECL(CITY_LIBRARY),
  595. DECL(CITY_UNDERPASS),
  596. DECL(CITY_ABANDONED),
  597. DECL(DUSTYROOM),
  598. DECL(CHAPEL),
  599. DECL(SMALLWATERROOM),
  600. };
  601. #undef DECL
  602. void LoadReverbPreset(const std::string_view name, ALeffect *effect)
  603. {
  604. using namespace std::string_view_literals;
  605. if(al::case_compare(name, "NONE"sv) == 0)
  606. {
  607. InitEffectParams(effect, AL_EFFECT_NULL);
  608. TRACE("Loading reverb '{}'", "NONE");
  609. return;
  610. }
  611. if(!DisabledEffects.test(EAXREVERB_EFFECT))
  612. InitEffectParams(effect, AL_EFFECT_EAXREVERB);
  613. else if(!DisabledEffects.test(REVERB_EFFECT))
  614. InitEffectParams(effect, AL_EFFECT_REVERB);
  615. else
  616. InitEffectParams(effect, AL_EFFECT_NULL);
  617. for(const auto &reverbitem : reverblist)
  618. {
  619. if(al::case_compare(name, std::data(reverbitem.name)) != 0)
  620. continue;
  621. TRACE("Loading reverb '{}'", std::data(reverbitem.name));
  622. const auto &props = reverbitem.props;
  623. auto &dst = std::get<ReverbProps>(effect->Props);
  624. dst.Density = props.flDensity;
  625. dst.Diffusion = props.flDiffusion;
  626. dst.Gain = props.flGain;
  627. dst.GainHF = props.flGainHF;
  628. dst.GainLF = props.flGainLF;
  629. dst.DecayTime = props.flDecayTime;
  630. dst.DecayHFRatio = props.flDecayHFRatio;
  631. dst.DecayLFRatio = props.flDecayLFRatio;
  632. dst.ReflectionsGain = props.flReflectionsGain;
  633. dst.ReflectionsDelay = props.flReflectionsDelay;
  634. dst.ReflectionsPan[0] = props.flReflectionsPan[0];
  635. dst.ReflectionsPan[1] = props.flReflectionsPan[1];
  636. dst.ReflectionsPan[2] = props.flReflectionsPan[2];
  637. dst.LateReverbGain = props.flLateReverbGain;
  638. dst.LateReverbDelay = props.flLateReverbDelay;
  639. dst.LateReverbPan[0] = props.flLateReverbPan[0];
  640. dst.LateReverbPan[1] = props.flLateReverbPan[1];
  641. dst.LateReverbPan[2] = props.flLateReverbPan[2];
  642. dst.EchoTime = props.flEchoTime;
  643. dst.EchoDepth = props.flEchoDepth;
  644. dst.ModulationTime = props.flModulationTime;
  645. dst.ModulationDepth = props.flModulationDepth;
  646. dst.AirAbsorptionGainHF = props.flAirAbsorptionGainHF;
  647. dst.HFReference = props.flHFReference;
  648. dst.LFReference = props.flLFReference;
  649. dst.RoomRolloffFactor = props.flRoomRolloffFactor;
  650. dst.DecayHFLimit = props.iDecayHFLimit ? AL_TRUE : AL_FALSE;
  651. return;
  652. }
  653. WARN("Reverb preset '{}' not found", name);
  654. }
  655. bool IsValidEffectType(ALenum type) noexcept
  656. {
  657. if(type == AL_EFFECT_NULL)
  658. return true;
  659. auto check_effect = [type](const EffectList &item) noexcept -> bool
  660. { return type == item.val && !DisabledEffects.test(item.type); };
  661. return std::any_of(gEffectList.cbegin(), gEffectList.cend(), check_effect);
  662. }