Implement NFC filters for Ambisonic rendering
[openal-soft.git] / OpenAL32 / Include / alMain.h
blobe4c5b94a0a4d129510e5a49ddf010f3d46039792
1 #ifndef AL_MAIN_H
2 #define AL_MAIN_H
4 #include <string.h>
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <assert.h>
8 #include <math.h>
9 #include <limits.h>
11 #ifdef HAVE_STRINGS_H
12 #include <strings.h>
13 #endif
15 #ifdef HAVE_FENV_H
16 #include <fenv.h>
17 #endif
19 #include "AL/al.h"
20 #include "AL/alc.h"
21 #include "AL/alext.h"
23 #include "static_assert.h"
24 #include "align.h"
25 #include "atomic.h"
26 #include "uintmap.h"
27 #include "vector.h"
28 #include "alstring.h"
29 #include "almalloc.h"
30 #include "threads.h"
32 #ifndef ALC_SOFT_loopback2
33 #define ALC_SOFT_loopback2 1
34 #define ALC_AMBISONIC_LAYOUT_SOFT 0x1997
35 #define ALC_AMBISONIC_SCALING_SOFT 0x1998
36 #define ALC_AMBISONIC_ORDER_SOFT 0x1999
38 #define ALC_BFORMAT3D_SOFT 0x1508
40 /* Ambisonic layouts */
41 #define ALC_ACN_SOFT 0x1600
42 #define ALC_FUMA_SOFT 0x1601
44 /* Ambisonic scalings (normalization) */
45 #define ALC_N3D_SOFT 0x1700
46 #define ALC_SN3D_SOFT 0x1701
47 /*#define ALC_FUMA_SOFT*/
49 typedef ALCboolean (ALC_APIENTRY*LPALCISAMBISONICFORMATSUPPORTEDSOFT)(ALCdevice *device, ALCenum layout, ALCenum scaling, ALsizei order);
50 #ifdef AL_ALEXT_PROTOTYPES
51 ALC_API ALCboolean ALC_APIENTRY alcIsAmbisonicFormatSupportedSOFT(ALCdevice *device, ALCenum layout, ALCenum scaling, ALsizei order);
52 #endif
53 #endif
55 #ifndef ALC_SOFT_device_clock
56 #define ALC_SOFT_device_clock 1
57 typedef int64_t ALCint64SOFT;
58 typedef uint64_t ALCuint64SOFT;
59 #define ALC_DEVICE_CLOCK_SOFT 0x1600
60 #define ALC_DEVICE_LATENCY_SOFT 0x1601
61 #define ALC_DEVICE_CLOCK_LATENCY_SOFT 0x1602
62 typedef void (ALC_APIENTRY*LPALCGETINTEGER64VSOFT)(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
63 #ifdef AL_ALEXT_PROTOTYPES
64 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
65 #endif
66 #endif
68 #ifndef AL_SOFT_buffer_samples2
69 #define AL_SOFT_buffer_samples2 1
70 /* Channel configurations */
71 #define AL_MONO_SOFT 0x1500
72 #define AL_STEREO_SOFT 0x1501
73 #define AL_REAR_SOFT 0x1502
74 #define AL_QUAD_SOFT 0x1503
75 #define AL_5POINT1_SOFT 0x1504
76 #define AL_6POINT1_SOFT 0x1505
77 #define AL_7POINT1_SOFT 0x1506
78 #define AL_BFORMAT2D_SOFT 0x1507
79 #define AL_BFORMAT3D_SOFT 0x1508
81 /* Sample types */
82 #define AL_BYTE_SOFT 0x1400
83 #define AL_UNSIGNED_BYTE_SOFT 0x1401
84 #define AL_SHORT_SOFT 0x1402
85 #define AL_UNSIGNED_SHORT_SOFT 0x1403
86 #define AL_INT_SOFT 0x1404
87 #define AL_UNSIGNED_INT_SOFT 0x1405
88 #define AL_FLOAT_SOFT 0x1406
89 #define AL_DOUBLE_SOFT 0x1407
90 #define AL_BYTE3_SOFT 0x1408
91 #define AL_UNSIGNED_BYTE3_SOFT 0x1409
92 #define AL_MULAW_SOFT 0x140A
94 /* Storage formats */
95 #define AL_MONO8_SOFT 0x1100
96 #define AL_MONO16_SOFT 0x1101
97 #define AL_MONO32F_SOFT 0x10010
98 #define AL_STEREO8_SOFT 0x1102
99 #define AL_STEREO16_SOFT 0x1103
100 #define AL_STEREO32F_SOFT 0x10011
101 #define AL_QUAD8_SOFT 0x1204
102 #define AL_QUAD16_SOFT 0x1205
103 #define AL_QUAD32F_SOFT 0x1206
104 #define AL_REAR8_SOFT 0x1207
105 #define AL_REAR16_SOFT 0x1208
106 #define AL_REAR32F_SOFT 0x1209
107 #define AL_5POINT1_8_SOFT 0x120A
108 #define AL_5POINT1_16_SOFT 0x120B
109 #define AL_5POINT1_32F_SOFT 0x120C
110 #define AL_6POINT1_8_SOFT 0x120D
111 #define AL_6POINT1_16_SOFT 0x120E
112 #define AL_6POINT1_32F_SOFT 0x120F
113 #define AL_7POINT1_8_SOFT 0x1210
114 #define AL_7POINT1_16_SOFT 0x1211
115 #define AL_7POINT1_32F_SOFT 0x1212
116 #define AL_BFORMAT2D_8_SOFT 0x20021
117 #define AL_BFORMAT2D_16_SOFT 0x20022
118 #define AL_BFORMAT2D_32F_SOFT 0x20023
119 #define AL_BFORMAT3D_8_SOFT 0x20031
120 #define AL_BFORMAT3D_16_SOFT 0x20032
121 #define AL_BFORMAT3D_32F_SOFT 0x20033
123 /* Buffer attributes */
124 #define AL_INTERNAL_FORMAT_SOFT 0x2008
125 #define AL_BYTE_LENGTH_SOFT 0x2009
126 #define AL_SAMPLE_LENGTH_SOFT 0x200A
127 #define AL_SEC_LENGTH_SOFT 0x200B
129 #if 0
130 typedef void (AL_APIENTRY*LPALBUFFERSAMPLESSOFT)(ALuint,ALuint,ALenum,ALsizei,ALenum,ALenum,const ALvoid*);
131 typedef void (AL_APIENTRY*LPALGETBUFFERSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,ALvoid*);
132 typedef ALboolean (AL_APIENTRY*LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum);
133 #ifdef AL_ALEXT_PROTOTYPES
134 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer, ALuint samplerate, ALenum internalformat, ALsizei samples, ALenum channels, ALenum type, const ALvoid *data);
135 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer, ALsizei offset, ALsizei samples, ALenum channels, ALenum type, ALvoid *data);
136 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format);
137 #endif
138 #endif
139 #endif
142 #if defined(_WIN64)
143 #define SZFMT "%I64u"
144 #elif defined(_WIN32)
145 #define SZFMT "%u"
146 #else
147 #define SZFMT "%zu"
148 #endif
151 #ifdef __GNUC__
152 /* Because of a long-standing deficiency in C, you're not allowed to implicitly
153 * cast a pointer-to-type-array to a pointer-to-const-type-array. For example,
155 * int (*ptr)[10];
156 * const int (*cptr)[10] = ptr;
158 * is not allowed and most compilers will generate noisy warnings about
159 * incompatible types, even though it just makes the array elements const.
160 * Clang will allow it if you make the array type a typedef, like this:
162 * typedef int int10[10];
163 * int10 *ptr;
164 * const int10 *cptr = ptr;
166 * however GCC does not and still issues the incompatible type warning. The
167 * "proper" way to fix it is to add an explicit cast for the constified type,
168 * but that removes the vast majority of otherwise useful type-checking you'd
169 * get, and runs the risk of improper casts if types are later changed. Leaving
170 * it non-const can also be an issue if you use it as a function parameter, and
171 * happen to have a const type as input (and also reduce the capabilities of
172 * the compiler to better optimize the function).
174 * So to work around the problem, we use a macro. The macro first assigns the
175 * incoming variable to the specified non-const type to ensure it's the correct
176 * type, then casts the variable as the desired constified type. Very ugly, but
177 * I'd rather not have hundreds of lines of warnings because I want to tell the
178 * compiler that some array(s) can't be changed by the code, or have lots of
179 * error-prone casts.
181 #define SAFE_CONST(T, var) __extension__({ \
182 T _tmp = (var); \
183 (const T)_tmp; \
185 #else
186 /* Non-GNU-compatible compilers have to use a straight cast with no extra
187 * checks, due to the lack of multi-statement expressions.
189 #define SAFE_CONST(T, var) ((const T)(var))
190 #endif
193 typedef ALint64SOFT ALint64;
194 typedef ALuint64SOFT ALuint64;
196 #ifndef U64
197 #if defined(_MSC_VER)
198 #define U64(x) ((ALuint64)(x##ui64))
199 #elif SIZEOF_LONG == 8
200 #define U64(x) ((ALuint64)(x##ul))
201 #elif SIZEOF_LONG_LONG == 8
202 #define U64(x) ((ALuint64)(x##ull))
203 #endif
204 #endif
206 #ifndef UINT64_MAX
207 #define UINT64_MAX U64(18446744073709551615)
208 #endif
210 #ifndef UNUSED
211 #if defined(__cplusplus)
212 #define UNUSED(x)
213 #elif defined(__GNUC__)
214 #define UNUSED(x) UNUSED_##x __attribute__((unused))
215 #elif defined(__LCLINT__)
216 #define UNUSED(x) /*@unused@*/ x
217 #else
218 #define UNUSED(x) x
219 #endif
220 #endif
222 #ifdef __GNUC__
223 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
224 #else
225 #define DECL_FORMAT(x, y, z)
226 #endif
228 #if defined(__GNUC__) && defined(__i386__)
229 /* force_align_arg_pointer is required for proper function arguments aligning
230 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
231 * thread functions will be properly aligned on the stack, even though GCC may
232 * generate code with the assumption that it is. */
233 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
234 #else
235 #define FORCE_ALIGN
236 #endif
238 #ifdef HAVE_C99_VLA
239 #define DECL_VLA(T, _name, _size) T _name[(_size)]
240 #else
241 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
242 #endif
244 #ifndef PATH_MAX
245 #ifdef MAX_PATH
246 #define PATH_MAX MAX_PATH
247 #else
248 #define PATH_MAX 4096
249 #endif
250 #endif
253 static const union {
254 ALuint u;
255 ALubyte b[sizeof(ALuint)];
256 } EndianTest = { 1 };
257 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
259 #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
262 #define DERIVE_FROM_TYPE(t) t t##_parent
263 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
264 #ifdef __GNUC__
265 #define STATIC_UPCAST(to, from, obj) __extension__({ \
266 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
267 "Invalid upcast object from type"); \
268 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
270 #else
271 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
272 #endif
274 #define DECLARE_FORWARD(T1, T2, rettype, func) \
275 rettype T1##_##func(T1 *obj) \
276 { return T2##_##func(STATIC_CAST(T2, obj)); }
278 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
279 rettype T1##_##func(T1 *obj, argtype1 a) \
280 { return T2##_##func(STATIC_CAST(T2, obj), a); }
282 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
283 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
284 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
286 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
287 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
288 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
291 #define GET_VTABLE1(T1) (&(T1##_vtable))
292 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
294 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
295 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
297 #define DECLARE_THUNK(T1, T2, rettype, func) \
298 static rettype T1##_##T2##_##func(T2 *obj) \
299 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
301 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
302 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
303 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
305 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
306 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
307 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
309 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
310 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
311 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
313 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
314 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
315 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
317 #define DECLARE_DEFAULT_ALLOCATORS(T) \
318 static void* T##_New(size_t size) { return al_malloc(16, size); } \
319 static void T##_Delete(void *ptr) { al_free(ptr); }
321 /* Helper to extract an argument list for VCALL. Not used directly. */
322 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
324 /* Call a "virtual" method on an object, with arguments. */
325 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
326 /* Call a "virtual" method on an object, with no arguments. */
327 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
329 #define DELETE_OBJ(obj) do { \
330 if((obj) != NULL) \
332 V0((obj),Destruct)(); \
333 V0((obj),Delete)(); \
335 } while(0)
338 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
340 } while(0)
342 #define NEW_OBJ(_res, T) do { \
343 _res = T##_New(sizeof(T)); \
344 if(_res) \
346 memset(_res, 0, sizeof(T)); \
347 T##_Construct(_res, EXTRACT_NEW_ARGS
348 #define NEW_OBJ0(_res, T) do { \
349 _res = T##_New(sizeof(T)); \
350 if(_res) \
352 memset(_res, 0, sizeof(T)); \
353 T##_Construct(_res EXTRACT_NEW_ARGS
356 #ifdef __cplusplus
357 extern "C" {
358 #endif
360 struct Hrtf;
363 #define DEFAULT_OUTPUT_RATE (44100)
364 #define MIN_OUTPUT_RATE (8000)
367 /* Find the next power-of-2 for non-power-of-2 numbers. */
368 inline ALuint NextPowerOf2(ALuint value)
370 if(value > 0)
372 value--;
373 value |= value>>1;
374 value |= value>>2;
375 value |= value>>4;
376 value |= value>>8;
377 value |= value>>16;
379 return value+1;
382 /** Round up a value to the next multiple. */
383 inline size_t RoundUp(size_t value, size_t r)
385 value += r-1;
386 return value - (value%r);
389 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
390 * mode. */
391 inline ALint fastf2i(ALfloat f)
393 #ifdef HAVE_LRINTF
394 return lrintf(f);
395 #elif defined(_MSC_VER) && defined(_M_IX86)
396 ALint i;
397 __asm fld f
398 __asm fistp i
399 return i;
400 #else
401 return (ALint)f;
402 #endif
405 /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
406 * mode. */
407 inline ALuint fastf2u(ALfloat f)
408 { return fastf2i(f); }
411 enum DevProbe {
412 ALL_DEVICE_PROBE,
413 CAPTURE_DEVICE_PROBE
416 typedef struct {
417 ALCenum (*OpenPlayback)(ALCdevice*, const ALCchar*);
418 void (*ClosePlayback)(ALCdevice*);
419 ALCboolean (*ResetPlayback)(ALCdevice*);
420 ALCboolean (*StartPlayback)(ALCdevice*);
421 void (*StopPlayback)(ALCdevice*);
423 ALCenum (*OpenCapture)(ALCdevice*, const ALCchar*);
424 void (*CloseCapture)(ALCdevice*);
425 void (*StartCapture)(ALCdevice*);
426 void (*StopCapture)(ALCdevice*);
427 ALCenum (*CaptureSamples)(ALCdevice*, void*, ALCuint);
428 ALCuint (*AvailableSamples)(ALCdevice*);
429 } BackendFuncs;
431 ALCboolean alc_ca_init(BackendFuncs *func_list);
432 void alc_ca_deinit(void);
433 void alc_ca_probe(enum DevProbe type);
434 ALCboolean alc_qsa_init(BackendFuncs *func_list);
435 void alc_qsa_deinit(void);
436 void alc_qsa_probe(enum DevProbe type);
438 struct ALCbackend;
441 enum DistanceModel {
442 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
443 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
444 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
445 InverseDistance = AL_INVERSE_DISTANCE,
446 LinearDistance = AL_LINEAR_DISTANCE,
447 ExponentDistance = AL_EXPONENT_DISTANCE,
448 DisableDistance = AL_NONE,
450 DefaultDistanceModel = InverseDistanceClamped
453 enum Channel {
454 FrontLeft = 0,
455 FrontRight,
456 FrontCenter,
457 LFE,
458 BackLeft,
459 BackRight,
460 BackCenter,
461 SideLeft,
462 SideRight,
464 UpperFrontLeft,
465 UpperFrontRight,
466 UpperBackLeft,
467 UpperBackRight,
468 LowerFrontLeft,
469 LowerFrontRight,
470 LowerBackLeft,
471 LowerBackRight,
473 Aux0,
474 Aux1,
475 Aux2,
476 Aux3,
477 Aux4,
478 Aux5,
479 Aux6,
480 Aux7,
481 Aux8,
482 Aux9,
483 Aux10,
484 Aux11,
485 Aux12,
486 Aux13,
487 Aux14,
488 Aux15,
490 InvalidChannel
494 /* Device formats */
495 enum DevFmtType {
496 DevFmtByte = ALC_BYTE_SOFT,
497 DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
498 DevFmtShort = ALC_SHORT_SOFT,
499 DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
500 DevFmtInt = ALC_INT_SOFT,
501 DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
502 DevFmtFloat = ALC_FLOAT_SOFT,
504 DevFmtTypeDefault = DevFmtFloat
506 enum DevFmtChannels {
507 DevFmtMono = ALC_MONO_SOFT,
508 DevFmtStereo = ALC_STEREO_SOFT,
509 DevFmtQuad = ALC_QUAD_SOFT,
510 DevFmtX51 = ALC_5POINT1_SOFT,
511 DevFmtX61 = ALC_6POINT1_SOFT,
512 DevFmtX71 = ALC_7POINT1_SOFT,
514 /* Similar to 5.1, except using rear channels instead of sides */
515 DevFmtX51Rear = 0x80000000,
517 /* Ambisonic formats should be kept together */
518 DevFmtAmbi1,
519 DevFmtAmbi2,
520 DevFmtAmbi3,
522 DevFmtChannelsDefault = DevFmtStereo
524 #define MAX_OUTPUT_CHANNELS (16)
526 ALsizei BytesFromDevFmt(enum DevFmtType type);
527 ALsizei ChannelsFromDevFmt(enum DevFmtChannels chans);
528 inline ALsizei FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
530 return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
533 enum AmbiLayout {
534 AmbiLayout_FuMa = ALC_FUMA_SOFT, /* FuMa channel order */
535 AmbiLayout_ACN = ALC_ACN_SOFT, /* ACN channel order */
537 AmbiLayout_Default = AmbiLayout_ACN
540 enum AmbiNorm {
541 AmbiNorm_FuMa = ALC_FUMA_SOFT, /* FuMa normalization */
542 AmbiNorm_SN3D = ALC_SN3D_SOFT, /* SN3D normalization */
543 AmbiNorm_N3D = ALC_N3D_SOFT, /* N3D normalization */
545 AmbiNorm_Default = AmbiNorm_SN3D
549 extern const struct EffectList {
550 const char *name;
551 int type;
552 const char *ename;
553 ALenum val;
554 } EffectList[];
557 enum DeviceType {
558 Playback,
559 Capture,
560 Loopback
564 enum RenderMode {
565 NormalRender,
566 StereoPair,
567 HrtfRender
571 /* The maximum number of Ambisonics coefficients. For a given order (o), the
572 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
573 * second-order has 9, third-order has 16, and fourth-order has 25.
575 #define MAX_AMBI_ORDER 3
576 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
578 /* A bitmask of ambisonic channels with height information. If none of these
579 * channels are used/needed, there's no height (e.g. with most surround sound
580 * speaker setups). This only specifies up to 4th order, which is the highest
581 * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
582 * order). This is ACN ordering, with bit 0 being ACN 0, etc.
584 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
586 /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
587 * representation. This is 2 per each order above zero-order, plus 1 for zero-
588 * order. Or simply, o*2 + 1.
590 #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
593 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
594 typedef struct BFChannelConfig {
595 ALfloat Scale;
596 ALsizei Index;
597 } BFChannelConfig;
599 typedef union AmbiConfig {
600 /* Ambisonic coefficients for mixing to the dry buffer. */
601 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
602 /* Coefficient channel mapping for mixing to the dry buffer. */
603 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
604 } AmbiConfig;
607 #define HRTF_HISTORY_BITS (6)
608 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
609 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
611 #define HRIR_BITS (7)
612 #define HRIR_LENGTH (1<<HRIR_BITS)
613 #define HRIR_MASK (HRIR_LENGTH-1)
615 typedef struct HrtfState {
616 alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
617 alignas(16) ALfloat Values[HRIR_LENGTH][2];
618 } HrtfState;
620 typedef struct HrtfParams {
621 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
622 ALsizei Delay[2];
623 } HrtfParams;
625 typedef struct HrtfEntry {
626 al_string name;
628 const struct Hrtf *hrtf;
629 } HrtfEntry;
630 TYPEDEF_VECTOR(HrtfEntry, vector_HrtfEntry)
633 /* Maximum delay in samples for speaker distance compensation. */
634 #define MAX_DELAY_LENGTH 1024
636 typedef struct DistanceComp {
637 ALfloat Gain;
638 ALsizei Length; /* Valid range is [0...MAX_DELAY_LENGTH). */
639 ALfloat *Buffer;
640 } DistanceComp;
642 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
643 * more memory, while smaller values may need more iterations. The value needs
644 * to be a sensible size, however, as it constrains the max stepping value used
645 * for mixing, as well as the maximum number of samples per mixing iteration.
647 #define BUFFERSIZE 2048
649 struct ALCdevice_struct
651 RefCount ref;
653 ALCboolean Connected;
654 enum DeviceType Type;
656 ALuint Frequency;
657 ALuint UpdateSize;
658 ALuint NumUpdates;
659 enum DevFmtChannels FmtChans;
660 enum DevFmtType FmtType;
661 ALboolean IsHeadphones;
662 /* For DevFmtAmbi* output only, specifies the channel order and
663 * normalization.
665 enum AmbiLayout AmbiLayout;
666 enum AmbiNorm AmbiScale;
668 al_string DeviceName;
670 ATOMIC(ALCenum) LastError;
672 // Maximum number of sources that can be created
673 ALuint SourcesMax;
674 // Maximum number of slots that can be created
675 ALuint AuxiliaryEffectSlotMax;
677 ALCuint NumMonoSources;
678 ALCuint NumStereoSources;
679 ALsizei NumAuxSends;
681 // Map of Buffers for this device
682 UIntMap BufferMap;
684 // Map of Effects for this device
685 UIntMap EffectMap;
687 // Map of Filters for this device
688 UIntMap FilterMap;
690 /* HRTF filter tables */
691 struct {
692 vector_HrtfEntry List;
693 al_string Name;
694 ALCenum Status;
695 const struct Hrtf *Handle;
697 /* HRTF filter state for dry buffer content */
698 alignas(16) ALfloat Values[9][HRIR_LENGTH][2];
699 alignas(16) ALfloat Coeffs[9][HRIR_LENGTH][2];
700 ALsizei Offset;
701 ALsizei IrSize;
702 } Hrtf;
704 /* UHJ encoder state */
705 struct Uhj2Encoder *Uhj_Encoder;
707 /* High quality Ambisonic decoder */
708 struct BFormatDec *AmbiDecoder;
710 /* Stereo-to-binaural filter */
711 struct bs2b *Bs2b;
713 /* First-order ambisonic upsampler for higher-order output */
714 struct AmbiUpsampler *AmbiUp;
716 /* Rendering mode. */
717 enum RenderMode Render_Mode;
719 // Device flags
720 ALuint Flags;
722 ALuint64 ClockBase;
723 ALuint SamplesDone;
725 /* Temp storage used for each source when mixing. */
726 alignas(16) ALfloat SourceData[BUFFERSIZE];
727 alignas(16) ALfloat ResampledData[BUFFERSIZE];
728 alignas(16) ALfloat FilteredData[BUFFERSIZE];
729 alignas(16) ALfloat NFCtrlData[BUFFERSIZE];
731 /* The "dry" path corresponds to the main output. */
732 struct {
733 AmbiConfig Ambi;
734 /* Number of coefficients in each Ambi.Coeffs to mix together (4 for
735 * first-order, 9 for second-order, etc). If the count is 0, Ambi.Map
736 * is used instead to map each output to a coefficient index.
738 ALsizei CoeffCount;
740 ALfloat (*Buffer)[BUFFERSIZE];
741 ALsizei NumChannels;
742 ALsizei NumChannelsPerOrder[MAX_AMBI_ORDER+1];
743 } Dry;
745 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
746 struct {
747 AmbiConfig Ambi;
748 /* Will only be 4 or 0. */
749 ALsizei CoeffCount;
751 ALfloat (*Buffer)[BUFFERSIZE];
752 ALsizei NumChannels;
753 } FOAOut;
755 /* "Real" output, which will be written to the device buffer. May alias the
756 * dry buffer.
758 struct {
759 enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
761 ALfloat (*Buffer)[BUFFERSIZE];
762 ALsizei NumChannels;
763 } RealOut;
765 /* The average speaker distance as determined by the ambdec configuration
766 * (or alternatively, by the NFC-HOA reference delay). Only used for NFC.
768 ALfloat AvgSpeakerDist;
770 /* Delay buffers used to compensate for speaker distances. */
771 DistanceComp ChannelDelay[MAX_OUTPUT_CHANNELS];
773 /* Running count of the mixer invocations, in 31.1 fixed point. This
774 * actually increments *twice* when mixing, first at the start and then at
775 * the end, so the bottom bit indicates if the device is currently mixing
776 * and the upper bits indicates how many mixes have been done.
778 RefCount MixCount;
780 /* Default effect slot */
781 struct ALeffectslot *DefaultSlot;
783 // Contexts created on this device
784 ATOMIC(ALCcontext*) ContextList;
786 almtx_t BackendLock;
787 struct ALCbackend *Backend;
789 void *ExtraData; // For the backend's use
791 ALCdevice *volatile next;
793 /* Memory space used by the default slot (Playback devices only) */
794 alignas(16) ALCbyte _slot_mem[];
797 // Frequency was requested by the app or config file
798 #define DEVICE_FREQUENCY_REQUEST (1u<<1)
799 // Channel configuration was requested by the config file
800 #define DEVICE_CHANNELS_REQUEST (1u<<2)
801 // Sample type was requested by the config file
802 #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
804 // Specifies if the DSP is paused at user request
805 #define DEVICE_PAUSED (1u<<30)
807 // Specifies if the device is currently running
808 #define DEVICE_RUNNING (1u<<31)
811 /* Nanosecond resolution for the device clock time. */
812 #define DEVICE_CLOCK_RES U64(1000000000)
815 /* Must be less than 15 characters (16 including terminating null) for
816 * compatibility with pthread_setname_np limitations. */
817 #define MIXER_THREAD_NAME "alsoft-mixer"
819 #define RECORD_THREAD_NAME "alsoft-record"
822 struct ALCcontext_struct {
823 RefCount ref;
825 struct ALlistener *Listener;
827 UIntMap SourceMap;
828 UIntMap EffectSlotMap;
830 ATOMIC(ALenum) LastError;
832 enum DistanceModel DistanceModel;
833 ALboolean SourceDistanceModel;
835 ALfloat DopplerFactor;
836 ALfloat DopplerVelocity;
837 ALfloat SpeedOfSound;
838 ATOMIC(ALenum) DeferUpdates;
840 RWLock PropLock;
842 /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
843 * indicates if updates are currently happening).
845 RefCount UpdateCount;
846 ATOMIC(ALenum) HoldUpdates;
848 ALfloat GainBoost;
850 struct ALvoice **Voices;
851 ALsizei VoiceCount;
852 ALsizei MaxVoices;
854 ATOMIC(struct ALeffectslot*) ActiveAuxSlotList;
856 ALCdevice *Device;
857 const ALCchar *ExtensionList;
859 ALCcontext *volatile next;
861 /* Memory space used by the listener */
862 alignas(16) ALCbyte _listener_mem[];
865 ALCcontext *GetContextRef(void);
867 void ALCcontext_IncRef(ALCcontext *context);
868 void ALCcontext_DecRef(ALCcontext *context);
870 void AllocateVoices(ALCcontext *context, ALsizei num_voices, ALsizei old_sends);
872 void AppendAllDevicesList(const ALCchar *name);
873 void AppendCaptureDeviceList(const ALCchar *name);
875 void ALCdevice_Lock(ALCdevice *device);
876 void ALCdevice_Unlock(ALCdevice *device);
878 void ALCcontext_DeferUpdates(ALCcontext *context, ALenum type);
879 void ALCcontext_ProcessUpdates(ALCcontext *context);
881 enum {
882 DeferOff = AL_FALSE,
883 DeferAll,
884 DeferAllowPlay
888 typedef struct {
889 #ifdef HAVE_FENV_H
890 DERIVE_FROM_TYPE(fenv_t);
891 #else
892 int state;
893 #endif
894 #ifdef HAVE_SSE
895 int sse_state;
896 #endif
897 } FPUCtl;
898 void SetMixerFPUMode(FPUCtl *ctl);
899 void RestoreFPUMode(const FPUCtl *ctl);
902 typedef struct ll_ringbuffer ll_ringbuffer_t;
903 typedef struct ll_ringbuffer_data {
904 char *buf;
905 size_t len;
906 } ll_ringbuffer_data_t;
907 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
908 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
909 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
910 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
911 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
912 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
913 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
914 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
915 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
916 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
917 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
918 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
919 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
921 void ReadALConfig(void);
922 void FreeALConfig(void);
923 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
924 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
925 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
926 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
927 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
928 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
929 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
930 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
932 void SetRTPriority(void);
934 void SetDefaultChannelOrder(ALCdevice *device);
935 void SetDefaultWFXChannelOrder(ALCdevice *device);
937 const ALCchar *DevFmtTypeString(enum DevFmtType type);
938 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
941 * GetChannelIdxByName
943 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
944 * doesn't exist.
946 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
948 ALint i;
949 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
951 if(names[i] == chan)
952 return i;
954 return -1;
956 #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
958 extern FILE *LogFile;
960 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
961 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
962 #else
963 void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
964 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
965 #endif
967 #ifdef __ANDROID__
968 #include <android/log.h>
969 #define LOG_ANDROID(T, MSG, ...) __android_log_print(T, "openal", "AL lib: %s: "MSG, __FUNCTION__ , ## __VA_ARGS__)
970 #else
971 #define LOG_ANDROID(T, MSG, ...) ((void)0)
972 #endif
974 enum LogLevel {
975 NoLog,
976 LogError,
977 LogWarning,
978 LogTrace,
979 LogRef
981 extern enum LogLevel LogLevel;
983 #define TRACEREF(...) do { \
984 if(LogLevel >= LogRef) \
985 AL_PRINT("(--)", __VA_ARGS__); \
986 } while(0)
988 #define TRACE(...) do { \
989 if(LogLevel >= LogTrace) \
990 AL_PRINT("(II)", __VA_ARGS__); \
991 LOG_ANDROID(ANDROID_LOG_DEBUG, __VA_ARGS__); \
992 } while(0)
994 #define WARN(...) do { \
995 if(LogLevel >= LogWarning) \
996 AL_PRINT("(WW)", __VA_ARGS__); \
997 LOG_ANDROID(ANDROID_LOG_WARN, __VA_ARGS__); \
998 } while(0)
1000 #define ERR(...) do { \
1001 if(LogLevel >= LogError) \
1002 AL_PRINT("(EE)", __VA_ARGS__); \
1003 LOG_ANDROID(ANDROID_LOG_ERROR, __VA_ARGS__); \
1004 } while(0)
1007 extern ALint RTPrioLevel;
1010 extern ALuint CPUCapFlags;
1011 enum {
1012 CPU_CAP_SSE = 1<<0,
1013 CPU_CAP_SSE2 = 1<<1,
1014 CPU_CAP_SSE3 = 1<<2,
1015 CPU_CAP_SSE4_1 = 1<<3,
1016 CPU_CAP_NEON = 1<<4,
1019 void FillCPUCaps(ALuint capfilter);
1021 vector_al_string SearchDataFiles(const char *match, const char *subdir);
1023 /* Small hack to use a pointer-to-array type as a normal argument type.
1024 * Shouldn't be used directly. */
1025 typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
1028 #ifdef __cplusplus
1030 #endif
1032 #endif