Use a boolean check instead of a function pointer
[openal-soft.git] / alc / context.cpp
blob33c888e7757a09bfb5c500c5943a93404a81acce
2 #include "config.h"
4 #include "context.h"
6 #include <algorithm>
7 #include <functional>
8 #include <limits>
9 #include <numeric>
10 #include <stddef.h>
11 #include <stdexcept>
13 #include "AL/efx.h"
15 #include "al/auxeffectslot.h"
16 #include "al/source.h"
17 #include "al/effect.h"
18 #include "al/event.h"
19 #include "al/listener.h"
20 #include "albit.h"
21 #include "alc/alu.h"
22 #include "core/async_event.h"
23 #include "core/device.h"
24 #include "core/effectslot.h"
25 #include "core/logging.h"
26 #include "core/voice.h"
27 #include "core/voice_change.h"
28 #include "device.h"
29 #include "ringbuffer.h"
30 #include "vecmat.h"
32 #ifdef ALSOFT_EAX
33 #include <cassert>
34 #include <cstring>
36 #include "alstring.h"
37 #include "al/eax/exception.h"
38 #include "al/eax/globals.h"
39 #endif // ALSOFT_EAX
41 namespace {
43 using namespace std::placeholders;
45 using voidp = void*;
47 /* Default context extensions */
48 constexpr ALchar alExtList[] =
49 "AL_EXT_ALAW "
50 "AL_EXT_BFORMAT "
51 "AL_EXT_DOUBLE "
52 "AL_EXT_EXPONENT_DISTANCE "
53 "AL_EXT_FLOAT32 "
54 "AL_EXT_IMA4 "
55 "AL_EXT_LINEAR_DISTANCE "
56 "AL_EXT_MCFORMATS "
57 "AL_EXT_MULAW "
58 "AL_EXT_MULAW_BFORMAT "
59 "AL_EXT_MULAW_MCFORMATS "
60 "AL_EXT_OFFSET "
61 "AL_EXT_source_distance_model "
62 "AL_EXT_SOURCE_RADIUS "
63 "AL_EXT_STEREO_ANGLES "
64 "AL_LOKI_quadriphonic "
65 "AL_SOFT_bformat_ex "
66 "AL_SOFTX_bformat_hoa "
67 "AL_SOFT_block_alignment "
68 "AL_SOFT_callback_buffer "
69 "AL_SOFTX_convolution_reverb "
70 "AL_SOFT_deferred_updates "
71 "AL_SOFT_direct_channels "
72 "AL_SOFT_direct_channels_remix "
73 "AL_SOFT_effect_target "
74 "AL_SOFT_events "
75 "AL_SOFT_gain_clamp_ex "
76 "AL_SOFTX_hold_on_disconnect "
77 "AL_SOFT_loop_points "
78 "AL_SOFTX_map_buffer "
79 "AL_SOFT_MSADPCM "
80 "AL_SOFT_source_latency "
81 "AL_SOFT_source_length "
82 "AL_SOFT_source_resampler "
83 "AL_SOFT_source_spatialize "
84 "AL_SOFT_UHJ";
86 } // namespace
89 std::atomic<ALCcontext*> ALCcontext::sGlobalContext{nullptr};
91 thread_local ALCcontext *ALCcontext::sLocalContext{nullptr};
92 ALCcontext::ThreadCtx::~ThreadCtx()
94 if(ALCcontext *ctx{ALCcontext::sLocalContext})
96 const bool result{ctx->releaseIfNoDelete()};
97 ERR("Context %p current for thread being destroyed%s!\n", voidp{ctx},
98 result ? "" : ", leak detected");
101 thread_local ALCcontext::ThreadCtx ALCcontext::sThreadContext;
103 ALeffect ALCcontext::sDefaultEffect;
106 #ifdef __MINGW32__
107 ALCcontext *ALCcontext::getThreadContext() noexcept
108 { return sLocalContext; }
109 void ALCcontext::setThreadContext(ALCcontext *context) noexcept
110 { sThreadContext.set(context); }
111 #endif
113 ALCcontext::ALCcontext(al::intrusive_ptr<ALCdevice> device)
114 : ContextBase{device.get()}, mALDevice{std::move(device)}
118 ALCcontext::~ALCcontext()
120 TRACE("Freeing context %p\n", voidp{this});
122 size_t count{std::accumulate(mSourceList.cbegin(), mSourceList.cend(), size_t{0u},
123 [](size_t cur, const SourceSubList &sublist) noexcept -> size_t
124 { return cur + static_cast<uint>(al::popcount(~sublist.FreeMask)); })};
125 if(count > 0)
126 WARN("%zu Source%s not deleted\n", count, (count==1)?"":"s");
127 mSourceList.clear();
128 mNumSources = 0;
130 #ifdef ALSOFT_EAX
131 eax_uninitialize();
132 #endif // ALSOFT_EAX
134 mDefaultSlot = nullptr;
135 count = std::accumulate(mEffectSlotList.cbegin(), mEffectSlotList.cend(), size_t{0u},
136 [](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t
137 { return cur + static_cast<uint>(al::popcount(~sublist.FreeMask)); });
138 if(count > 0)
139 WARN("%zu AuxiliaryEffectSlot%s not deleted\n", count, (count==1)?"":"s");
140 mEffectSlotList.clear();
141 mNumEffectSlots = 0;
144 void ALCcontext::init()
146 if(sDefaultEffect.type != AL_EFFECT_NULL && mDevice->Type == DeviceType::Playback)
148 mDefaultSlot = std::make_unique<ALeffectslot>();
149 aluInitEffectPanning(&mDefaultSlot->mSlot, this);
152 EffectSlotArray *auxslots;
153 if(!mDefaultSlot)
154 auxslots = EffectSlot::CreatePtrArray(0);
155 else
157 auxslots = EffectSlot::CreatePtrArray(1);
158 (*auxslots)[0] = &mDefaultSlot->mSlot;
159 mDefaultSlot->mState = SlotState::Playing;
161 mActiveAuxSlots.store(auxslots, std::memory_order_relaxed);
163 allocVoiceChanges();
165 VoiceChange *cur{mVoiceChangeTail};
166 while(VoiceChange *next{cur->mNext.load(std::memory_order_relaxed)})
167 cur = next;
168 mCurrentVoiceChange.store(cur, std::memory_order_relaxed);
171 mExtensionList = alExtList;
173 #ifdef ALSOFT_EAX
174 eax_initialize_extensions();
175 #endif // ALSOFT_EAX
177 mParams.Position = alu::Vector{0.0f, 0.0f, 0.0f, 1.0f};
178 mParams.Matrix = alu::Matrix::Identity();
179 mParams.Velocity = alu::Vector{};
180 mParams.Gain = mListener.Gain;
181 mParams.MetersPerUnit = mListener.mMetersPerUnit;
182 mParams.AirAbsorptionGainHF = mAirAbsorptionGainHF;
183 mParams.DopplerFactor = mDopplerFactor;
184 mParams.SpeedOfSound = mSpeedOfSound * mDopplerVelocity;
185 mParams.SourceDistanceModel = mSourceDistanceModel;
186 mParams.mDistanceModel = mDistanceModel;
189 mAsyncEvents = RingBuffer::Create(511, sizeof(AsyncEvent), false);
190 StartEventThrd(this);
193 allocVoices(256);
194 mActiveVoiceCount.store(64, std::memory_order_relaxed);
197 bool ALCcontext::deinit()
199 if(sLocalContext == this)
201 WARN("%p released while current on thread\n", voidp{this});
202 sThreadContext.set(nullptr);
203 release();
206 ALCcontext *origctx{this};
207 if(sGlobalContext.compare_exchange_strong(origctx, nullptr))
208 release();
210 bool ret{};
211 /* First make sure this context exists in the device's list. */
212 auto *oldarray = mDevice->mContexts.load(std::memory_order_acquire);
213 if(auto toremove = static_cast<size_t>(std::count(oldarray->begin(), oldarray->end(), this)))
215 using ContextArray = al::FlexArray<ContextBase*>;
216 auto alloc_ctx_array = [](const size_t count) -> ContextArray*
218 if(count == 0) return &DeviceBase::sEmptyContextArray;
219 return ContextArray::Create(count).release();
221 auto *newarray = alloc_ctx_array(oldarray->size() - toremove);
223 /* Copy the current/old context handles to the new array, excluding the
224 * given context.
226 std::copy_if(oldarray->begin(), oldarray->end(), newarray->begin(),
227 std::bind(std::not_equal_to<>{}, _1, this));
229 /* Store the new context array in the device. Wait for any current mix
230 * to finish before deleting the old array.
232 mDevice->mContexts.store(newarray);
233 if(oldarray != &DeviceBase::sEmptyContextArray)
235 mDevice->waitForMix();
236 delete oldarray;
239 ret = !newarray->empty();
241 else
242 ret = !oldarray->empty();
244 StopEventThrd(this);
246 return ret;
249 void ALCcontext::applyAllUpdates()
251 /* Tell the mixer to stop applying updates, then wait for any active
252 * updating to finish, before providing updates.
254 mHoldUpdates.store(true, std::memory_order_release);
255 while((mUpdateCount.load(std::memory_order_acquire)&1) != 0) {
256 /* busy-wait */
259 #ifdef ALSOFT_EAX
260 eax_apply_deferred();
261 #endif
262 if(std::exchange(mPropsDirty, false))
263 UpdateContextProps(this);
264 UpdateAllEffectSlotProps(this);
265 UpdateAllSourceProps(this);
267 /* Now with all updates declared, let the mixer continue applying them so
268 * they all happen at once.
270 mHoldUpdates.store(false, std::memory_order_release);
273 #ifdef ALSOFT_EAX
274 namespace {
276 class ContextException :
277 public EaxException
279 public:
280 explicit ContextException(
281 const char* message)
283 EaxException{"EAX_CONTEXT", message}
286 }; // ContextException
289 template<typename F>
290 void ForEachSource(ALCcontext *context, F func)
292 for(auto &sublist : context->mSourceList)
294 uint64_t usemask{~sublist.FreeMask};
295 while(usemask)
297 const int idx{al::countr_zero(usemask)};
298 usemask &= ~(1_u64 << idx);
300 func(sublist.Sources[idx]);
305 } // namespace
308 bool ALCcontext::eax_is_capable() const noexcept
310 return eax_has_enough_aux_sends();
313 void ALCcontext::eax_uninitialize() noexcept
315 if (!eax_is_initialized_)
317 return;
320 eax_is_initialized_ = true;
321 eax_is_tried_ = false;
323 eax_fx_slots_.uninitialize();
326 ALenum ALCcontext::eax_eax_set(
327 const GUID* property_set_id,
328 ALuint property_id,
329 ALuint property_source_id,
330 ALvoid* property_value,
331 ALuint property_value_size)
333 const auto call = create_eax_call(
334 EaxCallType::set,
335 property_set_id,
336 property_id,
337 property_source_id,
338 property_value,
339 property_value_size);
340 eax_initialize(call);
341 eax_unlock_legacy_fx_slots(call);
343 switch (call.get_property_set_id())
345 case EaxCallPropertySetId::context:
346 eax_set(call);
347 break;
349 case EaxCallPropertySetId::fx_slot:
350 case EaxCallPropertySetId::fx_slot_effect:
351 eax_dispatch_fx_slot(call);
352 break;
354 case EaxCallPropertySetId::source:
355 eax_dispatch_source(call);
356 break;
358 default:
359 eax_fail("Unsupported property set id.");
362 static constexpr auto deferred_flag = 0x80000000u;
363 if(!(property_id&deferred_flag) && !mDeferUpdates)
364 applyAllUpdates();
366 return AL_NO_ERROR;
369 ALenum ALCcontext::eax_eax_get(
370 const GUID* property_set_id,
371 ALuint property_id,
372 ALuint property_source_id,
373 ALvoid* property_value,
374 ALuint property_value_size)
376 const auto call = create_eax_call(
377 EaxCallType::get,
378 property_set_id,
379 property_id,
380 property_source_id,
381 property_value,
382 property_value_size);
383 eax_initialize(call);
384 eax_unlock_legacy_fx_slots(call);
386 switch (call.get_property_set_id())
388 case EaxCallPropertySetId::context:
389 eax_get(call);
390 break;
392 case EaxCallPropertySetId::fx_slot:
393 case EaxCallPropertySetId::fx_slot_effect:
394 eax_dispatch_fx_slot(call);
395 break;
397 case EaxCallPropertySetId::source:
398 eax_dispatch_source(call);
399 break;
401 default:
402 eax_fail("Unsupported property set id.");
405 return AL_NO_ERROR;
408 void ALCcontext::eax_update_filters()
410 ForEachSource(this, std::mem_fn(&ALsource::eax_update_filters));
413 void ALCcontext::eax_commit_and_update_sources()
415 std::unique_lock<std::mutex> source_lock{mSourceLock};
416 ForEachSource(this, std::mem_fn(&ALsource::eax_commit_and_update));
419 void ALCcontext::eax_set_last_error() noexcept
421 eax_last_error_ = EAXERR_INVALID_OPERATION;
424 [[noreturn]]
425 void ALCcontext::eax_fail(
426 const char* message)
428 throw ContextException{message};
431 void ALCcontext::eax_initialize_extensions()
433 if (!eax_g_is_enabled)
435 return;
438 const auto string_max_capacity =
439 std::strlen(mExtensionList) + 1 +
440 std::strlen(eax1_ext_name) + 1 +
441 std::strlen(eax2_ext_name) + 1 +
442 std::strlen(eax3_ext_name) + 1 +
443 std::strlen(eax4_ext_name) + 1 +
444 std::strlen(eax5_ext_name) + 1 +
445 std::strlen(eax_x_ram_ext_name) + 1 +
448 eax_extension_list_.reserve(string_max_capacity);
450 if (eax_is_capable())
452 eax_extension_list_ += eax1_ext_name;
453 eax_extension_list_ += ' ';
455 eax_extension_list_ += eax2_ext_name;
456 eax_extension_list_ += ' ';
458 eax_extension_list_ += eax3_ext_name;
459 eax_extension_list_ += ' ';
461 eax_extension_list_ += eax4_ext_name;
462 eax_extension_list_ += ' ';
464 eax_extension_list_ += eax5_ext_name;
465 eax_extension_list_ += ' ';
468 eax_extension_list_ += eax_x_ram_ext_name;
469 eax_extension_list_ += ' ';
471 eax_extension_list_ += mExtensionList;
472 mExtensionList = eax_extension_list_.c_str();
475 void ALCcontext::eax_initialize(const EaxCall& call)
477 if (eax_is_initialized_)
479 return;
482 if (eax_is_tried_)
484 eax_fail("No EAX.");
487 eax_is_tried_ = true;
489 if (!eax_g_is_enabled)
491 eax_fail("EAX disabled by a configuration.");
494 eax_ensure_compatibility();
495 eax_set_defaults();
496 eax_set_air_absorbtion_hf();
497 eax_update_speaker_configuration();
498 eax_initialize_fx_slots(call);
499 eax_initialize_sources();
501 eax_is_initialized_ = true;
504 bool ALCcontext::eax_has_no_default_effect_slot() const noexcept
506 return mDefaultSlot == nullptr;
509 void ALCcontext::eax_ensure_no_default_effect_slot() const
511 if (!eax_has_no_default_effect_slot())
513 eax_fail("There is a default effect slot in the context.");
517 bool ALCcontext::eax_has_enough_aux_sends() const noexcept
519 return mALDevice->NumAuxSends >= EAX_MAX_FXSLOTS;
522 void ALCcontext::eax_ensure_enough_aux_sends() const
524 if (!eax_has_enough_aux_sends())
526 eax_fail("Not enough aux sends.");
530 void ALCcontext::eax_ensure_compatibility()
532 eax_ensure_enough_aux_sends();
535 unsigned long ALCcontext::eax_detect_speaker_configuration() const
537 #define EAX_PREFIX "[EAX_DETECT_SPEAKER_CONFIG]"
539 switch(mDevice->FmtChans)
541 case DevFmtMono: return SPEAKERS_2;
542 case DevFmtStereo:
543 /* Pretend 7.1 if using UHJ output, since they both provide full
544 * horizontal surround.
546 if(mDevice->mUhjEncoder)
547 return SPEAKERS_7;
548 if(mDevice->Flags.test(DirectEar))
549 return HEADPHONES;
550 return SPEAKERS_2;
551 case DevFmtQuad: return SPEAKERS_4;
552 case DevFmtX51: return SPEAKERS_5;
553 case DevFmtX61: return SPEAKERS_6;
554 case DevFmtX71: return SPEAKERS_7;
555 /* 3D7.1 is only compatible with 5.1. This could instead be HEADPHONES to
556 * suggest full-sphere surround sound (like HRTF).
558 case DevFmtX3D71: return SPEAKERS_5;
559 /* This could also be HEADPHONES, since headphones-based HRTF and Ambi3D
560 * provide full-sphere surround sound. Depends if apps are more likely to
561 * consider headphones or 7.1 for surround sound support.
563 case DevFmtAmbi3D: return SPEAKERS_7;
565 ERR(EAX_PREFIX "Unexpected device channel format 0x%x.\n", mDevice->FmtChans);
566 return HEADPHONES;
568 #undef EAX_PREFIX
571 void ALCcontext::eax_update_speaker_configuration()
573 eax_speaker_config_ = eax_detect_speaker_configuration();
576 void ALCcontext::eax_set_last_error_defaults() noexcept
578 eax_last_error_ = EAX_OK;
581 void ALCcontext::eax_set_session_defaults() noexcept
583 eax_session_.ulEAXVersion = EAXCONTEXT_MINEAXSESSION;
584 eax_session_.ulMaxActiveSends = EAXCONTEXT_DEFAULTMAXACTIVESENDS;
587 void ALCcontext::eax_set_context_defaults() noexcept
589 eax_.context.guidPrimaryFXSlotID = EAXCONTEXT_DEFAULTPRIMARYFXSLOTID;
590 eax_.context.flDistanceFactor = EAXCONTEXT_DEFAULTDISTANCEFACTOR;
591 eax_.context.flAirAbsorptionHF = EAXCONTEXT_DEFAULTAIRABSORPTIONHF;
592 eax_.context.flHFReference = EAXCONTEXT_DEFAULTHFREFERENCE;
595 void ALCcontext::eax_set_defaults() noexcept
597 eax_set_last_error_defaults();
598 eax_set_session_defaults();
599 eax_set_context_defaults();
601 eax_d_ = eax_;
604 void ALCcontext::eax_unlock_legacy_fx_slots(const EaxCall& call) noexcept
606 if (call.get_version() != 5 || eax_are_legacy_fx_slots_unlocked_)
607 return;
609 eax_are_legacy_fx_slots_unlocked_ = true;
610 eax_fx_slots_.unlock_legacy();
613 void ALCcontext::eax_dispatch_fx_slot(const EaxCall& call)
615 const auto fx_slot_index = call.get_fx_slot_index();
616 if(!fx_slot_index.has_value())
617 eax_fail("Invalid fx slot index.");
619 auto& fx_slot = eax_get_fx_slot(*fx_slot_index);
620 if(fx_slot.eax_dispatch(call))
622 std::lock_guard<std::mutex> source_lock{mSourceLock};
623 eax_update_filters();
627 void ALCcontext::eax_dispatch_source(const EaxCall& call)
629 const auto source_id = call.get_property_al_name();
631 std::lock_guard<std::mutex> source_lock{mSourceLock};
633 const auto source = ALsource::eax_lookup_source(*this, source_id);
635 if (!source)
637 eax_fail("Source not found.");
640 source->eax_dispatch(call);
643 void ALCcontext::eax_get_primary_fx_slot_id(const EaxCall& call)
645 call.set_value<ContextException>(eax_.context.guidPrimaryFXSlotID);
648 void ALCcontext::eax_get_distance_factor(const EaxCall& call)
650 call.set_value<ContextException>(eax_.context.flDistanceFactor);
653 void ALCcontext::eax_get_air_absorption_hf(const EaxCall& call)
655 call.set_value<ContextException>(eax_.context.flAirAbsorptionHF);
658 void ALCcontext::eax_get_hf_reference(const EaxCall& call)
660 call.set_value<ContextException>(eax_.context.flHFReference);
663 void ALCcontext::eax_get_last_error(const EaxCall& call)
665 const auto eax_last_error = eax_last_error_;
666 eax_last_error_ = EAX_OK;
667 call.set_value<ContextException>(eax_last_error);
670 void ALCcontext::eax_get_speaker_config(const EaxCall& call)
672 call.set_value<ContextException>(eax_speaker_config_);
675 void ALCcontext::eax_get_session(const EaxCall& call)
677 call.set_value<ContextException>(eax_session_);
680 void ALCcontext::eax_get_macro_fx_factor(const EaxCall& call)
682 call.set_value<ContextException>(eax_.context.flMacroFXFactor);
685 void ALCcontext::eax_get_context_all(const EaxCall& call)
687 switch (call.get_version())
689 case 4:
690 call.set_value<ContextException>(static_cast<const EAX40CONTEXTPROPERTIES&>(eax_.context));
691 break;
693 case 5:
694 call.set_value<ContextException>(static_cast<const EAX50CONTEXTPROPERTIES&>(eax_.context));
695 break;
697 default:
698 eax_fail("Unsupported EAX version.");
702 void ALCcontext::eax_get(const EaxCall& call)
704 switch (call.get_property_id())
706 case EAXCONTEXT_NONE:
707 break;
709 case EAXCONTEXT_ALLPARAMETERS:
710 eax_get_context_all(call);
711 break;
713 case EAXCONTEXT_PRIMARYFXSLOTID:
714 eax_get_primary_fx_slot_id(call);
715 break;
717 case EAXCONTEXT_DISTANCEFACTOR:
718 eax_get_distance_factor(call);
719 break;
721 case EAXCONTEXT_AIRABSORPTIONHF:
722 eax_get_air_absorption_hf(call);
723 break;
725 case EAXCONTEXT_HFREFERENCE:
726 eax_get_hf_reference(call);
727 break;
729 case EAXCONTEXT_LASTERROR:
730 eax_get_last_error(call);
731 break;
733 case EAXCONTEXT_SPEAKERCONFIG:
734 eax_get_speaker_config(call);
735 break;
737 case EAXCONTEXT_EAXSESSION:
738 eax_get_session(call);
739 break;
741 case EAXCONTEXT_MACROFXFACTOR:
742 eax_get_macro_fx_factor(call);
743 break;
745 default:
746 eax_fail("Unsupported property id.");
750 void ALCcontext::eax_set_primary_fx_slot_id()
752 eax_previous_primary_fx_slot_index_ = eax_primary_fx_slot_index_;
753 eax_primary_fx_slot_index_ = eax_.context.guidPrimaryFXSlotID;
756 void ALCcontext::eax_set_distance_factor()
758 mListener.mMetersPerUnit = eax_.context.flDistanceFactor;
759 mPropsDirty = true;
762 void ALCcontext::eax_set_air_absorbtion_hf()
764 mAirAbsorptionGainHF = level_mb_to_gain(eax_.context.flAirAbsorptionHF);
765 mPropsDirty = true;
768 void ALCcontext::eax_set_hf_reference()
770 // TODO
773 void ALCcontext::eax_set_macro_fx_factor()
775 // TODO
778 void ALCcontext::eax_set_context()
780 eax_set_primary_fx_slot_id();
781 eax_set_distance_factor();
782 eax_set_air_absorbtion_hf();
783 eax_set_hf_reference();
786 void ALCcontext::eax_initialize_fx_slots(const EaxCall& call)
788 eax_fx_slots_.initialize(call, *this);
789 eax_previous_primary_fx_slot_index_ = eax_.context.guidPrimaryFXSlotID;
790 eax_primary_fx_slot_index_ = eax_.context.guidPrimaryFXSlotID;
793 void ALCcontext::eax_initialize_sources()
795 std::unique_lock<std::mutex> source_lock{mSourceLock};
796 auto init_source = [this](ALsource &source) noexcept
797 { source.eax_initialize(this); };
798 ForEachSource(this, init_source);
801 void ALCcontext::eax_update_sources()
803 std::unique_lock<std::mutex> source_lock{mSourceLock};
804 auto update_source = [this](ALsource &source)
805 { source.eax_update(eax_context_shared_dirty_flags_); };
806 ForEachSource(this, update_source);
809 void ALCcontext::eax_validate_primary_fx_slot_id(
810 const GUID& primary_fx_slot_id)
812 if (primary_fx_slot_id != EAX_NULL_GUID &&
813 primary_fx_slot_id != EAXPROPERTYID_EAX40_FXSlot0 &&
814 primary_fx_slot_id != EAXPROPERTYID_EAX50_FXSlot0 &&
815 primary_fx_slot_id != EAXPROPERTYID_EAX40_FXSlot1 &&
816 primary_fx_slot_id != EAXPROPERTYID_EAX50_FXSlot1 &&
817 primary_fx_slot_id != EAXPROPERTYID_EAX40_FXSlot2 &&
818 primary_fx_slot_id != EAXPROPERTYID_EAX50_FXSlot2 &&
819 primary_fx_slot_id != EAXPROPERTYID_EAX40_FXSlot3 &&
820 primary_fx_slot_id != EAXPROPERTYID_EAX50_FXSlot3)
822 eax_fail("Unsupported primary FX slot id.");
826 void ALCcontext::eax_validate_distance_factor(
827 float distance_factor)
829 eax_validate_range<ContextException>(
830 "Distance Factor",
831 distance_factor,
832 EAXCONTEXT_MINDISTANCEFACTOR,
833 EAXCONTEXT_MAXDISTANCEFACTOR);
836 void ALCcontext::eax_validate_air_absorption_hf(
837 float air_absorption_hf)
839 eax_validate_range<ContextException>(
840 "Air Absorption HF",
841 air_absorption_hf,
842 EAXCONTEXT_MINAIRABSORPTIONHF,
843 EAXCONTEXT_MAXAIRABSORPTIONHF);
846 void ALCcontext::eax_validate_hf_reference(
847 float hf_reference)
849 eax_validate_range<ContextException>(
850 "HF Reference",
851 hf_reference,
852 EAXCONTEXT_MINHFREFERENCE,
853 EAXCONTEXT_MAXHFREFERENCE);
856 void ALCcontext::eax_validate_speaker_config(
857 unsigned long speaker_config)
859 switch (speaker_config)
861 case HEADPHONES:
862 case SPEAKERS_2:
863 case SPEAKERS_4:
864 case SPEAKERS_5:
865 case SPEAKERS_6:
866 case SPEAKERS_7:
867 break;
869 default:
870 eax_fail("Unsupported speaker configuration.");
874 void ALCcontext::eax_validate_session_eax_version(
875 unsigned long eax_version)
877 switch (eax_version)
879 case EAX_40:
880 case EAX_50:
881 break;
883 default:
884 eax_fail("Unsupported session EAX version.");
888 void ALCcontext::eax_validate_session_max_active_sends(
889 unsigned long max_active_sends)
891 eax_validate_range<ContextException>(
892 "Max Active Sends",
893 max_active_sends,
894 EAXCONTEXT_MINMAXACTIVESENDS,
895 EAXCONTEXT_MAXMAXACTIVESENDS);
898 void ALCcontext::eax_validate_session(
899 const EAXSESSIONPROPERTIES& eax_session)
901 eax_validate_session_eax_version(eax_session.ulEAXVersion);
902 eax_validate_session_max_active_sends(eax_session.ulMaxActiveSends);
905 void ALCcontext::eax_validate_macro_fx_factor(
906 float macro_fx_factor)
908 eax_validate_range<ContextException>(
909 "Macro FX Factor",
910 macro_fx_factor,
911 EAXCONTEXT_MINMACROFXFACTOR,
912 EAXCONTEXT_MAXMACROFXFACTOR);
915 void ALCcontext::eax_validate_context_all(
916 const EAX40CONTEXTPROPERTIES& context_all)
918 eax_validate_primary_fx_slot_id(context_all.guidPrimaryFXSlotID);
919 eax_validate_distance_factor(context_all.flDistanceFactor);
920 eax_validate_air_absorption_hf(context_all.flAirAbsorptionHF);
921 eax_validate_hf_reference(context_all.flHFReference);
924 void ALCcontext::eax_validate_context_all(
925 const EAX50CONTEXTPROPERTIES& context_all)
927 eax_validate_context_all(static_cast<const EAX40CONTEXTPROPERTIES>(context_all));
928 eax_validate_macro_fx_factor(context_all.flMacroFXFactor);
931 void ALCcontext::eax_defer_primary_fx_slot_id(
932 const GUID& primary_fx_slot_id)
934 eax_d_.context.guidPrimaryFXSlotID = primary_fx_slot_id;
936 eax_context_dirty_flags_.guidPrimaryFXSlotID =
937 (eax_.context.guidPrimaryFXSlotID != eax_d_.context.guidPrimaryFXSlotID);
940 void ALCcontext::eax_defer_distance_factor(
941 float distance_factor)
943 eax_d_.context.flDistanceFactor = distance_factor;
945 eax_context_dirty_flags_.flDistanceFactor =
946 (eax_.context.flDistanceFactor != eax_d_.context.flDistanceFactor);
949 void ALCcontext::eax_defer_air_absorption_hf(
950 float air_absorption_hf)
952 eax_d_.context.flAirAbsorptionHF = air_absorption_hf;
954 eax_context_dirty_flags_.flAirAbsorptionHF =
955 (eax_.context.flAirAbsorptionHF != eax_d_.context.flAirAbsorptionHF);
958 void ALCcontext::eax_defer_hf_reference(
959 float hf_reference)
961 eax_d_.context.flHFReference = hf_reference;
963 eax_context_dirty_flags_.flHFReference =
964 (eax_.context.flHFReference != eax_d_.context.flHFReference);
967 void ALCcontext::eax_defer_macro_fx_factor(
968 float macro_fx_factor)
970 eax_d_.context.flMacroFXFactor = macro_fx_factor;
972 eax_context_dirty_flags_.flMacroFXFactor =
973 (eax_.context.flMacroFXFactor != eax_d_.context.flMacroFXFactor);
976 void ALCcontext::eax_defer_context_all(
977 const EAX40CONTEXTPROPERTIES& context_all)
979 eax_defer_primary_fx_slot_id(context_all.guidPrimaryFXSlotID);
980 eax_defer_distance_factor(context_all.flDistanceFactor);
981 eax_defer_air_absorption_hf(context_all.flAirAbsorptionHF);
982 eax_defer_hf_reference(context_all.flHFReference);
985 void ALCcontext::eax_defer_context_all(
986 const EAX50CONTEXTPROPERTIES& context_all)
988 eax_defer_context_all(static_cast<const EAX40CONTEXTPROPERTIES&>(context_all));
989 eax_defer_macro_fx_factor(context_all.flMacroFXFactor);
992 void ALCcontext::eax_defer_context_all(const EaxCall& call)
994 switch(call.get_version())
996 case 4:
998 const auto& context_all =
999 call.get_value<ContextException, EAX40CONTEXTPROPERTIES>();
1001 eax_validate_context_all(context_all);
1002 eax_defer_context_all(context_all);
1004 break;
1006 case 5:
1008 const auto& context_all =
1009 call.get_value<ContextException, EAX50CONTEXTPROPERTIES>();
1011 eax_validate_context_all(context_all);
1012 eax_defer_context_all(context_all);
1014 break;
1016 default:
1017 eax_fail("Unsupported EAX version.");
1021 void ALCcontext::eax_defer_primary_fx_slot_id(const EaxCall& call)
1023 const auto& primary_fx_slot_id =
1024 call.get_value<ContextException, const decltype(EAX50CONTEXTPROPERTIES::guidPrimaryFXSlotID)>();
1026 eax_validate_primary_fx_slot_id(primary_fx_slot_id);
1027 eax_defer_primary_fx_slot_id(primary_fx_slot_id);
1030 void ALCcontext::eax_defer_distance_factor(const EaxCall& call)
1032 const auto& distance_factor =
1033 call.get_value<ContextException, const decltype(EAX50CONTEXTPROPERTIES::flDistanceFactor)>();
1035 eax_validate_distance_factor(distance_factor);
1036 eax_defer_distance_factor(distance_factor);
1039 void ALCcontext::eax_defer_air_absorption_hf(const EaxCall& call)
1041 const auto& air_absorption_hf =
1042 call.get_value<ContextException, const decltype(EAX50CONTEXTPROPERTIES::flAirAbsorptionHF)>();
1044 eax_validate_air_absorption_hf(air_absorption_hf);
1045 eax_defer_air_absorption_hf(air_absorption_hf);
1048 void ALCcontext::eax_defer_hf_reference(const EaxCall& call)
1050 const auto& hf_reference =
1051 call.get_value<ContextException, const decltype(EAX50CONTEXTPROPERTIES::flHFReference)>();
1053 eax_validate_hf_reference(hf_reference);
1054 eax_defer_hf_reference(hf_reference);
1057 void ALCcontext::eax_set_session(const EaxCall& call)
1059 const auto& eax_session =
1060 call.get_value<ContextException, const EAXSESSIONPROPERTIES>();
1062 eax_validate_session(eax_session);
1064 eax_session_ = eax_session;
1067 void ALCcontext::eax_defer_macro_fx_factor(const EaxCall& call)
1069 const auto& macro_fx_factor =
1070 call.get_value<ContextException, const decltype(EAX50CONTEXTPROPERTIES::flMacroFXFactor)>();
1072 eax_validate_macro_fx_factor(macro_fx_factor);
1073 eax_defer_macro_fx_factor(macro_fx_factor);
1076 void ALCcontext::eax_set(const EaxCall& call)
1078 switch (call.get_property_id())
1080 case EAXCONTEXT_NONE:
1081 break;
1083 case EAXCONTEXT_ALLPARAMETERS:
1084 eax_defer_context_all(call);
1085 break;
1087 case EAXCONTEXT_PRIMARYFXSLOTID:
1088 eax_defer_primary_fx_slot_id(call);
1089 break;
1091 case EAXCONTEXT_DISTANCEFACTOR:
1092 eax_defer_distance_factor(call);
1093 break;
1095 case EAXCONTEXT_AIRABSORPTIONHF:
1096 eax_defer_air_absorption_hf(call);
1097 break;
1099 case EAXCONTEXT_HFREFERENCE:
1100 eax_defer_hf_reference(call);
1101 break;
1103 case EAXCONTEXT_LASTERROR:
1104 eax_fail("Last error is read-only.");
1106 case EAXCONTEXT_SPEAKERCONFIG:
1107 eax_fail("Speaker configuration is read-only.");
1109 case EAXCONTEXT_EAXSESSION:
1110 eax_set_session(call);
1111 break;
1113 case EAXCONTEXT_MACROFXFACTOR:
1114 eax_defer_macro_fx_factor(call);
1115 break;
1117 default:
1118 eax_fail("Unsupported property id.");
1122 void ALCcontext::eax_apply_deferred()
1124 if (eax_context_dirty_flags_ == ContextDirtyFlags{})
1126 return;
1129 eax_ = eax_d_;
1131 if (eax_context_dirty_flags_.guidPrimaryFXSlotID)
1133 eax_context_shared_dirty_flags_.primary_fx_slot_id = true;
1134 eax_set_primary_fx_slot_id();
1137 if (eax_context_dirty_flags_.flDistanceFactor)
1139 eax_set_distance_factor();
1142 if (eax_context_dirty_flags_.flAirAbsorptionHF)
1144 eax_set_air_absorbtion_hf();
1147 if (eax_context_dirty_flags_.flHFReference)
1149 eax_set_hf_reference();
1152 if (eax_context_dirty_flags_.flMacroFXFactor)
1154 eax_set_macro_fx_factor();
1157 if (eax_context_shared_dirty_flags_ != EaxContextSharedDirtyFlags{})
1159 eax_update_sources();
1162 eax_context_shared_dirty_flags_ = EaxContextSharedDirtyFlags{};
1163 eax_context_dirty_flags_ = ContextDirtyFlags{};
1167 namespace
1171 class EaxSetException :
1172 public EaxException
1174 public:
1175 explicit EaxSetException(
1176 const char* message)
1178 EaxException{"EAX_SET", message}
1181 }; // EaxSetException
1184 [[noreturn]]
1185 void eax_fail_set(
1186 const char* message)
1188 throw EaxSetException{message};
1192 class EaxGetException :
1193 public EaxException
1195 public:
1196 explicit EaxGetException(
1197 const char* message)
1199 EaxException{"EAX_GET", message}
1202 }; // EaxGetException
1205 [[noreturn]]
1206 void eax_fail_get(
1207 const char* message)
1209 throw EaxGetException{message};
1213 } // namespace
1216 FORCE_ALIGN ALenum AL_APIENTRY EAXSet(
1217 const GUID* property_set_id,
1218 ALuint property_id,
1219 ALuint property_source_id,
1220 ALvoid* property_value,
1221 ALuint property_value_size) noexcept
1224 auto context = GetContextRef();
1226 if (!context)
1228 eax_fail_set("No current context.");
1231 std::lock_guard<std::mutex> prop_lock{context->mPropLock};
1233 return context->eax_eax_set(
1234 property_set_id,
1235 property_id,
1236 property_source_id,
1237 property_value,
1238 property_value_size
1241 catch (...)
1243 eax_log_exception(__func__);
1244 return AL_INVALID_OPERATION;
1247 FORCE_ALIGN ALenum AL_APIENTRY EAXGet(
1248 const GUID* property_set_id,
1249 ALuint property_id,
1250 ALuint property_source_id,
1251 ALvoid* property_value,
1252 ALuint property_value_size) noexcept
1255 auto context = GetContextRef();
1257 if (!context)
1259 eax_fail_get("No current context.");
1262 std::lock_guard<std::mutex> prop_lock{context->mPropLock};
1264 return context->eax_eax_get(
1265 property_set_id,
1266 property_id,
1267 property_source_id,
1268 property_value,
1269 property_value_size
1272 catch (...)
1274 eax_log_exception(__func__);
1275 return AL_INVALID_OPERATION;
1277 #endif // ALSOFT_EAX