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 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
)
59 AL_API ALvoid AL_APIENTRY
alGenAuxiliaryEffectSlots(ALsizei n
, ALuint
*effectslots
)
62 ALeffectslot
*first
, *last
;
66 context
= GetContextRef();
70 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
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
)
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
);
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
);
103 effectslots
[cur
] = slot
->id
;
105 AddEffectSlotList(context
, first
, last
);
108 ALCcontext_DecRef(context
);
111 AL_API ALvoid AL_APIENTRY
alDeleteAuxiliaryEffectSlots(ALsizei n
, const ALuint
*effectslots
)
117 context
= GetContextRef();
120 LockEffectSlotsWrite(context
);
122 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
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
134 if((slot
=RemoveEffectSlot(context
, effectslots
[i
])) == NULL
)
136 FreeThunkEntry(slot
->id
);
138 RemoveEffectSlotList(context
, slot
);
139 DeinitEffectSlot(slot
);
141 memset(slot
, 0, sizeof(*slot
));
146 UnlockEffectSlotsWrite(context
);
147 ALCcontext_DecRef(context
);
150 AL_API ALboolean AL_APIENTRY
alIsAuxiliaryEffectSlot(ALuint effectslot
)
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
);
167 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint value
)
172 ALeffect
*effect
= NULL
;
175 context
= GetContextRef();
178 WriteLock(&context
->PropLock
);
179 LockEffectSlotsRead(context
);
180 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
181 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
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
);
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
);
211 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, 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
)
226 case AL_EFFECTSLOT_EFFECT
:
227 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
228 alAuxiliaryEffectSloti(effectslot
, param
, values
[0]);
232 context
= GetContextRef();
235 LockEffectSlotsRead(context
);
236 if(LookupEffectSlot(context
, effectslot
) == NULL
)
237 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
241 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
245 UnlockEffectSlotsRead(context
);
246 ALCcontext_DecRef(context
);
249 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat value
)
254 context
= GetContextRef();
257 WriteLock(&context
->PropLock
);
258 LockEffectSlotsRead(context
);
259 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
260 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
263 case AL_EFFECTSLOT_GAIN
:
264 if(!(value
>= 0.0f
&& value
<= 1.0f
))
265 SET_ERROR_AND_GOTO(context
, AL_INVALID_VALUE
, done
);
270 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
272 UpdateEffectSlotProps(slot
);
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
)
286 case AL_EFFECTSLOT_GAIN
:
287 alAuxiliaryEffectSlotf(effectslot
, param
, values
[0]);
291 context
= GetContextRef();
294 LockEffectSlotsRead(context
);
295 if(LookupEffectSlot(context
, effectslot
) == NULL
)
296 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
300 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
304 UnlockEffectSlotsRead(context
);
305 ALCcontext_DecRef(context
);
308 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint
*value
)
313 context
= GetContextRef();
316 LockEffectSlotsRead(context
);
317 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
318 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
321 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
322 *value
= slot
->AuxSendAuto
;
326 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
330 UnlockEffectSlotsRead(context
);
331 ALCcontext_DecRef(context
);
334 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, ALint
*values
)
340 case AL_EFFECTSLOT_EFFECT
:
341 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
342 alGetAuxiliaryEffectSloti(effectslot
, param
, values
);
346 context
= GetContextRef();
349 LockEffectSlotsRead(context
);
350 if(LookupEffectSlot(context
, effectslot
) == NULL
)
351 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
355 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
359 UnlockEffectSlotsRead(context
);
360 ALCcontext_DecRef(context
);
363 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat
*value
)
368 context
= GetContextRef();
371 LockEffectSlotsRead(context
);
372 if((slot
=LookupEffectSlot(context
, effectslot
)) == NULL
)
373 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
376 case AL_EFFECTSLOT_GAIN
:
381 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, done
);
385 UnlockEffectSlotsRead(context
);
386 ALCcontext_DecRef(context
);
389 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, ALfloat
*values
)
395 case AL_EFFECTSLOT_GAIN
:
396 alGetAuxiliaryEffectSlotf(effectslot
, param
, values
);
400 context
= GetContextRef();
403 LockEffectSlotsRead(context
);
404 if(LookupEffectSlot(context
, effectslot
) == NULL
)
405 SET_ERROR_AND_GOTO(context
, AL_INVALID_NAME
, done
);
409 SET_ERROR_AND_GOTO(context
, AL_INVALID_ENUM
, 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
);
422 ATOMIC_STORE(&last
->next
, root
, almemory_order_relaxed
);
423 } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALeffectslot
*, &context
->ActiveAuxSlotList
,
427 static void RemoveEffectSlotList(ALCcontext
*context
, const ALeffectslot
*slot
)
429 ALCdevice
*device
= context
->Device
;
430 const ALeffectslot
*root
, *next
;
433 next
= ATOMIC_LOAD(&slot
->next
);
434 if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALeffectslot
*, &context
->ActiveAuxSlotList
, &root
, next
))
436 const ALeffectslot
*cur
;
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)
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
;
482 factory
= getFactoryByType(newtype
);
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
);
500 return AL_OUT_OF_MEMORY
;
502 almtx_unlock(&Device
->BackendLock
);
503 RestoreFPUMode(&oldMode
);
507 EffectSlot
->Effect
.Type
= AL_EFFECT_NULL
;
508 memset(&EffectSlot
->Effect
.Props
, 0, sizeof(EffectSlot
->Effect
.Props
));
512 EffectSlot
->Effect
.Type
= effect
->type
;
513 EffectSlot
->Effect
.Props
= effect
->Props
;
516 EffectSlot
->Effect
.State
= State
;
517 UpdateEffectSlotProps(EffectSlot
);
521 EffectSlot
->Effect
.Props
= effect
->Props
;
522 UpdateEffectSlotProps(EffectSlot
);
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
;
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
);
563 void DeinitEffectSlot(ALeffectslot
*slot
)
565 struct ALeffectslotProps
*props
;
566 ALeffectState
*state
;
569 props
= ATOMIC_LOAD(&slot
->Update
);
572 state
= ATOMIC_LOAD(&props
->State
, almemory_order_relaxed
);
573 if(state
!= slot
->Params
.EffectState
)
575 TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", props
);
578 props
= ATOMIC_LOAD(&slot
->FreeList
, almemory_order_relaxed
);
581 struct ALeffectslotProps
*next
;
582 state
= ATOMIC_LOAD(&props
->State
, almemory_order_relaxed
);
583 next
= ATOMIC_LOAD(&props
->next
, almemory_order_relaxed
);
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
);
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
);
612 /* Get an unused property container, or allocate a new one as needed. */
613 props
= ATOMIC_LOAD(&slot
->FreeList
, almemory_order_relaxed
);
615 props
= al_calloc(16, sizeof(*props
));
618 struct ALeffectslotProps
*next
;
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
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
);
644 /* If there was an unused update container, put it back in the
647 struct ALeffectslotProps
*first
= ATOMIC_LOAD(&slot
->FreeList
);
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
)
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
));