1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
|
#include "config.h"
#include "context.h"
#include <algorithm>
#include <array>
#include <cstring>
#include <functional>
#include <limits>
#include <numeric>
#include <stddef.h>
#include <stdexcept>
#include <string_view>
#include <utility>
#include "AL/efx.h"
#include "al/auxeffectslot.h"
#include "al/debug.h"
#include "al/source.h"
#include "al/effect.h"
#include "al/event.h"
#include "al/listener.h"
#include "albit.h"
#include "alc/alu.h"
#include "alspan.h"
#include "core/async_event.h"
#include "core/device.h"
#include "core/effectslot.h"
#include "core/logging.h"
#include "core/voice.h"
#include "core/voice_change.h"
#include "device.h"
#include "ringbuffer.h"
#include "threads.h"
#include "vecmat.h"
#ifdef ALSOFT_EAX
#include <cstring>
#include "alstring.h"
#include "al/eax/globals.h"
#endif // ALSOFT_EAX
namespace {
using namespace std::placeholders;
using voidp = void*;
/* Default context extensions */
std::vector<std::string_view> getContextExtensions() noexcept
{
return std::vector<std::string_view>{
"AL_EXT_ALAW",
"AL_EXT_BFORMAT",
"AL_EXTX_debug",
"AL_EXT_DOUBLE",
"AL_EXT_EXPONENT_DISTANCE",
"AL_EXT_FLOAT32",
"AL_EXT_IMA4",
"AL_EXT_LINEAR_DISTANCE",
"AL_EXT_MCFORMATS",
"AL_EXT_MULAW",
"AL_EXT_MULAW_BFORMAT",
"AL_EXT_MULAW_MCFORMATS",
"AL_EXT_OFFSET",
"AL_EXT_source_distance_model",
"AL_EXT_SOURCE_RADIUS",
"AL_EXT_STATIC_BUFFER",
"AL_EXT_STEREO_ANGLES",
"AL_LOKI_quadriphonic",
"AL_SOFT_bformat_ex",
"AL_SOFTX_bformat_hoa",
"AL_SOFT_block_alignment",
"AL_SOFT_buffer_length_query",
"AL_SOFT_callback_buffer",
"AL_SOFTX_convolution_reverb",
"AL_SOFT_deferred_updates",
"AL_SOFT_direct_channels",
"AL_SOFT_direct_channels_remix",
"AL_SOFT_effect_target",
"AL_SOFT_events",
"AL_SOFT_gain_clamp_ex",
"AL_SOFTX_hold_on_disconnect",
"AL_SOFT_loop_points",
"AL_SOFTX_map_buffer",
"AL_SOFT_MSADPCM",
"AL_SOFT_source_latency",
"AL_SOFT_source_length",
"AL_SOFT_source_resampler",
"AL_SOFT_source_spatialize",
"AL_SOFT_source_start_delay",
"AL_SOFT_UHJ",
"AL_SOFT_UHJ_ex",
};
}
} // namespace
std::atomic<bool> ALCcontext::sGlobalContextLock{false};
std::atomic<ALCcontext*> ALCcontext::sGlobalContext{nullptr};
thread_local ALCcontext *ALCcontext::sLocalContext{nullptr};
ALCcontext::ThreadCtx::~ThreadCtx()
{
if(ALCcontext *ctx{std::exchange(ALCcontext::sLocalContext, nullptr)})
{
const bool result{ctx->releaseIfNoDelete()};
ERR("Context %p current for thread being destroyed%s!\n", voidp{ctx},
result ? "" : ", leak detected");
}
}
thread_local ALCcontext::ThreadCtx ALCcontext::sThreadContext;
ALeffect ALCcontext::sDefaultEffect;
#ifdef __MINGW32__
ALCcontext *ALCcontext::getThreadContext() noexcept
{ return sLocalContext; }
void ALCcontext::setThreadContext(ALCcontext *context) noexcept
{ sThreadContext.set(context); }
#endif
ALCcontext::ALCcontext(al::intrusive_ptr<ALCdevice> device, ContextFlagBitset flags)
: ContextBase{device.get()}, mALDevice{std::move(device)}, mContextFlags{flags}
{
mDebugGroups.emplace_back(DebugSource::Other, 0, std::string{});
mDebugEnabled.store(mContextFlags.test(ContextFlags::DebugBit), std::memory_order_relaxed);
}
ALCcontext::~ALCcontext()
{
TRACE("Freeing context %p\n", voidp{this});
size_t count{std::accumulate(mSourceList.cbegin(), mSourceList.cend(), size_t{0u},
[](size_t cur, const SourceSubList &sublist) noexcept -> size_t
{ return cur + static_cast<uint>(al::popcount(~sublist.FreeMask)); })};
if(count > 0)
WARN("%zu Source%s not deleted\n", count, (count==1)?"":"s");
mSourceList.clear();
mNumSources = 0;
#ifdef ALSOFT_EAX
eaxUninitialize();
#endif // ALSOFT_EAX
mDefaultSlot = nullptr;
count = std::accumulate(mEffectSlotList.cbegin(), mEffectSlotList.cend(), size_t{0u},
[](size_t cur, const EffectSlotSubList &sublist) noexcept -> size_t
{ return cur + static_cast<uint>(al::popcount(~sublist.FreeMask)); });
if(count > 0)
WARN("%zu AuxiliaryEffectSlot%s not deleted\n", count, (count==1)?"":"s");
mEffectSlotList.clear();
mNumEffectSlots = 0;
}
void ALCcontext::init()
{
if(sDefaultEffect.type != AL_EFFECT_NULL && mDevice->Type == DeviceType::Playback)
{
mDefaultSlot = std::make_unique<ALeffectslot>(this);
aluInitEffectPanning(mDefaultSlot->mSlot, this);
}
EffectSlotArray *auxslots;
if(!mDefaultSlot)
auxslots = EffectSlot::CreatePtrArray(0);
else
{
auxslots = EffectSlot::CreatePtrArray(1);
(*auxslots)[0] = mDefaultSlot->mSlot;
mDefaultSlot->mState = SlotState::Playing;
}
mActiveAuxSlots.store(auxslots, std::memory_order_relaxed);
allocVoiceChanges();
{
VoiceChange *cur{mVoiceChangeTail};
while(VoiceChange *next{cur->mNext.load(std::memory_order_relaxed)})
cur = next;
mCurrentVoiceChange.store(cur, std::memory_order_relaxed);
}
mExtensions = getContextExtensions();
if(sBufferSubDataCompat)
{
auto iter = std::find(mExtensions.begin(), mExtensions.end(), "AL_EXT_SOURCE_RADIUS");
if(iter != mExtensions.end()) mExtensions.erase(iter);
/* TODO: Would be nice to sort this alphabetically. Needs case-
* insensitive searching.
*/
mExtensions.emplace_back("AL_SOFT_buffer_sub_data");
}
#ifdef ALSOFT_EAX
eax_initialize_extensions();
#endif // ALSOFT_EAX
if(!mExtensions.empty())
{
const size_t len{std::accumulate(mExtensions.cbegin()+1, mExtensions.cend(),
mExtensions.front().length(),
[](size_t current, std::string_view ext) noexcept
{ return current + ext.length() + 1; })};
std::string extensions;
extensions.reserve(len);
extensions += mExtensions.front();
for(std::string_view ext : al::span{mExtensions}.subspan<1>())
{
extensions += ' ';
extensions += ext;
}
mExtensionsString = std::move(extensions);
}
mParams.Position = alu::Vector{0.0f, 0.0f, 0.0f, 1.0f};
mParams.Matrix = alu::Matrix::Identity();
mParams.Velocity = alu::Vector{};
mParams.Gain = mListener.Gain;
mParams.MetersPerUnit = mListener.mMetersPerUnit;
mParams.AirAbsorptionGainHF = mAirAbsorptionGainHF;
mParams.DopplerFactor = mDopplerFactor;
mParams.SpeedOfSound = mSpeedOfSound * mDopplerVelocity;
mParams.SourceDistanceModel = mSourceDistanceModel;
mParams.mDistanceModel = mDistanceModel;
mAsyncEvents = RingBuffer::Create(511, sizeof(AsyncEvent), false);
StartEventThrd(this);
allocVoices(256);
mActiveVoiceCount.store(64, std::memory_order_relaxed);
}
bool ALCcontext::deinit()
{
if(sLocalContext == this)
{
WARN("%p released while current on thread\n", voidp{this});
sThreadContext.set(nullptr);
dec_ref();
}
ALCcontext *origctx{this};
if(sGlobalContext.compare_exchange_strong(origctx, nullptr))
{
while(sGlobalContextLock.load()) {
/* Wait to make sure another thread didn't get the context and is
* trying to increment its refcount.
*/
}
dec_ref();
}
bool ret{};
/* First make sure this context exists in the device's list. */
auto *oldarray = mDevice->mContexts.load(std::memory_order_acquire);
if(auto toremove = static_cast<size_t>(std::count(oldarray->begin(), oldarray->end(), this)))
{
using ContextArray = al::FlexArray<ContextBase*>;
auto alloc_ctx_array = [](const size_t count) -> ContextArray*
{
if(count == 0) return &DeviceBase::sEmptyContextArray;
return ContextArray::Create(count).release();
};
auto *newarray = alloc_ctx_array(oldarray->size() - toremove);
/* Copy the current/old context handles to the new array, excluding the
* given context.
*/
std::copy_if(oldarray->begin(), oldarray->end(), newarray->begin(),
[this](ContextBase *ctx) { return ctx != this; });
/* Store the new context array in the device. Wait for any current mix
* to finish before deleting the old array.
*/
mDevice->mContexts.store(newarray);
if(oldarray != &DeviceBase::sEmptyContextArray)
{
mDevice->waitForMix();
delete oldarray;
}
ret = !newarray->empty();
}
else
ret = !oldarray->empty();
StopEventThrd(this);
return ret;
}
void ALCcontext::applyAllUpdates()
{
/* Tell the mixer to stop applying updates, then wait for any active
* updating to finish, before providing updates.
*/
mHoldUpdates.store(true, std::memory_order_release);
while((mUpdateCount.load(std::memory_order_acquire)&1) != 0) {
/* busy-wait */
}
#ifdef ALSOFT_EAX
if(mEaxNeedsCommit)
eaxCommit();
#endif
if(std::exchange(mPropsDirty, false))
UpdateContextProps(this);
UpdateAllEffectSlotProps(this);
UpdateAllSourceProps(this);
/* Now with all updates declared, let the mixer continue applying them so
* they all happen at once.
*/
mHoldUpdates.store(false, std::memory_order_release);
}
#ifdef ALSOFT_EAX
namespace {
template<typename F>
void ForEachSource(ALCcontext *context, F func)
{
for(auto &sublist : context->mSourceList)
{
uint64_t usemask{~sublist.FreeMask};
while(usemask)
{
const int idx{al::countr_zero(usemask)};
usemask &= ~(1_u64 << idx);
func(sublist.Sources[idx]);
}
}
}
} // namespace
bool ALCcontext::eaxIsCapable() const noexcept
{
return eax_has_enough_aux_sends();
}
void ALCcontext::eaxUninitialize() noexcept
{
if(!mEaxIsInitialized)
return;
mEaxIsInitialized = false;
mEaxIsTried = false;
mEaxFxSlots.uninitialize();
}
ALenum ALCcontext::eax_eax_set(
const GUID* property_set_id,
ALuint property_id,
ALuint property_source_id,
ALvoid* property_value,
ALuint property_value_size)
{
const auto call = create_eax_call(
EaxCallType::set,
property_set_id,
property_id,
property_source_id,
property_value,
property_value_size);
eax_initialize();
switch(call.get_property_set_id())
{
case EaxCallPropertySetId::context:
eax_set(call);
break;
case EaxCallPropertySetId::fx_slot:
case EaxCallPropertySetId::fx_slot_effect:
eax_dispatch_fx_slot(call);
break;
case EaxCallPropertySetId::source:
eax_dispatch_source(call);
break;
default:
eax_fail_unknown_property_set_id();
}
mEaxNeedsCommit = true;
if(!call.is_deferred())
{
eaxCommit();
if(!mDeferUpdates)
applyAllUpdates();
}
return AL_NO_ERROR;
}
ALenum ALCcontext::eax_eax_get(
const GUID* property_set_id,
ALuint property_id,
ALuint property_source_id,
ALvoid* property_value,
ALuint property_value_size)
{
const auto call = create_eax_call(
EaxCallType::get,
property_set_id,
property_id,
property_source_id,
property_value,
property_value_size);
eax_initialize();
switch(call.get_property_set_id())
{
case EaxCallPropertySetId::context:
eax_get(call);
break;
case EaxCallPropertySetId::fx_slot:
case EaxCallPropertySetId::fx_slot_effect:
eax_dispatch_fx_slot(call);
break;
case EaxCallPropertySetId::source:
eax_dispatch_source(call);
break;
default:
eax_fail_unknown_property_set_id();
}
return AL_NO_ERROR;
}
void ALCcontext::eaxSetLastError() noexcept
{
mEaxLastError = EAXERR_INVALID_OPERATION;
}
[[noreturn]] void ALCcontext::eax_fail(const char* message)
{
throw ContextException{message};
}
[[noreturn]] void ALCcontext::eax_fail_unknown_property_set_id()
{
eax_fail("Unknown property ID.");
}
[[noreturn]] void ALCcontext::eax_fail_unknown_primary_fx_slot_id()
{
eax_fail("Unknown primary FX Slot ID.");
}
[[noreturn]] void ALCcontext::eax_fail_unknown_property_id()
{
eax_fail("Unknown property ID.");
}
[[noreturn]] void ALCcontext::eax_fail_unknown_version()
{
eax_fail("Unknown version.");
}
void ALCcontext::eax_initialize_extensions()
{
if(!eax_g_is_enabled)
return;
mExtensions.emplace(mExtensions.begin(), eax_x_ram_ext_name);
if(eaxIsCapable())
{
mExtensions.emplace(mExtensions.begin(), eax5_ext_name);
mExtensions.emplace(mExtensions.begin(), eax4_ext_name);
mExtensions.emplace(mExtensions.begin(), eax3_ext_name);
mExtensions.emplace(mExtensions.begin(), eax2_ext_name);
mExtensions.emplace(mExtensions.begin(), eax1_ext_name);
}
}
void ALCcontext::eax_initialize()
{
if(mEaxIsInitialized)
return;
if(mEaxIsTried)
eax_fail("No EAX.");
mEaxIsTried = true;
if(!eax_g_is_enabled)
eax_fail("EAX disabled by a configuration.");
eax_ensure_compatibility();
eax_set_defaults();
eax_context_commit_air_absorbtion_hf();
eax_update_speaker_configuration();
eax_initialize_fx_slots();
mEaxIsInitialized = true;
}
bool ALCcontext::eax_has_no_default_effect_slot() const noexcept
{
return mDefaultSlot == nullptr;
}
void ALCcontext::eax_ensure_no_default_effect_slot() const
{
if(!eax_has_no_default_effect_slot())
eax_fail("There is a default effect slot in the context.");
}
bool ALCcontext::eax_has_enough_aux_sends() const noexcept
{
return mALDevice->NumAuxSends >= EAX_MAX_FXSLOTS;
}
void ALCcontext::eax_ensure_enough_aux_sends() const
{
if(!eax_has_enough_aux_sends())
eax_fail("Not enough aux sends.");
}
void ALCcontext::eax_ensure_compatibility()
{
eax_ensure_enough_aux_sends();
}
unsigned long ALCcontext::eax_detect_speaker_configuration() const
{
#define EAX_PREFIX "[EAX_DETECT_SPEAKER_CONFIG]"
switch(mDevice->FmtChans)
{
case DevFmtMono: return SPEAKERS_2;
case DevFmtStereo:
/* Pretend 7.1 if using UHJ output, since they both provide full
* horizontal surround.
*/
if(mDevice->mUhjEncoder)
return SPEAKERS_7;
if(mDevice->Flags.test(DirectEar))
return HEADPHONES;
return SPEAKERS_2;
case DevFmtQuad: return SPEAKERS_4;
case DevFmtX51: return SPEAKERS_5;
case DevFmtX61: return SPEAKERS_6;
case DevFmtX71: return SPEAKERS_7;
/* 7.1.4 is compatible with 7.1. This could instead be HEADPHONES to
* suggest with-height surround sound (like HRTF).
*/
case DevFmtX714: return SPEAKERS_7;
/* 3D7.1 is only compatible with 5.1. This could instead be HEADPHONES to
* suggest full-sphere surround sound (like HRTF).
*/
case DevFmtX3D71: return SPEAKERS_5;
/* This could also be HEADPHONES, since headphones-based HRTF and Ambi3D
* provide full-sphere surround sound. Depends if apps are more likely to
* consider headphones or 7.1 for surround sound support.
*/
case DevFmtAmbi3D: return SPEAKERS_7;
}
ERR(EAX_PREFIX "Unexpected device channel format 0x%x.\n", mDevice->FmtChans);
return HEADPHONES;
#undef EAX_PREFIX
}
void ALCcontext::eax_update_speaker_configuration()
{
mEaxSpeakerConfig = eax_detect_speaker_configuration();
}
void ALCcontext::eax_set_last_error_defaults() noexcept
{
mEaxLastError = EAX_OK;
}
void ALCcontext::eax_session_set_defaults() noexcept
{
mEaxSession.ulEAXVersion = EAXCONTEXT_DEFAULTEAXSESSION;
mEaxSession.ulMaxActiveSends = EAXCONTEXT_DEFAULTMAXACTIVESENDS;
}
void ALCcontext::eax4_context_set_defaults(Eax4Props& props) noexcept
{
props.guidPrimaryFXSlotID = EAX40CONTEXT_DEFAULTPRIMARYFXSLOTID;
props.flDistanceFactor = EAXCONTEXT_DEFAULTDISTANCEFACTOR;
props.flAirAbsorptionHF = EAXCONTEXT_DEFAULTAIRABSORPTIONHF;
props.flHFReference = EAXCONTEXT_DEFAULTHFREFERENCE;
}
void ALCcontext::eax4_context_set_defaults(Eax4State& state) noexcept
{
eax4_context_set_defaults(state.i);
state.d = state.i;
}
void ALCcontext::eax5_context_set_defaults(Eax5Props& props) noexcept
{
props.guidPrimaryFXSlotID = EAX50CONTEXT_DEFAULTPRIMARYFXSLOTID;
props.flDistanceFactor = EAXCONTEXT_DEFAULTDISTANCEFACTOR;
props.flAirAbsorptionHF = EAXCONTEXT_DEFAULTAIRABSORPTIONHF;
props.flHFReference = EAXCONTEXT_DEFAULTHFREFERENCE;
props.flMacroFXFactor = EAXCONTEXT_DEFAULTMACROFXFACTOR;
}
void ALCcontext::eax5_context_set_defaults(Eax5State& state) noexcept
{
eax5_context_set_defaults(state.i);
state.d = state.i;
}
void ALCcontext::eax_context_set_defaults()
{
eax5_context_set_defaults(mEax123);
eax4_context_set_defaults(mEax4);
eax5_context_set_defaults(mEax5);
mEax = mEax5.i;
mEaxVersion = 5;
mEaxDf = EaxDirtyFlags{};
}
void ALCcontext::eax_set_defaults()
{
eax_set_last_error_defaults();
eax_session_set_defaults();
eax_context_set_defaults();
}
void ALCcontext::eax_dispatch_fx_slot(const EaxCall& call)
{
const auto fx_slot_index = call.get_fx_slot_index();
if(!fx_slot_index.has_value())
eax_fail("Invalid fx slot index.");
auto& fx_slot = eaxGetFxSlot(*fx_slot_index);
if(fx_slot.eax_dispatch(call))
{
std::lock_guard<std::mutex> source_lock{mSourceLock};
ForEachSource(this, std::mem_fn(&ALsource::eaxMarkAsChanged));
}
}
void ALCcontext::eax_dispatch_source(const EaxCall& call)
{
const auto source_id = call.get_property_al_name();
std::lock_guard<std::mutex> source_lock{mSourceLock};
const auto source = ALsource::EaxLookupSource(*this, source_id);
if (source == nullptr)
eax_fail("Source not found.");
source->eaxDispatch(call);
}
void ALCcontext::eax_get_misc(const EaxCall& call)
{
switch(call.get_property_id())
{
case EAXCONTEXT_NONE:
break;
case EAXCONTEXT_LASTERROR:
call.set_value<ContextException>(mEaxLastError);
break;
case EAXCONTEXT_SPEAKERCONFIG:
call.set_value<ContextException>(mEaxSpeakerConfig);
break;
case EAXCONTEXT_EAXSESSION:
call.set_value<ContextException>(mEaxSession);
break;
default:
eax_fail_unknown_property_id();
}
}
void ALCcontext::eax4_get(const EaxCall& call, const Eax4Props& props)
{
switch(call.get_property_id())
{
case EAXCONTEXT_ALLPARAMETERS:
call.set_value<ContextException>(props);
break;
case EAXCONTEXT_PRIMARYFXSLOTID:
call.set_value<ContextException>(props.guidPrimaryFXSlotID);
break;
case EAXCONTEXT_DISTANCEFACTOR:
call.set_value<ContextException>(props.flDistanceFactor);
break;
case EAXCONTEXT_AIRABSORPTIONHF:
call.set_value<ContextException>(props.flAirAbsorptionHF);
break;
case EAXCONTEXT_HFREFERENCE:
call.set_value<ContextException>(props.flHFReference);
break;
default:
eax_get_misc(call);
break;
}
}
void ALCcontext::eax5_get(const EaxCall& call, const Eax5Props& props)
{
switch(call.get_property_id())
{
case EAXCONTEXT_ALLPARAMETERS:
call.set_value<ContextException>(props);
break;
case EAXCONTEXT_PRIMARYFXSLOTID:
call.set_value<ContextException>(props.guidPrimaryFXSlotID);
break;
case EAXCONTEXT_DISTANCEFACTOR:
call.set_value<ContextException>(props.flDistanceFactor);
break;
case EAXCONTEXT_AIRABSORPTIONHF:
call.set_value<ContextException>(props.flAirAbsorptionHF);
break;
case EAXCONTEXT_HFREFERENCE:
call.set_value<ContextException>(props.flHFReference);
break;
case EAXCONTEXT_MACROFXFACTOR:
call.set_value<ContextException>(props.flMacroFXFactor);
break;
default:
eax_get_misc(call);
break;
}
}
void ALCcontext::eax_get(const EaxCall& call)
{
switch(call.get_version())
{
case 4: eax4_get(call, mEax4.i); break;
case 5: eax5_get(call, mEax5.i); break;
default: eax_fail_unknown_version();
}
}
void ALCcontext::eax_context_commit_primary_fx_slot_id()
{
mEaxPrimaryFxSlotIndex = mEax.guidPrimaryFXSlotID;
}
void ALCcontext::eax_context_commit_distance_factor()
{
if(mListener.mMetersPerUnit == mEax.flDistanceFactor)
return;
mListener.mMetersPerUnit = mEax.flDistanceFactor;
mPropsDirty = true;
}
void ALCcontext::eax_context_commit_air_absorbtion_hf()
{
const auto new_value = level_mb_to_gain(mEax.flAirAbsorptionHF);
if(mAirAbsorptionGainHF == new_value)
return;
mAirAbsorptionGainHF = new_value;
mPropsDirty = true;
}
void ALCcontext::eax_context_commit_hf_reference()
{
// TODO
}
void ALCcontext::eax_context_commit_macro_fx_factor()
{
// TODO
}
void ALCcontext::eax_initialize_fx_slots()
{
mEaxFxSlots.initialize(*this);
mEaxPrimaryFxSlotIndex = mEax.guidPrimaryFXSlotID;
}
void ALCcontext::eax_update_sources()
{
std::unique_lock<std::mutex> source_lock{mSourceLock};
auto update_source = [](ALsource &source)
{ source.eaxCommit(); };
ForEachSource(this, update_source);
}
void ALCcontext::eax_set_misc(const EaxCall& call)
{
switch(call.get_property_id())
{
case EAXCONTEXT_NONE:
break;
case EAXCONTEXT_SPEAKERCONFIG:
eax_set<Eax5SpeakerConfigValidator>(call, mEaxSpeakerConfig);
break;
case EAXCONTEXT_EAXSESSION:
eax_set<Eax5SessionAllValidator>(call, mEaxSession);
break;
default:
eax_fail_unknown_property_id();
}
}
void ALCcontext::eax4_defer_all(const EaxCall& call, Eax4State& state)
{
const auto& src = call.get_value<ContextException, const EAX40CONTEXTPROPERTIES>();
Eax4AllValidator{}(src);
const auto& dst_i = state.i;
auto& dst_d = state.d;
dst_d = src;
if(dst_i.guidPrimaryFXSlotID != dst_d.guidPrimaryFXSlotID)
mEaxDf |= eax_primary_fx_slot_id_dirty_bit;
if(dst_i.flDistanceFactor != dst_d.flDistanceFactor)
mEaxDf |= eax_distance_factor_dirty_bit;
if(dst_i.flAirAbsorptionHF != dst_d.flAirAbsorptionHF)
mEaxDf |= eax_air_absorption_hf_dirty_bit;
if(dst_i.flHFReference != dst_d.flHFReference)
mEaxDf |= eax_hf_reference_dirty_bit;
}
void ALCcontext::eax4_defer(const EaxCall& call, Eax4State& state)
{
switch(call.get_property_id())
{
case EAXCONTEXT_ALLPARAMETERS:
eax4_defer_all(call, state);
break;
case EAXCONTEXT_PRIMARYFXSLOTID:
eax_defer<Eax4PrimaryFxSlotIdValidator, eax_primary_fx_slot_id_dirty_bit>(
call, state, &EAX40CONTEXTPROPERTIES::guidPrimaryFXSlotID);
break;
case EAXCONTEXT_DISTANCEFACTOR:
eax_defer<Eax4DistanceFactorValidator, eax_distance_factor_dirty_bit>(
call, state, &EAX40CONTEXTPROPERTIES::flDistanceFactor);
break;
case EAXCONTEXT_AIRABSORPTIONHF:
eax_defer<Eax4AirAbsorptionHfValidator, eax_air_absorption_hf_dirty_bit>(
call, state, &EAX40CONTEXTPROPERTIES::flAirAbsorptionHF);
break;
case EAXCONTEXT_HFREFERENCE:
eax_defer<Eax4HfReferenceValidator, eax_hf_reference_dirty_bit>(
call, state, &EAX40CONTEXTPROPERTIES::flHFReference);
break;
default:
eax_set_misc(call);
break;
}
}
void ALCcontext::eax5_defer_all(const EaxCall& call, Eax5State& state)
{
const auto& src = call.get_value<ContextException, const EAX50CONTEXTPROPERTIES>();
Eax4AllValidator{}(src);
const auto& dst_i = state.i;
auto& dst_d = state.d;
dst_d = src;
if(dst_i.guidPrimaryFXSlotID != dst_d.guidPrimaryFXSlotID)
mEaxDf |= eax_primary_fx_slot_id_dirty_bit;
if(dst_i.flDistanceFactor != dst_d.flDistanceFactor)
mEaxDf |= eax_distance_factor_dirty_bit;
if(dst_i.flAirAbsorptionHF != dst_d.flAirAbsorptionHF)
mEaxDf |= eax_air_absorption_hf_dirty_bit;
if(dst_i.flHFReference != dst_d.flHFReference)
mEaxDf |= eax_hf_reference_dirty_bit;
if(dst_i.flMacroFXFactor != dst_d.flMacroFXFactor)
mEaxDf |= eax_macro_fx_factor_dirty_bit;
}
void ALCcontext::eax5_defer(const EaxCall& call, Eax5State& state)
{
switch(call.get_property_id())
{
case EAXCONTEXT_ALLPARAMETERS:
eax5_defer_all(call, state);
break;
case EAXCONTEXT_PRIMARYFXSLOTID:
eax_defer<Eax5PrimaryFxSlotIdValidator, eax_primary_fx_slot_id_dirty_bit>(
call, state, &EAX50CONTEXTPROPERTIES::guidPrimaryFXSlotID);
break;
case EAXCONTEXT_DISTANCEFACTOR:
eax_defer<Eax4DistanceFactorValidator, eax_distance_factor_dirty_bit>(
call, state, &EAX50CONTEXTPROPERTIES::flDistanceFactor);
break;
case EAXCONTEXT_AIRABSORPTIONHF:
eax_defer<Eax4AirAbsorptionHfValidator, eax_air_absorption_hf_dirty_bit>(
call, state, &EAX50CONTEXTPROPERTIES::flAirAbsorptionHF);
break;
case EAXCONTEXT_HFREFERENCE:
eax_defer<Eax4HfReferenceValidator, eax_hf_reference_dirty_bit>(
call, state, &EAX50CONTEXTPROPERTIES::flHFReference);
break;
case EAXCONTEXT_MACROFXFACTOR:
eax_defer<Eax5MacroFxFactorValidator, eax_macro_fx_factor_dirty_bit>(
call, state, &EAX50CONTEXTPROPERTIES::flMacroFXFactor);
break;
default:
eax_set_misc(call);
break;
}
}
void ALCcontext::eax_set(const EaxCall& call)
{
const auto version = call.get_version();
switch(version)
{
case 4: eax4_defer(call, mEax4); break;
case 5: eax5_defer(call, mEax5); break;
default: eax_fail_unknown_version();
}
if(version != mEaxVersion)
mEaxDf = ~EaxDirtyFlags();
mEaxVersion = version;
}
void ALCcontext::eax4_context_commit(Eax4State& state, EaxDirtyFlags& dst_df)
{
if(mEaxDf == EaxDirtyFlags{})
return;
eax_context_commit_property<eax_primary_fx_slot_id_dirty_bit>(
state, dst_df, &EAX40CONTEXTPROPERTIES::guidPrimaryFXSlotID);
eax_context_commit_property<eax_distance_factor_dirty_bit>(
state, dst_df, &EAX40CONTEXTPROPERTIES::flDistanceFactor);
eax_context_commit_property<eax_air_absorption_hf_dirty_bit>(
state, dst_df, &EAX40CONTEXTPROPERTIES::flAirAbsorptionHF);
eax_context_commit_property<eax_hf_reference_dirty_bit>(
state, dst_df, &EAX40CONTEXTPROPERTIES::flHFReference);
mEaxDf = EaxDirtyFlags{};
}
void ALCcontext::eax5_context_commit(Eax5State& state, EaxDirtyFlags& dst_df)
{
if(mEaxDf == EaxDirtyFlags{})
return;
eax_context_commit_property<eax_primary_fx_slot_id_dirty_bit>(
state, dst_df, &EAX50CONTEXTPROPERTIES::guidPrimaryFXSlotID);
eax_context_commit_property<eax_distance_factor_dirty_bit>(
state, dst_df, &EAX50CONTEXTPROPERTIES::flDistanceFactor);
eax_context_commit_property<eax_air_absorption_hf_dirty_bit>(
state, dst_df, &EAX50CONTEXTPROPERTIES::flAirAbsorptionHF);
eax_context_commit_property<eax_hf_reference_dirty_bit>(
state, dst_df, &EAX50CONTEXTPROPERTIES::flHFReference);
eax_context_commit_property<eax_macro_fx_factor_dirty_bit>(
state, dst_df, &EAX50CONTEXTPROPERTIES::flMacroFXFactor);
mEaxDf = EaxDirtyFlags{};
}
void ALCcontext::eax_context_commit()
{
auto dst_df = EaxDirtyFlags{};
switch(mEaxVersion)
{
case 1:
case 2:
case 3:
eax5_context_commit(mEax123, dst_df);
break;
case 4:
eax4_context_commit(mEax4, dst_df);
break;
case 5:
eax5_context_commit(mEax5, dst_df);
break;
}
if(dst_df == EaxDirtyFlags{})
return;
if((dst_df & eax_primary_fx_slot_id_dirty_bit) != EaxDirtyFlags{})
eax_context_commit_primary_fx_slot_id();
if((dst_df & eax_distance_factor_dirty_bit) != EaxDirtyFlags{})
eax_context_commit_distance_factor();
if((dst_df & eax_air_absorption_hf_dirty_bit) != EaxDirtyFlags{})
eax_context_commit_air_absorbtion_hf();
if((dst_df & eax_hf_reference_dirty_bit) != EaxDirtyFlags{})
eax_context_commit_hf_reference();
if((dst_df & eax_macro_fx_factor_dirty_bit) != EaxDirtyFlags{})
eax_context_commit_macro_fx_factor();
if((dst_df & eax_primary_fx_slot_id_dirty_bit) != EaxDirtyFlags{})
eax_update_sources();
}
void ALCcontext::eaxCommit()
{
mEaxNeedsCommit = false;
eax_context_commit();
eaxCommitFxSlots();
eax_update_sources();
}
namespace {
class EaxSetException : public EaxException {
public:
explicit EaxSetException(const char* message)
: EaxException{"EAX_SET", message}
{}
};
[[noreturn]] void eax_fail_set(const char* message)
{
throw EaxSetException{message};
}
class EaxGetException : public EaxException {
public:
explicit EaxGetException(const char* message)
: EaxException{"EAX_GET", message}
{}
};
[[noreturn]] void eax_fail_get(const char* message)
{
throw EaxGetException{message};
}
} // namespace
FORCE_ALIGN ALenum AL_APIENTRY EAXSet(const GUID *property_set_id, ALuint property_id,
ALuint property_source_id, ALvoid *property_value, ALuint property_value_size) noexcept
{ return EAXSetDirect(GetContextRef().get(), property_set_id, property_id, property_source_id, property_value, property_value_size); }
FORCE_ALIGN ALenum AL_APIENTRY EAXSetDirect(ALCcontext *context, const GUID *property_set_id,
ALuint property_id, ALuint property_source_id, ALvoid *property_value,
ALuint property_value_size) noexcept
try
{
if(!context)
eax_fail_set("No current context.");
std::lock_guard<std::mutex> prop_lock{context->mPropLock};
return context->eax_eax_set(
property_set_id,
property_id,
property_source_id,
property_value,
property_value_size);
}
catch (...)
{
eax_log_exception(__func__);
return AL_INVALID_OPERATION;
}
FORCE_ALIGN ALenum AL_APIENTRY EAXGet(const GUID *property_set_id, ALuint property_id,
ALuint property_source_id, ALvoid *property_value, ALuint property_value_size) noexcept
{ return EAXGetDirect(GetContextRef().get(), property_set_id, property_id, property_source_id, property_value, property_value_size); }
FORCE_ALIGN ALenum AL_APIENTRY EAXGetDirect(ALCcontext *context, const GUID *property_set_id,
ALuint property_id, ALuint property_source_id, ALvoid *property_value,
ALuint property_value_size) noexcept
try
{
if(!context)
eax_fail_get("No current context.");
std::lock_guard<std::mutex> prop_lock{context->mPropLock};
return context->eax_eax_get(
property_set_id,
property_id,
property_source_id,
property_value,
property_value_size);
}
catch (...)
{
eax_log_exception(__func__);
return AL_INVALID_OPERATION;
}
#endif // ALSOFT_EAX
|