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
29 #include "alAuxEffectSlot.h"
32 #include "alListener.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 RemoveEffectSlotList(ALCcontext
*Context
, ALeffectslot
*slot
);
47 static UIntMap EffectStateFactoryMap
;
48 static inline ALeffectStateFactory
*getFactoryByType(ALenum type
)
50 ALeffectStateFactory
* (*getFactory
)(void) = LookupUIntMapKey(&EffectStateFactoryMap
, type
);
51 if(getFactory
!= NULL
)
56 static void ALeffectState_IncRef(ALeffectState
*state
);
57 static void ALeffectState_DecRef(ALeffectState
*state
);
59 #define DO_UPDATEPROPS() do { \
60 if(!ATOMIC_LOAD(&context->DeferUpdates, almemory_order_acquire)) \
61 UpdateEffectSlotProps(slot); \
63 slot->NeedsUpdate = AL_TRUE; \
67 AL_API ALvoid AL_APIENTRY
alGenAuxiliaryEffectSlots(ALsizei n
, ALuint
*effectslots
)
70 ALeffectslot
*first
, *last
;
74 context
= GetContextRef();
78 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
81 for(cur
= 0;cur
< n
;cur
++)
83 ALeffectslot
*slot
= al_calloc(16, sizeof(ALeffectslot
));
84 err
= AL_OUT_OF_MEMORY
;
85 if(!slot
|| (err
=InitEffectSlot(slot
)) != AL_NO_ERROR
)
88 alDeleteAuxiliaryEffectSlots(cur
, effectslots
);
89 SET_ERROR_AND_GOTO(context
, err
, done
);
92 err
= NewThunkEntry(&slot
->id
);
93 if(err
== AL_NO_ERROR
)
94 err
= InsertUIntMapEntry(&context
->EffectSlotMap
, slot
->id
, slot
);
95 if(err
!= AL_NO_ERROR
)
97 FreeThunkEntry(slot
->id
);
98 ALeffectState_DecRef(slot
->Effect
.State
);
99 if(slot
->Params
.EffectState
)
100 ALeffectState_DecRef(slot
->Params
.EffectState
);
103 alDeleteAuxiliaryEffectSlots(cur
, effectslots
);
104 SET_ERROR_AND_GOTO(context
, err
, done
);
107 aluInitEffectPanning(slot
);
109 if(!first
) first
= slot
;
110 if(last
) ATOMIC_STORE(&last
->next
, slot
, almemory_order_relaxed
);
113 effectslots
[cur
] = slot
->id
;
117 ALeffectslot
*root
= ATOMIC_LOAD_SEQ(&context
->ActiveAuxSlotList
);
119 ATOMIC_STORE(&last
->next
, root
, almemory_order_relaxed
);
120 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK_SEQ(ALeffectslot
*, &context
->ActiveAuxSlotList
,
125 ALCcontext_DecRef(context
);
128 AL_API ALvoid AL_APIENTRY
alDeleteAuxiliaryEffectSlots(ALsizei n
, const ALuint
*effectslots
)
134 context
= GetContextRef();
137 LockEffectSlotsWrite(context
);
139 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
142 if((slot
=LookupEffectSlot(context
, effectslots
[i
])) == NULL
)
143 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
144 if(ReadRef(&slot
->ref
) != 0)
145 SET_ERROR_AND_GOTO(context
, AL_INVALID_OPERATION
, done
);
148 // All effectslots are valid
151 if((slot
=RemoveEffectSlot(context
, effectslots
[i
])) == NULL
)
153 FreeThunkEntry(slot
->id
);
155 RemoveEffectSlotList(context
, slot
);
156 DeinitEffectSlot(slot
);
158 memset(slot
, 0, sizeof(*slot
));
163 UnlockEffectSlotsWrite(context
);
164 ALCcontext_DecRef(context
);
167 AL_API ALboolean AL_APIENTRY
alIsAuxiliaryEffectSlot(ALuint effectslot
)
172 context
= GetContextRef();
173 if(!context
) return AL_FALSE
;
175 LockEffectSlotsRead(context
);
176 ret
= (LookupEffectSlot(context
, effectslot
) ? AL_TRUE
: AL_FALSE
);
177 UnlockEffectSlotsRead(context
);
179 ALCcontext_DecRef(context
);
184 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint value
)
189 ALeffect
*effect
= NULL
;
192 context
= GetContextRef();
195 WriteLock(&context
->PropLock
);
196 LockEffectSlotsRead(context
);
197 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
198 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
201 case AL_EFFECTSLOT_EFFECT
:
202 device
= context
->Device
;
204 LockEffectsRead(device
);
205 effect
= (value
? LookupEffect(device
, value
) : NULL
);
206 if(!(value
== 0 || effect
!= NULL
))
208 UnlockEffectsRead(device
);
209 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
211 err
= InitializeEffect(device
, slot
, effect
);
212 UnlockEffectsRead(device
);
214 if(err
!= AL_NO_ERROR
)
215 SET_ERROR_AND_GOTO(context
, err
, done
);
218 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
219 if(!(value
== AL_TRUE
|| value
== AL_FALSE
))
220 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
221 slot
->AuxSendAuto
= value
;
225 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
230 UnlockEffectSlotsRead(context
);
231 WriteUnlock(&context
->PropLock
);
232 ALCcontext_DecRef(context
);
235 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, const ALint
*values
)
241 case AL_EFFECTSLOT_EFFECT
:
242 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
243 alAuxiliaryEffectSloti(effectslot
, param
, values
[0]);
247 context
= GetContextRef();
250 LockEffectSlotsRead(context
);
251 if(LookupEffectSlot(context
, effectslot
) == NULL
)
252 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
256 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
260 UnlockEffectSlotsRead(context
);
261 ALCcontext_DecRef(context
);
264 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat value
)
269 context
= GetContextRef();
272 WriteLock(&context
->PropLock
);
273 LockEffectSlotsRead(context
);
274 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
275 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
278 case AL_EFFECTSLOT_GAIN
:
279 if(!(value
>= 0.0f
&& value
<= 1.0f
))
280 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
285 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
290 UnlockEffectSlotsRead(context
);
291 WriteUnlock(&context
->PropLock
);
292 ALCcontext_DecRef(context
);
295 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, const ALfloat
*values
)
301 case AL_EFFECTSLOT_GAIN
:
302 alAuxiliaryEffectSlotf(effectslot
, param
, values
[0]);
306 context
= GetContextRef();
309 LockEffectSlotsRead(context
);
310 if(LookupEffectSlot(context
, effectslot
) == NULL
)
311 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
315 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
319 UnlockEffectSlotsRead(context
);
320 ALCcontext_DecRef(context
);
323 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint
*value
)
328 context
= GetContextRef();
331 LockEffectSlotsRead(context
);
332 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
333 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
336 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
337 *value
= slot
->AuxSendAuto
;
341 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
345 UnlockEffectSlotsRead(context
);
346 ALCcontext_DecRef(context
);
349 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, ALint
*values
)
355 case AL_EFFECTSLOT_EFFECT
:
356 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
357 alGetAuxiliaryEffectSloti(effectslot
, param
, values
);
361 context
= GetContextRef();
364 LockEffectSlotsRead(context
);
365 if(LookupEffectSlot(context
, effectslot
) == NULL
)
366 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
370 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
374 UnlockEffectSlotsRead(context
);
375 ALCcontext_DecRef(context
);
378 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat
*value
)
383 context
= GetContextRef();
386 LockEffectSlotsRead(context
);
387 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
388 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
391 case AL_EFFECTSLOT_GAIN
:
396 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
400 UnlockEffectSlotsRead(context
);
401 ALCcontext_DecRef(context
);
404 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, ALfloat
*values
)
410 case AL_EFFECTSLOT_GAIN
:
411 alGetAuxiliaryEffectSlotf(effectslot
, param
, values
);
415 context
= GetContextRef();
418 LockEffectSlotsRead(context
);
419 if(LookupEffectSlot(context
, effectslot
) == NULL
)
420 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
424 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
428 UnlockEffectSlotsRead(context
);
429 ALCcontext_DecRef(context
);
433 static void RemoveEffectSlotList(ALCcontext
*context
, ALeffectslot
*slot
)
435 ALCdevice
*device
= context
->Device
;
436 ALeffectslot
*root
, *next
;
439 next
= ATOMIC_LOAD_SEQ(&slot
->next
);
440 if(!ATOMIC_COMPARE_EXCHANGE_STRONG_SEQ(ALeffectslot
*, &context
->ActiveAuxSlotList
, &root
, next
))
446 } while(!ATOMIC_COMPARE_EXCHANGE_STRONG_SEQ(ALeffectslot
*, &cur
->next
, &root
, next
));
448 /* Wait for any mix that may be using these effect slots to finish. */
449 while((ReadRef(&device
->MixCount
)&1) != 0)
454 void InitEffectFactoryMap(void)
456 InitUIntMap(&EffectStateFactoryMap
, ~0);
458 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_NULL
, ALnullStateFactory_getFactory
);
459 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_EAXREVERB
, ALreverbStateFactory_getFactory
);
460 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_REVERB
, ALreverbStateFactory_getFactory
);
461 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_CHORUS
, ALchorusStateFactory_getFactory
);
462 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_COMPRESSOR
, ALcompressorStateFactory_getFactory
);
463 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_DISTORTION
, ALdistortionStateFactory_getFactory
);
464 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_ECHO
, ALechoStateFactory_getFactory
);
465 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_EQUALIZER
, ALequalizerStateFactory_getFactory
);
466 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_FLANGER
, ALflangerStateFactory_getFactory
);
467 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_RING_MODULATOR
, ALmodulatorStateFactory_getFactory
);
468 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_DEDICATED_DIALOGUE
, ALdedicatedStateFactory_getFactory
);
469 InsertUIntMapEntry(&EffectStateFactoryMap
, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT
, ALdedicatedStateFactory_getFactory
);
472 void DeinitEffectFactoryMap(void)
474 ResetUIntMap(&EffectStateFactoryMap
);
478 ALenum
InitializeEffect(ALCdevice
*Device
, ALeffectslot
*EffectSlot
, ALeffect
*effect
)
480 ALenum newtype
= (effect
? effect
->type
: AL_EFFECT_NULL
);
481 struct ALeffectslotProps
*props
;
482 ALeffectState
*State
;
484 if(newtype
!= EffectSlot
->Effect
.Type
)
486 ALeffectStateFactory
*factory
;
489 factory
= getFactoryByType(newtype
);
492 ERR("Failed to find factory for effect type 0x%04x\n", newtype
);
493 return AL_INVALID_ENUM
;
495 State
= V0(factory
,create
)();
496 if(!State
) return AL_OUT_OF_MEMORY
;
498 SetMixerFPUMode(&oldMode
);
499 almtx_lock(&Device
->BackendLock
);
500 State
->OutBuffer
= Device
->Dry
.Buffer
;
501 State
->OutChannels
= Device
->Dry
.NumChannels
;
502 if(V(State
,deviceUpdate
)(Device
) == AL_FALSE
)
504 almtx_unlock(&Device
->BackendLock
);
505 RestoreFPUMode(&oldMode
);
506 ALeffectState_DecRef(State
);
507 return AL_OUT_OF_MEMORY
;
509 almtx_unlock(&Device
->BackendLock
);
510 RestoreFPUMode(&oldMode
);
514 EffectSlot
->Effect
.Type
= AL_EFFECT_NULL
;
515 memset(&EffectSlot
->Effect
.Props
, 0, sizeof(EffectSlot
->Effect
.Props
));
519 EffectSlot
->Effect
.Type
= effect
->type
;
520 EffectSlot
->Effect
.Props
= effect
->Props
;
523 ALeffectState_DecRef(EffectSlot
->Effect
.State
);
524 EffectSlot
->Effect
.State
= State
;
527 EffectSlot
->Effect
.Props
= effect
->Props
;
529 /* Remove state references from old effect slot property updates. */
530 props
= ATOMIC_LOAD_SEQ(&EffectSlot
->FreeList
);
533 State
= ATOMIC_EXCHANGE(ALeffectState
*, &props
->State
, NULL
, almemory_order_relaxed
);
534 if(State
) ALeffectState_DecRef(State
);
535 props
= ATOMIC_LOAD(&props
->next
, almemory_order_relaxed
);
542 static void ALeffectState_IncRef(ALeffectState
*state
)
545 ref
= IncrementRef(&state
->Ref
);
546 TRACEREF("%p increasing refcount to %u\n", state
, ref
);
549 static void ALeffectState_DecRef(ALeffectState
*state
)
552 ref
= DecrementRef(&state
->Ref
);
553 TRACEREF("%p decreasing refcount to %u\n", state
, ref
);
554 if(ref
== 0) DELETE_OBJ(state
);
558 void ALeffectState_Construct(ALeffectState
*state
)
560 InitRef(&state
->Ref
, 1);
562 state
->OutBuffer
= NULL
;
563 state
->OutChannels
= 0;
566 void ALeffectState_Destruct(ALeffectState
*UNUSED(state
))
571 ALenum
InitEffectSlot(ALeffectslot
*slot
)
573 ALeffectStateFactory
*factory
;
575 slot
->Effect
.Type
= AL_EFFECT_NULL
;
577 factory
= getFactoryByType(AL_EFFECT_NULL
);
578 if(!(slot
->Effect
.State
=V0(factory
,create
)()))
579 return AL_OUT_OF_MEMORY
;
581 slot
->NeedsUpdate
= AL_FALSE
;
583 slot
->AuxSendAuto
= AL_TRUE
;
584 InitRef(&slot
->ref
, 0);
586 ATOMIC_INIT(&slot
->Update
, NULL
);
587 ATOMIC_INIT(&slot
->FreeList
, NULL
);
589 slot
->Params
.Gain
= 1.0f
;
590 slot
->Params
.AuxSendAuto
= AL_TRUE
;
591 ALeffectState_IncRef(slot
->Effect
.State
);
592 slot
->Params
.EffectState
= slot
->Effect
.State
;
593 slot
->Params
.RoomRolloff
= 0.0f
;
594 slot
->Params
.DecayTime
= 0.0f
;
595 slot
->Params
.AirAbsorptionGainHF
= 1.0f
;
597 ATOMIC_INIT(&slot
->next
, NULL
);
602 void DeinitEffectSlot(ALeffectslot
*slot
)
604 struct ALeffectslotProps
*props
;
605 ALeffectState
*state
;
608 props
= ATOMIC_LOAD_SEQ(&slot
->Update
);
611 state
= ATOMIC_LOAD(&props
->State
, almemory_order_relaxed
);
612 if(state
) ALeffectState_DecRef(state
);
613 TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", props
);
616 props
= ATOMIC_LOAD(&slot
->FreeList
, almemory_order_relaxed
);
619 struct ALeffectslotProps
*next
;
620 state
= ATOMIC_LOAD(&props
->State
, almemory_order_relaxed
);
621 next
= ATOMIC_LOAD(&props
->next
, almemory_order_relaxed
);
622 if(state
) ALeffectState_DecRef(state
);
627 TRACE("Freed "SZFMT
" AuxiliaryEffectSlot property object%s\n", count
, (count
==1)?"":"s");
629 ALeffectState_DecRef(slot
->Effect
.State
);
630 if(slot
->Params
.EffectState
)
631 ALeffectState_DecRef(slot
->Params
.EffectState
);
634 void UpdateEffectSlotProps(ALeffectslot
*slot
)
636 struct ALeffectslotProps
*props
;
637 ALeffectState
*oldstate
;
639 /* Get an unused property container, or allocate a new one as needed. */
640 props
= ATOMIC_LOAD(&slot
->FreeList
, almemory_order_relaxed
);
642 props
= al_calloc(16, sizeof(*props
));
645 struct ALeffectslotProps
*next
;
647 next
= ATOMIC_LOAD(&props
->next
, almemory_order_relaxed
);
648 } while(ATOMIC_COMPARE_EXCHANGE_WEAK(struct ALeffectslotProps
*,
649 &slot
->FreeList
, &props
, next
, almemory_order_seq_cst
,
650 almemory_order_acquire
) == 0);
653 /* Copy in current property values. */
654 ATOMIC_STORE(&props
->Gain
, slot
->Gain
, almemory_order_relaxed
);
655 ATOMIC_STORE(&props
->AuxSendAuto
, slot
->AuxSendAuto
, almemory_order_relaxed
);
657 ATOMIC_STORE(&props
->Type
, slot
->Effect
.Type
, almemory_order_relaxed
);
658 props
->Props
= slot
->Effect
.Props
;
659 /* Swap out any stale effect state object there may be in the container, to
662 ALeffectState_IncRef(slot
->Effect
.State
);
663 oldstate
= ATOMIC_EXCHANGE(ALeffectState
*, &props
->State
, slot
->Effect
.State
,
664 almemory_order_relaxed
);
666 /* Set the new container for updating internal parameters. */
667 props
= ATOMIC_EXCHANGE(struct ALeffectslotProps
*, &slot
->Update
, props
,
668 almemory_order_acq_rel
);
671 /* If there was an unused update container, put it back in the
674 ATOMIC_REPLACE_HEAD(struct ALeffectslotProps
*, &slot
->FreeList
, props
);
678 ALeffectState_DecRef(oldstate
);
681 void UpdateAllEffectSlotProps(ALCcontext
*context
)
685 LockEffectSlotsRead(context
);
686 slot
= ATOMIC_LOAD_SEQ(&context
->ActiveAuxSlotList
);
689 if(slot
->NeedsUpdate
)
690 UpdateEffectSlotProps(slot
);
691 slot
->NeedsUpdate
= AL_FALSE
;
692 slot
= ATOMIC_LOAD(&slot
->next
, almemory_order_relaxed
);
694 UnlockEffectSlotsRead(context
);
697 ALvoid
ReleaseALAuxiliaryEffectSlots(ALCcontext
*Context
)
700 for(pos
= 0;pos
< Context
->EffectSlotMap
.size
;pos
++)
702 ALeffectslot
*temp
= Context
->EffectSlotMap
.values
[pos
];
703 Context
->EffectSlotMap
.values
[pos
] = NULL
;
705 DeinitEffectSlot(temp
);
707 FreeThunkEntry(temp
->id
);
708 memset(temp
, 0, sizeof(ALeffectslot
));