Remove broken autowah effect code
[openal-soft.git] / OpenAL32 / alAuxEffectSlot.c
blob796eec5bc3aaa3181aee1dbe3943222e4c918cf4
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.
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.
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
21 #include "config.h"
23 #include <stdlib.h>
24 #include <math.h>
26 #include "AL/al.h"
27 #include "AL/alc.h"
28 #include "alMain.h"
29 #include "alAuxEffectSlot.h"
30 #include "alThunk.h"
31 #include "alError.h"
32 #include "alListener.h"
33 #include "alSource.h"
35 #include "almalloc.h"
38 extern inline void LockEffectSlotsRead(ALCcontext *context);
39 extern inline void UnlockEffectSlotsRead(ALCcontext *context);
40 extern inline void LockEffectSlotsWrite(ALCcontext *context);
41 extern inline void UnlockEffectSlotsWrite(ALCcontext *context);
42 extern inline struct ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id);
43 extern inline struct ALeffectslot *RemoveEffectSlot(ALCcontext *context, ALuint id);
45 static void AddEffectSlotList(ALCcontext *Context, ALeffectslot *first, ALeffectslot *last);
46 static void RemoveEffectSlotList(ALCcontext *Context, const ALeffectslot *slot);
49 static UIntMap EffectStateFactoryMap;
50 static inline ALeffectStateFactory *getFactoryByType(ALenum type)
52 ALeffectStateFactory* (*getFactory)(void) = LookupUIntMapKey(&EffectStateFactoryMap, type);
53 if(getFactory != NULL)
54 return getFactory();
55 return NULL;
59 AL_API ALvoid AL_APIENTRY alGenAuxiliaryEffectSlots(ALsizei n, ALuint *effectslots)
61 ALCcontext *context;
62 ALeffectslot *first, *last;
63 ALsizei cur;
64 ALenum err;
66 context = GetContextRef();
67 if(!context) return;
69 if(!(n >= 0))
70 SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
72 first = last = NULL;
73 for(cur = 0;cur < n;cur++)
75 ALeffectslot *slot = al_calloc(16, sizeof(ALeffectslot));
76 err = AL_OUT_OF_MEMORY;
77 if(!slot || (err=InitEffectSlot(slot)) != AL_NO_ERROR)
79 al_free(slot);
80 alDeleteAuxiliaryEffectSlots(cur, effectslots);
81 SET_ERROR_AND_GOTO(context, err, done);
84 err = NewThunkEntry(&slot->id);
85 if(err == AL_NO_ERROR)
86 err = InsertUIntMapEntry(&context->EffectSlotMap, slot->id, slot);
87 if(err != AL_NO_ERROR)
89 FreeThunkEntry(slot->id);
90 DELETE_OBJ(slot->Params.EffectState);
91 al_free(slot);
93 alDeleteAuxiliaryEffectSlots(cur, effectslots);
94 SET_ERROR_AND_GOTO(context, err, done);
97 aluInitEffectPanning(slot);
99 if(!first) first = slot;
100 if(last) ATOMIC_STORE(&last->next, slot);
101 last = slot;
103 effectslots[cur] = slot->id;
105 AddEffectSlotList(context, first, last);
107 done:
108 ALCcontext_DecRef(context);
111 AL_API ALvoid AL_APIENTRY alDeleteAuxiliaryEffectSlots(ALsizei n, const ALuint *effectslots)
113 ALCcontext *context;
114 ALeffectslot *slot;
115 ALsizei i;
117 context = GetContextRef();
118 if(!context) return;
120 LockEffectSlotsWrite(context);
121 if(!(n >= 0))
122 SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
123 for(i = 0;i < n;i++)
125 if((slot=LookupEffectSlot(context, effectslots[i])) == NULL)
126 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
127 if(ReadRef(&slot->ref) != 0)
128 SET_ERROR_AND_GOTO(context, AL_INVALID_OPERATION, done);
131 // All effectslots are valid
132 for(i = 0;i < n;i++)
134 if((slot=RemoveEffectSlot(context, effectslots[i])) == NULL)
135 continue;
136 FreeThunkEntry(slot->id);
138 RemoveEffectSlotList(context, slot);
139 DeinitEffectSlot(slot);
141 memset(slot, 0, sizeof(*slot));
142 al_free(slot);
145 done:
146 UnlockEffectSlotsWrite(context);
147 ALCcontext_DecRef(context);
150 AL_API ALboolean AL_APIENTRY alIsAuxiliaryEffectSlot(ALuint effectslot)
152 ALCcontext *context;
153 ALboolean ret;
155 context = GetContextRef();
156 if(!context) return AL_FALSE;
158 LockEffectSlotsRead(context);
159 ret = (LookupEffectSlot(context, effectslot) ? AL_TRUE : AL_FALSE);
160 UnlockEffectSlotsRead(context);
162 ALCcontext_DecRef(context);
164 return ret;
167 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint value)
169 ALCdevice *device;
170 ALCcontext *context;
171 ALeffectslot *slot;
172 ALeffect *effect = NULL;
173 ALenum err;
175 context = GetContextRef();
176 if(!context) return;
178 WriteLock(&context->PropLock);
179 LockEffectSlotsRead(context);
180 if((slot=LookupEffectSlot(context, effectslot)) == NULL)
181 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
182 switch(param)
184 case AL_EFFECTSLOT_EFFECT:
185 device = context->Device;
187 LockEffectsRead(device);
188 effect = (value ? LookupEffect(device, value) : NULL);
189 if(!(value == 0 || effect != NULL))
191 UnlockEffectsRead(device);
192 SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
194 err = InitializeEffect(device, slot, effect);
195 UnlockEffectsRead(device);
197 if(err != AL_NO_ERROR)
198 SET_ERROR_AND_GOTO(context, err, done);
199 break;
201 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
202 if(!(value == AL_TRUE || value == AL_FALSE))
203 SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
204 slot->AuxSendAuto = value;
205 UpdateEffectSlotProps(slot);
206 if(!ATOMIC_LOAD(&context->DeferUpdates, almemory_order_acquire))
207 UpdateAllSourceProps(context);
208 break;
210 default:
211 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
214 done:
215 UnlockEffectSlotsRead(context);
216 WriteUnlock(&context->PropLock);
217 ALCcontext_DecRef(context);
220 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, const ALint *values)
222 ALCcontext *context;
224 switch(param)
226 case AL_EFFECTSLOT_EFFECT:
227 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
228 alAuxiliaryEffectSloti(effectslot, param, values[0]);
229 return;
232 context = GetContextRef();
233 if(!context) return;
235 LockEffectSlotsRead(context);
236 if(LookupEffectSlot(context, effectslot) == NULL)
237 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
238 switch(param)
240 default:
241 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
244 done:
245 UnlockEffectSlotsRead(context);
246 ALCcontext_DecRef(context);
249 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat value)
251 ALCcontext *context;
252 ALeffectslot *slot;
254 context = GetContextRef();
255 if(!context) return;
257 WriteLock(&context->PropLock);
258 LockEffectSlotsRead(context);
259 if((slot=LookupEffectSlot(context, effectslot)) == NULL)
260 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
261 switch(param)
263 case AL_EFFECTSLOT_GAIN:
264 if(!(value >= 0.0f && value <= 1.0f))
265 SET_ERROR_AND_GOTO(context, AL_INVALID_VALUE, done);
266 slot->Gain = value;
267 break;
269 default:
270 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
272 UpdateEffectSlotProps(slot);
274 done:
275 UnlockEffectSlotsRead(context);
276 WriteUnlock(&context->PropLock);
277 ALCcontext_DecRef(context);
280 AL_API ALvoid AL_APIENTRY alAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, const ALfloat *values)
282 ALCcontext *context;
284 switch(param)
286 case AL_EFFECTSLOT_GAIN:
287 alAuxiliaryEffectSlotf(effectslot, param, values[0]);
288 return;
291 context = GetContextRef();
292 if(!context) return;
294 LockEffectSlotsRead(context);
295 if(LookupEffectSlot(context, effectslot) == NULL)
296 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
297 switch(param)
299 default:
300 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
303 done:
304 UnlockEffectSlotsRead(context);
305 ALCcontext_DecRef(context);
308 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSloti(ALuint effectslot, ALenum param, ALint *value)
310 ALCcontext *context;
311 ALeffectslot *slot;
313 context = GetContextRef();
314 if(!context) return;
316 LockEffectSlotsRead(context);
317 if((slot=LookupEffectSlot(context, effectslot)) == NULL)
318 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
319 switch(param)
321 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
322 *value = slot->AuxSendAuto;
323 break;
325 default:
326 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
329 done:
330 UnlockEffectSlotsRead(context);
331 ALCcontext_DecRef(context);
334 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotiv(ALuint effectslot, ALenum param, ALint *values)
336 ALCcontext *context;
338 switch(param)
340 case AL_EFFECTSLOT_EFFECT:
341 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO:
342 alGetAuxiliaryEffectSloti(effectslot, param, values);
343 return;
346 context = GetContextRef();
347 if(!context) return;
349 LockEffectSlotsRead(context);
350 if(LookupEffectSlot(context, effectslot) == NULL)
351 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
352 switch(param)
354 default:
355 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
358 done:
359 UnlockEffectSlotsRead(context);
360 ALCcontext_DecRef(context);
363 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotf(ALuint effectslot, ALenum param, ALfloat *value)
365 ALCcontext *context;
366 ALeffectslot *slot;
368 context = GetContextRef();
369 if(!context) return;
371 LockEffectSlotsRead(context);
372 if((slot=LookupEffectSlot(context, effectslot)) == NULL)
373 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
374 switch(param)
376 case AL_EFFECTSLOT_GAIN:
377 *value = slot->Gain;
378 break;
380 default:
381 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
384 done:
385 UnlockEffectSlotsRead(context);
386 ALCcontext_DecRef(context);
389 AL_API ALvoid AL_APIENTRY alGetAuxiliaryEffectSlotfv(ALuint effectslot, ALenum param, ALfloat *values)
391 ALCcontext *context;
393 switch(param)
395 case AL_EFFECTSLOT_GAIN:
396 alGetAuxiliaryEffectSlotf(effectslot, param, values);
397 return;
400 context = GetContextRef();
401 if(!context) return;
403 LockEffectSlotsRead(context);
404 if(LookupEffectSlot(context, effectslot) == NULL)
405 SET_ERROR_AND_GOTO(context, AL_INVALID_NAME, done);
406 switch(param)
408 default:
409 SET_ERROR_AND_GOTO(context, AL_INVALID_ENUM, done);
412 done:
413 UnlockEffectSlotsRead(context);
414 ALCcontext_DecRef(context);
418 static void AddEffectSlotList(ALCcontext *context, ALeffectslot *start, ALeffectslot *last)
420 ALeffectslot *root = ATOMIC_LOAD(&context->ActiveAuxSlotList);
421 do {
422 ATOMIC_STORE(&last->next, root, almemory_order_relaxed);
423 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALeffectslot*, &context->ActiveAuxSlotList,
424 &root, start));
427 static void RemoveEffectSlotList(ALCcontext *context, const ALeffectslot *slot)
429 ALCdevice *device = context->Device;
430 const ALeffectslot *root, *next;
432 root = slot;
433 next = ATOMIC_LOAD(&slot->next);
434 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALeffectslot*, &context->ActiveAuxSlotList, &root, next))
436 const ALeffectslot *cur;
437 do {
438 cur = root;
439 root = slot;
440 } while(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALeffectslot*, &cur->next, &root, next));
442 /* Wait for any mix that may be using these effect slots to finish. */
443 while((ReadRef(&device->MixCount)&1) != 0)
444 althrd_yield();
448 void InitEffectFactoryMap(void)
450 InitUIntMap(&EffectStateFactoryMap, ~0);
452 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_NULL, ALnullStateFactory_getFactory);
453 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EAXREVERB, ALreverbStateFactory_getFactory);
454 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_REVERB, ALreverbStateFactory_getFactory);
455 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_CHORUS, ALchorusStateFactory_getFactory);
456 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_COMPRESSOR, ALcompressorStateFactory_getFactory);
457 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DISTORTION, ALdistortionStateFactory_getFactory);
458 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_ECHO, ALechoStateFactory_getFactory);
459 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_EQUALIZER, ALequalizerStateFactory_getFactory);
460 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_FLANGER, ALflangerStateFactory_getFactory);
461 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_RING_MODULATOR, ALmodulatorStateFactory_getFactory);
462 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_DIALOGUE, ALdedicatedStateFactory_getFactory);
463 InsertUIntMapEntry(&EffectStateFactoryMap, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT, ALdedicatedStateFactory_getFactory);
466 void DeinitEffectFactoryMap(void)
468 ResetUIntMap(&EffectStateFactoryMap);
472 ALenum InitializeEffect(ALCdevice *Device, ALeffectslot *EffectSlot, ALeffect *effect)
474 ALenum newtype = (effect ? effect->type : AL_EFFECT_NULL);
475 ALeffectStateFactory *factory;
477 if(newtype != EffectSlot->Effect.Type)
479 ALeffectState *State;
480 FPUCtl oldMode;
482 factory = getFactoryByType(newtype);
483 if(!factory)
485 ERR("Failed to find factory for effect type 0x%04x\n", newtype);
486 return AL_INVALID_ENUM;
488 State = V0(factory,create)();
489 if(!State) return AL_OUT_OF_MEMORY;
491 SetMixerFPUMode(&oldMode);
492 almtx_lock(&Device->BackendLock);
493 State->OutBuffer = Device->Dry.Buffer;
494 State->OutChannels = Device->Dry.NumChannels;
495 if(V(State,deviceUpdate)(Device) == AL_FALSE)
497 almtx_unlock(&Device->BackendLock);
498 RestoreFPUMode(&oldMode);
499 DELETE_OBJ(State);
500 return AL_OUT_OF_MEMORY;
502 almtx_unlock(&Device->BackendLock);
503 RestoreFPUMode(&oldMode);
505 if(!effect)
507 EffectSlot->Effect.Type = AL_EFFECT_NULL;
508 memset(&EffectSlot->Effect.Props, 0, sizeof(EffectSlot->Effect.Props));
510 else
512 EffectSlot->Effect.Type = effect->type;
513 EffectSlot->Effect.Props = effect->Props;
516 EffectSlot->Effect.State = State;
517 UpdateEffectSlotProps(EffectSlot);
519 else if(effect)
521 EffectSlot->Effect.Props = effect->Props;
522 UpdateEffectSlotProps(EffectSlot);
525 return AL_NO_ERROR;
529 void ALeffectState_Destruct(ALeffectState *UNUSED(state))
534 ALenum InitEffectSlot(ALeffectslot *slot)
536 ALeffectStateFactory *factory;
538 slot->Effect.Type = AL_EFFECT_NULL;
540 factory = getFactoryByType(AL_EFFECT_NULL);
541 if(!(slot->Effect.State=V0(factory,create)()))
542 return AL_OUT_OF_MEMORY;
544 slot->Gain = 1.0;
545 slot->AuxSendAuto = AL_TRUE;
546 InitRef(&slot->ref, 0);
548 ATOMIC_INIT(&slot->Update, NULL);
549 ATOMIC_INIT(&slot->FreeList, NULL);
551 slot->Params.Gain = 1.0f;
552 slot->Params.AuxSendAuto = AL_TRUE;
553 slot->Params.EffectState = slot->Effect.State;
554 slot->Params.RoomRolloff = 0.0f;
555 slot->Params.DecayTime = 0.0f;
556 slot->Params.AirAbsorptionGainHF = 1.0f;
558 ATOMIC_INIT(&slot->next, NULL);
560 return AL_NO_ERROR;
563 void DeinitEffectSlot(ALeffectslot *slot)
565 struct ALeffectslotProps *props;
566 ALeffectState *state;
567 size_t count = 0;
569 props = ATOMIC_LOAD(&slot->Update);
570 if(props)
572 state = ATOMIC_LOAD(&props->State, almemory_order_relaxed);
573 if(state != slot->Params.EffectState)
574 DELETE_OBJ(state);
575 TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", props);
576 al_free(props);
578 props = ATOMIC_LOAD(&slot->FreeList, almemory_order_relaxed);
579 while(props)
581 struct ALeffectslotProps *next;
582 state = ATOMIC_LOAD(&props->State, almemory_order_relaxed);
583 next = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
584 DELETE_OBJ(state);
585 al_free(props);
586 props = next;
587 ++count;
589 TRACE("Freed "SZFMT" AuxiliaryEffectSlot property object%s\n", count, (count==1)?"":"s");
591 DELETE_OBJ(slot->Params.EffectState);
594 void UpdateEffectSlotProps(ALeffectslot *slot)
596 struct ALeffectslotProps *props;
597 ALeffectState *oldstate;
599 props = ATOMIC_EXCHANGE(struct ALeffectslotProps*, &slot->Update, NULL);
600 if(props)
602 /* If there was an unapplied update, check if its state object is the
603 * same as the current in-use one, or the one that will be set. If
604 * neither, delete it.
606 oldstate = ATOMIC_EXCHANGE(ALeffectState*, &props->State, NULL, almemory_order_relaxed);
607 if(oldstate != slot->Params.EffectState && oldstate != slot->Effect.State)
608 DELETE_OBJ(oldstate);
610 else
612 /* Get an unused property container, or allocate a new one as needed. */
613 props = ATOMIC_LOAD(&slot->FreeList, almemory_order_relaxed);
614 if(!props)
615 props = al_calloc(16, sizeof(*props));
616 else
618 struct ALeffectslotProps *next;
619 do {
620 next = ATOMIC_LOAD(&props->next, almemory_order_relaxed);
621 } while(ATOMIC_COMPARE_EXCHANGE_WEAK(struct ALeffectslotProps*,
622 &slot->FreeList, &props, next, almemory_order_seq_cst,
623 almemory_order_consume) == 0);
627 /* Copy in current property values. */
628 ATOMIC_STORE(&props->Gain, slot->Gain, almemory_order_relaxed);
629 ATOMIC_STORE(&props->AuxSendAuto, slot->AuxSendAuto, almemory_order_relaxed);
631 ATOMIC_STORE(&props->Type, slot->Effect.Type, almemory_order_relaxed);
632 props->Props = slot->Effect.Props;
633 /* Swap out any stale effect state object there may be in the container, to
634 * delete it.
636 oldstate = ATOMIC_EXCHANGE(ALeffectState*, &props->State, slot->Effect.State,
637 almemory_order_relaxed);
639 /* Set the new container for updating internal parameters. */
640 props = ATOMIC_EXCHANGE(struct ALeffectslotProps*, &slot->Update, props,
641 almemory_order_acq_rel);
642 if(props)
644 /* If there was an unused update container, put it back in the
645 * freelist.
647 struct ALeffectslotProps *first = ATOMIC_LOAD(&slot->FreeList);
648 do {
649 ATOMIC_STORE(&props->next, first, almemory_order_relaxed);
650 } while(ATOMIC_COMPARE_EXCHANGE_WEAK(struct ALeffectslotProps*,
651 &slot->FreeList, &first, props) == 0);
654 DELETE_OBJ(oldstate);
657 ALvoid ReleaseALAuxiliaryEffectSlots(ALCcontext *Context)
659 ALsizei pos;
660 for(pos = 0;pos < Context->EffectSlotMap.size;pos++)
662 ALeffectslot *temp = Context->EffectSlotMap.values[pos];
663 Context->EffectSlotMap.values[pos] = NULL;
665 DeinitEffectSlot(temp);
667 FreeThunkEntry(temp->id);
668 memset(temp, 0, sizeof(ALeffectslot));
669 al_free(temp);