auxeffectslot.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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 "auxeffectslot.h"
  22. #include <algorithm>
  23. #include <cassert>
  24. #include <cstdint>
  25. #include <iterator>
  26. #include <memory>
  27. #include <mutex>
  28. #include <numeric>
  29. #include <thread>
  30. #include "AL/al.h"
  31. #include "AL/alc.h"
  32. #include "AL/efx.h"
  33. #include "albit.h"
  34. #include "alcmain.h"
  35. #include "alcontext.h"
  36. #include "almalloc.h"
  37. #include "alnumeric.h"
  38. #include "alspan.h"
  39. #include "alu.h"
  40. #include "buffer.h"
  41. #include "core/except.h"
  42. #include "core/fpu_ctrl.h"
  43. #include "core/logging.h"
  44. #include "effect.h"
  45. #include "inprogext.h"
  46. #include "opthelpers.h"
  47. namespace {
  48. struct FactoryItem {
  49. EffectSlotType Type;
  50. EffectStateFactory* (&GetFactory)(void);
  51. };
  52. constexpr FactoryItem FactoryList[] = {
  53. { EffectSlotType::None, NullStateFactory_getFactory },
  54. { EffectSlotType::EAXReverb, ReverbStateFactory_getFactory },
  55. { EffectSlotType::Reverb, StdReverbStateFactory_getFactory },
  56. { EffectSlotType::Autowah, AutowahStateFactory_getFactory },
  57. { EffectSlotType::Chorus, ChorusStateFactory_getFactory },
  58. { EffectSlotType::Compressor, CompressorStateFactory_getFactory },
  59. { EffectSlotType::Distortion, DistortionStateFactory_getFactory },
  60. { EffectSlotType::Echo, EchoStateFactory_getFactory },
  61. { EffectSlotType::Equalizer, EqualizerStateFactory_getFactory },
  62. { EffectSlotType::Flanger, FlangerStateFactory_getFactory },
  63. { EffectSlotType::FrequencyShifter, FshifterStateFactory_getFactory },
  64. { EffectSlotType::RingModulator, ModulatorStateFactory_getFactory },
  65. { EffectSlotType::PitchShifter, PshifterStateFactory_getFactory },
  66. { EffectSlotType::VocalMorpher, VmorpherStateFactory_getFactory },
  67. { EffectSlotType::DedicatedDialog, DedicatedStateFactory_getFactory },
  68. { EffectSlotType::DedicatedLFE, DedicatedStateFactory_getFactory },
  69. { EffectSlotType::Convolution, ConvolutionStateFactory_getFactory },
  70. };
  71. EffectStateFactory *getFactoryByType(EffectSlotType type)
  72. {
  73. auto iter = std::find_if(std::begin(FactoryList), std::end(FactoryList),
  74. [type](const FactoryItem &item) noexcept -> bool
  75. { return item.Type == type; });
  76. return (iter != std::end(FactoryList)) ? iter->GetFactory() : nullptr;
  77. }
  78. inline ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id) noexcept
  79. {
  80. const size_t lidx{(id-1) >> 6};
  81. const ALuint slidx{(id-1) & 0x3f};
  82. if UNLIKELY(lidx >= context->mEffectSlotList.size())
  83. return nullptr;
  84. EffectSlotSubList &sublist{context->mEffectSlotList[lidx]};
  85. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  86. return nullptr;
  87. return sublist.EffectSlots + slidx;
  88. }
  89. inline ALeffect *LookupEffect(ALCdevice *device, ALuint id) noexcept
  90. {
  91. const size_t lidx{(id-1) >> 6};
  92. const ALuint slidx{(id-1) & 0x3f};
  93. if UNLIKELY(lidx >= device->EffectList.size())
  94. return nullptr;
  95. EffectSubList &sublist = device->EffectList[lidx];
  96. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  97. return nullptr;
  98. return sublist.Effects + slidx;
  99. }
  100. inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id) noexcept
  101. {
  102. const size_t lidx{(id-1) >> 6};
  103. const ALuint slidx{(id-1) & 0x3f};
  104. if UNLIKELY(lidx >= device->BufferList.size())
  105. return nullptr;
  106. BufferSubList &sublist = device->BufferList[lidx];
  107. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  108. return nullptr;
  109. return sublist.Buffers + slidx;
  110. }
  111. inline auto GetEffectBuffer(ALbuffer *buffer) noexcept -> EffectState::Buffer
  112. {
  113. if(!buffer) return EffectState::Buffer{};
  114. return EffectState::Buffer{buffer, buffer->mData};
  115. }
  116. void AddActiveEffectSlots(const al::span<ALeffectslot*> auxslots, ALCcontext *context)
  117. {
  118. if(auxslots.empty()) return;
  119. EffectSlotArray *curarray{context->mActiveAuxSlots.load(std::memory_order_acquire)};
  120. size_t newcount{curarray->size() + auxslots.size()};
  121. /* Insert the new effect slots into the head of the array, followed by the
  122. * existing ones.
  123. */
  124. EffectSlotArray *newarray = EffectSlot::CreatePtrArray(newcount);
  125. auto slotiter = std::transform(auxslots.begin(), auxslots.end(), newarray->begin(),
  126. [](ALeffectslot *auxslot) noexcept { return &auxslot->mSlot; });
  127. std::copy(curarray->begin(), curarray->end(), slotiter);
  128. /* Remove any duplicates (first instance of each will be kept). */
  129. auto last = newarray->end();
  130. for(auto start=newarray->begin()+1;;)
  131. {
  132. last = std::remove(start, last, *(start-1));
  133. if(start == last) break;
  134. ++start;
  135. }
  136. newcount = static_cast<size_t>(std::distance(newarray->begin(), last));
  137. /* Reallocate newarray if the new size ended up smaller from duplicate
  138. * removal.
  139. */
  140. if UNLIKELY(newcount < newarray->size())
  141. {
  142. curarray = newarray;
  143. newarray = EffectSlot::CreatePtrArray(newcount);
  144. std::copy_n(curarray->begin(), newcount, newarray->begin());
  145. delete curarray;
  146. curarray = nullptr;
  147. }
  148. std::uninitialized_fill_n(newarray->end(), newcount, nullptr);
  149. curarray = context->mActiveAuxSlots.exchange(newarray, std::memory_order_acq_rel);
  150. context->mDevice->waitForMix();
  151. al::destroy_n(curarray->end(), curarray->size());
  152. delete curarray;
  153. }
  154. void RemoveActiveEffectSlots(const al::span<ALeffectslot*> auxslots, ALCcontext *context)
  155. {
  156. if(auxslots.empty()) return;
  157. EffectSlotArray *curarray{context->mActiveAuxSlots.load(std::memory_order_acquire)};
  158. /* Don't shrink the allocated array size since we don't know how many (if
  159. * any) of the effect slots to remove are in the array.
  160. */
  161. EffectSlotArray *newarray = EffectSlot::CreatePtrArray(curarray->size());
  162. auto new_end = std::copy(curarray->begin(), curarray->end(), newarray->begin());
  163. /* Remove elements from newarray that match any ID in slotids. */
  164. for(const ALeffectslot *auxslot : auxslots)
  165. {
  166. auto slot_match = [auxslot](EffectSlot *slot) noexcept -> bool
  167. { return (slot == &auxslot->mSlot); };
  168. new_end = std::remove_if(newarray->begin(), new_end, slot_match);
  169. }
  170. /* Reallocate with the new size. */
  171. auto newsize = static_cast<size_t>(std::distance(newarray->begin(), new_end));
  172. if LIKELY(newsize != newarray->size())
  173. {
  174. curarray = newarray;
  175. newarray = EffectSlot::CreatePtrArray(newsize);
  176. std::copy_n(curarray->begin(), newsize, newarray->begin());
  177. delete curarray;
  178. curarray = nullptr;
  179. }
  180. std::uninitialized_fill_n(newarray->end(), newsize, nullptr);
  181. curarray = context->mActiveAuxSlots.exchange(newarray, std::memory_order_acq_rel);
  182. context->mDevice->waitForMix();
  183. al::destroy_n(curarray->end(), curarray->size());
  184. delete curarray;
  185. }
  186. EffectSlotType EffectSlotTypeFromEnum(ALenum type)
  187. {
  188. switch(type)
  189. {
  190. case AL_EFFECT_NULL: return EffectSlotType::None;
  191. case AL_EFFECT_REVERB: return EffectSlotType::Reverb;
  192. case AL_EFFECT_CHORUS: return EffectSlotType::Chorus;
  193. case AL_EFFECT_DISTORTION: return EffectSlotType::Distortion;
  194. case AL_EFFECT_ECHO: return EffectSlotType::Echo;
  195. case AL_EFFECT_FLANGER: return EffectSlotType::Flanger;
  196. case AL_EFFECT_FREQUENCY_SHIFTER: return EffectSlotType::FrequencyShifter;
  197. case AL_EFFECT_VOCAL_MORPHER: return EffectSlotType::VocalMorpher;
  198. case AL_EFFECT_PITCH_SHIFTER: return EffectSlotType::PitchShifter;
  199. case AL_EFFECT_RING_MODULATOR: return EffectSlotType::RingModulator;
  200. case AL_EFFECT_AUTOWAH: return EffectSlotType::Autowah;
  201. case AL_EFFECT_COMPRESSOR: return EffectSlotType::Compressor;
  202. case AL_EFFECT_EQUALIZER: return EffectSlotType::Equalizer;
  203. case AL_EFFECT_EAXREVERB: return EffectSlotType::EAXReverb;
  204. case AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT: return EffectSlotType::DedicatedLFE;
  205. case AL_EFFECT_DEDICATED_DIALOGUE: return EffectSlotType::DedicatedDialog;
  206. case AL_EFFECT_CONVOLUTION_REVERB_SOFT: return EffectSlotType::Convolution;
  207. }
  208. ERR("Unhandled effect enum: 0x%04x\n", type);
  209. return EffectSlotType::None;
  210. }
  211. bool EnsureEffectSlots(ALCcontext *context, size_t needed)
  212. {
  213. size_t count{std::accumulate(context->mEffectSlotList.cbegin(),
  214. context->mEffectSlotList.cend(), size_t{0},
  215. [](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t
  216. { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
  217. while(needed > count)
  218. {
  219. if UNLIKELY(context->mEffectSlotList.size() >= 1<<25)
  220. return false;
  221. context->mEffectSlotList.emplace_back();
  222. auto sublist = context->mEffectSlotList.end() - 1;
  223. sublist->FreeMask = ~0_u64;
  224. sublist->EffectSlots = static_cast<ALeffectslot*>(
  225. al_calloc(alignof(ALeffectslot), sizeof(ALeffectslot)*64));
  226. if UNLIKELY(!sublist->EffectSlots)
  227. {
  228. context->mEffectSlotList.pop_back();
  229. return false;
  230. }
  231. count += 64;
  232. }
  233. return true;
  234. }
  235. ALeffectslot *AllocEffectSlot(ALCcontext *context)
  236. {
  237. auto sublist = std::find_if(context->mEffectSlotList.begin(), context->mEffectSlotList.end(),
  238. [](const EffectSlotSubList &entry) noexcept -> bool
  239. { return entry.FreeMask != 0; });
  240. auto lidx = static_cast<ALuint>(std::distance(context->mEffectSlotList.begin(), sublist));
  241. auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
  242. ALeffectslot *slot{::new(sublist->EffectSlots + slidx) ALeffectslot{}};
  243. aluInitEffectPanning(&slot->mSlot, context);
  244. /* Add 1 to avoid source ID 0. */
  245. slot->id = ((lidx<<6) | slidx) + 1;
  246. context->mNumEffectSlots += 1;
  247. sublist->FreeMask &= ~(1_u64 << slidx);
  248. return slot;
  249. }
  250. void FreeEffectSlot(ALCcontext *context, ALeffectslot *slot)
  251. {
  252. const ALuint id{slot->id - 1};
  253. const size_t lidx{id >> 6};
  254. const ALuint slidx{id & 0x3f};
  255. al::destroy_at(slot);
  256. context->mEffectSlotList[lidx].FreeMask |= 1_u64 << slidx;
  257. context->mNumEffectSlots--;
  258. }
  259. #define DO_UPDATEPROPS() do { \
  260. if(!context->mDeferUpdates.load(std::memory_order_acquire) \
  261. && slot->mState == SlotState::Playing) \
  262. slot->updateProps(context.get()); \
  263. else \
  264. slot->PropsClean.clear(std::memory_order_release); \
  265. } while(0)
  266. } // namespace
  267. AL_API void AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
  268. START_API_FUNC
  269. {
  270. ContextRef context{GetContextRef()};
  271. if UNLIKELY(!context) return;
  272. if UNLIKELY(n < 0)
  273. context->setError(AL_INVALID_VALUE, "Generating %d effect slots", n);
  274. if UNLIKELY(n <= 0) return;
  275. std::unique_lock<std::mutex> slotlock{context->mEffectSlotLock};
  276. ALCdevice *device{context->mDevice.get()};
  277. if(static_cast<ALuint>(n) > device->AuxiliaryEffectSlotMax-context->mNumEffectSlots)
  278. {
  279. context->setError(AL_OUT_OF_MEMORY, "Exceeding %u effect slot limit (%u + %d)",
  280. device->AuxiliaryEffectSlotMax, context->mNumEffectSlots, n);
  281. return;
  282. }
  283. if(!EnsureEffectSlots(context.get(), static_cast<ALuint>(n)))
  284. {
  285. context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d effectslot%s", n,
  286. (n==1) ? "" : "s");
  287. return;
  288. }
  289. if(n == 1)
  290. {
  291. ALeffectslot *slot{AllocEffectSlot(context.get())};
  292. if(!slot) return;
  293. effectslots[0] = slot->id;
  294. }
  295. else
  296. {
  297. al::vector<ALuint> ids;
  298. ALsizei count{n};
  299. ids.reserve(static_cast<ALuint>(count));
  300. do {
  301. ALeffectslot *slot{AllocEffectSlot(context.get())};
  302. if(!slot)
  303. {
  304. slotlock.unlock();
  305. alDeleteAuxiliaryEffectSlots(static_cast<ALsizei>(ids.size()), ids.data());
  306. return;
  307. }
  308. ids.emplace_back(slot->id);
  309. } while(--count);
  310. std::copy(ids.cbegin(), ids.cend(), effectslots);
  311. }
  312. }
  313. END_API_FUNC
  314. AL_API void AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, const ALuint *effectslots)
  315. START_API_FUNC
  316. {
  317. ContextRef context{GetContextRef()};
  318. if UNLIKELY(!context) return;
  319. if UNLIKELY(n < 0)
  320. context->setError(AL_INVALID_VALUE, "Deleting %d effect slots", n);
  321. if UNLIKELY(n <= 0) return;
  322. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  323. if(n == 1)
  324. {
  325. ALeffectslot *slot{LookupEffectSlot(context.get(), effectslots[0])};
  326. if UNLIKELY(!slot)
  327. {
  328. context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", effectslots[0]);
  329. return;
  330. }
  331. if UNLIKELY(ReadRef(slot->ref) != 0)
  332. {
  333. context->setError(AL_INVALID_OPERATION, "Deleting in-use effect slot %u",
  334. effectslots[0]);
  335. return;
  336. }
  337. RemoveActiveEffectSlots({&slot, 1u}, context.get());
  338. FreeEffectSlot(context.get(), slot);
  339. }
  340. else
  341. {
  342. auto slots = al::vector<ALeffectslot*>(static_cast<ALuint>(n));
  343. for(size_t i{0};i < slots.size();++i)
  344. {
  345. ALeffectslot *slot{LookupEffectSlot(context.get(), effectslots[i])};
  346. if UNLIKELY(!slot)
  347. {
  348. context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", effectslots[i]);
  349. return;
  350. }
  351. if UNLIKELY(ReadRef(slot->ref) != 0)
  352. {
  353. context->setError(AL_INVALID_OPERATION, "Deleting in-use effect slot %u",
  354. effectslots[i]);
  355. return;
  356. }
  357. slots[i] = slot;
  358. }
  359. /* Remove any duplicates. */
  360. auto slots_end = slots.end();
  361. for(auto start=slots.begin()+1;start != slots_end;++start)
  362. {
  363. slots_end = std::remove(start, slots_end, *(start-1));
  364. if(start == slots_end) break;
  365. }
  366. slots.erase(slots_end, slots.end());
  367. /* All effectslots are valid, remove and delete them */
  368. RemoveActiveEffectSlots(slots, context.get());
  369. for(ALeffectslot *slot : slots)
  370. FreeEffectSlot(context.get(), slot);
  371. }
  372. }
  373. END_API_FUNC
  374. AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
  375. START_API_FUNC
  376. {
  377. ContextRef context{GetContextRef()};
  378. if LIKELY(context)
  379. {
  380. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  381. if(LookupEffectSlot(context.get(), effectslot) != nullptr)
  382. return AL_TRUE;
  383. }
  384. return AL_FALSE;
  385. }
  386. END_API_FUNC
  387. AL_API void AL_APIENTRY alAuxiliaryEffectSlotPlaySOFT(ALuint slotid)
  388. START_API_FUNC
  389. {
  390. ContextRef context{GetContextRef()};
  391. if UNLIKELY(!context) return;
  392. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  393. ALeffectslot *slot{LookupEffectSlot(context.get(), slotid)};
  394. if UNLIKELY(!slot)
  395. {
  396. context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", slotid);
  397. return;
  398. }
  399. if(slot->mState == SlotState::Playing)
  400. return;
  401. slot->PropsClean.test_and_set(std::memory_order_acq_rel);
  402. slot->updateProps(context.get());
  403. AddActiveEffectSlots({&slot, 1}, context.get());
  404. slot->mState = SlotState::Playing;
  405. }
  406. END_API_FUNC
  407. AL_API void AL_APIENTRY alAuxiliaryEffectSlotPlayvSOFT(ALsizei n, const ALuint *slotids)
  408. START_API_FUNC
  409. {
  410. ContextRef context{GetContextRef()};
  411. if UNLIKELY(!context) return;
  412. if UNLIKELY(n < 0)
  413. context->setError(AL_INVALID_VALUE, "Playing %d effect slots", n);
  414. if UNLIKELY(n <= 0) return;
  415. auto slots = al::vector<ALeffectslot*>(static_cast<ALuint>(n));
  416. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  417. for(size_t i{0};i < slots.size();++i)
  418. {
  419. ALeffectslot *slot{LookupEffectSlot(context.get(), slotids[i])};
  420. if UNLIKELY(!slot)
  421. {
  422. context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", slotids[i]);
  423. return;
  424. }
  425. if(slot->mState != SlotState::Playing)
  426. {
  427. slot->PropsClean.test_and_set(std::memory_order_acq_rel);
  428. slot->updateProps(context.get());
  429. }
  430. slots[i] = slot;
  431. };
  432. AddActiveEffectSlots(slots, context.get());
  433. for(auto slot : slots)
  434. slot->mState = SlotState::Playing;
  435. }
  436. END_API_FUNC
  437. AL_API void AL_APIENTRY alAuxiliaryEffectSlotStopSOFT(ALuint slotid)
  438. START_API_FUNC
  439. {
  440. ContextRef context{GetContextRef()};
  441. if UNLIKELY(!context) return;
  442. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  443. ALeffectslot *slot{LookupEffectSlot(context.get(), slotid)};
  444. if UNLIKELY(!slot)
  445. {
  446. context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", slotid);
  447. return;
  448. }
  449. RemoveActiveEffectSlots({&slot, 1}, context.get());
  450. slot->mState = SlotState::Stopped;
  451. }
  452. END_API_FUNC
  453. AL_API void AL_APIENTRY alAuxiliaryEffectSlotStopvSOFT(ALsizei n, const ALuint *slotids)
  454. START_API_FUNC
  455. {
  456. ContextRef context{GetContextRef()};
  457. if UNLIKELY(!context) return;
  458. if UNLIKELY(n < 0)
  459. context->setError(AL_INVALID_VALUE, "Stopping %d effect slots", n);
  460. if UNLIKELY(n <= 0) return;
  461. auto slots = al::vector<ALeffectslot*>(static_cast<ALuint>(n));
  462. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  463. for(size_t i{0};i < slots.size();++i)
  464. {
  465. ALeffectslot *slot{LookupEffectSlot(context.get(), slotids[i])};
  466. if UNLIKELY(!slot)
  467. {
  468. context->setError(AL_INVALID_NAME, "Invalid effect slot ID %u", slotids[i]);
  469. return;
  470. }
  471. slots[i] = slot;
  472. };
  473. RemoveActiveEffectSlots(slots, context.get());
  474. for(auto slot : slots)
  475. slot->mState = SlotState::Stopped;
  476. }
  477. END_API_FUNC
  478. AL_API void AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint value)
  479. START_API_FUNC
  480. {
  481. ContextRef context{GetContextRef()};
  482. if UNLIKELY(!context) return;
  483. std::lock_guard<std::mutex> _{context->mPropLock};
  484. std::lock_guard<std::mutex> __{context->mEffectSlotLock};
  485. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  486. if UNLIKELY(!slot)
  487. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  488. ALeffectslot *target{};
  489. ALCdevice *device{};
  490. ALenum err{};
  491. switch(param)
  492. {
  493. case AL_EFFECTSLOT_EFFECT:
  494. device = context->mDevice.get();
  495. {
  496. std::lock_guard<std::mutex> ___{device->EffectLock};
  497. ALeffect *effect{value ? LookupEffect(device, static_cast<ALuint>(value)) : nullptr};
  498. if(!(value == 0 || effect != nullptr))
  499. SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid effect ID %u", value);
  500. err = slot->initEffect(effect, context.get());
  501. }
  502. if UNLIKELY(err != AL_NO_ERROR)
  503. {
  504. context->setError(err, "Effect initialization failed");
  505. return;
  506. }
  507. if UNLIKELY(slot->mState == SlotState::Initial)
  508. {
  509. AddActiveEffectSlots({&slot, 1}, context.get());
  510. slot->mState = SlotState::Playing;
  511. }
  512. break;
  513. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  514. if(!(value == AL_TRUE || value == AL_FALSE))
  515. SETERR_RETURN(context, AL_INVALID_VALUE,,
  516. "Effect slot auxiliary send auto out of range");
  517. slot->AuxSendAuto = !!value;
  518. break;
  519. case AL_EFFECTSLOT_TARGET_SOFT:
  520. target = LookupEffectSlot(context.get(), static_cast<ALuint>(value));
  521. if(value && !target)
  522. SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid effect slot target ID");
  523. if(target)
  524. {
  525. ALeffectslot *checker{target};
  526. while(checker && checker != slot)
  527. checker = checker->Target;
  528. if(checker)
  529. SETERR_RETURN(context, AL_INVALID_OPERATION,,
  530. "Setting target of effect slot ID %u to %u creates circular chain", slot->id,
  531. target->id);
  532. }
  533. if(ALeffectslot *oldtarget{slot->Target})
  534. {
  535. /* We must force an update if there was an existing effect slot
  536. * target, in case it's about to be deleted.
  537. */
  538. if(target) IncrementRef(target->ref);
  539. DecrementRef(oldtarget->ref);
  540. slot->Target = target;
  541. slot->updateProps(context.get());
  542. return;
  543. }
  544. if(target) IncrementRef(target->ref);
  545. slot->Target = target;
  546. break;
  547. case AL_BUFFER:
  548. device = context->mDevice.get();
  549. if(slot->mState == SlotState::Playing)
  550. SETERR_RETURN(context, AL_INVALID_OPERATION,,
  551. "Setting buffer on playing effect slot %u", slot->id);
  552. {
  553. std::lock_guard<std::mutex> ___{device->BufferLock};
  554. ALbuffer *buffer{};
  555. if(value)
  556. {
  557. buffer = LookupBuffer(device, static_cast<ALuint>(value));
  558. if(!buffer) SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid buffer ID");
  559. if(buffer->mCallback)
  560. SETERR_RETURN(context, AL_INVALID_OPERATION,,
  561. "Callback buffer not valid for effects");
  562. IncrementRef(buffer->ref);
  563. }
  564. if(ALbuffer *oldbuffer{slot->Buffer})
  565. DecrementRef(oldbuffer->ref);
  566. slot->Buffer = buffer;
  567. FPUCtl mixer_mode{};
  568. auto *state = slot->Effect.State.get();
  569. state->deviceUpdate(device, GetEffectBuffer(buffer));
  570. }
  571. break;
  572. case AL_EFFECTSLOT_STATE_SOFT:
  573. SETERR_RETURN(context, AL_INVALID_OPERATION,, "AL_EFFECTSLOT_STATE_SOFT is read-only");
  574. default:
  575. SETERR_RETURN(context, AL_INVALID_ENUM,, "Invalid effect slot integer property 0x%04x",
  576. param);
  577. }
  578. DO_UPDATEPROPS();
  579. }
  580. END_API_FUNC
  581. AL_API void AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, const ALint *values)
  582. START_API_FUNC
  583. {
  584. switch(param)
  585. {
  586. case AL_EFFECTSLOT_EFFECT:
  587. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  588. case AL_EFFECTSLOT_TARGET_SOFT:
  589. case AL_EFFECTSLOT_STATE_SOFT:
  590. case AL_BUFFER:
  591. alAuxiliaryEffectSloti(effectslot, param, values[0]);
  592. return;
  593. }
  594. ContextRef context{GetContextRef()};
  595. if UNLIKELY(!context) return;
  596. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  597. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  598. if UNLIKELY(!slot)
  599. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  600. switch(param)
  601. {
  602. default:
  603. SETERR_RETURN(context, AL_INVALID_ENUM,,
  604. "Invalid effect slot integer-vector property 0x%04x", param);
  605. }
  606. }
  607. END_API_FUNC
  608. AL_API void AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat value)
  609. START_API_FUNC
  610. {
  611. ContextRef context{GetContextRef()};
  612. if UNLIKELY(!context) return;
  613. std::lock_guard<std::mutex> _{context->mPropLock};
  614. std::lock_guard<std::mutex> __{context->mEffectSlotLock};
  615. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  616. if UNLIKELY(!slot)
  617. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  618. switch(param)
  619. {
  620. case AL_EFFECTSLOT_GAIN:
  621. if(!(value >= 0.0f && value <= 1.0f))
  622. SETERR_RETURN(context, AL_INVALID_VALUE,, "Effect slot gain out of range");
  623. slot->Gain = value;
  624. break;
  625. default:
  626. SETERR_RETURN(context, AL_INVALID_ENUM,, "Invalid effect slot float property 0x%04x",
  627. param);
  628. }
  629. DO_UPDATEPROPS();
  630. }
  631. END_API_FUNC
  632. AL_API void AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, const ALfloat *values)
  633. START_API_FUNC
  634. {
  635. switch(param)
  636. {
  637. case AL_EFFECTSLOT_GAIN:
  638. alAuxiliaryEffectSlotf(effectslot, param, values[0]);
  639. return;
  640. }
  641. ContextRef context{GetContextRef()};
  642. if UNLIKELY(!context) return;
  643. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  644. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  645. if UNLIKELY(!slot)
  646. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  647. switch(param)
  648. {
  649. default:
  650. SETERR_RETURN(context, AL_INVALID_ENUM,,
  651. "Invalid effect slot float-vector property 0x%04x", param);
  652. }
  653. }
  654. END_API_FUNC
  655. AL_API void AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *value)
  656. START_API_FUNC
  657. {
  658. ContextRef context{GetContextRef()};
  659. if UNLIKELY(!context) return;
  660. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  661. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  662. if UNLIKELY(!slot)
  663. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  664. switch(param)
  665. {
  666. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  667. *value = slot->AuxSendAuto ? AL_TRUE : AL_FALSE;
  668. break;
  669. case AL_EFFECTSLOT_TARGET_SOFT:
  670. if(auto *target = slot->Target)
  671. *value = static_cast<ALint>(target->id);
  672. else
  673. *value = 0;
  674. break;
  675. case AL_EFFECTSLOT_STATE_SOFT:
  676. *value = static_cast<int>(slot->mState);
  677. break;
  678. case AL_BUFFER:
  679. if(auto *buffer = slot->Buffer)
  680. *value = static_cast<ALint>(buffer->id);
  681. else
  682. *value = 0;
  683. break;
  684. default:
  685. context->setError(AL_INVALID_ENUM, "Invalid effect slot integer property 0x%04x", param);
  686. }
  687. }
  688. END_API_FUNC
  689. AL_API void AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *values)
  690. START_API_FUNC
  691. {
  692. switch(param)
  693. {
  694. case AL_EFFECTSLOT_EFFECT:
  695. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  696. case AL_EFFECTSLOT_TARGET_SOFT:
  697. case AL_EFFECTSLOT_STATE_SOFT:
  698. case AL_BUFFER:
  699. alGetAuxiliaryEffectSloti(effectslot, param, values);
  700. return;
  701. }
  702. ContextRef context{GetContextRef()};
  703. if UNLIKELY(!context) return;
  704. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  705. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  706. if UNLIKELY(!slot)
  707. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  708. switch(param)
  709. {
  710. default:
  711. context->setError(AL_INVALID_ENUM, "Invalid effect slot integer-vector property 0x%04x",
  712. param);
  713. }
  714. }
  715. END_API_FUNC
  716. AL_API void AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *value)
  717. START_API_FUNC
  718. {
  719. ContextRef context{GetContextRef()};
  720. if UNLIKELY(!context) return;
  721. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  722. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  723. if UNLIKELY(!slot)
  724. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  725. switch(param)
  726. {
  727. case AL_EFFECTSLOT_GAIN:
  728. *value = slot->Gain;
  729. break;
  730. default:
  731. context->setError(AL_INVALID_ENUM, "Invalid effect slot float property 0x%04x", param);
  732. }
  733. }
  734. END_API_FUNC
  735. AL_API void AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *values)
  736. START_API_FUNC
  737. {
  738. switch(param)
  739. {
  740. case AL_EFFECTSLOT_GAIN:
  741. alGetAuxiliaryEffectSlotf(effectslot, param, values);
  742. return;
  743. }
  744. ContextRef context{GetContextRef()};
  745. if UNLIKELY(!context) return;
  746. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  747. ALeffectslot *slot = LookupEffectSlot(context.get(), effectslot);
  748. if UNLIKELY(!slot)
  749. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid effect slot ID %u", effectslot);
  750. switch(param)
  751. {
  752. default:
  753. context->setError(AL_INVALID_ENUM, "Invalid effect slot float-vector property 0x%04x",
  754. param);
  755. }
  756. }
  757. END_API_FUNC
  758. ALeffectslot::ALeffectslot()
  759. {
  760. PropsClean.test_and_set(std::memory_order_relaxed);
  761. EffectStateFactory *factory{getFactoryByType(EffectSlotType::None)};
  762. assert(factory != nullptr);
  763. al::intrusive_ptr<EffectState> state{factory->create()};
  764. Effect.State = state;
  765. mSlot.mEffectState = state.release();
  766. }
  767. ALeffectslot::~ALeffectslot()
  768. {
  769. if(Target)
  770. DecrementRef(Target->ref);
  771. Target = nullptr;
  772. if(Buffer)
  773. DecrementRef(Buffer->ref);
  774. Buffer = nullptr;
  775. EffectSlotProps *props{mSlot.Update.exchange(nullptr)};
  776. if(props)
  777. {
  778. TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n",
  779. decltype(std::declval<void*>()){props});
  780. delete props;
  781. }
  782. if(mSlot.mEffectState)
  783. mSlot.mEffectState->release();
  784. }
  785. ALenum ALeffectslot::initEffect(ALeffect *effect, ALCcontext *context)
  786. {
  787. EffectSlotType newtype{EffectSlotTypeFromEnum(effect ? effect->type : AL_EFFECT_NULL)};
  788. if(newtype != Effect.Type)
  789. {
  790. EffectStateFactory *factory{getFactoryByType(newtype)};
  791. if(!factory)
  792. {
  793. ERR("Failed to find factory for effect slot type %d\n", static_cast<int>(newtype));
  794. return AL_INVALID_ENUM;
  795. }
  796. al::intrusive_ptr<EffectState> state{factory->create()};
  797. ALCdevice *device{context->mDevice.get()};
  798. std::unique_lock<std::mutex> statelock{device->StateLock};
  799. state->mOutTarget = device->Dry.Buffer;
  800. {
  801. FPUCtl mixer_mode{};
  802. state->deviceUpdate(device, GetEffectBuffer(Buffer));
  803. }
  804. Effect.Type = newtype;
  805. Effect.Props = effect ? effect->Props : EffectProps{};
  806. Effect.State = std::move(state);
  807. }
  808. else if(effect)
  809. Effect.Props = effect->Props;
  810. /* Remove state references from old effect slot property updates. */
  811. EffectSlotProps *props{context->mFreeEffectslotProps.load()};
  812. while(props)
  813. {
  814. props->State = nullptr;
  815. props = props->next.load(std::memory_order_relaxed);
  816. }
  817. return AL_NO_ERROR;
  818. }
  819. void ALeffectslot::updateProps(ALCcontext *context)
  820. {
  821. /* Get an unused property container, or allocate a new one as needed. */
  822. EffectSlotProps *props{context->mFreeEffectslotProps.load(std::memory_order_relaxed)};
  823. if(!props)
  824. props = new EffectSlotProps{};
  825. else
  826. {
  827. EffectSlotProps *next;
  828. do {
  829. next = props->next.load(std::memory_order_relaxed);
  830. } while(context->mFreeEffectslotProps.compare_exchange_weak(props, next,
  831. std::memory_order_seq_cst, std::memory_order_acquire) == 0);
  832. }
  833. /* Copy in current property values. */
  834. props->Gain = Gain;
  835. props->AuxSendAuto = AuxSendAuto;
  836. props->Target = Target ? &Target->mSlot : nullptr;
  837. props->Type = Effect.Type;
  838. props->Props = Effect.Props;
  839. props->State = Effect.State;
  840. /* Set the new container for updating internal parameters. */
  841. props = mSlot.Update.exchange(props, std::memory_order_acq_rel);
  842. if(props)
  843. {
  844. /* If there was an unused update container, put it back in the
  845. * freelist.
  846. */
  847. props->State = nullptr;
  848. AtomicReplaceHead(context->mFreeEffectslotProps, props);
  849. }
  850. }
  851. void UpdateAllEffectSlotProps(ALCcontext *context)
  852. {
  853. std::lock_guard<std::mutex> _{context->mEffectSlotLock};
  854. for(auto &sublist : context->mEffectSlotList)
  855. {
  856. uint64_t usemask{~sublist.FreeMask};
  857. while(usemask)
  858. {
  859. const int idx{al::countr_zero(usemask)};
  860. ALeffectslot *slot{sublist.EffectSlots + idx};
  861. usemask &= ~(1_u64 << idx);
  862. if(slot->mState != SlotState::Stopped
  863. && slot->PropsClean.test_and_set(std::memory_order_acq_rel))
  864. slot->updateProps(context);
  865. }
  866. }
  867. }
  868. EffectSlotSubList::~EffectSlotSubList()
  869. {
  870. uint64_t usemask{~FreeMask};
  871. while(usemask)
  872. {
  873. const int idx{al::countr_zero(usemask)};
  874. al::destroy_at(EffectSlots+idx);
  875. usemask &= ~(1_u64 << idx);
  876. }
  877. FreeMask = ~usemask;
  878. al_free(EffectSlots);
  879. EffectSlots = nullptr;
  880. }