Make the voices' Send[] array dynamically sized
[openal-soft.git] / OpenAL32 / Include / alMain.h
blob76862f38db0a604d09b58609f0224eeb5bbc5e1a
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_device_clock
33 #define ALC_SOFT_device_clock 1
34 typedef int64_t ALCint64SOFT;
35 typedef uint64_t ALCuint64SOFT;
36 #define ALC_DEVICE_CLOCK_SOFT 0x1600
37 #define ALC_DEVICE_LATENCY_SOFT 0x1601
38 #define ALC_DEVICE_CLOCK_LATENCY_SOFT 0x1602
39 typedef void (ALC_APIENTRY*LPALCGETINTEGER64VSOFT)(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
40 #ifdef AL_ALEXT_PROTOTYPES
41 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
42 #endif
43 #endif
45 #ifndef AL_SOFT_buffer_samples2
46 #define AL_SOFT_buffer_samples2 1
47 /* Channel configurations */
48 #define AL_MONO_SOFT 0x1500
49 #define AL_STEREO_SOFT 0x1501
50 #define AL_REAR_SOFT 0x1502
51 #define AL_QUAD_SOFT 0x1503
52 #define AL_5POINT1_SOFT 0x1504
53 #define AL_6POINT1_SOFT 0x1505
54 #define AL_7POINT1_SOFT 0x1506
55 #define AL_BFORMAT2D_SOFT 0x1507
56 #define AL_BFORMAT3D_SOFT 0x1508
58 /* Sample types */
59 #define AL_BYTE_SOFT 0x1400
60 #define AL_UNSIGNED_BYTE_SOFT 0x1401
61 #define AL_SHORT_SOFT 0x1402
62 #define AL_UNSIGNED_SHORT_SOFT 0x1403
63 #define AL_INT_SOFT 0x1404
64 #define AL_UNSIGNED_INT_SOFT 0x1405
65 #define AL_FLOAT_SOFT 0x1406
66 #define AL_DOUBLE_SOFT 0x1407
67 #define AL_BYTE3_SOFT 0x1408
68 #define AL_UNSIGNED_BYTE3_SOFT 0x1409
69 #define AL_MULAW_SOFT 0x140A
71 /* Storage formats */
72 #define AL_MONO8_SOFT 0x1100
73 #define AL_MONO16_SOFT 0x1101
74 #define AL_MONO32F_SOFT 0x10010
75 #define AL_STEREO8_SOFT 0x1102
76 #define AL_STEREO16_SOFT 0x1103
77 #define AL_STEREO32F_SOFT 0x10011
78 #define AL_QUAD8_SOFT 0x1204
79 #define AL_QUAD16_SOFT 0x1205
80 #define AL_QUAD32F_SOFT 0x1206
81 #define AL_REAR8_SOFT 0x1207
82 #define AL_REAR16_SOFT 0x1208
83 #define AL_REAR32F_SOFT 0x1209
84 #define AL_5POINT1_8_SOFT 0x120A
85 #define AL_5POINT1_16_SOFT 0x120B
86 #define AL_5POINT1_32F_SOFT 0x120C
87 #define AL_6POINT1_8_SOFT 0x120D
88 #define AL_6POINT1_16_SOFT 0x120E
89 #define AL_6POINT1_32F_SOFT 0x120F
90 #define AL_7POINT1_8_SOFT 0x1210
91 #define AL_7POINT1_16_SOFT 0x1211
92 #define AL_7POINT1_32F_SOFT 0x1212
93 #define AL_BFORMAT2D_8_SOFT 0x20021
94 #define AL_BFORMAT2D_16_SOFT 0x20022
95 #define AL_BFORMAT2D_32F_SOFT 0x20023
96 #define AL_BFORMAT3D_8_SOFT 0x20031
97 #define AL_BFORMAT3D_16_SOFT 0x20032
98 #define AL_BFORMAT3D_32F_SOFT 0x20033
100 /* Buffer attributes */
101 #define AL_INTERNAL_FORMAT_SOFT 0x2008
102 #define AL_BYTE_LENGTH_SOFT 0x2009
103 #define AL_SAMPLE_LENGTH_SOFT 0x200A
104 #define AL_SEC_LENGTH_SOFT 0x200B
106 #if 0
107 typedef void (AL_APIENTRY*LPALBUFFERSAMPLESSOFT)(ALuint,ALuint,ALenum,ALsizei,ALenum,ALenum,const ALvoid*);
108 typedef void (AL_APIENTRY*LPALGETBUFFERSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,ALvoid*);
109 typedef ALboolean (AL_APIENTRY*LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum);
110 #ifdef AL_ALEXT_PROTOTYPES
111 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer, ALuint samplerate, ALenum internalformat, ALsizei samples, ALenum channels, ALenum type, const ALvoid *data);
112 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer, ALsizei offset, ALsizei samples, ALenum channels, ALenum type, ALvoid *data);
113 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format);
114 #endif
115 #endif
116 #endif
119 #if defined(_WIN64)
120 #define SZFMT "%I64u"
121 #elif defined(_WIN32)
122 #define SZFMT "%u"
123 #else
124 #define SZFMT "%zu"
125 #endif
128 #ifdef __GNUC__
129 /* Because of a long-standing deficiency in C, you're not allowed to implicitly
130 * cast a pointer-to-type-array to a pointer-to-const-type-array. For example,
132 * int (*ptr)[10];
133 * const int (*cptr)[10] = ptr;
135 * is not allowed and most compilers will generate noisy warnings about
136 * incompatible types, even though it just makes the array elements const.
137 * Clang will allow it if you make the array type a typedef, like this:
139 * typedef int int10[10];
140 * int10 *ptr;
141 * const int10 *cptr = ptr;
143 * however GCC does not and still issues the incompatible type warning. The
144 * "proper" way to fix it is to add an explicit cast for the constified type,
145 * but that removes the vast majority of otherwise useful type-checking you'd
146 * get, and runs the risk of improper casts if types are later changed. Leaving
147 * it non-const can also be an issue if you use it as a function parameter, and
148 * happen to have a const type as input (and also reduce the capabilities of
149 * the compiler to better optimize the function).
151 * So to work around the problem, we use a macro. The macro first assigns the
152 * incoming variable to the specified non-const type to ensure it's the correct
153 * type, then casts the variable as the desired constified type. Very ugly, but
154 * I'd rather not have hundreds of lines of warnings because I want to tell the
155 * compiler that some array(s) can't be changed by the code, or have lots of
156 * error-prone casts.
158 #define SAFE_CONST(T, var) __extension__({ \
159 T _tmp = (var); \
160 (const T)_tmp; \
162 #else
163 /* Non-GNU-compatible compilers have to use a straight cast with no extra
164 * checks, due to the lack of multi-statement expressions.
166 #define SAFE_CONST(T, var) ((const T)(var))
167 #endif
170 typedef ALint64SOFT ALint64;
171 typedef ALuint64SOFT ALuint64;
173 #ifndef U64
174 #if defined(_MSC_VER)
175 #define U64(x) ((ALuint64)(x##ui64))
176 #elif SIZEOF_LONG == 8
177 #define U64(x) ((ALuint64)(x##ul))
178 #elif SIZEOF_LONG_LONG == 8
179 #define U64(x) ((ALuint64)(x##ull))
180 #endif
181 #endif
183 #ifndef UINT64_MAX
184 #define UINT64_MAX U64(18446744073709551615)
185 #endif
187 #ifndef UNUSED
188 #if defined(__cplusplus)
189 #define UNUSED(x)
190 #elif defined(__GNUC__)
191 #define UNUSED(x) UNUSED_##x __attribute__((unused))
192 #elif defined(__LCLINT__)
193 #define UNUSED(x) /*@unused@*/ x
194 #else
195 #define UNUSED(x) x
196 #endif
197 #endif
199 #ifdef __GNUC__
200 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
201 #else
202 #define DECL_FORMAT(x, y, z)
203 #endif
205 #if defined(__GNUC__) && defined(__i386__)
206 /* force_align_arg_pointer is required for proper function arguments aligning
207 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
208 * thread functions will be properly aligned on the stack, even though GCC may
209 * generate code with the assumption that it is. */
210 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
211 #else
212 #define FORCE_ALIGN
213 #endif
215 #ifdef HAVE_C99_VLA
216 #define DECL_VLA(T, _name, _size) T _name[(_size)]
217 #else
218 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
219 #endif
221 #ifndef PATH_MAX
222 #ifdef MAX_PATH
223 #define PATH_MAX MAX_PATH
224 #else
225 #define PATH_MAX 4096
226 #endif
227 #endif
230 static const union {
231 ALuint u;
232 ALubyte b[sizeof(ALuint)];
233 } EndianTest = { 1 };
234 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
236 #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
239 #define DERIVE_FROM_TYPE(t) t t##_parent
240 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
241 #ifdef __GNUC__
242 #define STATIC_UPCAST(to, from, obj) __extension__({ \
243 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
244 "Invalid upcast object from type"); \
245 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
247 #else
248 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
249 #endif
251 #define DECLARE_FORWARD(T1, T2, rettype, func) \
252 rettype T1##_##func(T1 *obj) \
253 { return T2##_##func(STATIC_CAST(T2, obj)); }
255 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
256 rettype T1##_##func(T1 *obj, argtype1 a) \
257 { return T2##_##func(STATIC_CAST(T2, obj), a); }
259 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
260 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
261 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
263 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
264 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
265 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
268 #define GET_VTABLE1(T1) (&(T1##_vtable))
269 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
271 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
272 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
274 #define DECLARE_THUNK(T1, T2, rettype, func) \
275 static rettype T1##_##T2##_##func(T2 *obj) \
276 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
278 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
279 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
280 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
282 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
283 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
284 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
286 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
287 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
288 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
290 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
291 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
292 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
294 #define DECLARE_DEFAULT_ALLOCATORS(T) \
295 static void* T##_New(size_t size) { return al_malloc(16, size); } \
296 static void T##_Delete(void *ptr) { al_free(ptr); }
298 /* Helper to extract an argument list for VCALL. Not used directly. */
299 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
301 /* Call a "virtual" method on an object, with arguments. */
302 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
303 /* Call a "virtual" method on an object, with no arguments. */
304 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
306 #define DELETE_OBJ(obj) do { \
307 if((obj) != NULL) \
309 V0((obj),Destruct)(); \
310 V0((obj),Delete)(); \
312 } while(0)
315 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
317 } while(0)
319 #define NEW_OBJ(_res, T) do { \
320 _res = T##_New(sizeof(T)); \
321 if(_res) \
323 memset(_res, 0, sizeof(T)); \
324 T##_Construct(_res, EXTRACT_NEW_ARGS
325 #define NEW_OBJ0(_res, T) do { \
326 _res = T##_New(sizeof(T)); \
327 if(_res) \
329 memset(_res, 0, sizeof(T)); \
330 T##_Construct(_res EXTRACT_NEW_ARGS
333 #ifdef __cplusplus
334 extern "C" {
335 #endif
337 struct Hrtf;
340 #define DEFAULT_OUTPUT_RATE (44100)
341 #define MIN_OUTPUT_RATE (8000)
344 /* Find the next power-of-2 for non-power-of-2 numbers. */
345 inline ALuint NextPowerOf2(ALuint value)
347 if(value > 0)
349 value--;
350 value |= value>>1;
351 value |= value>>2;
352 value |= value>>4;
353 value |= value>>8;
354 value |= value>>16;
356 return value+1;
359 /** Round up a value to the next multiple. */
360 inline size_t RoundUp(size_t value, size_t r)
362 value += r-1;
363 return value - (value%r);
366 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
367 * mode. */
368 inline ALint fastf2i(ALfloat f)
370 #ifdef HAVE_LRINTF
371 return lrintf(f);
372 #elif defined(_MSC_VER) && defined(_M_IX86)
373 ALint i;
374 __asm fld f
375 __asm fistp i
376 return i;
377 #else
378 return (ALint)f;
379 #endif
382 /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
383 * mode. */
384 inline ALuint fastf2u(ALfloat f)
385 { return fastf2i(f); }
388 enum DevProbe {
389 ALL_DEVICE_PROBE,
390 CAPTURE_DEVICE_PROBE
393 typedef struct {
394 ALCenum (*OpenPlayback)(ALCdevice*, const ALCchar*);
395 void (*ClosePlayback)(ALCdevice*);
396 ALCboolean (*ResetPlayback)(ALCdevice*);
397 ALCboolean (*StartPlayback)(ALCdevice*);
398 void (*StopPlayback)(ALCdevice*);
400 ALCenum (*OpenCapture)(ALCdevice*, const ALCchar*);
401 void (*CloseCapture)(ALCdevice*);
402 void (*StartCapture)(ALCdevice*);
403 void (*StopCapture)(ALCdevice*);
404 ALCenum (*CaptureSamples)(ALCdevice*, void*, ALCuint);
405 ALCuint (*AvailableSamples)(ALCdevice*);
406 } BackendFuncs;
408 ALCboolean alc_ca_init(BackendFuncs *func_list);
409 void alc_ca_deinit(void);
410 void alc_ca_probe(enum DevProbe type);
411 ALCboolean alc_opensl_init(BackendFuncs *func_list);
412 void alc_opensl_deinit(void);
413 void alc_opensl_probe(enum DevProbe type);
414 ALCboolean alc_qsa_init(BackendFuncs *func_list);
415 void alc_qsa_deinit(void);
416 void alc_qsa_probe(enum DevProbe type);
418 struct ALCbackend;
421 enum DistanceModel {
422 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
423 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
424 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
425 InverseDistance = AL_INVERSE_DISTANCE,
426 LinearDistance = AL_LINEAR_DISTANCE,
427 ExponentDistance = AL_EXPONENT_DISTANCE,
428 DisableDistance = AL_NONE,
430 DefaultDistanceModel = InverseDistanceClamped
433 enum Channel {
434 FrontLeft = 0,
435 FrontRight,
436 FrontCenter,
437 LFE,
438 BackLeft,
439 BackRight,
440 BackCenter,
441 SideLeft,
442 SideRight,
444 UpperFrontLeft,
445 UpperFrontRight,
446 UpperBackLeft,
447 UpperBackRight,
448 LowerFrontLeft,
449 LowerFrontRight,
450 LowerBackLeft,
451 LowerBackRight,
453 Aux0,
454 Aux1,
455 Aux2,
456 Aux3,
457 Aux4,
458 Aux5,
459 Aux6,
460 Aux7,
461 Aux8,
462 Aux9,
463 Aux10,
464 Aux11,
465 Aux12,
466 Aux13,
467 Aux14,
468 Aux15,
470 InvalidChannel
474 /* Device formats */
475 enum DevFmtType {
476 DevFmtByte = ALC_BYTE_SOFT,
477 DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
478 DevFmtShort = ALC_SHORT_SOFT,
479 DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
480 DevFmtInt = ALC_INT_SOFT,
481 DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
482 DevFmtFloat = ALC_FLOAT_SOFT,
484 DevFmtTypeDefault = DevFmtFloat
486 enum DevFmtChannels {
487 DevFmtMono = ALC_MONO_SOFT,
488 DevFmtStereo = ALC_STEREO_SOFT,
489 DevFmtQuad = ALC_QUAD_SOFT,
490 DevFmtX51 = ALC_5POINT1_SOFT,
491 DevFmtX61 = ALC_6POINT1_SOFT,
492 DevFmtX71 = ALC_7POINT1_SOFT,
494 /* Similar to 5.1, except using rear channels instead of sides */
495 DevFmtX51Rear = 0x80000000,
497 /* Ambisonic formats should be kept together */
498 DevFmtAmbi1,
499 DevFmtAmbi2,
500 DevFmtAmbi3,
502 DevFmtChannelsDefault = DevFmtStereo
504 #define MAX_OUTPUT_CHANNELS (16)
506 ALsizei BytesFromDevFmt(enum DevFmtType type);
507 ALsizei ChannelsFromDevFmt(enum DevFmtChannels chans);
508 inline ALsizei FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
510 return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
513 enum AmbiFormat {
514 AmbiFormat_FuMa, /* FuMa channel order and normalization */
515 AmbiFormat_ACN_SN3D, /* ACN channel order and SN3D normalization */
516 AmbiFormat_ACN_N3D, /* ACN channel order and N3D normalization */
518 AmbiFormat_Default = AmbiFormat_ACN_SN3D
522 extern const struct EffectList {
523 const char *name;
524 int type;
525 const char *ename;
526 ALenum val;
527 } EffectList[];
530 enum DeviceType {
531 Playback,
532 Capture,
533 Loopback
537 enum RenderMode {
538 NormalRender,
539 StereoPair,
540 HrtfRender
544 /* The maximum number of Ambisonics coefficients. For a given order (o), the
545 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
546 * second-order has 9, third-order has 16, and fourth-order has 25.
548 #define MAX_AMBI_ORDER 3
549 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
551 /* A bitmask of ambisonic channels with height information. If none of these
552 * channels are used/needed, there's no height (e.g. with most surround sound
553 * speaker setups). This only specifies up to 4th order, which is the highest
554 * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
555 * order). This is ACN ordering, with bit 0 being ACN 0, etc.
557 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
559 /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
560 * representation. This is 2 per each order above zero-order, plus 1 for zero-
561 * order. Or simply, o*2 + 1.
563 #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
566 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
567 typedef struct BFChannelConfig {
568 ALfloat Scale;
569 ALsizei Index;
570 } BFChannelConfig;
572 typedef union AmbiConfig {
573 /* Ambisonic coefficients for mixing to the dry buffer. */
574 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
575 /* Coefficient channel mapping for mixing to the dry buffer. */
576 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
577 } AmbiConfig;
580 #define HRTF_HISTORY_BITS (6)
581 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
582 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
584 #define HRIR_BITS (7)
585 #define HRIR_LENGTH (1<<HRIR_BITS)
586 #define HRIR_MASK (HRIR_LENGTH-1)
588 typedef struct HrtfState {
589 alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
590 alignas(16) ALfloat Values[HRIR_LENGTH][2];
591 } HrtfState;
593 typedef struct HrtfParams {
594 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
595 ALsizei Delay[2];
596 } HrtfParams;
598 typedef struct HrtfEntry {
599 al_string name;
601 const struct Hrtf *hrtf;
602 } HrtfEntry;
603 TYPEDEF_VECTOR(HrtfEntry, vector_HrtfEntry)
606 /* Maximum delay in samples for speaker distance compensation. */
607 #define MAX_DELAY_LENGTH 128
609 typedef struct DistanceComp {
610 ALfloat Gain;
611 ALsizei Length; /* Valid range is [0...MAX_DELAY_LENGTH). */
612 alignas(16) ALfloat Buffer[MAX_DELAY_LENGTH];
613 } DistanceComp;
615 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
616 * more memory, while smaller values may need more iterations. The value needs
617 * to be a sensible size, however, as it constrains the max stepping value used
618 * for mixing, as well as the maximum number of samples per mixing iteration.
620 #define BUFFERSIZE 2048
622 struct ALCdevice_struct
624 RefCount ref;
626 ALCboolean Connected;
627 enum DeviceType Type;
629 ALuint Frequency;
630 ALuint UpdateSize;
631 ALuint NumUpdates;
632 enum DevFmtChannels FmtChans;
633 enum DevFmtType FmtType;
634 ALboolean IsHeadphones;
635 /* For DevFmtAmbi* output only, specifies the channel order and
636 * normalization.
638 enum AmbiFormat AmbiFmt;
640 al_string DeviceName;
642 ATOMIC(ALCenum) LastError;
644 // Maximum number of sources that can be created
645 ALuint SourcesMax;
646 // Maximum number of slots that can be created
647 ALuint AuxiliaryEffectSlotMax;
649 ALCuint NumMonoSources;
650 ALCuint NumStereoSources;
651 ALuint NumAuxSends;
653 // Map of Buffers for this device
654 UIntMap BufferMap;
656 // Map of Effects for this device
657 UIntMap EffectMap;
659 // Map of Filters for this device
660 UIntMap FilterMap;
662 /* HRTF filter tables */
663 struct {
664 vector_HrtfEntry List;
665 al_string Name;
666 ALCenum Status;
667 const struct Hrtf *Handle;
669 /* HRTF filter state for dry buffer content */
670 alignas(16) ALfloat Values[9][HRIR_LENGTH][2];
671 alignas(16) ALfloat Coeffs[9][HRIR_LENGTH][2];
672 ALsizei Offset;
673 ALsizei IrSize;
674 } Hrtf;
676 /* UHJ encoder state */
677 struct Uhj2Encoder *Uhj_Encoder;
679 /* High quality Ambisonic decoder */
680 struct BFormatDec *AmbiDecoder;
682 /* Stereo-to-binaural filter */
683 struct bs2b *Bs2b;
685 /* First-order ambisonic upsampler for higher-order output */
686 struct AmbiUpsampler *AmbiUp;
688 /* Rendering mode. */
689 enum RenderMode Render_Mode;
691 // Device flags
692 ALuint Flags;
694 ALuint64 ClockBase;
695 ALuint SamplesDone;
697 /* Temp storage used for each source when mixing. */
698 alignas(16) ALfloat SourceData[BUFFERSIZE];
699 alignas(16) ALfloat ResampledData[BUFFERSIZE];
700 alignas(16) ALfloat FilteredData[BUFFERSIZE];
702 /* The "dry" path corresponds to the main output. */
703 struct {
704 AmbiConfig Ambi;
705 /* Number of coefficients in each Ambi.Coeffs to mix together (4 for
706 * first-order, 9 for second-order, etc). If the count is 0, Ambi.Map
707 * is used instead to map each output to a coefficient index.
709 ALsizei CoeffCount;
711 ALfloat (*Buffer)[BUFFERSIZE];
712 ALsizei NumChannels;
713 } Dry;
715 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
716 struct {
717 AmbiConfig Ambi;
718 /* Will only be 4 or 0. */
719 ALsizei CoeffCount;
721 ALfloat (*Buffer)[BUFFERSIZE];
722 ALsizei NumChannels;
723 } FOAOut;
725 /* "Real" output, which will be written to the device buffer. May alias the
726 * dry buffer.
728 struct {
729 enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
731 ALfloat (*Buffer)[BUFFERSIZE];
732 ALsizei NumChannels;
733 } RealOut;
735 /* Delay buffers used to compensate for speaker distances. */
736 DistanceComp ChannelDelay[MAX_OUTPUT_CHANNELS];
738 /* Running count of the mixer invocations, in 31.1 fixed point. This
739 * actually increments *twice* when mixing, first at the start and then at
740 * the end, so the bottom bit indicates if the device is currently mixing
741 * and the upper bits indicates how many mixes have been done.
743 RefCount MixCount;
745 /* Default effect slot */
746 struct ALeffectslot *DefaultSlot;
748 // Contexts created on this device
749 ATOMIC(ALCcontext*) ContextList;
751 almtx_t BackendLock;
752 struct ALCbackend *Backend;
754 void *ExtraData; // For the backend's use
756 ALCdevice *volatile next;
758 /* Memory space used by the default slot (Playback devices only) */
759 alignas(16) ALCbyte _slot_mem[];
762 // Frequency was requested by the app or config file
763 #define DEVICE_FREQUENCY_REQUEST (1u<<1)
764 // Channel configuration was requested by the config file
765 #define DEVICE_CHANNELS_REQUEST (1u<<2)
766 // Sample type was requested by the config file
767 #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
769 // Specifies if the DSP is paused at user request
770 #define DEVICE_PAUSED (1u<<30)
772 // Specifies if the device is currently running
773 #define DEVICE_RUNNING (1u<<31)
776 /* Nanosecond resolution for the device clock time. */
777 #define DEVICE_CLOCK_RES U64(1000000000)
780 /* Must be less than 15 characters (16 including terminating null) for
781 * compatibility with pthread_setname_np limitations. */
782 #define MIXER_THREAD_NAME "alsoft-mixer"
784 #define RECORD_THREAD_NAME "alsoft-record"
787 struct ALCcontext_struct {
788 RefCount ref;
790 struct ALlistener *Listener;
792 UIntMap SourceMap;
793 UIntMap EffectSlotMap;
795 ATOMIC(ALenum) LastError;
797 enum DistanceModel DistanceModel;
798 ALboolean SourceDistanceModel;
800 ALfloat DopplerFactor;
801 ALfloat DopplerVelocity;
802 ALfloat SpeedOfSound;
803 ATOMIC(ALenum) DeferUpdates;
805 RWLock PropLock;
807 /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
808 * indicates if updates are currently happening).
810 RefCount UpdateCount;
811 ATOMIC(ALenum) HoldUpdates;
813 ALfloat GainBoost;
815 struct ALvoice **Voices;
816 ALsizei VoiceCount;
817 ALsizei MaxVoices;
819 ATOMIC(struct ALeffectslot*) ActiveAuxSlotList;
821 ALCdevice *Device;
822 const ALCchar *ExtensionList;
824 ALCcontext *volatile next;
826 /* Memory space used by the listener */
827 alignas(16) ALCbyte _listener_mem[];
830 ALCcontext *GetContextRef(void);
832 void ALCcontext_IncRef(ALCcontext *context);
833 void ALCcontext_DecRef(ALCcontext *context);
835 void AllocateVoices(ALCcontext *context, ALsizei num_voices, ALsizei old_sends);
837 void AppendAllDevicesList(const ALCchar *name);
838 void AppendCaptureDeviceList(const ALCchar *name);
840 void ALCdevice_Lock(ALCdevice *device);
841 void ALCdevice_Unlock(ALCdevice *device);
843 void ALCcontext_DeferUpdates(ALCcontext *context, ALenum type);
844 void ALCcontext_ProcessUpdates(ALCcontext *context);
846 enum {
847 DeferOff = AL_FALSE,
848 DeferAll,
849 DeferAllowPlay
853 typedef struct {
854 #ifdef HAVE_FENV_H
855 DERIVE_FROM_TYPE(fenv_t);
856 #else
857 int state;
858 #endif
859 #ifdef HAVE_SSE
860 int sse_state;
861 #endif
862 } FPUCtl;
863 void SetMixerFPUMode(FPUCtl *ctl);
864 void RestoreFPUMode(const FPUCtl *ctl);
867 typedef struct ll_ringbuffer ll_ringbuffer_t;
868 typedef struct ll_ringbuffer_data {
869 char *buf;
870 size_t len;
871 } ll_ringbuffer_data_t;
872 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
873 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
874 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
875 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
876 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
877 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
878 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
879 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
880 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
881 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
882 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
883 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
884 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
886 void ReadALConfig(void);
887 void FreeALConfig(void);
888 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
889 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
890 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
891 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
892 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
893 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
894 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
895 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
897 void SetRTPriority(void);
899 void SetDefaultChannelOrder(ALCdevice *device);
900 void SetDefaultWFXChannelOrder(ALCdevice *device);
902 const ALCchar *DevFmtTypeString(enum DevFmtType type);
903 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
906 * GetChannelIdxByName
908 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
909 * doesn't exist.
911 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
913 ALint i;
914 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
916 if(names[i] == chan)
917 return i;
919 return -1;
921 #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
923 extern FILE *LogFile;
925 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
926 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
927 #else
928 void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
929 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
930 #endif
932 #ifdef __ANDROID__
933 #include <android/log.h>
934 #define LOG_ANDROID(T, MSG, ...) __android_log_print(T, "openal", "AL lib: %s: "MSG, __FUNCTION__ , ## __VA_ARGS__)
935 #else
936 #define LOG_ANDROID(T, MSG, ...) ((void)0)
937 #endif
939 enum LogLevel {
940 NoLog,
941 LogError,
942 LogWarning,
943 LogTrace,
944 LogRef
946 extern enum LogLevel LogLevel;
948 #define TRACEREF(...) do { \
949 if(LogLevel >= LogRef) \
950 AL_PRINT("(--)", __VA_ARGS__); \
951 } while(0)
953 #define TRACE(...) do { \
954 if(LogLevel >= LogTrace) \
955 AL_PRINT("(II)", __VA_ARGS__); \
956 LOG_ANDROID(ANDROID_LOG_DEBUG, __VA_ARGS__); \
957 } while(0)
959 #define WARN(...) do { \
960 if(LogLevel >= LogWarning) \
961 AL_PRINT("(WW)", __VA_ARGS__); \
962 LOG_ANDROID(ANDROID_LOG_WARN, __VA_ARGS__); \
963 } while(0)
965 #define ERR(...) do { \
966 if(LogLevel >= LogError) \
967 AL_PRINT("(EE)", __VA_ARGS__); \
968 LOG_ANDROID(ANDROID_LOG_ERROR, __VA_ARGS__); \
969 } while(0)
972 extern ALint RTPrioLevel;
975 extern ALuint CPUCapFlags;
976 enum {
977 CPU_CAP_SSE = 1<<0,
978 CPU_CAP_SSE2 = 1<<1,
979 CPU_CAP_SSE3 = 1<<2,
980 CPU_CAP_SSE4_1 = 1<<3,
981 CPU_CAP_NEON = 1<<4,
984 void FillCPUCaps(ALuint capfilter);
986 vector_al_string SearchDataFiles(const char *match, const char *subdir);
988 /* Small hack to use a pointer-to-array type as a normal argument type.
989 * Shouldn't be used directly. */
990 typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
993 #ifdef __cplusplus
995 #endif
997 #endif