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
33 #include "alcontext.h"
34 #include "alAuxEffectSlot.h"
36 #include "alListener.h"
39 #include "fpu_modes.h"
45 inline ALeffectslot
*LookupEffectSlot(ALCcontext
*context
, ALuint id
) noexcept
48 if(UNLIKELY(id
>= context
->EffectSlotList
.size()))
50 return context
->EffectSlotList
[id
].get();
53 inline ALeffect
*LookupEffect(ALCdevice
*device
, ALuint id
) noexcept
55 ALuint lidx
= (id
-1) >> 6;
56 ALsizei slidx
= (id
-1) & 0x3f;
58 if(UNLIKELY(lidx
>= device
->EffectList
.size()))
60 EffectSubList
&sublist
= device
->EffectList
[lidx
];
61 if(UNLIKELY(sublist
.FreeMask
& (U64(1)<<slidx
)))
63 return sublist
.Effects
+ slidx
;
67 void AddActiveEffectSlots(const ALuint
*slotids
, ALsizei count
, ALCcontext
*context
)
70 ALeffectslotArray
*curarray
{context
->ActiveAuxSlots
.load(std::memory_order_acquire
)};
71 ALsizei newcount
{curarray
->count
+ count
};
73 /* Insert the new effect slots into the head of the array, followed by the
76 auto newarray
= static_cast<ALeffectslotArray
*>(al_calloc(DEF_ALIGN
,
77 FAM_SIZE(ALeffectslotArray
, slot
, newcount
)));
78 newarray
->count
= newcount
;
79 auto slotiter
= std::transform(slotids
, slotids
+count
, newarray
->slot
,
80 [context
](ALuint id
) noexcept
-> ALeffectslot
*
81 { return LookupEffectSlot(context
, id
); }
83 std::copy_n(curarray
->slot
, curarray
->count
, slotiter
);
85 /* Remove any duplicates (first instance of each will be kept). */
86 ALeffectslot
**last
= newarray
->slot
+ newarray
->count
;
87 for(ALeffectslot
**start
=newarray
->slot
+1;;)
89 last
= std::remove(start
, last
, *(start
-1));
90 if(start
== last
) break;
93 newcount
= std::distance(newarray
->slot
, last
);
95 /* Reallocate newarray if the new size ended up smaller from duplicate
98 if(UNLIKELY(newcount
< newarray
->count
))
101 newarray
= static_cast<ALeffectslotArray
*>(al_calloc(DEF_ALIGN
,
102 FAM_SIZE(ALeffectslotArray
, slot
, newcount
)));
103 newarray
->count
= newcount
;
104 std::copy_n(curarray
->slot
, newcount
, newarray
->slot
);
109 curarray
= context
->ActiveAuxSlots
.exchange(newarray
, std::memory_order_acq_rel
);
110 ALCdevice
*device
{context
->Device
};
111 while((device
->MixCount
.load(std::memory_order_acquire
)&1))
112 std::this_thread::yield();
116 void RemoveActiveEffectSlots(const ALuint
*slotids
, ALsizei count
, ALCcontext
*context
)
118 if(count
< 1) return;
119 ALeffectslotArray
*curarray
{context
->ActiveAuxSlots
.load(std::memory_order_acquire
)};
121 /* Don't shrink the allocated array size since we don't know how many (if
122 * any) of the effect slots to remove are in the array.
124 auto newarray
= static_cast<ALeffectslotArray
*>(al_calloc(DEF_ALIGN
,
125 FAM_SIZE(ALeffectslotArray
, slot
, curarray
->count
)));
127 /* Copy each element in curarray to newarray whose ID is not in slotids. */
128 const ALuint
*slotids_end
{slotids
+ count
};
129 auto slotiter
= std::copy_if(curarray
->slot
, curarray
->slot
+curarray
->count
, newarray
->slot
,
130 [slotids
, slotids_end
](const ALeffectslot
*slot
) -> bool
131 { return std::find(slotids
, slotids_end
, slot
->id
) == slotids_end
; }
133 newarray
->count
= std::distance(newarray
->slot
, slotiter
);
135 /* TODO: Could reallocate newarray now that we know it's needed size. */
137 curarray
= context
->ActiveAuxSlots
.exchange(newarray
, std::memory_order_acq_rel
);
138 ALCdevice
*device
{context
->Device
};
139 while((device
->MixCount
.load(std::memory_order_acquire
)&1))
140 std::this_thread::yield();
144 constexpr struct FactoryItem
{
146 EffectStateFactory
* (&GetFactory
)(void);
148 { AL_EFFECT_NULL
, NullStateFactory_getFactory
},
149 { AL_EFFECT_EAXREVERB
, ReverbStateFactory_getFactory
},
150 { AL_EFFECT_REVERB
, ReverbStateFactory_getFactory
},
151 { AL_EFFECT_AUTOWAH
, AutowahStateFactory_getFactory
},
152 { AL_EFFECT_CHORUS
, ChorusStateFactory_getFactory
},
153 { AL_EFFECT_COMPRESSOR
, CompressorStateFactory_getFactory
},
154 { AL_EFFECT_DISTORTION
, DistortionStateFactory_getFactory
},
155 { AL_EFFECT_ECHO
, EchoStateFactory_getFactory
},
156 { AL_EFFECT_EQUALIZER
, EqualizerStateFactory_getFactory
},
157 { AL_EFFECT_FLANGER
, FlangerStateFactory_getFactory
},
158 { AL_EFFECT_FREQUENCY_SHIFTER
, FshifterStateFactory_getFactory
},
159 { AL_EFFECT_RING_MODULATOR
, ModulatorStateFactory_getFactory
},
160 { AL_EFFECT_PITCH_SHIFTER
, PshifterStateFactory_getFactory
},
161 { AL_EFFECT_DEDICATED_DIALOGUE
, DedicatedStateFactory_getFactory
},
162 { AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT
, DedicatedStateFactory_getFactory
}
165 inline EffectStateFactory
*getFactoryByType(ALenum type
)
167 auto iter
= std::find_if(std::begin(FactoryList
), std::end(FactoryList
),
168 [type
](const FactoryItem
&item
) noexcept
-> bool
169 { return item
.Type
== type
; }
171 return (iter
!= std::end(FactoryList
)) ? iter
->GetFactory() : nullptr;
175 #define DO_UPDATEPROPS() do { \
176 if(!context->DeferUpdates.load(std::memory_order_acquire)) \
177 UpdateEffectSlotProps(slot, context.get()); \
179 slot->PropsClean.clear(std::memory_order_release); \
184 AL_API ALvoid AL_APIENTRY
alGenAuxiliaryEffectSlots(ALsizei n
, ALuint
*effectslots
)
186 ContextRef context
{GetContextRef()};
187 if(UNLIKELY(!context
)) return;
190 SETERR_RETURN(context
.get(), AL_INVALID_VALUE
,, "Generating %d effect slots", n
);
193 std::unique_lock
<std::mutex
> slotlock
{context
->EffectSlotLock
};
194 ALCdevice
*device
{context
->Device
};
195 for(ALsizei cur
{0};cur
< n
;cur
++)
197 auto iter
= std::find_if(context
->EffectSlotList
.begin(), context
->EffectSlotList
.end(),
198 [](const ALeffectslotPtr
&entry
) noexcept
-> bool
201 if(iter
== context
->EffectSlotList
.end())
203 if(UNLIKELY(device
->AuxiliaryEffectSlotMax
== context
->EffectSlotList
.size()))
206 alDeleteAuxiliaryEffectSlots(cur
, effectslots
);
207 alSetError(context
.get(), AL_OUT_OF_MEMORY
,
208 "Exceeding %u auxiliary effect slot limit", device
->AuxiliaryEffectSlotMax
);
211 context
->EffectSlotList
.emplace_back(nullptr);
212 iter
= context
->EffectSlotList
.end() - 1;
215 *iter
= std::unique_ptr
<ALeffectslot
>(new ALeffectslot
{});
216 ALenum err
{InitEffectSlot(iter
->get())};
217 if(err
!= AL_NO_ERROR
)
222 alDeleteAuxiliaryEffectSlots(cur
, effectslots
);
223 alSetError(context
.get(), err
, "Effect slot object allocation failed");
226 aluInitEffectPanning(iter
->get());
228 ALuint id
= std::distance(context
->EffectSlotList
.begin(), iter
) + 1;
230 effectslots
[cur
] = id
;
232 AddActiveEffectSlots(effectslots
, n
, context
.get());
235 AL_API ALvoid AL_APIENTRY
alDeleteAuxiliaryEffectSlots(ALsizei n
, const ALuint
*effectslots
)
237 ContextRef context
{GetContextRef()};
238 if(UNLIKELY(!context
)) return;
241 SETERR_RETURN(context
.get(), AL_INVALID_VALUE
,, "Deleting %d effect slots", n
);
244 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
245 auto effectslots_end
= effectslots
+ n
;
246 auto bad_slot
= std::find_if(effectslots
, effectslots_end
,
247 [&context
](ALuint id
) -> bool
249 ALeffectslot
*slot
{LookupEffectSlot(context
.get(), id
)};
252 alSetError(context
.get(), AL_INVALID_NAME
, "Invalid effect slot ID %u", id
);
255 if(ReadRef(&slot
->ref
) != 0)
257 alSetError(context
.get(), AL_INVALID_NAME
, "Deleting in-use effect slot %u", id
);
263 if(bad_slot
!= effectslots_end
)
266 // All effectslots are valid, remove and delete them
267 RemoveActiveEffectSlots(effectslots
, n
, context
.get());
268 std::for_each(effectslots
, effectslots_end
,
269 [&context
](ALuint id
) noexcept
-> void
270 { context
->EffectSlotList
[id
-1] = nullptr; }
274 AL_API ALboolean AL_APIENTRY
alIsAuxiliaryEffectSlot(ALuint effectslot
)
276 ContextRef context
{GetContextRef()};
279 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
280 if(LookupEffectSlot(context
.get(), effectslot
) != nullptr)
286 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint value
)
288 ContextRef context
{GetContextRef()};
289 if(UNLIKELY(!context
)) return;
291 std::lock_guard
<std::mutex
> _
{context
->PropLock
};
292 std::lock_guard
<std::mutex
> __
{context
->EffectSlotLock
};
293 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
295 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
301 case AL_EFFECTSLOT_EFFECT
:
302 device
= context
->Device
;
304 { std::lock_guard
<std::mutex
> ___
{device
->EffectLock
};
305 ALeffect
*effect
{value
? LookupEffect(device
, value
) : nullptr};
306 if(!(value
== 0 || effect
!= nullptr))
307 SETERR_RETURN(context
.get(), AL_INVALID_VALUE
,, "Invalid effect ID %u", value
);
308 err
= InitializeEffect(context
.get(), slot
, effect
);
310 if(err
!= AL_NO_ERROR
)
312 alSetError(context
.get(), err
, "Effect initialization failed");
317 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
318 if(!(value
== AL_TRUE
|| value
== AL_FALSE
))
319 SETERR_RETURN(context
.get(), AL_INVALID_VALUE
,,
320 "Effect slot auxiliary send auto out of range");
321 slot
->AuxSendAuto
= value
;
325 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,,
326 "Invalid effect slot integer property 0x%04x", param
);
331 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, const ALint
*values
)
335 case AL_EFFECTSLOT_EFFECT
:
336 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
337 alAuxiliaryEffectSloti(effectslot
, param
, values
[0]);
341 ContextRef context
{GetContextRef()};
342 if(UNLIKELY(!context
)) return;
344 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
345 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
347 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
352 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,,
353 "Invalid effect slot integer-vector property 0x%04x", param
);
357 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat value
)
359 ContextRef context
{GetContextRef()};
360 if(UNLIKELY(!context
)) return;
362 std::lock_guard
<std::mutex
> _
{context
->PropLock
};
363 std::lock_guard
<std::mutex
> __
{context
->EffectSlotLock
};
364 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
366 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
370 case AL_EFFECTSLOT_GAIN
:
371 if(!(value
>= 0.0f
&& value
<= 1.0f
))
372 SETERR_RETURN(context
.get(), AL_INVALID_VALUE
,, "Effect slot gain out of range");
377 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,, "Invalid effect slot float property 0x%04x",
383 AL_API ALvoid AL_APIENTRY
alAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, const ALfloat
*values
)
387 case AL_EFFECTSLOT_GAIN
:
388 alAuxiliaryEffectSlotf(effectslot
, param
, values
[0]);
392 ContextRef context
{GetContextRef()};
393 if(UNLIKELY(!context
)) return;
395 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
396 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
398 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
403 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,,
404 "Invalid effect slot float-vector property 0x%04x", param
);
408 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSloti(ALuint effectslot
, ALenum param
, ALint
*value
)
410 ContextRef context
{GetContextRef()};
411 if(UNLIKELY(!context
)) return;
413 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
414 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
416 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
420 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
421 *value
= slot
->AuxSendAuto
;
425 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,,
426 "Invalid effect slot integer property 0x%04x", param
);
430 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotiv(ALuint effectslot
, ALenum param
, ALint
*values
)
434 case AL_EFFECTSLOT_EFFECT
:
435 case AL_EFFECTSLOT_AUXILIARY_SEND_AUTO
:
436 alGetAuxiliaryEffectSloti(effectslot
, param
, values
);
440 ContextRef context
{GetContextRef()};
441 if(UNLIKELY(!context
)) return;
443 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
444 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
446 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
451 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,,
452 "Invalid effect slot integer-vector property 0x%04x", param
);
456 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotf(ALuint effectslot
, ALenum param
, ALfloat
*value
)
458 ContextRef context
{GetContextRef()};
459 if(UNLIKELY(!context
)) return;
461 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
462 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
464 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
468 case AL_EFFECTSLOT_GAIN
:
473 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,,
474 "Invalid effect slot float property 0x%04x", param
);
478 AL_API ALvoid AL_APIENTRY
alGetAuxiliaryEffectSlotfv(ALuint effectslot
, ALenum param
, ALfloat
*values
)
482 case AL_EFFECTSLOT_GAIN
:
483 alGetAuxiliaryEffectSlotf(effectslot
, param
, values
);
487 ContextRef context
{GetContextRef()};
488 if(UNLIKELY(!context
)) return;
490 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
491 ALeffectslot
*slot
= LookupEffectSlot(context
.get(), effectslot
);
493 SETERR_RETURN(context
.get(), AL_INVALID_NAME
,, "Invalid effect slot ID %u", effectslot
);
498 SETERR_RETURN(context
.get(), AL_INVALID_ENUM
,,
499 "Invalid effect slot float-vector property 0x%04x", param
);
504 ALenum
InitializeEffect(ALCcontext
*Context
, ALeffectslot
*EffectSlot
, ALeffect
*effect
)
506 ALenum newtype
{effect
? effect
->type
: AL_EFFECT_NULL
};
507 if(newtype
!= EffectSlot
->Effect
.Type
)
509 EffectStateFactory
*factory
{getFactoryByType(newtype
)};
512 ERR("Failed to find factory for effect type 0x%04x\n", newtype
);
513 return AL_INVALID_ENUM
;
515 EffectState
*State
{factory
->create()};
516 if(!State
) return AL_OUT_OF_MEMORY
;
519 ALCdevice
*Device
{Context
->Device
};
520 std::unique_lock
<std::mutex
> backlock
{Device
->BackendLock
};
521 State
->mOutBuffer
= Device
->Dry
.Buffer
;
522 State
->mOutChannels
= Device
->Dry
.NumChannels
;
523 if(State
->deviceUpdate(Device
) == AL_FALSE
)
528 return AL_OUT_OF_MEMORY
;
534 EffectSlot
->Effect
.Type
= AL_EFFECT_NULL
;
535 memset(&EffectSlot
->Effect
.Props
, 0, sizeof(EffectSlot
->Effect
.Props
));
539 EffectSlot
->Effect
.Type
= effect
->type
;
540 EffectSlot
->Effect
.Props
= effect
->Props
;
543 EffectSlot
->Effect
.State
->DecRef();
544 EffectSlot
->Effect
.State
= State
;
547 EffectSlot
->Effect
.Props
= effect
->Props
;
549 /* Remove state references from old effect slot property updates. */
550 ALeffectslotProps
*props
{Context
->FreeEffectslotProps
.load()};
554 props
->State
->DecRef();
555 props
->State
= nullptr;
556 props
= props
->next
.load(std::memory_order_relaxed
);
563 void EffectState::IncRef() noexcept
565 auto ref
= IncrementRef(&mRef
);
566 TRACEREF("%p increasing refcount to %u\n", this, ref
);
569 void EffectState::DecRef() noexcept
571 auto ref
= DecrementRef(&mRef
);
572 TRACEREF("%p decreasing refcount to %u\n", this, ref
);
573 if(ref
== 0) delete this;
577 ALenum
InitEffectSlot(ALeffectslot
*slot
)
579 EffectStateFactory
*factory
{getFactoryByType(slot
->Effect
.Type
)};
580 slot
->Effect
.State
= factory
->create();
581 if(!slot
->Effect
.State
) return AL_OUT_OF_MEMORY
;
583 slot
->Effect
.State
->IncRef();
584 slot
->Params
.mEffectState
= slot
->Effect
.State
;
588 ALeffectslot::~ALeffectslot()
590 struct ALeffectslotProps
*props
{Update
.load()};
593 if(props
->State
) props
->State
->DecRef();
594 TRACE("Freed unapplied AuxiliaryEffectSlot update %p\n", props
);
599 Effect
.State
->DecRef();
600 if(Params
.mEffectState
)
601 Params
.mEffectState
->DecRef();
604 void UpdateEffectSlotProps(ALeffectslot
*slot
, ALCcontext
*context
)
606 /* Get an unused property container, or allocate a new one as needed. */
607 ALeffectslotProps
*props
{context
->FreeEffectslotProps
.load(std::memory_order_relaxed
)};
609 props
= static_cast<ALeffectslotProps
*>(al_calloc(16, sizeof(*props
)));
612 struct ALeffectslotProps
*next
;
614 next
= props
->next
.load(std::memory_order_relaxed
);
615 } while(context
->FreeEffectslotProps
.compare_exchange_weak(props
, next
,
616 std::memory_order_seq_cst
, std::memory_order_acquire
) == 0);
619 /* Copy in current property values. */
620 props
->Gain
= slot
->Gain
;
621 props
->AuxSendAuto
= slot
->AuxSendAuto
;
623 props
->Type
= slot
->Effect
.Type
;
624 props
->Props
= slot
->Effect
.Props
;
625 /* Swap out any stale effect state object there may be in the container, to
628 EffectState
*oldstate
{props
->State
};
629 slot
->Effect
.State
->IncRef();
630 props
->State
= slot
->Effect
.State
;
632 /* Set the new container for updating internal parameters. */
633 props
= slot
->Update
.exchange(props
, std::memory_order_acq_rel
);
636 /* If there was an unused update container, put it back in the
640 props
->State
->DecRef();
641 props
->State
= nullptr;
642 AtomicReplaceHead(context
->FreeEffectslotProps
, props
);
649 void UpdateAllEffectSlotProps(ALCcontext
*context
)
651 std::lock_guard
<std::mutex
> _
{context
->EffectSlotLock
};
652 ALeffectslotArray
*auxslots
{context
->ActiveAuxSlots
.load(std::memory_order_acquire
)};
653 for(ALsizei i
{0};i
< auxslots
->count
;i
++)
655 ALeffectslot
*slot
= auxslots
->slot
[i
];
656 if(!slot
->PropsClean
.test_and_set(std::memory_order_acq_rel
))
657 UpdateEffectSlotProps(slot
, context
);