filter.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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 "filter.h"
  22. #include <algorithm>
  23. #include <cstdarg>
  24. #include <cstdint>
  25. #include <cstdio>
  26. #include <iterator>
  27. #include <memory>
  28. #include <mutex>
  29. #include <numeric>
  30. #include <unordered_map>
  31. #include <vector>
  32. #include "AL/al.h"
  33. #include "AL/alc.h"
  34. #include "AL/efx.h"
  35. #include "albit.h"
  36. #include "alc/context.h"
  37. #include "alc/device.h"
  38. #include "almalloc.h"
  39. #include "alnumeric.h"
  40. #include "alspan.h"
  41. #include "core/except.h"
  42. #include "core/logging.h"
  43. #include "direct_defs.h"
  44. #include "intrusive_ptr.h"
  45. #include "opthelpers.h"
  46. namespace {
  47. using SubListAllocator = al::allocator<std::array<ALfilter,64>>;
  48. void InitFilterParams(ALfilter *filter, ALenum type)
  49. {
  50. if(type == AL_FILTER_LOWPASS)
  51. {
  52. filter->Gain = AL_LOWPASS_DEFAULT_GAIN;
  53. filter->GainHF = AL_LOWPASS_DEFAULT_GAINHF;
  54. filter->HFReference = LowPassFreqRef;
  55. filter->GainLF = 1.0f;
  56. filter->LFReference = HighPassFreqRef;
  57. filter->mTypeVariant.emplace<LowpassFilterTable>();
  58. }
  59. else if(type == AL_FILTER_HIGHPASS)
  60. {
  61. filter->Gain = AL_HIGHPASS_DEFAULT_GAIN;
  62. filter->GainHF = 1.0f;
  63. filter->HFReference = LowPassFreqRef;
  64. filter->GainLF = AL_HIGHPASS_DEFAULT_GAINLF;
  65. filter->LFReference = HighPassFreqRef;
  66. filter->mTypeVariant.emplace<HighpassFilterTable>();
  67. }
  68. else if(type == AL_FILTER_BANDPASS)
  69. {
  70. filter->Gain = AL_BANDPASS_DEFAULT_GAIN;
  71. filter->GainHF = AL_BANDPASS_DEFAULT_GAINHF;
  72. filter->HFReference = LowPassFreqRef;
  73. filter->GainLF = AL_BANDPASS_DEFAULT_GAINLF;
  74. filter->LFReference = HighPassFreqRef;
  75. filter->mTypeVariant.emplace<BandpassFilterTable>();
  76. }
  77. else
  78. {
  79. filter->Gain = 1.0f;
  80. filter->GainHF = 1.0f;
  81. filter->HFReference = LowPassFreqRef;
  82. filter->GainLF = 1.0f;
  83. filter->LFReference = HighPassFreqRef;
  84. filter->mTypeVariant.emplace<NullFilterTable>();
  85. }
  86. filter->type = type;
  87. }
  88. [[nodiscard]]
  89. auto EnsureFilters(al::Device *device, size_t needed) noexcept -> bool
  90. try {
  91. size_t count{std::accumulate(device->FilterList.cbegin(), device->FilterList.cend(), 0_uz,
  92. [](size_t cur, const FilterSubList &sublist) noexcept -> size_t
  93. { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
  94. while(needed > count)
  95. {
  96. if(device->FilterList.size() >= 1<<25) UNLIKELY
  97. return false;
  98. FilterSubList sublist{};
  99. sublist.FreeMask = ~0_u64;
  100. sublist.Filters = SubListAllocator{}.allocate(1);
  101. device->FilterList.emplace_back(std::move(sublist));
  102. count += std::tuple_size_v<SubListAllocator::value_type>;
  103. }
  104. return true;
  105. }
  106. catch(...) {
  107. return false;
  108. }
  109. [[nodiscard]]
  110. auto AllocFilter(al::Device *device) noexcept -> ALfilter*
  111. {
  112. auto sublist = std::find_if(device->FilterList.begin(), device->FilterList.end(),
  113. [](const FilterSubList &entry) noexcept -> bool
  114. { return entry.FreeMask != 0; });
  115. auto lidx = static_cast<ALuint>(std::distance(device->FilterList.begin(), sublist));
  116. auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
  117. ASSUME(slidx < 64);
  118. ALfilter *filter{al::construct_at(al::to_address(sublist->Filters->begin() + slidx))};
  119. InitFilterParams(filter, AL_FILTER_NULL);
  120. /* Add 1 to avoid filter ID 0. */
  121. filter->id = ((lidx<<6) | slidx) + 1;
  122. sublist->FreeMask &= ~(1_u64 << slidx);
  123. return filter;
  124. }
  125. void FreeFilter(al::Device *device, ALfilter *filter)
  126. {
  127. device->mFilterNames.erase(filter->id);
  128. const ALuint id{filter->id - 1};
  129. const size_t lidx{id >> 6};
  130. const ALuint slidx{id & 0x3f};
  131. std::destroy_at(filter);
  132. device->FilterList[lidx].FreeMask |= 1_u64 << slidx;
  133. }
  134. [[nodiscard]]
  135. auto LookupFilter(al::Device *device, ALuint id) noexcept -> ALfilter*
  136. {
  137. const size_t lidx{(id-1) >> 6};
  138. const ALuint slidx{(id-1) & 0x3f};
  139. if(lidx >= device->FilterList.size()) UNLIKELY
  140. return nullptr;
  141. FilterSubList &sublist = device->FilterList[lidx];
  142. if(sublist.FreeMask & (1_u64 << slidx)) UNLIKELY
  143. return nullptr;
  144. return al::to_address(sublist.Filters->begin() + slidx);
  145. }
  146. } // namespace
  147. /* Null filter parameter handlers */
  148. template<>
  149. void FilterTable<NullFilterTable>::setParami(ALCcontext *context, ALfilter*, ALenum param, int)
  150. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  151. template<>
  152. void FilterTable<NullFilterTable>::setParamiv(ALCcontext *context, ALfilter*, ALenum param, const int*)
  153. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  154. template<>
  155. void FilterTable<NullFilterTable>::setParamf(ALCcontext *context, ALfilter*, ALenum param, float)
  156. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  157. template<>
  158. void FilterTable<NullFilterTable>::setParamfv(ALCcontext *context, ALfilter*, ALenum param, const float*)
  159. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  160. template<>
  161. void FilterTable<NullFilterTable>::getParami(ALCcontext *context, const ALfilter*, ALenum param, int*)
  162. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  163. template<>
  164. void FilterTable<NullFilterTable>::getParamiv(ALCcontext *context, const ALfilter*, ALenum param, int*)
  165. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  166. template<>
  167. void FilterTable<NullFilterTable>::getParamf(ALCcontext *context, const ALfilter*, ALenum param, float*)
  168. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  169. template<>
  170. void FilterTable<NullFilterTable>::getParamfv(ALCcontext *context, const ALfilter*, ALenum param, float*)
  171. { context->throw_error(AL_INVALID_ENUM, "Invalid null filter property {:#04x}", as_unsigned(param)); }
  172. /* Lowpass parameter handlers */
  173. template<>
  174. void FilterTable<LowpassFilterTable>::setParami(ALCcontext *context, ALfilter*, ALenum param, int)
  175. { context->throw_error(AL_INVALID_ENUM, "Invalid low-pass integer property {:#04x}", as_unsigned(param)); }
  176. template<>
  177. void FilterTable<LowpassFilterTable>::setParamiv(ALCcontext *context, ALfilter *filter, ALenum param, const int *values)
  178. { setParami(context, filter, param, *values); }
  179. template<>
  180. void FilterTable<LowpassFilterTable>::setParamf(ALCcontext *context, ALfilter *filter, ALenum param, float val)
  181. {
  182. switch(param)
  183. {
  184. case AL_LOWPASS_GAIN:
  185. if(!(val >= AL_LOWPASS_MIN_GAIN && val <= AL_LOWPASS_MAX_GAIN))
  186. context->throw_error(AL_INVALID_VALUE, "Low-pass gain {:f} out of range", val);
  187. filter->Gain = val;
  188. return;
  189. case AL_LOWPASS_GAINHF:
  190. if(!(val >= AL_LOWPASS_MIN_GAINHF && val <= AL_LOWPASS_MAX_GAINHF))
  191. context->throw_error(AL_INVALID_VALUE, "Low-pass gainhf {:f} out of range", val);
  192. filter->GainHF = val;
  193. return;
  194. }
  195. context->throw_error(AL_INVALID_ENUM, "Invalid low-pass float property {:#04x}",
  196. as_unsigned(param));
  197. }
  198. template<>
  199. void FilterTable<LowpassFilterTable>::setParamfv(ALCcontext *context, ALfilter *filter, ALenum param, const float *vals)
  200. { setParamf(context, filter, param, *vals); }
  201. template<>
  202. void FilterTable<LowpassFilterTable>::getParami(ALCcontext *context, const ALfilter*, ALenum param, int*)
  203. { context->throw_error(AL_INVALID_ENUM, "Invalid low-pass integer property {:#04x}", as_unsigned(param)); }
  204. template<>
  205. void FilterTable<LowpassFilterTable>::getParamiv(ALCcontext *context, const ALfilter *filter, ALenum param, int *values)
  206. { getParami(context, filter, param, values); }
  207. template<>
  208. void FilterTable<LowpassFilterTable>::getParamf(ALCcontext *context, const ALfilter *filter, ALenum param, float *val)
  209. {
  210. switch(param)
  211. {
  212. case AL_LOWPASS_GAIN: *val = filter->Gain; return;
  213. case AL_LOWPASS_GAINHF: *val = filter->GainHF; return;
  214. }
  215. context->throw_error(AL_INVALID_ENUM, "Invalid low-pass float property {:#04x}",
  216. as_unsigned(param));
  217. }
  218. template<>
  219. void FilterTable<LowpassFilterTable>::getParamfv(ALCcontext *context, const ALfilter *filter, ALenum param, float *vals)
  220. { getParamf(context, filter, param, vals); }
  221. /* Highpass parameter handlers */
  222. template<>
  223. void FilterTable<HighpassFilterTable>::setParami(ALCcontext *context, ALfilter*, ALenum param, int)
  224. { context->throw_error(AL_INVALID_ENUM, "Invalid high-pass integer property {:#04x}", as_unsigned(param)); }
  225. template<>
  226. void FilterTable<HighpassFilterTable>::setParamiv(ALCcontext *context, ALfilter *filter, ALenum param, const int *values)
  227. { setParami(context, filter, param, *values); }
  228. template<>
  229. void FilterTable<HighpassFilterTable>::setParamf(ALCcontext *context, ALfilter *filter, ALenum param, float val)
  230. {
  231. switch(param)
  232. {
  233. case AL_HIGHPASS_GAIN:
  234. if(!(val >= AL_HIGHPASS_MIN_GAIN && val <= AL_HIGHPASS_MAX_GAIN))
  235. context->throw_error(AL_INVALID_VALUE, "High-pass gain {:f} out of range", val);
  236. filter->Gain = val;
  237. return;
  238. case AL_HIGHPASS_GAINLF:
  239. if(!(val >= AL_HIGHPASS_MIN_GAINLF && val <= AL_HIGHPASS_MAX_GAINLF))
  240. context->throw_error(AL_INVALID_VALUE, "High-pass gainlf {:f} out of range", val);
  241. filter->GainLF = val;
  242. return;
  243. }
  244. context->throw_error(AL_INVALID_ENUM, "Invalid high-pass float property {:#04x}",
  245. as_unsigned(param));
  246. }
  247. template<>
  248. void FilterTable<HighpassFilterTable>::setParamfv(ALCcontext *context, ALfilter *filter, ALenum param, const float *vals)
  249. { setParamf(context, filter, param, *vals); }
  250. template<>
  251. void FilterTable<HighpassFilterTable>::getParami(ALCcontext *context, const ALfilter*, ALenum param, int*)
  252. { context->throw_error(AL_INVALID_ENUM, "Invalid high-pass integer property {:#04x}", as_unsigned(param)); }
  253. template<>
  254. void FilterTable<HighpassFilterTable>::getParamiv(ALCcontext *context, const ALfilter *filter, ALenum param, int *values)
  255. { getParami(context, filter, param, values); }
  256. template<>
  257. void FilterTable<HighpassFilterTable>::getParamf(ALCcontext *context, const ALfilter *filter, ALenum param, float *val)
  258. {
  259. switch(param)
  260. {
  261. case AL_HIGHPASS_GAIN: *val = filter->Gain; return;
  262. case AL_HIGHPASS_GAINLF: *val = filter->GainLF; return;
  263. }
  264. context->throw_error(AL_INVALID_ENUM, "Invalid high-pass float property {:#04x}",
  265. as_unsigned(param));
  266. }
  267. template<>
  268. void FilterTable<HighpassFilterTable>::getParamfv(ALCcontext *context, const ALfilter *filter, ALenum param, float *vals)
  269. { getParamf(context, filter, param, vals); }
  270. /* Bandpass parameter handlers */
  271. template<>
  272. void FilterTable<BandpassFilterTable>::setParami(ALCcontext *context, ALfilter*, ALenum param, int)
  273. { context->throw_error(AL_INVALID_ENUM, "Invalid band-pass integer property {:#04x}", as_unsigned(param)); }
  274. template<>
  275. void FilterTable<BandpassFilterTable>::setParamiv(ALCcontext *context, ALfilter *filter, ALenum param, const int *values)
  276. { setParami(context, filter, param, *values); }
  277. template<>
  278. void FilterTable<BandpassFilterTable>::setParamf(ALCcontext *context, ALfilter *filter, ALenum param, float val)
  279. {
  280. switch(param)
  281. {
  282. case AL_BANDPASS_GAIN:
  283. if(!(val >= AL_BANDPASS_MIN_GAIN && val <= AL_BANDPASS_MAX_GAIN))
  284. context->throw_error(AL_INVALID_VALUE, "Band-pass gain {:f} out of range", val);
  285. filter->Gain = val;
  286. return;
  287. case AL_BANDPASS_GAINHF:
  288. if(!(val >= AL_BANDPASS_MIN_GAINHF && val <= AL_BANDPASS_MAX_GAINHF))
  289. context->throw_error(AL_INVALID_VALUE, "Band-pass gainhf {:f} out of range", val);
  290. filter->GainHF = val;
  291. return;
  292. case AL_BANDPASS_GAINLF:
  293. if(!(val >= AL_BANDPASS_MIN_GAINLF && val <= AL_BANDPASS_MAX_GAINLF))
  294. context->throw_error(AL_INVALID_VALUE, "Band-pass gainlf {:f} out of range", val);
  295. filter->GainLF = val;
  296. return;
  297. }
  298. context->throw_error(AL_INVALID_ENUM, "Invalid band-pass float property {:#04x}",
  299. as_unsigned(param));
  300. }
  301. template<>
  302. void FilterTable<BandpassFilterTable>::setParamfv(ALCcontext *context, ALfilter *filter, ALenum param, const float *vals)
  303. { setParamf(context, filter, param, *vals); }
  304. template<>
  305. void FilterTable<BandpassFilterTable>::getParami(ALCcontext *context, const ALfilter*, ALenum param, int*)
  306. { context->throw_error(AL_INVALID_ENUM, "Invalid band-pass integer property {:#04x}", as_unsigned(param)); }
  307. template<>
  308. void FilterTable<BandpassFilterTable>::getParamiv(ALCcontext *context, const ALfilter *filter, ALenum param, int *values)
  309. { getParami(context, filter, param, values); }
  310. template<>
  311. void FilterTable<BandpassFilterTable>::getParamf(ALCcontext *context, const ALfilter *filter, ALenum param, float *val)
  312. {
  313. switch(param)
  314. {
  315. case AL_BANDPASS_GAIN: *val = filter->Gain; return;
  316. case AL_BANDPASS_GAINHF: *val = filter->GainHF; return;
  317. case AL_BANDPASS_GAINLF: *val = filter->GainLF; return;
  318. }
  319. context->throw_error(AL_INVALID_ENUM, "Invalid band-pass float property {:#04x}",
  320. as_unsigned(param));
  321. }
  322. template<>
  323. void FilterTable<BandpassFilterTable>::getParamfv(ALCcontext *context, const ALfilter *filter, ALenum param, float *vals)
  324. { getParamf(context, filter, param, vals); }
  325. AL_API DECL_FUNC2(void, alGenFilters, ALsizei,n, ALuint*,filters)
  326. FORCE_ALIGN void AL_APIENTRY alGenFiltersDirect(ALCcontext *context, ALsizei n, ALuint *filters) noexcept
  327. try {
  328. if(n < 0)
  329. context->throw_error(AL_INVALID_VALUE, "Generating {} filters", n);
  330. if(n <= 0) UNLIKELY return;
  331. auto *device = context->mALDevice.get();
  332. auto filterlock = std::lock_guard{device->FilterLock};
  333. const al::span fids{filters, static_cast<ALuint>(n)};
  334. if(!EnsureFilters(device, fids.size()))
  335. context->throw_error(AL_OUT_OF_MEMORY, "Failed to allocate {} filter{}", n,
  336. (n==1) ? "" : "s");
  337. std::generate(fids.begin(), fids.end(), [device]{ return AllocFilter(device)->id; });
  338. }
  339. catch(al::base_exception&) {
  340. }
  341. catch(std::exception &e) {
  342. ERR("Caught exception: {}", e.what());
  343. }
  344. AL_API DECL_FUNC2(void, alDeleteFilters, ALsizei,n, const ALuint*,filters)
  345. FORCE_ALIGN void AL_APIENTRY alDeleteFiltersDirect(ALCcontext *context, ALsizei n,
  346. const ALuint *filters) noexcept
  347. try {
  348. if(n < 0)
  349. context->throw_error(AL_INVALID_VALUE, "Deleting {} filters", n);
  350. if(n <= 0) UNLIKELY return;
  351. auto *device = context->mALDevice.get();
  352. auto filterlock = std::lock_guard{device->FilterLock};
  353. /* First try to find any filters that are invalid. */
  354. auto validate_filter = [device](const ALuint fid) -> bool
  355. { return !fid || LookupFilter(device, fid) != nullptr; };
  356. const al::span fids{filters, static_cast<ALuint>(n)};
  357. auto invflt = std::find_if_not(fids.begin(), fids.end(), validate_filter);
  358. if(invflt != fids.end())
  359. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", *invflt);
  360. /* All good. Delete non-0 filter IDs. */
  361. auto delete_filter = [device](const ALuint fid) -> void
  362. {
  363. if(ALfilter *filter{fid ? LookupFilter(device, fid) : nullptr})
  364. FreeFilter(device, filter);
  365. };
  366. std::for_each(fids.begin(), fids.end(), delete_filter);
  367. }
  368. catch(al::base_exception&) {
  369. }
  370. catch(std::exception &e) {
  371. ERR("Caught exception: {}", e.what());
  372. }
  373. AL_API DECL_FUNC1(ALboolean, alIsFilter, ALuint,filter)
  374. FORCE_ALIGN ALboolean AL_APIENTRY alIsFilterDirect(ALCcontext *context, ALuint filter) noexcept
  375. {
  376. auto *device = context->mALDevice.get();
  377. auto filterlock = std::lock_guard{device->FilterLock};
  378. if(!filter || LookupFilter(device, filter))
  379. return AL_TRUE;
  380. return AL_FALSE;
  381. }
  382. AL_API DECL_FUNC3(void, alFilteri, ALuint,filter, ALenum,param, ALint,value)
  383. FORCE_ALIGN void AL_APIENTRY alFilteriDirect(ALCcontext *context, ALuint filter, ALenum param,
  384. ALint value) noexcept
  385. try {
  386. auto *device = context->mALDevice.get();
  387. auto filterlock = std::lock_guard{device->FilterLock};
  388. ALfilter *alfilt{LookupFilter(device, filter)};
  389. if(!alfilt)
  390. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  391. switch(param)
  392. {
  393. case AL_FILTER_TYPE:
  394. if(!(value == AL_FILTER_NULL || value == AL_FILTER_LOWPASS
  395. || value == AL_FILTER_HIGHPASS || value == AL_FILTER_BANDPASS))
  396. context->throw_error(AL_INVALID_VALUE, "Invalid filter type {:#04x}",
  397. as_unsigned(value));
  398. InitFilterParams(alfilt, value);
  399. return;
  400. }
  401. /* Call the appropriate handler */
  402. std::visit([context,alfilt,param,value](auto&& thunk)
  403. { thunk.setParami(context, alfilt, param, value); }, alfilt->mTypeVariant);
  404. }
  405. catch(al::base_exception&) {
  406. }
  407. catch(std::exception &e) {
  408. ERR("Caught exception: {}", e.what());
  409. }
  410. AL_API DECL_FUNC3(void, alFilteriv, ALuint,filter, ALenum,param, const ALint*,values)
  411. FORCE_ALIGN void AL_APIENTRY alFilterivDirect(ALCcontext *context, ALuint filter, ALenum param,
  412. const ALint *values) noexcept
  413. try {
  414. switch(param)
  415. {
  416. case AL_FILTER_TYPE:
  417. alFilteriDirect(context, filter, param, *values);
  418. return;
  419. }
  420. auto *device = context->mALDevice.get();
  421. auto filterlock = std::lock_guard{device->FilterLock};
  422. ALfilter *alfilt{LookupFilter(device, filter)};
  423. if(!alfilt)
  424. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  425. /* Call the appropriate handler */
  426. std::visit([context,alfilt,param,values](auto&& thunk)
  427. { thunk.setParamiv(context, alfilt, param, values); }, alfilt->mTypeVariant);
  428. }
  429. catch(al::base_exception&) {
  430. }
  431. catch(std::exception &e) {
  432. ERR("Caught exception: {}", e.what());
  433. }
  434. AL_API DECL_FUNC3(void, alFilterf, ALuint,filter, ALenum,param, ALfloat,value)
  435. FORCE_ALIGN void AL_APIENTRY alFilterfDirect(ALCcontext *context, ALuint filter, ALenum param,
  436. ALfloat value) noexcept
  437. try {
  438. auto *device = context->mALDevice.get();
  439. auto filterlock = std::lock_guard{device->FilterLock};
  440. ALfilter *alfilt{LookupFilter(device, filter)};
  441. if(!alfilt)
  442. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  443. /* Call the appropriate handler */
  444. std::visit([context,alfilt,param,value](auto&& thunk)
  445. { thunk.setParamf(context, alfilt, param, value); }, alfilt->mTypeVariant);
  446. }
  447. catch(al::base_exception&) {
  448. }
  449. catch(std::exception &e) {
  450. ERR("Caught exception: {}", e.what());
  451. }
  452. AL_API DECL_FUNC3(void, alFilterfv, ALuint,filter, ALenum,param, const ALfloat*,values)
  453. FORCE_ALIGN void AL_APIENTRY alFilterfvDirect(ALCcontext *context, ALuint filter, ALenum param,
  454. const ALfloat *values) noexcept
  455. try {
  456. auto *device = context->mALDevice.get();
  457. auto filterlock = std::lock_guard{device->FilterLock};
  458. ALfilter *alfilt{LookupFilter(device, filter)};
  459. if(!alfilt)
  460. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  461. /* Call the appropriate handler */
  462. std::visit([context,alfilt,param,values](auto&& thunk)
  463. { thunk.setParamfv(context, alfilt, param, values); }, alfilt->mTypeVariant);
  464. }
  465. catch(al::base_exception&) {
  466. }
  467. catch(std::exception &e) {
  468. ERR("Caught exception: {}", e.what());
  469. }
  470. AL_API DECL_FUNC3(void, alGetFilteri, ALuint,filter, ALenum,param, ALint*,value)
  471. FORCE_ALIGN void AL_APIENTRY alGetFilteriDirect(ALCcontext *context, ALuint filter, ALenum param,
  472. ALint *value) noexcept
  473. try {
  474. auto *device = context->mALDevice.get();
  475. auto filterlock = std::lock_guard{device->FilterLock};
  476. const ALfilter *alfilt{LookupFilter(device, filter)};
  477. if(!alfilt)
  478. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  479. switch(param)
  480. {
  481. case AL_FILTER_TYPE: *value = alfilt->type; return;
  482. }
  483. /* Call the appropriate handler */
  484. std::visit([context,alfilt,param,value](auto&& thunk)
  485. { thunk.getParami(context, alfilt, param, value); }, alfilt->mTypeVariant);
  486. }
  487. catch(al::base_exception&) {
  488. }
  489. catch(std::exception &e) {
  490. ERR("Caught exception: {}", e.what());
  491. }
  492. AL_API DECL_FUNC3(void, alGetFilteriv, ALuint,filter, ALenum,param, ALint*,values)
  493. FORCE_ALIGN void AL_APIENTRY alGetFilterivDirect(ALCcontext *context, ALuint filter, ALenum param,
  494. ALint *values) noexcept
  495. try {
  496. switch(param)
  497. {
  498. case AL_FILTER_TYPE:
  499. alGetFilteriDirect(context, filter, param, values);
  500. return;
  501. }
  502. auto *device = context->mALDevice.get();
  503. auto filterlock = std::lock_guard{device->FilterLock};
  504. const ALfilter *alfilt{LookupFilter(device, filter)};
  505. if(!alfilt)
  506. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  507. /* Call the appropriate handler */
  508. std::visit([context,alfilt,param,values](auto&& thunk)
  509. { thunk.getParamiv(context, alfilt, param, values); }, alfilt->mTypeVariant);
  510. }
  511. catch(al::base_exception&) {
  512. }
  513. catch(std::exception &e) {
  514. ERR("Caught exception: {}", e.what());
  515. }
  516. AL_API DECL_FUNC3(void, alGetFilterf, ALuint,filter, ALenum,param, ALfloat*,value)
  517. FORCE_ALIGN void AL_APIENTRY alGetFilterfDirect(ALCcontext *context, ALuint filter, ALenum param,
  518. ALfloat *value) noexcept
  519. try {
  520. auto *device = context->mALDevice.get();
  521. auto filterlock = std::lock_guard{device->FilterLock};
  522. const ALfilter *alfilt{LookupFilter(device, filter)};
  523. if(!alfilt)
  524. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  525. /* Call the appropriate handler */
  526. std::visit([context,alfilt,param,value](auto&& thunk)
  527. { thunk.getParamf(context, alfilt, param, value); }, alfilt->mTypeVariant);
  528. }
  529. catch(al::base_exception&) {
  530. }
  531. catch(std::exception &e) {
  532. ERR("Caught exception: {}", e.what());
  533. }
  534. AL_API DECL_FUNC3(void, alGetFilterfv, ALuint,filter, ALenum,param, ALfloat*,values)
  535. FORCE_ALIGN void AL_APIENTRY alGetFilterfvDirect(ALCcontext *context, ALuint filter, ALenum param,
  536. ALfloat *values) noexcept
  537. try {
  538. auto *device = context->mALDevice.get();
  539. auto filterlock = std::lock_guard{device->FilterLock};
  540. const ALfilter *alfilt{LookupFilter(device, filter)};
  541. if(!alfilt)
  542. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", filter);
  543. /* Call the appropriate handler */
  544. std::visit([context,alfilt,param,values](auto&& thunk)
  545. { thunk.getParamfv(context, alfilt, param, values); }, alfilt->mTypeVariant);
  546. }
  547. catch(al::base_exception&) {
  548. }
  549. catch(std::exception &e) {
  550. ERR("Caught exception: {}", e.what());
  551. }
  552. void ALfilter::SetName(ALCcontext *context, ALuint id, std::string_view name)
  553. {
  554. auto *device = context->mALDevice.get();
  555. auto filterlock = std::lock_guard{device->FilterLock};
  556. auto filter = LookupFilter(device, id);
  557. if(!filter)
  558. context->throw_error(AL_INVALID_NAME, "Invalid filter ID {}", id);
  559. device->mFilterNames.insert_or_assign(id, name);
  560. }
  561. FilterSubList::~FilterSubList()
  562. {
  563. if(!Filters)
  564. return;
  565. uint64_t usemask{~FreeMask};
  566. while(usemask)
  567. {
  568. const int idx{al::countr_zero(usemask)};
  569. std::destroy_at(al::to_address(Filters->begin() + idx));
  570. usemask &= ~(1_u64 << idx);
  571. }
  572. FreeMask = ~usemask;
  573. SubListAllocator{}.deallocate(Filters, 1);
  574. Filters = nullptr;
  575. }