Don't update context and listener props unnecessarily
[openal-soft.git] / OpenAL32 / Include / alMain.h
blob7ac7eb1388368fa2dc9716afcd35e4eafb8f5cc1
1 #ifndef AL_MAIN_H
2 #define AL_MAIN_H
4 #include <string.h>
5 #include <stdio.h>
6 #include <stddef.h>
7 #include <stdarg.h>
8 #include <assert.h>
9 #include <math.h>
10 #include <limits.h>
12 #ifdef HAVE_STRINGS_H
13 #include <strings.h>
14 #endif
16 #ifdef HAVE_FENV_H
17 #include <fenv.h>
18 #endif
20 #include "AL/al.h"
21 #include "AL/alc.h"
22 #include "AL/alext.h"
24 #include "static_assert.h"
25 #include "align.h"
26 #include "atomic.h"
27 #include "uintmap.h"
28 #include "vector.h"
29 #include "alstring.h"
30 #include "almalloc.h"
31 #include "threads.h"
33 #ifndef ALC_SOFT_loopback2
34 #define ALC_SOFT_loopback2 1
35 #define ALC_AMBISONIC_LAYOUT_SOFT 0x1997
36 #define ALC_AMBISONIC_SCALING_SOFT 0x1998
37 #define ALC_AMBISONIC_ORDER_SOFT 0x1999
39 #define ALC_BFORMAT3D_SOFT 0x1508
41 /* Ambisonic layouts */
42 #define ALC_ACN_SOFT 0x1600
43 #define ALC_FUMA_SOFT 0x1601
45 /* Ambisonic scalings (normalization) */
46 /*#define ALC_FUMA_SOFT*/
47 #define ALC_SN3D_SOFT 0x1602
48 #define ALC_N3D_SOFT 0x1603
50 typedef ALCboolean (ALC_APIENTRY*LPALCISAMBISONICFORMATSUPPORTEDSOFT)(ALCdevice *device, ALCenum layout, ALCenum scaling, ALsizei order);
51 #ifdef AL_ALEXT_PROTOTYPES
52 ALC_API ALCboolean ALC_APIENTRY alcIsAmbisonicFormatSupportedSOFT(ALCdevice *device, ALCenum layout, ALCenum scaling, ALsizei order);
53 #endif
54 #endif
56 #ifndef ALC_SOFT_device_clock
57 #define ALC_SOFT_device_clock 1
58 typedef int64_t ALCint64SOFT;
59 typedef uint64_t ALCuint64SOFT;
60 #define ALC_DEVICE_CLOCK_SOFT 0x1600
61 #define ALC_DEVICE_LATENCY_SOFT 0x1601
62 #define ALC_DEVICE_CLOCK_LATENCY_SOFT 0x1602
63 typedef void (ALC_APIENTRY*LPALCGETINTEGER64VSOFT)(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
64 #ifdef AL_ALEXT_PROTOTYPES
65 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
66 #endif
67 #endif
69 #ifndef AL_SOFT_buffer_samples2
70 #define AL_SOFT_buffer_samples2 1
71 /* Channel configurations */
72 #define AL_MONO_SOFT 0x1500
73 #define AL_STEREO_SOFT 0x1501
74 #define AL_REAR_SOFT 0x1502
75 #define AL_QUAD_SOFT 0x1503
76 #define AL_5POINT1_SOFT 0x1504
77 #define AL_6POINT1_SOFT 0x1505
78 #define AL_7POINT1_SOFT 0x1506
79 #define AL_BFORMAT2D_SOFT 0x1507
80 #define AL_BFORMAT3D_SOFT 0x1508
82 /* Sample types */
83 #define AL_BYTE_SOFT 0x1400
84 #define AL_UNSIGNED_BYTE_SOFT 0x1401
85 #define AL_SHORT_SOFT 0x1402
86 #define AL_UNSIGNED_SHORT_SOFT 0x1403
87 #define AL_INT_SOFT 0x1404
88 #define AL_UNSIGNED_INT_SOFT 0x1405
89 #define AL_FLOAT_SOFT 0x1406
90 #define AL_DOUBLE_SOFT 0x1407
91 #define AL_BYTE3_SOFT 0x1408
92 #define AL_UNSIGNED_BYTE3_SOFT 0x1409
93 #define AL_MULAW_SOFT 0x140A
95 /* Storage formats */
96 #define AL_MONO8_SOFT 0x1100
97 #define AL_MONO16_SOFT 0x1101
98 #define AL_MONO32F_SOFT 0x10010
99 #define AL_STEREO8_SOFT 0x1102
100 #define AL_STEREO16_SOFT 0x1103
101 #define AL_STEREO32F_SOFT 0x10011
102 #define AL_QUAD8_SOFT 0x1204
103 #define AL_QUAD16_SOFT 0x1205
104 #define AL_QUAD32F_SOFT 0x1206
105 #define AL_REAR8_SOFT 0x1207
106 #define AL_REAR16_SOFT 0x1208
107 #define AL_REAR32F_SOFT 0x1209
108 #define AL_5POINT1_8_SOFT 0x120A
109 #define AL_5POINT1_16_SOFT 0x120B
110 #define AL_5POINT1_32F_SOFT 0x120C
111 #define AL_6POINT1_8_SOFT 0x120D
112 #define AL_6POINT1_16_SOFT 0x120E
113 #define AL_6POINT1_32F_SOFT 0x120F
114 #define AL_7POINT1_8_SOFT 0x1210
115 #define AL_7POINT1_16_SOFT 0x1211
116 #define AL_7POINT1_32F_SOFT 0x1212
117 #define AL_BFORMAT2D_8_SOFT 0x20021
118 #define AL_BFORMAT2D_16_SOFT 0x20022
119 #define AL_BFORMAT2D_32F_SOFT 0x20023
120 #define AL_BFORMAT3D_8_SOFT 0x20031
121 #define AL_BFORMAT3D_16_SOFT 0x20032
122 #define AL_BFORMAT3D_32F_SOFT 0x20033
124 /* Buffer attributes */
125 #define AL_INTERNAL_FORMAT_SOFT 0x2008
126 #define AL_BYTE_LENGTH_SOFT 0x2009
127 #define AL_SAMPLE_LENGTH_SOFT 0x200A
128 #define AL_SEC_LENGTH_SOFT 0x200B
130 #if 0
131 typedef void (AL_APIENTRY*LPALBUFFERSAMPLESSOFT)(ALuint,ALuint,ALenum,ALsizei,ALenum,ALenum,const ALvoid*);
132 typedef void (AL_APIENTRY*LPALGETBUFFERSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,ALvoid*);
133 typedef ALboolean (AL_APIENTRY*LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum);
134 #ifdef AL_ALEXT_PROTOTYPES
135 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer, ALuint samplerate, ALenum internalformat, ALsizei samples, ALenum channels, ALenum type, const ALvoid *data);
136 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer, ALsizei offset, ALsizei samples, ALenum channels, ALenum type, ALvoid *data);
137 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format);
138 #endif
139 #endif
140 #endif
143 #if defined(_WIN64)
144 #define SZFMT "%I64u"
145 #elif defined(_WIN32)
146 #define SZFMT "%u"
147 #else
148 #define SZFMT "%zu"
149 #endif
152 #ifdef __GNUC__
153 /* Because of a long-standing deficiency in C, you're not allowed to implicitly
154 * cast a pointer-to-type-array to a pointer-to-const-type-array. For example,
156 * int (*ptr)[10];
157 * const int (*cptr)[10] = ptr;
159 * is not allowed and most compilers will generate noisy warnings about
160 * incompatible types, even though it just makes the array elements const.
161 * Clang will allow it if you make the array type a typedef, like this:
163 * typedef int int10[10];
164 * int10 *ptr;
165 * const int10 *cptr = ptr;
167 * however GCC does not and still issues the incompatible type warning. The
168 * "proper" way to fix it is to add an explicit cast for the constified type,
169 * but that removes the vast majority of otherwise useful type-checking you'd
170 * get, and runs the risk of improper casts if types are later changed. Leaving
171 * it non-const can also be an issue if you use it as a function parameter, and
172 * happen to have a const type as input (and also reduce the capabilities of
173 * the compiler to better optimize the function).
175 * So to work around the problem, we use a macro. The macro first assigns the
176 * incoming variable to the specified non-const type to ensure it's the correct
177 * type, then casts the variable as the desired constified type. Very ugly, but
178 * I'd rather not have hundreds of lines of warnings because I want to tell the
179 * compiler that some array(s) can't be changed by the code, or have lots of
180 * error-prone casts.
182 #define SAFE_CONST(T, var) __extension__({ \
183 T _tmp = (var); \
184 (const T)_tmp; \
186 #else
187 /* Non-GNU-compatible compilers have to use a straight cast with no extra
188 * checks, due to the lack of multi-statement expressions.
190 #define SAFE_CONST(T, var) ((const T)(var))
191 #endif
194 #ifdef __GNUC__
195 /* This helps cast away the const-ness of a pointer without accidentally
196 * changing the pointer type. This is necessary due to Clang's inability to use
197 * atomic_load on a const _Atomic variable.
199 #define CONST_CAST(T, V) __extension__({ \
200 const T _tmp = (V); \
201 (T)_tmp; \
203 #else
204 #define CONST_CAST(T, V) ((T)(V))
205 #endif
208 typedef ALint64SOFT ALint64;
209 typedef ALuint64SOFT ALuint64;
211 #ifndef U64
212 #if defined(_MSC_VER)
213 #define U64(x) ((ALuint64)(x##ui64))
214 #elif SIZEOF_LONG == 8
215 #define U64(x) ((ALuint64)(x##ul))
216 #elif SIZEOF_LONG_LONG == 8
217 #define U64(x) ((ALuint64)(x##ull))
218 #endif
219 #endif
221 #ifndef UINT64_MAX
222 #define UINT64_MAX U64(18446744073709551615)
223 #endif
225 #ifndef UNUSED
226 #if defined(__cplusplus)
227 #define UNUSED(x)
228 #elif defined(__GNUC__)
229 #define UNUSED(x) UNUSED_##x __attribute__((unused))
230 #elif defined(__LCLINT__)
231 #define UNUSED(x) /*@unused@*/ x
232 #else
233 #define UNUSED(x) x
234 #endif
235 #endif
237 #ifdef __GNUC__
238 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
239 #else
240 #define DECL_FORMAT(x, y, z)
241 #endif
243 /* Calculates the size of a struct with N elements of a flexible array member.
244 * GCC and Clang allow offsetof(Type, fam[N]) for this, but MSVC seems to have
245 * trouble, so a bit more verbose workaround is needed.
247 #define FAM_SIZE(T, M, N) (offsetof(T, M) + sizeof(((T*)NULL)->M[0])*(N))
249 #if defined(__GNUC__) && defined(__i386__)
250 /* force_align_arg_pointer is required for proper function arguments aligning
251 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
252 * thread functions will be properly aligned on the stack, even though GCC may
253 * generate code with the assumption that it is. */
254 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
255 #else
256 #define FORCE_ALIGN
257 #endif
259 #ifdef HAVE_C99_VLA
260 #define DECL_VLA(T, _name, _size) T _name[(_size)]
261 #else
262 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
263 #endif
265 #ifndef PATH_MAX
266 #ifdef MAX_PATH
267 #define PATH_MAX MAX_PATH
268 #else
269 #define PATH_MAX 4096
270 #endif
271 #endif
274 static const union {
275 ALuint u;
276 ALubyte b[sizeof(ALuint)];
277 } EndianTest = { 1 };
278 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
280 #define COUNTOF(x) (sizeof(x) / sizeof(0[x]))
283 #define DERIVE_FROM_TYPE(t) t t##_parent
284 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
285 #ifdef __GNUC__
286 #define STATIC_UPCAST(to, from, obj) __extension__({ \
287 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
288 "Invalid upcast object from type"); \
289 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
291 #else
292 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
293 #endif
295 #define DECLARE_FORWARD(T1, T2, rettype, func) \
296 rettype T1##_##func(T1 *obj) \
297 { return T2##_##func(STATIC_CAST(T2, obj)); }
299 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
300 rettype T1##_##func(T1 *obj, argtype1 a) \
301 { return T2##_##func(STATIC_CAST(T2, obj), a); }
303 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
304 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
305 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
307 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
308 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
309 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
312 #define GET_VTABLE1(T1) (&(T1##_vtable))
313 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
315 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
316 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
318 #define DECLARE_THUNK(T1, T2, rettype, func) \
319 static rettype T1##_##T2##_##func(T2 *obj) \
320 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
322 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
323 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
324 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
326 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
327 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
328 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
330 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
331 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
332 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
334 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
335 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
336 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
338 #define DECLARE_DEFAULT_ALLOCATORS(T) \
339 static void* T##_New(size_t size) { return al_malloc(16, size); } \
340 static void T##_Delete(void *ptr) { al_free(ptr); }
342 /* Helper to extract an argument list for VCALL. Not used directly. */
343 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
345 /* Call a "virtual" method on an object, with arguments. */
346 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
347 /* Call a "virtual" method on an object, with no arguments. */
348 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
350 #define DELETE_OBJ(obj) do { \
351 if((obj) != NULL) \
353 V0((obj),Destruct)(); \
354 V0((obj),Delete)(); \
356 } while(0)
359 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
361 } while(0)
363 #define NEW_OBJ(_res, T) do { \
364 _res = T##_New(sizeof(T)); \
365 if(_res) \
367 memset(_res, 0, sizeof(T)); \
368 T##_Construct(_res, EXTRACT_NEW_ARGS
369 #define NEW_OBJ0(_res, T) do { \
370 _res = T##_New(sizeof(T)); \
371 if(_res) \
373 memset(_res, 0, sizeof(T)); \
374 T##_Construct(_res EXTRACT_NEW_ARGS
377 #ifdef __cplusplus
378 extern "C" {
379 #endif
381 struct Hrtf;
382 struct HrtfEntry;
383 struct FrontStablizer;
384 struct Compressor;
385 struct ALcontextProps;
388 #define DEFAULT_OUTPUT_RATE (44100)
389 #define MIN_OUTPUT_RATE (8000)
392 /* Find the next power-of-2 for non-power-of-2 numbers. */
393 inline ALuint NextPowerOf2(ALuint value)
395 if(value > 0)
397 value--;
398 value |= value>>1;
399 value |= value>>2;
400 value |= value>>4;
401 value |= value>>8;
402 value |= value>>16;
404 return value+1;
407 /** Round up a value to the next multiple. */
408 inline size_t RoundUp(size_t value, size_t r)
410 value += r-1;
411 return value - (value%r);
414 /* Scales the given value using 64-bit integer math, rounding the result. */
415 inline ALuint64 ScaleRound(ALuint64 val, ALuint64 new_scale, ALuint64 old_scale)
417 return (val*new_scale + old_scale/2) / old_scale;
420 /* Scales the given value using 64-bit integer math, flooring the result. */
421 inline ALuint64 ScaleFloor(ALuint64 val, ALuint64 new_scale, ALuint64 old_scale)
423 return val * new_scale / old_scale;
426 /* Scales the given value using 64-bit integer math, ceiling the result. */
427 inline ALuint64 ScaleCeil(ALuint64 val, ALuint64 new_scale, ALuint64 old_scale)
429 return (val*new_scale + old_scale-1) / old_scale;
432 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
433 * mode. */
434 inline ALint fastf2i(ALfloat f)
436 #ifdef HAVE_LRINTF
437 return lrintf(f);
438 #elif defined(_MSC_VER) && defined(_M_IX86)
439 ALint i;
440 __asm fld f
441 __asm fistp i
442 return i;
443 #else
444 return (ALint)f;
445 #endif
449 enum DevProbe {
450 ALL_DEVICE_PROBE,
451 CAPTURE_DEVICE_PROBE
454 struct ALCbackend;
457 enum DistanceModel {
458 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
459 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
460 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
461 InverseDistance = AL_INVERSE_DISTANCE,
462 LinearDistance = AL_LINEAR_DISTANCE,
463 ExponentDistance = AL_EXPONENT_DISTANCE,
464 DisableDistance = AL_NONE,
466 DefaultDistanceModel = InverseDistanceClamped
469 enum Channel {
470 FrontLeft = 0,
471 FrontRight,
472 FrontCenter,
473 LFE,
474 BackLeft,
475 BackRight,
476 BackCenter,
477 SideLeft,
478 SideRight,
480 UpperFrontLeft,
481 UpperFrontRight,
482 UpperBackLeft,
483 UpperBackRight,
484 LowerFrontLeft,
485 LowerFrontRight,
486 LowerBackLeft,
487 LowerBackRight,
489 Aux0,
490 Aux1,
491 Aux2,
492 Aux3,
493 Aux4,
494 Aux5,
495 Aux6,
496 Aux7,
497 Aux8,
498 Aux9,
499 Aux10,
500 Aux11,
501 Aux12,
502 Aux13,
503 Aux14,
504 Aux15,
506 InvalidChannel
510 /* Device formats */
511 enum DevFmtType {
512 DevFmtByte = ALC_BYTE_SOFT,
513 DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
514 DevFmtShort = ALC_SHORT_SOFT,
515 DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
516 DevFmtInt = ALC_INT_SOFT,
517 DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
518 DevFmtFloat = ALC_FLOAT_SOFT,
520 DevFmtTypeDefault = DevFmtFloat
522 enum DevFmtChannels {
523 DevFmtMono = ALC_MONO_SOFT,
524 DevFmtStereo = ALC_STEREO_SOFT,
525 DevFmtQuad = ALC_QUAD_SOFT,
526 DevFmtX51 = ALC_5POINT1_SOFT,
527 DevFmtX61 = ALC_6POINT1_SOFT,
528 DevFmtX71 = ALC_7POINT1_SOFT,
529 DevFmtAmbi3D = ALC_BFORMAT3D_SOFT,
531 /* Similar to 5.1, except using rear channels instead of sides */
532 DevFmtX51Rear = 0x80000000,
534 DevFmtChannelsDefault = DevFmtStereo
536 #define MAX_OUTPUT_CHANNELS (16)
538 ALsizei BytesFromDevFmt(enum DevFmtType type);
539 ALsizei ChannelsFromDevFmt(enum DevFmtChannels chans, ALsizei ambiorder);
540 inline ALsizei FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type, ALsizei ambiorder)
542 return ChannelsFromDevFmt(chans, ambiorder) * BytesFromDevFmt(type);
545 enum AmbiLayout {
546 AmbiLayout_FuMa = ALC_FUMA_SOFT, /* FuMa channel order */
547 AmbiLayout_ACN = ALC_ACN_SOFT, /* ACN channel order */
549 AmbiLayout_Default = AmbiLayout_ACN
552 enum AmbiNorm {
553 AmbiNorm_FuMa = ALC_FUMA_SOFT, /* FuMa normalization */
554 AmbiNorm_SN3D = ALC_SN3D_SOFT, /* SN3D normalization */
555 AmbiNorm_N3D = ALC_N3D_SOFT, /* N3D normalization */
557 AmbiNorm_Default = AmbiNorm_SN3D
561 extern const struct EffectList {
562 const char *name;
563 int type;
564 const char *ename;
565 ALenum val;
566 } EffectList[];
569 enum DeviceType {
570 Playback,
571 Capture,
572 Loopback
576 enum RenderMode {
577 NormalRender,
578 StereoPair,
579 HrtfRender
583 /* The maximum number of Ambisonics coefficients. For a given order (o), the
584 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
585 * second-order has 9, third-order has 16, and fourth-order has 25.
587 #define MAX_AMBI_ORDER 3
588 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
590 /* A bitmask of ambisonic channels with height information. If none of these
591 * channels are used/needed, there's no height (e.g. with most surround sound
592 * speaker setups). This only specifies up to 4th order, which is the highest
593 * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
594 * order). This is ACN ordering, with bit 0 being ACN 0, etc.
596 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
598 /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
599 * representation. This is 2 per each order above zero-order, plus 1 for zero-
600 * order. Or simply, o*2 + 1.
602 #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
605 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
606 typedef struct BFChannelConfig {
607 ALfloat Scale;
608 ALsizei Index;
609 } BFChannelConfig;
611 typedef union AmbiConfig {
612 /* Ambisonic coefficients for mixing to the dry buffer. */
613 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
614 /* Coefficient channel mapping for mixing to the dry buffer. */
615 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
616 } AmbiConfig;
619 #define HRTF_HISTORY_BITS (6)
620 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
621 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
623 #define HRIR_BITS (7)
624 #define HRIR_LENGTH (1<<HRIR_BITS)
625 #define HRIR_MASK (HRIR_LENGTH-1)
627 typedef struct HrtfState {
628 alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
629 alignas(16) ALfloat Values[HRIR_LENGTH][2];
630 } HrtfState;
632 typedef struct HrtfParams {
633 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
634 ALsizei Delay[2];
635 ALfloat Gain;
636 } HrtfParams;
638 typedef struct DirectHrtfState {
639 /* HRTF filter state for dry buffer content */
640 ALsizei Offset;
641 ALsizei IrSize;
642 struct {
643 alignas(16) ALfloat Values[HRIR_LENGTH][2];
644 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
645 } Chan[];
646 } DirectHrtfState;
648 typedef struct EnumeratedHrtf {
649 al_string name;
651 struct HrtfEntry *hrtf;
652 } EnumeratedHrtf;
653 TYPEDEF_VECTOR(EnumeratedHrtf, vector_EnumeratedHrtf)
656 /* Maximum delay in samples for speaker distance compensation. */
657 #define MAX_DELAY_LENGTH 1024
659 typedef struct DistanceComp {
660 ALfloat Gain;
661 ALsizei Length; /* Valid range is [0...MAX_DELAY_LENGTH). */
662 ALfloat *Buffer;
663 } DistanceComp;
665 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
666 * more memory, while smaller values may need more iterations. The value needs
667 * to be a sensible size, however, as it constrains the max stepping value used
668 * for mixing, as well as the maximum number of samples per mixing iteration.
670 #define BUFFERSIZE 2048
672 struct ALCdevice_struct
674 RefCount ref;
676 ALCboolean Connected;
677 enum DeviceType Type;
679 ALuint Frequency;
680 ALuint UpdateSize;
681 ALuint NumUpdates;
682 enum DevFmtChannels FmtChans;
683 enum DevFmtType FmtType;
684 ALboolean IsHeadphones;
685 ALsizei AmbiOrder;
686 /* For DevFmtAmbi* output only, specifies the channel order and
687 * normalization.
689 enum AmbiLayout AmbiLayout;
690 enum AmbiNorm AmbiScale;
692 al_string DeviceName;
694 ATOMIC(ALCenum) LastError;
696 // Maximum number of sources that can be created
697 ALuint SourcesMax;
698 // Maximum number of slots that can be created
699 ALuint AuxiliaryEffectSlotMax;
701 ALCuint NumMonoSources;
702 ALCuint NumStereoSources;
703 ALsizei NumAuxSends;
705 // Map of Buffers for this device
706 UIntMap BufferMap;
708 // Map of Effects for this device
709 UIntMap EffectMap;
711 // Map of Filters for this device
712 UIntMap FilterMap;
714 /* HRTF state and info */
715 DirectHrtfState *Hrtf;
716 al_string HrtfName;
717 struct Hrtf *HrtfHandle;
718 vector_EnumeratedHrtf HrtfList;
719 ALCenum HrtfStatus;
721 /* UHJ encoder state */
722 struct Uhj2Encoder *Uhj_Encoder;
724 /* High quality Ambisonic decoder */
725 struct BFormatDec *AmbiDecoder;
727 /* Stereo-to-binaural filter */
728 struct bs2b *Bs2b;
730 /* First-order ambisonic upsampler for higher-order output */
731 struct AmbiUpsampler *AmbiUp;
733 /* Rendering mode. */
734 enum RenderMode Render_Mode;
736 // Device flags
737 ALuint Flags;
739 ALuint64 ClockBase;
740 ALuint SamplesDone;
742 /* Temp storage used for each source when mixing. */
743 alignas(16) ALfloat SourceData[BUFFERSIZE];
744 alignas(16) ALfloat ResampledData[BUFFERSIZE];
745 alignas(16) ALfloat FilteredData[BUFFERSIZE];
746 alignas(16) ALfloat NFCtrlData[BUFFERSIZE];
748 /* The "dry" path corresponds to the main output. */
749 struct {
750 AmbiConfig Ambi;
751 /* Number of coefficients in each Ambi.Coeffs to mix together (4 for
752 * first-order, 9 for second-order, etc). If the count is 0, Ambi.Map
753 * is used instead to map each output to a coefficient index.
755 ALsizei CoeffCount;
757 ALfloat (*Buffer)[BUFFERSIZE];
758 ALsizei NumChannels;
759 ALsizei NumChannelsPerOrder[MAX_AMBI_ORDER+1];
760 } Dry;
762 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
763 struct {
764 AmbiConfig Ambi;
765 /* Will only be 4 or 0. */
766 ALsizei CoeffCount;
768 ALfloat (*Buffer)[BUFFERSIZE];
769 ALsizei NumChannels;
770 } FOAOut;
772 /* "Real" output, which will be written to the device buffer. May alias the
773 * dry buffer.
775 struct {
776 enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
778 ALfloat (*Buffer)[BUFFERSIZE];
779 ALsizei NumChannels;
780 } RealOut;
782 struct FrontStablizer *Stablizer;
784 struct Compressor *Limiter;
786 /* The average speaker distance as determined by the ambdec configuration
787 * (or alternatively, by the NFC-HOA reference delay). Only used for NFC.
789 ALfloat AvgSpeakerDist;
791 /* Delay buffers used to compensate for speaker distances. */
792 DistanceComp ChannelDelay[MAX_OUTPUT_CHANNELS];
794 /* Dithering control. */
795 ALfloat DitherDepth;
796 ALuint DitherSeed;
798 /* Running count of the mixer invocations, in 31.1 fixed point. This
799 * actually increments *twice* when mixing, first at the start and then at
800 * the end, so the bottom bit indicates if the device is currently mixing
801 * and the upper bits indicates how many mixes have been done.
803 RefCount MixCount;
805 // Contexts created on this device
806 ATOMIC(ALCcontext*) ContextList;
808 almtx_t BackendLock;
809 struct ALCbackend *Backend;
811 ALCdevice *volatile next;
814 // Frequency was requested by the app or config file
815 #define DEVICE_FREQUENCY_REQUEST (1u<<1)
816 // Channel configuration was requested by the config file
817 #define DEVICE_CHANNELS_REQUEST (1u<<2)
818 // Sample type was requested by the config file
819 #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
821 // Specifies if the DSP is paused at user request
822 #define DEVICE_PAUSED (1u<<30)
824 // Specifies if the device is currently running
825 #define DEVICE_RUNNING (1u<<31)
828 /* Nanosecond resolution for the device clock time. */
829 #define DEVICE_CLOCK_RES U64(1000000000)
832 /* Must be less than 15 characters (16 including terminating null) for
833 * compatibility with pthread_setname_np limitations. */
834 #define MIXER_THREAD_NAME "alsoft-mixer"
836 #define RECORD_THREAD_NAME "alsoft-record"
839 struct ALCcontext_struct {
840 RefCount ref;
842 struct ALlistener *Listener;
844 UIntMap SourceMap;
845 UIntMap EffectSlotMap;
847 ATOMIC(ALenum) LastError;
849 enum DistanceModel DistanceModel;
850 ALboolean SourceDistanceModel;
852 ALfloat DopplerFactor;
853 ALfloat DopplerVelocity;
854 ALfloat SpeedOfSound;
855 ALfloat MetersPerUnit;
857 ATOMIC_FLAG PropsClean;
858 ATOMIC(ALenum) DeferUpdates;
860 RWLock PropLock;
862 /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
863 * indicates if updates are currently happening).
865 RefCount UpdateCount;
866 ATOMIC(ALenum) HoldUpdates;
868 ALfloat GainBoost;
870 ATOMIC(struct ALcontextProps*) Update;
871 ATOMIC(struct ALcontextProps*) FreeList;
873 struct ALvoice **Voices;
874 ALsizei VoiceCount;
875 ALsizei MaxVoices;
877 ATOMIC(struct ALeffectslotArray*) ActiveAuxSlots;
879 /* Default effect slot */
880 struct ALeffectslot *DefaultSlot;
882 ALCdevice *Device;
883 const ALCchar *ExtensionList;
885 ALCcontext *volatile next;
887 /* Memory space used by the listener (and possibly default effect slot) */
888 alignas(16) ALCbyte _listener_mem[];
891 ALCcontext *GetContextRef(void);
893 void ALCcontext_IncRef(ALCcontext *context);
894 void ALCcontext_DecRef(ALCcontext *context);
896 void AllocateVoices(ALCcontext *context, ALsizei num_voices, ALsizei old_sends);
898 void AppendAllDevicesList(const ALCchar *name);
899 void AppendCaptureDeviceList(const ALCchar *name);
901 void ALCdevice_Lock(ALCdevice *device);
902 void ALCdevice_Unlock(ALCdevice *device);
904 void ALCcontext_DeferUpdates(ALCcontext *context);
905 void ALCcontext_ProcessUpdates(ALCcontext *context);
908 typedef struct {
909 #ifdef HAVE_FENV_H
910 DERIVE_FROM_TYPE(fenv_t);
911 #ifdef _WIN32
912 int round_mode;
913 #endif
914 #else
915 int state;
916 #endif
917 #ifdef HAVE_SSE
918 int sse_state;
919 #endif
920 } FPUCtl;
921 void SetMixerFPUMode(FPUCtl *ctl);
922 void RestoreFPUMode(const FPUCtl *ctl);
923 #ifdef __GNUC__
924 /* Use an alternate macro set with GCC to avoid accidental continue or break
925 * statements within the mixer mode.
927 #define START_MIXER_MODE() __extension__({ FPUCtl _oldMode; SetMixerFPUMode(&_oldMode);
928 #define END_MIXER_MODE() RestoreFPUMode(&_oldMode); })
929 #else
930 #define START_MIXER_MODE() do { FPUCtl _oldMode; SetMixerFPUMode(&_oldMode);
931 #define END_MIXER_MODE() RestoreFPUMode(&_oldMode); } while(0)
932 #endif
933 #define LEAVE_MIXER_MODE() RestoreFPUMode(&_oldMode)
936 typedef struct ll_ringbuffer ll_ringbuffer_t;
937 typedef struct ll_ringbuffer_data {
938 char *buf;
939 size_t len;
940 } ll_ringbuffer_data_t;
941 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
942 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
943 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
944 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
945 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
946 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
947 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
948 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
949 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
950 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
951 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
952 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
953 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
955 void ReadALConfig(void);
956 void FreeALConfig(void);
957 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
958 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
959 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
960 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
961 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
962 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
963 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
964 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
966 void SetRTPriority(void);
968 void SetDefaultChannelOrder(ALCdevice *device);
969 void SetDefaultWFXChannelOrder(ALCdevice *device);
971 const ALCchar *DevFmtTypeString(enum DevFmtType type);
972 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
975 * GetChannelIdxByName
977 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
978 * doesn't exist.
980 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
982 ALint i;
983 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
985 if(names[i] == chan)
986 return i;
988 return -1;
990 #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
992 extern FILE *LogFile;
994 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
995 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
996 #else
997 void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
998 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
999 #endif
1001 #ifdef __ANDROID__
1002 #include <android/log.h>
1003 #define LOG_ANDROID(T, MSG, ...) __android_log_print(T, "openal", "AL lib: %s: "MSG, __FUNCTION__ , ## __VA_ARGS__)
1004 #else
1005 #define LOG_ANDROID(T, MSG, ...) ((void)0)
1006 #endif
1008 enum LogLevel {
1009 NoLog,
1010 LogError,
1011 LogWarning,
1012 LogTrace,
1013 LogRef
1015 extern enum LogLevel LogLevel;
1017 #define TRACEREF(...) do { \
1018 if(LogLevel >= LogRef) \
1019 AL_PRINT("(--)", __VA_ARGS__); \
1020 } while(0)
1022 #define TRACE(...) do { \
1023 if(LogLevel >= LogTrace) \
1024 AL_PRINT("(II)", __VA_ARGS__); \
1025 LOG_ANDROID(ANDROID_LOG_DEBUG, __VA_ARGS__); \
1026 } while(0)
1028 #define WARN(...) do { \
1029 if(LogLevel >= LogWarning) \
1030 AL_PRINT("(WW)", __VA_ARGS__); \
1031 LOG_ANDROID(ANDROID_LOG_WARN, __VA_ARGS__); \
1032 } while(0)
1034 #define ERR(...) do { \
1035 if(LogLevel >= LogError) \
1036 AL_PRINT("(EE)", __VA_ARGS__); \
1037 LOG_ANDROID(ANDROID_LOG_ERROR, __VA_ARGS__); \
1038 } while(0)
1041 extern ALint RTPrioLevel;
1044 extern ALuint CPUCapFlags;
1045 enum {
1046 CPU_CAP_SSE = 1<<0,
1047 CPU_CAP_SSE2 = 1<<1,
1048 CPU_CAP_SSE3 = 1<<2,
1049 CPU_CAP_SSE4_1 = 1<<3,
1050 CPU_CAP_NEON = 1<<4,
1053 void FillCPUCaps(ALuint capfilter);
1055 vector_al_string SearchDataFiles(const char *match, const char *subdir);
1057 /* Small hack to use a pointer-to-array types as a normal argument type.
1058 * Shouldn't be used directly.
1060 typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
1061 typedef ALfloat ALfloat2[2];
1064 /* The compressor requires the following information for proper
1065 * initialization:
1067 * PreGainDb - Gain applied before detection (in dB).
1068 * PostGainDb - Gain applied after compression (in dB).
1069 * SummedLink - Whether to use summed (true) or maxed (false) linking.
1070 * RmsSensing - Whether to use RMS (true) or Peak (false) sensing.
1071 * AttackTimeMin - Minimum attack time (in seconds).
1072 * AttackTimeMax - Maximum attack time. Automates when min != max.
1073 * ReleaseTimeMin - Minimum release time (in seconds).
1074 * ReleaseTimeMax - Maximum release time. Automates when min != max.
1075 * Ratio - Compression ratio (x:1). Set to 0 for true limiter.
1076 * ThresholdDb - Triggering threshold (in dB).
1077 * KneeDb - Knee width (below threshold; in dB).
1078 * SampleRate - Sample rate to process.
1080 struct Compressor *CompressorInit(const ALfloat PreGainDb, const ALfloat PostGainDb,
1081 const ALboolean SummedLink, const ALboolean RmsSensing, const ALfloat AttackTimeMin,
1082 const ALfloat AttackTimeMax, const ALfloat ReleaseTimeMin, const ALfloat ReleaseTimeMax,
1083 const ALfloat Ratio, const ALfloat ThresholdDb, const ALfloat KneeDb,
1084 const ALuint SampleRate);
1086 ALuint GetCompressorSampleRate(const struct Compressor *Comp);
1088 void ApplyCompression(struct Compressor *Comp, const ALsizei NumChans, const ALsizei SamplesToDo,
1089 ALfloat (*restrict OutBuffer)[BUFFERSIZE]);
1091 #ifdef __cplusplus
1093 #endif
1095 #endif