alAuxEffectSlot.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 <stdlib.h>
  22. #include <math.h>
  23. #include "AL/al.h"
  24. #include "AL/alc.h"
  25. #include "alMain.h"
  26. #include "alAuxEffectSlot.h"
  27. #include "alError.h"
  28. #include "alListener.h"
  29. #include "alSource.h"
  30. #include "fpu_modes.h"
  31. #include "almalloc.h"
  32. extern inline void LockEffectSlotList(ALCcontext *context);
  33. extern inline void UnlockEffectSlotList(ALCcontext *context);
  34. static void AddActiveEffectSlots(const ALuint *slotids, ALsizei count, ALCcontext *context);
  35. static void RemoveActiveEffectSlots(const ALuint *slotids, ALsizei count, ALCcontext *context);
  36. static const struct {
  37. ALenum Type;
  38. EffectStateFactory* (*GetFactory)(void);
  39. } FactoryList[] = {
  40. { AL_EFFECT_NULL, NullStateFactory_getFactory },
  41. { AL_EFFECT_EAXREVERB, ReverbStateFactory_getFactory },
  42. { AL_EFFECT_REVERB, ReverbStateFactory_getFactory },
  43. { AL_EFFECT_AUTOWAH, AutowahStateFactory_getFactory },
  44. { AL_EFFECT_CHORUS, ChorusStateFactory_getFactory },
  45. { AL_EFFECT_COMPRESSOR, CompressorStateFactory_getFactory },
  46. { AL_EFFECT_DISTORTION, DistortionStateFactory_getFactory },
  47. { AL_EFFECT_ECHO, EchoStateFactory_getFactory },
  48. { AL_EFFECT_EQUALIZER, EqualizerStateFactory_getFactory },
  49. { AL_EFFECT_FLANGER, FlangerStateFactory_getFactory },
  50. { AL_EFFECT_FREQUENCY_SHIFTER, FshifterStateFactory_getFactory },
  51. { AL_EFFECT_RING_MODULATOR, ModulatorStateFactory_getFactory },
  52. { AL_EFFECT_PITCH_SHIFTER, PshifterStateFactory_getFactory},
  53. { AL_EFFECT_DEDICATED_DIALOGUE, DedicatedStateFactory_getFactory },
  54. { AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT, DedicatedStateFactory_getFactory }
  55. };
  56. static inline EffectStateFactory *getFactoryByType(ALenum type)
  57. {
  58. size_t i;
  59. for(i = 0;i < COUNTOF(FactoryList);i++)
  60. {
  61. if(FactoryList[i].Type == type)
  62. return FactoryList[i].GetFactory();
  63. }
  64. return NULL;
  65. }
  66. static void ALeffectState_IncRef(ALeffectState *state);
  67. static inline ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id)
  68. {
  69. id--;
  70. if(UNLIKELY(id >= VECTOR_SIZE(context->EffectSlotList)))
  71. return NULL;
  72. return VECTOR_ELEM(context->EffectSlotList, id);
  73. }
  74. static inline ALeffect *LookupEffect(ALCdevice *device, ALuint id)
  75. {
  76. EffectSubList *sublist;
  77. ALuint lidx = (id-1) >> 6;
  78. ALsizei slidx = (id-1) & 0x3f;
  79. if(UNLIKELY(lidx >= VECTOR_SIZE(device->EffectList)))
  80. return NULL;
  81. sublist = &VECTOR_ELEM(device->EffectList, lidx);
  82. if(UNLIKELY(sublist->FreeMask & (U64(1)<<slidx)))
  83. return NULL;
  84. return sublist->Effects + slidx;
  85. }
  86. #define DO_UPDATEPROPS() do { \
  87. if(!ATOMIC_LOAD(&context->DeferUpdates, almemory_order_acquire)) \
  88. UpdateEffectSlotProps(slot, context); \
  89. else \
  90. ATOMIC_FLAG_CLEAR(&slot->PropsClean, almemory_order_release); \
  91. } while(0)
  92. AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
  93. {
  94. ALCdevice *device;
  95. ALCcontext *context;
  96. ALsizei cur;
  97. context = GetContextRef();
  98. if(!context) return;
  99. if(!(n >= 0))
  100. SETERR_GOTO(context, AL_INVALID_VALUE, done, "Generating %d effect slots", n);
  101. if(n == 0) goto done;
  102. LockEffectSlotList(context);
  103. device = context->Device;
  104. for(cur = 0;cur < n;cur++)
  105. {
  106. ALeffectslotPtr *iter = VECTOR_BEGIN(context->EffectSlotList);
  107. ALeffectslotPtr *end = VECTOR_END(context->EffectSlotList);
  108. ALeffectslot *slot = NULL;
  109. ALenum err = AL_OUT_OF_MEMORY;
  110. for(;iter != end;iter++)
  111. {
  112. if(!*iter)
  113. break;
  114. }
  115. if(iter == end)
  116. {
  117. if(device->AuxiliaryEffectSlotMax == VECTOR_SIZE(context->EffectSlotList))
  118. {
  119. UnlockEffectSlotList(context);
  120. alDeleteAuxiliaryEffectSlots(cur, effectslots);
  121. SETERR_GOTO(context, AL_OUT_OF_MEMORY, done,
  122. "Exceeding %u auxiliary effect slot limit", device->AuxiliaryEffectSlotMax);
  123. }
  124. VECTOR_PUSH_BACK(context->EffectSlotList, NULL);
  125. iter = &VECTOR_BACK(context->EffectSlotList);
  126. }
  127. slot = al_calloc(16, sizeof(ALeffectslot));
  128. if(!slot || (err=InitEffectSlot(slot)) != AL_NO_ERROR)
  129. {
  130. al_free(slot);
  131. UnlockEffectSlotList(context);
  132. alDeleteAuxiliaryEffectSlots(cur, effectslots);
  133. SETERR_GOTO(context, err, done, "Effect slot object allocation failed");
  134. }
  135. aluInitEffectPanning(slot);
  136. slot->id = (iter - VECTOR_BEGIN(context->EffectSlotList)) + 1;
  137. *iter = slot;
  138. effectslots[cur] = slot->id;
  139. }
  140. AddActiveEffectSlots(effectslots, n, context);
  141. UnlockEffectSlotList(context);
  142. done:
  143. ALCcontext_DecRef(context);
  144. }
  145. AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, const ALuint *effectslots)
  146. {
  147. ALCcontext *context;
  148. ALeffectslot *slot;
  149. ALsizei i;
  150. context = GetContextRef();
  151. if(!context) return;
  152. LockEffectSlotList(context);
  153. if(!(n >= 0))
  154. SETERR_GOTO(context, AL_INVALID_VALUE, done, "Deleting %d effect slots", n);
  155. if(n == 0) goto done;
  156. for(i = 0;i < n;i++)
  157. {
  158. if((slot=LookupEffectSlot(context, effectslots[i])) == NULL)
  159. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u",
  160. effectslots[i]);
  161. if(ReadRef(&slot->ref) != 0)
  162. SETERR_GOTO(context, AL_INVALID_NAME, done, "Deleting in-use effect slot %u",
  163. effectslots[i]);
  164. }
  165. // All effectslots are valid
  166. RemoveActiveEffectSlots(effectslots, n, context);
  167. for(i = 0;i < n;i++)
  168. {
  169. if((slot=LookupEffectSlot(context, effectslots[i])) == NULL)
  170. continue;
  171. VECTOR_ELEM(context->EffectSlotList, effectslots[i]-1) = NULL;
  172. DeinitEffectSlot(slot);
  173. memset(slot, 0, sizeof(*slot));
  174. al_free(slot);
  175. }
  176. done:
  177. UnlockEffectSlotList(context);
  178. ALCcontext_DecRef(context);
  179. }
  180. AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
  181. {
  182. ALCcontext *context;
  183. ALboolean ret;
  184. context = GetContextRef();
  185. if(!context) return AL_FALSE;
  186. LockEffectSlotList(context);
  187. ret = (LookupEffectSlot(context, effectslot) ? AL_TRUE : AL_FALSE);
  188. UnlockEffectSlotList(context);
  189. ALCcontext_DecRef(context);
  190. return ret;
  191. }
  192. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint value)
  193. {
  194. ALCdevice *device;
  195. ALCcontext *context;
  196. ALeffectslot *slot;
  197. ALeffect *effect = NULL;
  198. ALenum err;
  199. context = GetContextRef();
  200. if(!context) return;
  201. almtx_lock(&context->PropLock);
  202. LockEffectSlotList(context);
  203. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  204. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  205. switch(param)
  206. {
  207. case AL_EFFECTSLOT_EFFECT:
  208. device = context->Device;
  209. LockEffectList(device);
  210. effect = (value ? LookupEffect(device, value) : NULL);
  211. if(!(value == 0 || effect != NULL))
  212. {
  213. UnlockEffectList(device);
  214. SETERR_GOTO(context, AL_INVALID_VALUE, done, "Invalid effect ID %u", value);
  215. }
  216. err = InitializeEffect(context, slot, effect);
  217. UnlockEffectList(device);
  218. if(err != AL_NO_ERROR)
  219. SETERR_GOTO(context, err, done, "Effect initialization failed");
  220. break;
  221. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  222. if(!(value == AL_TRUE || value == AL_FALSE))
  223. SETERR_GOTO(context, AL_INVALID_VALUE, done,
  224. "Effect slot auxiliary send auto out of range");
  225. slot->AuxSendAuto = value;
  226. break;
  227. default:
  228. SETERR_GOTO(context, AL_INVALID_ENUM, done, "Invalid effect slot integer property 0x%04x",
  229. param);
  230. }
  231. DO_UPDATEPROPS();
  232. done:
  233. UnlockEffectSlotList(context);
  234. almtx_unlock(&context->PropLock);
  235. ALCcontext_DecRef(context);
  236. }
  237. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, const ALint *values)
  238. {
  239. ALCcontext *context;
  240. switch(param)
  241. {
  242. case AL_EFFECTSLOT_EFFECT:
  243. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  244. alAuxiliaryEffectSloti(effectslot, param, values[0]);
  245. return;
  246. }
  247. context = GetContextRef();
  248. if(!context) return;
  249. LockEffectSlotList(context);
  250. if(LookupEffectSlot(context, effectslot) == NULL)
  251. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  252. switch(param)
  253. {
  254. default:
  255. alSetError(context, AL_INVALID_ENUM, "Invalid effect slot integer-vector property 0x%04x",
  256. param);
  257. }
  258. done:
  259. UnlockEffectSlotList(context);
  260. ALCcontext_DecRef(context);
  261. }
  262. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat value)
  263. {
  264. ALCcontext *context;
  265. ALeffectslot *slot;
  266. context = GetContextRef();
  267. if(!context) return;
  268. almtx_lock(&context->PropLock);
  269. LockEffectSlotList(context);
  270. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  271. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  272. switch(param)
  273. {
  274. case AL_EFFECTSLOT_GAIN:
  275. if(!(value >= 0.0f && value <= 1.0f))
  276. SETERR_GOTO(context, AL_INVALID_VALUE, done, "Effect slot gain out of range");
  277. slot->Gain = value;
  278. break;
  279. default:
  280. SETERR_GOTO(context, AL_INVALID_ENUM, done, "Invalid effect slot float property 0x%04x",
  281. param);
  282. }
  283. DO_UPDATEPROPS();
  284. done:
  285. UnlockEffectSlotList(context);
  286. almtx_unlock(&context->PropLock);
  287. ALCcontext_DecRef(context);
  288. }
  289. AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, const ALfloat *values)
  290. {
  291. ALCcontext *context;
  292. switch(param)
  293. {
  294. case AL_EFFECTSLOT_GAIN:
  295. alAuxiliaryEffectSlotf(effectslot, param, values[0]);
  296. return;
  297. }
  298. context = GetContextRef();
  299. if(!context) return;
  300. LockEffectSlotList(context);
  301. if(LookupEffectSlot(context, effectslot) == NULL)
  302. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  303. switch(param)
  304. {
  305. default:
  306. alSetError(context, AL_INVALID_ENUM, "Invalid effect slot float-vector property 0x%04x",
  307. param);
  308. }
  309. done:
  310. UnlockEffectSlotList(context);
  311. ALCcontext_DecRef(context);
  312. }
  313. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *value)
  314. {
  315. ALCcontext *context;
  316. ALeffectslot *slot;
  317. context = GetContextRef();
  318. if(!context) return;
  319. LockEffectSlotList(context);
  320. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  321. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  322. switch(param)
  323. {
  324. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  325. *value = slot->AuxSendAuto;
  326. break;
  327. default:
  328. alSetError(context, AL_INVALID_ENUM, "Invalid effect slot integer property 0x%04x", param);
  329. }
  330. done:
  331. UnlockEffectSlotList(context);
  332. ALCcontext_DecRef(context);
  333. }
  334. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *values)
  335. {
  336. ALCcontext *context;
  337. switch(param)
  338. {
  339. case AL_EFFECTSLOT_EFFECT:
  340. case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
  341. alGetAuxiliaryEffectSloti(effectslot, param, values);
  342. return;
  343. }
  344. context = GetContextRef();
  345. if(!context) return;
  346. LockEffectSlotList(context);
  347. if(LookupEffectSlot(context, effectslot) == NULL)
  348. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  349. switch(param)
  350. {
  351. default:
  352. alSetError(context, AL_INVALID_ENUM, "Invalid effect slot integer-vector property 0x%04x",
  353. param);
  354. }
  355. done:
  356. UnlockEffectSlotList(context);
  357. ALCcontext_DecRef(context);
  358. }
  359. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *value)
  360. {
  361. ALCcontext *context;
  362. ALeffectslot *slot;
  363. context = GetContextRef();
  364. if(!context) return;
  365. LockEffectSlotList(context);
  366. if((slot=LookupEffectSlot(context, effectslot)) == NULL)
  367. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  368. switch(param)
  369. {
  370. case AL_EFFECTSLOT_GAIN:
  371. *value = slot->Gain;
  372. break;
  373. default:
  374. alSetError(context, AL_INVALID_ENUM, "Invalid effect slot float property 0x%04x", param);
  375. }
  376. done:
  377. UnlockEffectSlotList(context);
  378. ALCcontext_DecRef(context);
  379. }
  380. AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *values)
  381. {
  382. ALCcontext *context;
  383. switch(param)
  384. {
  385. case AL_EFFECTSLOT_GAIN:
  386. alGetAuxiliaryEffectSlotf(effectslot, param, values);
  387. return;
  388. }
  389. context = GetContextRef();
  390. if(!context) return;
  391. LockEffectSlotList(context);
  392. if(LookupEffectSlot(context, effectslot) == NULL)
  393. SETERR_GOTO(context, AL_INVALID_NAME, done, "Invalid effect slot ID %u", effectslot);
  394. switch(param)
  395. {
  396. default:
  397. alSetError(context, AL_INVALID_ENUM, "Invalid effect slot float-vector property 0x%04x",
  398. param);
  399. }
  400. done:
  401. UnlockEffectSlotList(context);
  402. ALCcontext_DecRef(context);
  403. }
  404. ALenum InitializeEffect(ALCcontext *Context, ALeffectslot *EffectSlot, ALeffect *effect)
  405. {
  406. ALCdevice *Device = Context->Device;
  407. ALenum newtype = (effect ? effect->type : AL_EFFECT_NULL);
  408. struct ALeffectslotProps *props;
  409. ALeffectState *State;
  410. if(newtype != EffectSlot->Effect.Type)
  411. {
  412. EffectStateFactory *factory;
  413. factory = getFactoryByType(newtype);
  414. if(!factory)
  415. {
  416. ERR("Failed to find factory for effect type 0x%04x\n", newtype);
  417. return AL_INVALID_ENUM;
  418. }
  419. State = EffectStateFactory_create(factory);
  420. if(!State) return AL_OUT_OF_MEMORY;
  421. START_MIXER_MODE();
  422. almtx_lock(&Device->BackendLock);
  423. State->OutBuffer = Device->Dry.Buffer;
  424. State->OutChannels = Device->Dry.NumChannels;
  425. if(V(State,deviceUpdate)(Device) == AL_FALSE)
  426. {
  427. almtx_unlock(&Device->BackendLock);
  428. LEAVE_MIXER_MODE();
  429. ALeffectState_DecRef(State);
  430. return AL_OUT_OF_MEMORY;
  431. }
  432. almtx_unlock(&Device->BackendLock);
  433. END_MIXER_MODE();
  434. if(!effect)
  435. {
  436. EffectSlot->Effect.Type = AL_EFFECT_NULL;
  437. memset(&EffectSlot->Effect.Props, 0, sizeof(EffectSlot->Effect.Props));
  438. }
  439. else
  440. {
  441. EffectSlot->Effect.Type = effect->type;
  442. EffectSlot->Effect.Props = effect->Props;
  443. }
  444. ALeffectState_DecRef(EffectSlot->Effect.State);
  445. EffectSlot->Effect.State = State;
  446. }
  447. else if(effect)
  448. EffectSlot->Effect.Props = effect->Props;
  449. /* Remove state references from old effect slot property updates. */
  450. props = ATOMIC_LOAD_SEQ(&Context->FreeEffectslotProps);
  451. while(props)
  452. {
  453. if(props->State)
  454. ALeffectState_DecRef(props->State);
  455. props->State = NULL;
  456. props = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
  457. }
  458. return AL_NO_ERROR;
  459. }
  460. static void ALeffectState_IncRef(ALeffectState *state)
  461. {
  462. uint ref;
  463. ref = IncrementRef(&state->Ref);
  464. TRACEREF("%p increasing refcount to %u\n", state, ref);
  465. }
  466. void ALeffectState_DecRef(ALeffectState *state)
  467. {
  468. uint ref;
  469. ref = DecrementRef(&state->Ref);
  470. TRACEREF("%p decreasing refcount to %u\n", state, ref);
  471. if(ref == 0) DELETE_OBJ(state);
  472. }
  473. void ALeffectState_Construct(ALeffectState *state)
  474. {
  475. InitRef(&state->Ref, 1);
  476. state->OutBuffer = NULL;
  477. state->OutChannels = 0;
  478. }
  479. void ALeffectState_Destruct(ALeffectState *UNUSED(state))
  480. {
  481. }
  482. static void AddActiveEffectSlots(const ALuint *slotids, ALsizei count, ALCcontext *context)
  483. {
  484. struct ALeffectslotArray *curarray = ATOMIC_LOAD(&context->ActiveAuxSlots,
  485. almemory_order_acquire);
  486. struct ALeffectslotArray *newarray = NULL;
  487. ALsizei newcount = curarray->count + count;
  488. ALCdevice *device = context->Device;
  489. ALsizei i, j;
  490. /* Insert the new effect slots into the head of the array, followed by the
  491. * existing ones.
  492. */
  493. newarray = al_calloc(DEF_ALIGN, FAM_SIZE(struct ALeffectslotArray, slot, newcount));
  494. newarray->count = newcount;
  495. for(i = 0;i < count;i++)
  496. newarray->slot[i] = LookupEffectSlot(context, slotids[i]);
  497. for(j = 0;i < newcount;)
  498. newarray->slot[i++] = curarray->slot[j++];
  499. /* Remove any duplicates (first instance of each will be kept). */
  500. for(i = 1;i < newcount;i++)
  501. {
  502. for(j = i;j != 0;)
  503. {
  504. if(UNLIKELY(newarray->slot[i] == newarray->slot[--j]))
  505. {
  506. newcount--;
  507. for(j = i;j < newcount;j++)
  508. newarray->slot[j] = newarray->slot[j+1];
  509. i--;
  510. break;
  511. }
  512. }
  513. }
  514. /* Reallocate newarray if the new size ended up smaller from duplicate
  515. * removal.
  516. */
  517. if(UNLIKELY(newcount < newarray->count))
  518. {
  519. struct ALeffectslotArray *tmpnewarray = al_calloc(DEF_ALIGN,
  520. FAM_SIZE(struct ALeffectslotArray, slot, newcount));
  521. memcpy(tmpnewarray, newarray, FAM_SIZE(struct ALeffectslotArray, slot, newcount));
  522. al_free(newarray);
  523. newarray = tmpnewarray;
  524. newarray->count = newcount;
  525. }
  526. curarray = ATOMIC_EXCHANGE_PTR(&context->ActiveAuxSlots, newarray, almemory_order_acq_rel);
  527. while((ATOMIC_LOAD(&device->MixCount, almemory_order_acquire)&1))
  528. althrd_yield();
  529. al_free(curarray);
  530. }
  531. static void RemoveActiveEffectSlots(const ALuint *slotids, ALsizei count, ALCcontext *context)
  532. {
  533. struct ALeffectslotArray *curarray = ATOMIC_LOAD(&context->ActiveAuxSlots,
  534. almemory_order_acquire);
  535. struct ALeffectslotArray *newarray = NULL;
  536. ALCdevice *device = context->Device;
  537. ALsizei i, j;
  538. /* Don't shrink the allocated array size since we don't know how many (if
  539. * any) of the effect slots to remove are in the array.
  540. */
  541. newarray = al_calloc(DEF_ALIGN, FAM_SIZE(struct ALeffectslotArray, slot, curarray->count));
  542. newarray->count = 0;
  543. for(i = 0;i < curarray->count;i++)
  544. {
  545. /* Insert this slot into the new array only if it's not one to remove. */
  546. ALeffectslot *slot = curarray->slot[i];
  547. for(j = count;j != 0;)
  548. {
  549. if(slot->id == slotids[--j])
  550. goto skip_ins;
  551. }
  552. newarray->slot[newarray->count++] = slot;
  553. skip_ins: ;
  554. }
  555. /* TODO: Could reallocate newarray now that we know it's needed size. */
  556. curarray = ATOMIC_EXCHANGE_PTR(&context->ActiveAuxSlots, newarray, almemory_order_acq_rel);
  557. while((ATOMIC_LOAD(&device->MixCount, almemory_order_acquire)&1))
  558. althrd_yield();
  559. al_free(curarray);
  560. }
  561. ALenum InitEffectSlot(ALeffectslot *slot)
  562. {
  563. EffectStateFactory *factory;
  564. slot->Effect.Type = AL_EFFECT_NULL;
  565. factory = getFactoryByType(AL_EFFECT_NULL);
  566. slot->Effect.State = EffectStateFactory_create(factory);
  567. if(!slot->Effect.State) return AL_OUT_OF_MEMORY;
  568. slot->Gain = 1.0;
  569. slot->AuxSendAuto = AL_TRUE;
  570. ATOMIC_FLAG_TEST_AND_SET(&slot->PropsClean, almemory_order_relaxed);
  571. InitRef(&slot->ref, 0);
  572. ATOMIC_INIT(&slot->Update, NULL);
  573. slot->Params.Gain = 1.0f;
  574. slot->Params.AuxSendAuto = AL_TRUE;
  575. ALeffectState_IncRef(slot->Effect.State);
  576. slot->Params.EffectState = slot->Effect.State;
  577. slot->Params.RoomRolloff = 0.0f;
  578. slot->Params.DecayTime = 0.0f;
  579. slot->Params.DecayLFRatio = 0.0f;
  580. slot->Params.DecayHFRatio = 0.0f;
  581. slot->Params.DecayHFLimit = AL_FALSE;
  582. slot->Params.AirAbsorptionGainHF = 1.0f;
  583. return AL_NO_ERROR;
  584. }
  585. void DeinitEffectSlot(ALeffectslot *slot)
  586. {
  587. struct ALeffectslotProps *props;
  588. props = ATOMIC_LOAD_SEQ(&slot->Update);
  589. if(props)
  590. {
  591. if(props->State) ALeffectState_DecRef(props->State);
  592. TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", props);
  593. al_free(props);
  594. }
  595. ALeffectState_DecRef(slot->Effect.State);
  596. if(slot->Params.EffectState)
  597. ALeffectState_DecRef(slot->Params.EffectState);
  598. }
  599. void UpdateEffectSlotProps(ALeffectslot *slot, ALCcontext *context)
  600. {
  601. struct ALeffectslotProps *props;
  602. ALeffectState *oldstate;
  603. /* Get an unused property container, or allocate a new one as needed. */
  604. props = ATOMIC_LOAD(&context->FreeEffectslotProps, almemory_order_relaxed);
  605. if(!props)
  606. props = al_calloc(16, sizeof(*props));
  607. else
  608. {
  609. struct ALeffectslotProps *next;
  610. do {
  611. next = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
  612. } while(ATOMIC_COMPARE_EXCHANGE_PTR_WEAK(&context->FreeEffectslotProps, &props, next,
  613. almemory_order_seq_cst, almemory_order_acquire) == 0);
  614. }
  615. /* Copy in current property values. */
  616. props->Gain = slot->Gain;
  617. props->AuxSendAuto = slot->AuxSendAuto;
  618. props->Type = slot->Effect.Type;
  619. props->Props = slot->Effect.Props;
  620. /* Swap out any stale effect state object there may be in the container, to
  621. * delete it.
  622. */
  623. ALeffectState_IncRef(slot->Effect.State);
  624. oldstate = props->State;
  625. props->State = slot->Effect.State;
  626. /* Set the new container for updating internal parameters. */
  627. props = ATOMIC_EXCHANGE_PTR(&slot->Update, props, almemory_order_acq_rel);
  628. if(props)
  629. {
  630. /* If there was an unused update container, put it back in the
  631. * freelist.
  632. */
  633. if(props->State)
  634. ALeffectState_DecRef(props->State);
  635. props->State = NULL;
  636. ATOMIC_REPLACE_HEAD(struct ALeffectslotProps*, &context->FreeEffectslotProps, props);
  637. }
  638. if(oldstate)
  639. ALeffectState_DecRef(oldstate);
  640. }
  641. void UpdateAllEffectSlotProps(ALCcontext *context)
  642. {
  643. struct ALeffectslotArray *auxslots;
  644. ALsizei i;
  645. LockEffectSlotList(context);
  646. auxslots = ATOMIC_LOAD(&context->ActiveAuxSlots, almemory_order_acquire);
  647. for(i = 0;i < auxslots->count;i++)
  648. {
  649. ALeffectslot *slot = auxslots->slot[i];
  650. if(!ATOMIC_FLAG_TEST_AND_SET(&slot->PropsClean, almemory_order_acq_rel))
  651. UpdateEffectSlotProps(slot, context);
  652. }
  653. UnlockEffectSlotList(context);
  654. }
  655. ALvoid ReleaseALAuxiliaryEffectSlots(ALCcontext *context)
  656. {
  657. ALeffectslotPtr *iter = VECTOR_BEGIN(context->EffectSlotList);
  658. ALeffectslotPtr *end = VECTOR_END(context->EffectSlotList);
  659. size_t leftover = 0;
  660. for(;iter != end;iter++)
  661. {
  662. ALeffectslot *slot = *iter;
  663. if(!slot) continue;
  664. *iter = NULL;
  665. DeinitEffectSlot(slot);
  666. memset(slot, 0, sizeof(*slot));
  667. al_free(slot);
  668. ++leftover;
  669. }
  670. if(leftover > 0)
  671. WARN("(%p) Deleted "SZFMT" AuxiliaryEffectSlot%s\n", context, leftover, (leftover==1)?"":"s");
  672. }