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
|
/**
* OpenAL cross platform audio library
* Copyright (C) 1999-2000 by authors.
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#include "config.h"
#include "version.h"
#include <atomic>
#include <cmath>
#include <cstring>
#include <mutex>
#include <optional>
#include <stdexcept>
#include <string>
#include "AL/al.h"
#include "AL/alc.h"
#include "AL/alext.h"
#include "al/debug.h"
#include "alc/alu.h"
#include "alc/context.h"
#include "alc/inprogext.h"
#include "alnumeric.h"
#include "atomic.h"
#include "core/context.h"
#include "core/except.h"
#include "core/mixer/defs.h"
#include "core/voice.h"
#include "intrusive_ptr.h"
#include "opthelpers.h"
#include "strutils.h"
#ifdef ALSOFT_EAX
#include "alc/device.h"
#include "eax/globals.h"
#include "eax/x_ram.h"
#endif // ALSOFT_EAX
namespace {
constexpr ALchar alVendor[] = "OpenAL Community";
constexpr ALchar alVersion[] = "1.1 ALSOFT " ALSOFT_VERSION;
constexpr ALchar alRenderer[] = "OpenAL Soft";
// Error Messages
constexpr ALchar alNoError[] = "No Error";
constexpr ALchar alErrInvalidName[] = "Invalid Name";
constexpr ALchar alErrInvalidEnum[] = "Invalid Enum";
constexpr ALchar alErrInvalidValue[] = "Invalid Value";
constexpr ALchar alErrInvalidOp[] = "Invalid Operation";
constexpr ALchar alErrOutOfMemory[] = "Out of Memory";
constexpr ALchar alStackOverflow[] = "Stack Overflow";
constexpr ALchar alStackUnderflow[] = "Stack Underflow";
/* Resampler strings */
template<Resampler rtype> struct ResamplerName { };
template<> struct ResamplerName<Resampler::Point>
{ static constexpr const ALchar *Get() noexcept { return "Nearest"; } };
template<> struct ResamplerName<Resampler::Linear>
{ static constexpr const ALchar *Get() noexcept { return "Linear"; } };
template<> struct ResamplerName<Resampler::Cubic>
{ static constexpr const ALchar *Get() noexcept { return "Cubic"; } };
template<> struct ResamplerName<Resampler::FastBSinc12>
{ static constexpr const ALchar *Get() noexcept { return "11th order Sinc (fast)"; } };
template<> struct ResamplerName<Resampler::BSinc12>
{ static constexpr const ALchar *Get() noexcept { return "11th order Sinc"; } };
template<> struct ResamplerName<Resampler::FastBSinc24>
{ static constexpr const ALchar *Get() noexcept { return "23rd order Sinc (fast)"; } };
template<> struct ResamplerName<Resampler::BSinc24>
{ static constexpr const ALchar *Get() noexcept { return "23rd order Sinc"; } };
const ALchar *GetResamplerName(const Resampler rtype)
{
#define HANDLE_RESAMPLER(r) case r: return ResamplerName<r>::Get()
switch(rtype)
{
HANDLE_RESAMPLER(Resampler::Point);
HANDLE_RESAMPLER(Resampler::Linear);
HANDLE_RESAMPLER(Resampler::Cubic);
HANDLE_RESAMPLER(Resampler::FastBSinc12);
HANDLE_RESAMPLER(Resampler::BSinc12);
HANDLE_RESAMPLER(Resampler::FastBSinc24);
HANDLE_RESAMPLER(Resampler::BSinc24);
}
#undef HANDLE_RESAMPLER
/* Should never get here. */
throw std::runtime_error{"Unexpected resampler index"};
}
std::optional<DistanceModel> DistanceModelFromALenum(ALenum model)
{
switch(model)
{
case AL_NONE: return DistanceModel::Disable;
case AL_INVERSE_DISTANCE: return DistanceModel::Inverse;
case AL_INVERSE_DISTANCE_CLAMPED: return DistanceModel::InverseClamped;
case AL_LINEAR_DISTANCE: return DistanceModel::Linear;
case AL_LINEAR_DISTANCE_CLAMPED: return DistanceModel::LinearClamped;
case AL_EXPONENT_DISTANCE: return DistanceModel::Exponent;
case AL_EXPONENT_DISTANCE_CLAMPED: return DistanceModel::ExponentClamped;
}
return std::nullopt;
}
ALenum ALenumFromDistanceModel(DistanceModel model)
{
switch(model)
{
case DistanceModel::Disable: return AL_NONE;
case DistanceModel::Inverse: return AL_INVERSE_DISTANCE;
case DistanceModel::InverseClamped: return AL_INVERSE_DISTANCE_CLAMPED;
case DistanceModel::Linear: return AL_LINEAR_DISTANCE;
case DistanceModel::LinearClamped: return AL_LINEAR_DISTANCE_CLAMPED;
case DistanceModel::Exponent: return AL_EXPONENT_DISTANCE;
case DistanceModel::ExponentClamped: return AL_EXPONENT_DISTANCE_CLAMPED;
}
throw std::runtime_error{"Unexpected distance model "+std::to_string(static_cast<int>(model))};
}
enum PropertyValue : ALenum {
DopplerFactor = AL_DOPPLER_FACTOR,
DopplerVelocity = AL_DOPPLER_VELOCITY,
DistanceModel = AL_DISTANCE_MODEL,
SpeedOfSound = AL_SPEED_OF_SOUND,
DeferredUpdates = AL_DEFERRED_UPDATES_SOFT,
GainLimit = AL_GAIN_LIMIT_SOFT,
NumResamplers = AL_NUM_RESAMPLERS_SOFT,
DefaultResampler = AL_DEFAULT_RESAMPLER_SOFT,
DebugLoggedMessages = AL_DEBUG_LOGGED_MESSAGES_EXT,
DebugNextLoggedMessageLength = AL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH_EXT,
MaxDebugMessageLength = AL_MAX_DEBUG_MESSAGE_LENGTH_EXT,
MaxDebugLoggedMessages = AL_MAX_DEBUG_LOGGED_MESSAGES_EXT,
MaxDebugGroupDepth = AL_MAX_DEBUG_GROUP_STACK_DEPTH_EXT,
ContextFlags = AL_CONTEXT_FLAGS_EXT,
#ifdef ALSOFT_EAX
EaxRamSize = AL_EAX_RAM_SIZE,
EaxRamFree = AL_EAX_RAM_FREE,
#endif
};
template<typename T>
struct PropertyCastType {
template<typename U>
constexpr auto operator()(U&& value) const noexcept
{ return static_cast<T>(std::forward<U>(value)); }
};
/* Special-case ALboolean to be an actual bool instead of a char type. */
template<>
struct PropertyCastType<ALboolean> {
template<typename U>
constexpr ALboolean operator()(U&& value) const noexcept
{ return static_cast<bool>(std::forward<U>(value)) ? AL_TRUE : AL_FALSE; }
};
template<typename T>
void GetValue(ALCcontext *context, ALenum pname, T *values)
{
auto cast_value = PropertyCastType<T>{};
switch(static_cast<PropertyValue>(pname))
{
case AL_DOPPLER_FACTOR:
*values = cast_value(context->mDopplerFactor);
return;
case AL_DOPPLER_VELOCITY:
if(context->mContextFlags.test(ContextFlags::DebugBit)) UNLIKELY
context->debugMessage(DebugSource::API, DebugType::DeprecatedBehavior, 0,
DebugSeverity::Medium, -1,
"AL_DOPPLER_VELOCITY is deprecated in AL 1.1, use AL_SPEED_OF_SOUND; "
"AL_DOPPLER_VELOCITY -> AL_SPEED_OF_SOUND / 343.3f");
*values = cast_value(context->mDopplerVelocity);
return;
case AL_SPEED_OF_SOUND:
*values = cast_value(context->mSpeedOfSound);
return;
case AL_GAIN_LIMIT_SOFT:
*values = cast_value(GainMixMax / context->mGainBoost);
return;
case AL_DEFERRED_UPDATES_SOFT:
*values = cast_value(context->mDeferUpdates ? AL_TRUE : AL_FALSE);
return;
case AL_DISTANCE_MODEL:
*values = cast_value(ALenumFromDistanceModel(context->mDistanceModel));
return;
case AL_NUM_RESAMPLERS_SOFT:
*values = cast_value(al::to_underlying(Resampler::Max) + 1);
return;
case AL_DEFAULT_RESAMPLER_SOFT:
*values = cast_value(al::to_underlying(ResamplerDefault));
return;
case AL_DEBUG_LOGGED_MESSAGES_EXT:
{
std::lock_guard<std::mutex> _{context->mDebugCbLock};
*values = cast_value(context->mDebugLog.size());
return;
}
case AL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH_EXT:
{
std::lock_guard<std::mutex> _{context->mDebugCbLock};
*values = cast_value(context->mDebugLog.empty() ? size_t{0}
: (context->mDebugLog.front().mMessage.size()+1));
return;
}
case AL_MAX_DEBUG_MESSAGE_LENGTH_EXT:
*values = cast_value(MaxDebugMessageLength);
return;
case AL_MAX_DEBUG_LOGGED_MESSAGES_EXT:
*values = cast_value(MaxDebugLoggedMessages);
return;
case AL_MAX_DEBUG_GROUP_STACK_DEPTH_EXT:
*values = cast_value(MaxDebugGroupDepth);
return;
case AL_CONTEXT_FLAGS_EXT:
*values = cast_value(context->mContextFlags.to_ulong());
return;
#ifdef ALSOFT_EAX
#define EAX_ERROR "[alGetInteger] EAX not enabled."
case AL_EAX_RAM_SIZE:
if(eax_g_is_enabled)
{
*values = cast_value(eax_x_ram_max_size);
return;
}
context->setError(AL_INVALID_ENUM, EAX_ERROR);
return;
case AL_EAX_RAM_FREE:
if(eax_g_is_enabled)
{
auto device = context->mALDevice.get();
std::lock_guard<std::mutex> device_lock{device->BufferLock};
*values = cast_value(device->eax_x_ram_free_size);
return;
}
context->setError(AL_INVALID_ENUM, EAX_ERROR);
return;
#undef EAX_ERROR
#endif // ALSOFT_EAX
}
context->setError(AL_INVALID_ENUM, "Invalid context property 0x%04x", pname);
}
} // namespace
/* WARNING: Non-standard export! Not part of any extension, or exposed in the
* alcFunctions list.
*/
AL_API const ALchar* AL_APIENTRY alsoft_get_version(void)
START_API_FUNC
{
static const auto spoof = al::getenv("ALSOFT_SPOOF_VERSION");
if(spoof) return spoof->c_str();
return ALSOFT_VERSION;
}
END_API_FUNC
#define DO_UPDATEPROPS() do { \
if(!context->mDeferUpdates) \
UpdateContextProps(context.get()); \
else \
context->mPropsDirty = true; \
} while(0)
AL_API void AL_APIENTRY alEnable(ALenum capability)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
switch(capability)
{
case AL_SOURCE_DISTANCE_MODEL:
{
std::lock_guard<std::mutex> _{context->mPropLock};
context->mSourceDistanceModel = true;
DO_UPDATEPROPS();
}
break;
case AL_DEBUG_OUTPUT_EXT:
context->mDebugEnabled = true;
break;
case AL_STOP_SOURCES_ON_DISCONNECT_SOFT:
context->setError(AL_INVALID_OPERATION, "Re-enabling AL_STOP_SOURCES_ON_DISCONNECT_SOFT not yet supported");
break;
default:
context->setError(AL_INVALID_VALUE, "Invalid enable property 0x%04x", capability);
}
}
END_API_FUNC
AL_API void AL_APIENTRY alDisable(ALenum capability)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
switch(capability)
{
case AL_SOURCE_DISTANCE_MODEL:
{
std::lock_guard<std::mutex> _{context->mPropLock};
context->mSourceDistanceModel = false;
DO_UPDATEPROPS();
}
break;
case AL_DEBUG_OUTPUT_EXT:
context->mDebugEnabled = false;
break;
case AL_STOP_SOURCES_ON_DISCONNECT_SOFT:
context->mStopVoicesOnDisconnect = false;
break;
default:
context->setError(AL_INVALID_VALUE, "Invalid disable property 0x%04x", capability);
}
}
END_API_FUNC
AL_API ALboolean AL_APIENTRY alIsEnabled(ALenum capability)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return AL_FALSE;
std::lock_guard<std::mutex> _{context->mPropLock};
ALboolean value{AL_FALSE};
switch(capability)
{
case AL_SOURCE_DISTANCE_MODEL:
value = context->mSourceDistanceModel ? AL_TRUE : AL_FALSE;
break;
case AL_DEBUG_OUTPUT_EXT:
value = context->mDebugEnabled ? AL_TRUE : AL_FALSE;
break;
case AL_STOP_SOURCES_ON_DISCONNECT_SOFT:
value = context->mStopVoicesOnDisconnect ? AL_TRUE : AL_FALSE;
break;
default:
context->setError(AL_INVALID_VALUE, "Invalid is enabled property 0x%04x", capability);
}
return value;
}
END_API_FUNC
AL_API ALboolean AL_APIENTRY alGetBoolean(ALenum pname)
START_API_FUNC
{
ALboolean value{AL_FALSE};
alGetBooleanv(pname, &value);
return value;
}
END_API_FUNC
AL_API ALdouble AL_APIENTRY alGetDouble(ALenum pname)
START_API_FUNC
{
ALdouble value{0.0};
alGetDoublev(pname, &value);
return value;
}
END_API_FUNC
AL_API ALfloat AL_APIENTRY alGetFloat(ALenum pname)
START_API_FUNC
{
ALfloat value{0.0f};
alGetFloatv(pname, &value);
return value;
}
END_API_FUNC
AL_API ALint AL_APIENTRY alGetInteger(ALenum pname)
START_API_FUNC
{
ALint value{0};
alGetIntegerv(pname, &value);
return value;
}
END_API_FUNC
AL_API ALint64SOFT AL_APIENTRY alGetInteger64SOFT(ALenum pname)
START_API_FUNC
{
ALint64SOFT value{0};
alGetInteger64vSOFT(pname, &value);
return value;
}
END_API_FUNC
AL_API ALvoid* AL_APIENTRY alGetPointerSOFT(ALenum pname)
START_API_FUNC
{
ALvoid *value{nullptr};
alGetPointervSOFT(pname, &value);
return value;
}
END_API_FUNC
AL_API void AL_APIENTRY alGetBooleanv(ALenum pname, ALboolean *values)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!values) UNLIKELY
return context->setError(AL_INVALID_VALUE, "NULL pointer");
GetValue(context.get(), pname, values);
}
END_API_FUNC
AL_API void AL_APIENTRY alGetDoublev(ALenum pname, ALdouble *values)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!values) UNLIKELY
return context->setError(AL_INVALID_VALUE, "NULL pointer");
GetValue(context.get(), pname, values);
}
END_API_FUNC
AL_API void AL_APIENTRY alGetFloatv(ALenum pname, ALfloat *values)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!values) UNLIKELY
return context->setError(AL_INVALID_VALUE, "NULL pointer");
GetValue(context.get(), pname, values);
}
END_API_FUNC
AL_API void AL_APIENTRY alGetIntegerv(ALenum pname, ALint *values)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!values) UNLIKELY
return context->setError(AL_INVALID_VALUE, "NULL pointer");
GetValue(context.get(), pname, values);
}
END_API_FUNC
AL_API void AL_APIENTRY alGetInteger64vSOFT(ALenum pname, ALint64SOFT *values)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!values) UNLIKELY
return context->setError(AL_INVALID_VALUE, "NULL pointer");
GetValue(context.get(), pname, values);
}
END_API_FUNC
AL_API void AL_APIENTRY alGetPointervSOFT(ALenum pname, ALvoid **values)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!values) UNLIKELY
return context->setError(AL_INVALID_VALUE, "NULL pointer");
switch(pname)
{
case AL_EVENT_CALLBACK_FUNCTION_SOFT:
*values = reinterpret_cast<void*>(context->mEventCb);
break;
case AL_EVENT_CALLBACK_USER_PARAM_SOFT:
*values = context->mEventParam;
break;
case AL_DEBUG_CALLBACK_FUNCTION_EXT:
*values = reinterpret_cast<void*>(context->mDebugCb);
break;
case AL_DEBUG_CALLBACK_USER_PARAM_EXT:
*values = context->mDebugParam;
break;
default:
context->setError(AL_INVALID_ENUM, "Invalid context pointer property 0x%04x", pname);
}
}
END_API_FUNC
AL_API const ALchar* AL_APIENTRY alGetString(ALenum pname)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return nullptr;
const ALchar *value{nullptr};
switch(pname)
{
case AL_VENDOR:
value = alVendor;
break;
case AL_VERSION:
value = alVersion;
break;
case AL_RENDERER:
value = alRenderer;
break;
case AL_EXTENSIONS:
value = context->mExtensionList;
break;
case AL_NO_ERROR:
value = alNoError;
break;
case AL_INVALID_NAME:
value = alErrInvalidName;
break;
case AL_INVALID_ENUM:
value = alErrInvalidEnum;
break;
case AL_INVALID_VALUE:
value = alErrInvalidValue;
break;
case AL_INVALID_OPERATION:
value = alErrInvalidOp;
break;
case AL_OUT_OF_MEMORY:
value = alErrOutOfMemory;
break;
case AL_STACK_OVERFLOW_EXT:
value = alStackOverflow;
break;
case AL_STACK_UNDERFLOW_EXT:
value = alStackUnderflow;
break;
default:
context->setError(AL_INVALID_VALUE, "Invalid string property 0x%04x", pname);
}
return value;
}
END_API_FUNC
AL_API void AL_APIENTRY alDopplerFactor(ALfloat value)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!(value >= 0.0f && std::isfinite(value)))
context->setError(AL_INVALID_VALUE, "Doppler factor %f out of range", value);
else
{
std::lock_guard<std::mutex> _{context->mPropLock};
context->mDopplerFactor = value;
DO_UPDATEPROPS();
}
}
END_API_FUNC
AL_API void AL_APIENTRY alDopplerVelocity(ALfloat value)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(context->mContextFlags.test(ContextFlags::DebugBit)) UNLIKELY
context->debugMessage(DebugSource::API, DebugType::DeprecatedBehavior, 0,
DebugSeverity::Medium, -1,
"alDopplerVelocity is deprecated in AL 1.1, use alSpeedOfSound; "
"alDopplerVelocity(x) -> alSpeedOfSound(343.3f * x)");
if(!(value >= 0.0f && std::isfinite(value)))
context->setError(AL_INVALID_VALUE, "Doppler velocity %f out of range", value);
else
{
std::lock_guard<std::mutex> _{context->mPropLock};
context->mDopplerVelocity = value;
DO_UPDATEPROPS();
}
}
END_API_FUNC
AL_API void AL_APIENTRY alSpeedOfSound(ALfloat value)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(!(value > 0.0f && std::isfinite(value)))
context->setError(AL_INVALID_VALUE, "Speed of sound %f out of range", value);
else
{
std::lock_guard<std::mutex> _{context->mPropLock};
context->mSpeedOfSound = value;
DO_UPDATEPROPS();
}
}
END_API_FUNC
AL_API void AL_APIENTRY alDistanceModel(ALenum value)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
if(auto model = DistanceModelFromALenum(value))
{
std::lock_guard<std::mutex> _{context->mPropLock};
context->mDistanceModel = *model;
if(!context->mSourceDistanceModel)
DO_UPDATEPROPS();
}
else
context->setError(AL_INVALID_VALUE, "Distance model 0x%04x out of range", value);
}
END_API_FUNC
AL_API void AL_APIENTRY alDeferUpdatesSOFT(void)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
std::lock_guard<std::mutex> _{context->mPropLock};
context->deferUpdates();
}
END_API_FUNC
AL_API void AL_APIENTRY alProcessUpdatesSOFT(void)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return;
std::lock_guard<std::mutex> _{context->mPropLock};
context->processUpdates();
}
END_API_FUNC
AL_API const ALchar* AL_APIENTRY alGetStringiSOFT(ALenum pname, ALsizei index)
START_API_FUNC
{
ContextRef context{GetContextRef()};
if(!context) UNLIKELY return nullptr;
const ALchar *value{nullptr};
switch(pname)
{
case AL_RESAMPLER_NAME_SOFT:
if(index < 0 || index > static_cast<ALint>(Resampler::Max))
context->setError(AL_INVALID_VALUE, "Resampler name index %d out of range", index);
else
value = GetResamplerName(static_cast<Resampler>(index));
break;
default:
context->setError(AL_INVALID_VALUE, "Invalid string indexed property");
}
return value;
}
END_API_FUNC
void UpdateContextProps(ALCcontext *context)
{
/* Get an unused proprty container, or allocate a new one as needed. */
ContextProps *props{context->mFreeContextProps.load(std::memory_order_acquire)};
if(!props)
props = new ContextProps{};
else
{
ContextProps *next;
do {
next = props->next.load(std::memory_order_relaxed);
} while(context->mFreeContextProps.compare_exchange_weak(props, next,
std::memory_order_seq_cst, std::memory_order_acquire) == 0);
}
/* Copy in current property values. */
ALlistener &listener = context->mListener;
props->Position = listener.Position;
props->Velocity = listener.Velocity;
props->OrientAt = listener.OrientAt;
props->OrientUp = listener.OrientUp;
props->Gain = listener.Gain;
props->MetersPerUnit = listener.mMetersPerUnit;
props->AirAbsorptionGainHF = context->mAirAbsorptionGainHF;
props->DopplerFactor = context->mDopplerFactor;
props->DopplerVelocity = context->mDopplerVelocity;
props->SpeedOfSound = context->mSpeedOfSound;
props->SourceDistanceModel = context->mSourceDistanceModel;
props->mDistanceModel = context->mDistanceModel;
/* Set the new container for updating internal parameters. */
props = context->mParams.ContextUpdate.exchange(props, std::memory_order_acq_rel);
if(props)
{
/* If there was an unused update container, put it back in the
* freelist.
*/
AtomicReplaceHead(context->mFreeContextProps, props);
}
}
|