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