Use a macro to specify the ambisonic periphonic channel mask
[openal-soft.git] / OpenAL32 / Include / alMain.h
blob18aa4df3883ee5e6765eadc626cd4a329e2e8a4a
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 typedef void (ALC_APIENTRY*LPALCGETINTEGER64VSOFT)(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
50 #ifdef AL_ALEXT_PROTOTYPES
51 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
52 #endif
53 #endif
55 #ifndef AL_SOFT_buffer_samples2
56 #define AL_SOFT_buffer_samples2 1
57 /* Channel configurations */
58 #define AL_MONO_SOFT 0x1500
59 #define AL_STEREO_SOFT 0x1501
60 #define AL_REAR_SOFT 0x1502
61 #define AL_QUAD_SOFT 0x1503
62 #define AL_5POINT1_SOFT 0x1504
63 #define AL_6POINT1_SOFT 0x1505
64 #define AL_7POINT1_SOFT 0x1506
65 #define AL_BFORMAT2D_SOFT 0x1507
66 #define AL_BFORMAT3D_SOFT 0x1508
68 /* Sample types */
69 #define AL_BYTE_SOFT 0x1400
70 #define AL_UNSIGNED_BYTE_SOFT 0x1401
71 #define AL_SHORT_SOFT 0x1402
72 #define AL_UNSIGNED_SHORT_SOFT 0x1403
73 #define AL_INT_SOFT 0x1404
74 #define AL_UNSIGNED_INT_SOFT 0x1405
75 #define AL_FLOAT_SOFT 0x1406
76 #define AL_DOUBLE_SOFT 0x1407
77 #define AL_BYTE3_SOFT 0x1408
78 #define AL_UNSIGNED_BYTE3_SOFT 0x1409
79 #define AL_MULAW_SOFT 0x140A
81 /* Storage formats */
82 #define AL_MONO8_SOFT 0x1100
83 #define AL_MONO16_SOFT 0x1101
84 #define AL_MONO32F_SOFT 0x10010
85 #define AL_STEREO8_SOFT 0x1102
86 #define AL_STEREO16_SOFT 0x1103
87 #define AL_STEREO32F_SOFT 0x10011
88 #define AL_QUAD8_SOFT 0x1204
89 #define AL_QUAD16_SOFT 0x1205
90 #define AL_QUAD32F_SOFT 0x1206
91 #define AL_REAR8_SOFT 0x1207
92 #define AL_REAR16_SOFT 0x1208
93 #define AL_REAR32F_SOFT 0x1209
94 #define AL_5POINT1_8_SOFT 0x120A
95 #define AL_5POINT1_16_SOFT 0x120B
96 #define AL_5POINT1_32F_SOFT 0x120C
97 #define AL_6POINT1_8_SOFT 0x120D
98 #define AL_6POINT1_16_SOFT 0x120E
99 #define AL_6POINT1_32F_SOFT 0x120F
100 #define AL_7POINT1_8_SOFT 0x1210
101 #define AL_7POINT1_16_SOFT 0x1211
102 #define AL_7POINT1_32F_SOFT 0x1212
103 #define AL_BFORMAT2D_8_SOFT 0x20021
104 #define AL_BFORMAT2D_16_SOFT 0x20022
105 #define AL_BFORMAT2D_32F_SOFT 0x20023
106 #define AL_BFORMAT3D_8_SOFT 0x20031
107 #define AL_BFORMAT3D_16_SOFT 0x20032
108 #define AL_BFORMAT3D_32F_SOFT 0x20033
110 /* Buffer attributes */
111 #define AL_INTERNAL_FORMAT_SOFT 0x2008
112 #define AL_BYTE_LENGTH_SOFT 0x2009
113 #define AL_SAMPLE_LENGTH_SOFT 0x200A
114 #define AL_SEC_LENGTH_SOFT 0x200B
116 #if 0
117 typedef void (AL_APIENTRY*LPALBUFFERSAMPLESSOFT)(ALuint,ALuint,ALenum,ALsizei,ALenum,ALenum,const ALvoid*);
118 typedef void (AL_APIENTRY*LPALGETBUFFERSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,ALvoid*);
119 typedef ALboolean (AL_APIENTRY*LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum);
120 #ifdef AL_ALEXT_PROTOTYPES
121 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer, ALuint samplerate, ALenum internalformat, ALsizei samples, ALenum channels, ALenum type, const ALvoid *data);
122 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer, ALsizei offset, ALsizei samples, ALenum channels, ALenum type, ALvoid *data);
123 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format);
124 #endif
125 #endif
126 #endif
129 typedef ALint64SOFT ALint64;
130 typedef ALuint64SOFT ALuint64;
132 #ifndef U64
133 #if defined(_MSC_VER)
134 #define U64(x) ((ALuint64)(x##ui64))
135 #elif SIZEOF_LONG == 8
136 #define U64(x) ((ALuint64)(x##ul))
137 #elif SIZEOF_LONG_LONG == 8
138 #define U64(x) ((ALuint64)(x##ull))
139 #endif
140 #endif
142 #ifndef UINT64_MAX
143 #define UINT64_MAX U64(18446744073709551615)
144 #endif
146 #ifndef UNUSED
147 #if defined(__cplusplus)
148 #define UNUSED(x)
149 #elif defined(__GNUC__)
150 #define UNUSED(x) UNUSED_##x __attribute__((unused))
151 #elif defined(__LCLINT__)
152 #define UNUSED(x) /*@unused@*/ x
153 #else
154 #define UNUSED(x) x
155 #endif
156 #endif
158 #ifdef __GNUC__
159 #define DECL_CONST __attribute__((const))
160 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
161 #else
162 #define DECL_CONST
163 #define DECL_FORMAT(x, y, z)
164 #endif
166 #if defined(__GNUC__) && defined(__i386__)
167 /* force_align_arg_pointer is required for proper function arguments aligning
168 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
169 * thread functions will be properly aligned on the stack, even though GCC may
170 * generate code with the assumption that it is. */
171 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
172 #else
173 #define FORCE_ALIGN
174 #endif
176 #ifdef HAVE_C99_VLA
177 #define DECL_VLA(T, _name, _size) T _name[(_size)]
178 #else
179 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
180 #endif
182 #ifndef PATH_MAX
183 #ifdef MAX_PATH
184 #define PATH_MAX MAX_PATH
185 #else
186 #define PATH_MAX 4096
187 #endif
188 #endif
191 static const union {
192 ALuint u;
193 ALubyte b[sizeof(ALuint)];
194 } EndianTest = { 1 };
195 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
197 #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
200 #define DERIVE_FROM_TYPE(t) t t##_parent
201 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
202 #ifdef __GNUC__
203 #define STATIC_UPCAST(to, from, obj) __extension__({ \
204 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
205 "Invalid upcast object from type"); \
206 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
208 #else
209 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
210 #endif
212 #define DECLARE_FORWARD(T1, T2, rettype, func) \
213 rettype T1##_##func(T1 *obj) \
214 { return T2##_##func(STATIC_CAST(T2, obj)); }
216 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
217 rettype T1##_##func(T1 *obj, argtype1 a) \
218 { return T2##_##func(STATIC_CAST(T2, obj), a); }
220 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
221 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
222 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
224 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
225 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
226 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
229 #define GET_VTABLE1(T1) (&(T1##_vtable))
230 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
232 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
233 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
235 #define DECLARE_THUNK(T1, T2, rettype, func) \
236 static rettype T1##_##T2##_##func(T2 *obj) \
237 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
239 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
240 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
241 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
243 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
244 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
245 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
247 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
248 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
249 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
251 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
252 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
253 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
255 #define DECLARE_DEFAULT_ALLOCATORS(T) \
256 static void* T##_New(size_t size) { return al_malloc(16, size); } \
257 static void T##_Delete(void *ptr) { al_free(ptr); }
259 /* Helper to extract an argument list for VCALL. Not used directly. */
260 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
262 /* Call a "virtual" method on an object, with arguments. */
263 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
264 /* Call a "virtual" method on an object, with no arguments. */
265 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
267 #define DELETE_OBJ(obj) do { \
268 if((obj) != NULL) \
270 V0((obj),Destruct)(); \
271 V0((obj),Delete)(); \
273 } while(0)
276 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
278 } while(0)
280 #define NEW_OBJ(_res, T) do { \
281 _res = T##_New(sizeof(T)); \
282 if(_res) \
284 memset(_res, 0, sizeof(T)); \
285 T##_Construct(_res, EXTRACT_NEW_ARGS
288 #ifdef __cplusplus
289 extern "C" {
290 #endif
292 struct Hrtf;
295 #define DEFAULT_OUTPUT_RATE (44100)
296 #define MIN_OUTPUT_RATE (8000)
299 /* Find the next power-of-2 for non-power-of-2 numbers. */
300 inline ALuint NextPowerOf2(ALuint value)
302 if(value > 0)
304 value--;
305 value |= value>>1;
306 value |= value>>2;
307 value |= value>>4;
308 value |= value>>8;
309 value |= value>>16;
311 return value+1;
314 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
315 * mode. */
316 inline ALint fastf2i(ALfloat f)
318 #ifdef HAVE_LRINTF
319 return lrintf(f);
320 #elif defined(_MSC_VER) && defined(_M_IX86)
321 ALint i;
322 __asm fld f
323 __asm fistp i
324 return i;
325 #else
326 return (ALint)f;
327 #endif
330 /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
331 * mode. */
332 inline ALuint fastf2u(ALfloat f)
333 { return fastf2i(f); }
336 enum DevProbe {
337 ALL_DEVICE_PROBE,
338 CAPTURE_DEVICE_PROBE
341 typedef struct {
342 ALCenum (*OpenPlayback)(ALCdevice*, const ALCchar*);
343 void (*ClosePlayback)(ALCdevice*);
344 ALCboolean (*ResetPlayback)(ALCdevice*);
345 ALCboolean (*StartPlayback)(ALCdevice*);
346 void (*StopPlayback)(ALCdevice*);
348 ALCenum (*OpenCapture)(ALCdevice*, const ALCchar*);
349 void (*CloseCapture)(ALCdevice*);
350 void (*StartCapture)(ALCdevice*);
351 void (*StopCapture)(ALCdevice*);
352 ALCenum (*CaptureSamples)(ALCdevice*, void*, ALCuint);
353 ALCuint (*AvailableSamples)(ALCdevice*);
354 } BackendFuncs;
356 ALCboolean alc_sndio_init(BackendFuncs *func_list);
357 void alc_sndio_deinit(void);
358 void alc_sndio_probe(enum DevProbe type);
359 ALCboolean alc_ca_init(BackendFuncs *func_list);
360 void alc_ca_deinit(void);
361 void alc_ca_probe(enum DevProbe type);
362 ALCboolean alc_opensl_init(BackendFuncs *func_list);
363 void alc_opensl_deinit(void);
364 void alc_opensl_probe(enum DevProbe type);
365 ALCboolean alc_qsa_init(BackendFuncs *func_list);
366 void alc_qsa_deinit(void);
367 void alc_qsa_probe(enum DevProbe type);
369 struct ALCbackend;
372 enum DistanceModel {
373 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
374 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
375 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
376 InverseDistance = AL_INVERSE_DISTANCE,
377 LinearDistance = AL_LINEAR_DISTANCE,
378 ExponentDistance = AL_EXPONENT_DISTANCE,
379 DisableDistance = AL_NONE,
381 DefaultDistanceModel = InverseDistanceClamped
384 enum Channel {
385 FrontLeft = 0,
386 FrontRight,
387 FrontCenter,
388 LFE,
389 BackLeft,
390 BackRight,
391 BackCenter,
392 SideLeft,
393 SideRight,
395 UpperFrontLeft,
396 UpperFrontRight,
397 UpperBackLeft,
398 UpperBackRight,
399 LowerFrontLeft,
400 LowerFrontRight,
401 LowerBackLeft,
402 LowerBackRight,
404 Aux0,
405 Aux1,
406 Aux2,
407 Aux3,
408 Aux4,
409 Aux5,
410 Aux6,
411 Aux7,
412 Aux8,
413 Aux9,
414 Aux10,
415 Aux11,
416 Aux12,
417 Aux13,
418 Aux14,
419 Aux15,
421 InvalidChannel
425 /* Device formats */
426 enum DevFmtType {
427 DevFmtByte = ALC_BYTE_SOFT,
428 DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
429 DevFmtShort = ALC_SHORT_SOFT,
430 DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
431 DevFmtInt = ALC_INT_SOFT,
432 DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
433 DevFmtFloat = ALC_FLOAT_SOFT,
435 DevFmtTypeDefault = DevFmtFloat
437 enum DevFmtChannels {
438 DevFmtMono = ALC_MONO_SOFT,
439 DevFmtStereo = ALC_STEREO_SOFT,
440 DevFmtQuad = ALC_QUAD_SOFT,
441 DevFmtX51 = ALC_5POINT1_SOFT,
442 DevFmtX61 = ALC_6POINT1_SOFT,
443 DevFmtX71 = ALC_7POINT1_SOFT,
445 /* Similar to 5.1, except using rear channels instead of sides */
446 DevFmtX51Rear = 0x80000000,
448 DevFmtBFormat3D,
450 DevFmtChannelsDefault = DevFmtStereo
452 #define MAX_OUTPUT_CHANNELS (16)
454 ALuint BytesFromDevFmt(enum DevFmtType type) DECL_CONST;
455 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans) DECL_CONST;
456 inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
458 return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
462 extern const struct EffectList {
463 const char *name;
464 int type;
465 const char *ename;
466 ALenum val;
467 } EffectList[];
470 enum DeviceType {
471 Playback,
472 Capture,
473 Loopback
477 enum RenderMode {
478 NormalRender,
479 StereoPair,
480 HrtfRender
484 /* The maximum number of Ambisonics coefficients. For a given order (o), the
485 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
486 * second-order has 9, third-order has 16, and fourth-order has 25. */
487 #define MAX_AMBI_ORDER 3
488 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
490 /* A bitmask of ambisonic channels with height information. If none of these
491 * channels are used/needed, there's no height (e.g. with most surround sound
492 * speaker setups). This only specifies up to 4th order, which is the highest
493 * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
494 * order). This is ACN ordering, with bit 0 being ACN 0, etc.
496 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
499 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
500 typedef struct BFChannelConfig {
501 ALfloat Scale;
502 ALuint Index;
503 } BFChannelConfig;
506 #define HRTF_HISTORY_BITS (6)
507 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
508 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
510 typedef struct HrtfState {
511 alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
512 alignas(16) ALfloat Values[HRIR_LENGTH][2];
513 } HrtfState;
515 typedef struct HrtfParams {
516 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
517 ALuint Delay[2];
518 } HrtfParams;
521 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
522 * more memory, while smaller values may need more iterations. The value needs
523 * to be a sensible size, however, as it constrains the max stepping value used
524 * for mixing, as well as the maximum number of samples per mixing iteration.
526 #define BUFFERSIZE (2048u)
528 struct ALCdevice_struct
530 RefCount ref;
532 ALCboolean Connected;
533 enum DeviceType Type;
535 ALuint Frequency;
536 ALuint UpdateSize;
537 ALuint NumUpdates;
538 enum DevFmtChannels FmtChans;
539 enum DevFmtType FmtType;
540 ALboolean IsHeadphones;
542 al_string DeviceName;
544 ATOMIC(ALCenum) LastError;
546 // Maximum number of sources that can be created
547 ALuint MaxNoOfSources;
548 // Maximum number of slots that can be created
549 ALuint AuxiliaryEffectSlotMax;
551 ALCuint NumMonoSources;
552 ALCuint NumStereoSources;
553 ALuint NumAuxSends;
555 // Map of Buffers for this device
556 UIntMap BufferMap;
558 // Map of Effects for this device
559 UIntMap EffectMap;
561 // Map of Filters for this device
562 UIntMap FilterMap;
564 /* HRTF filter tables */
565 vector_HrtfEntry Hrtf_List;
566 al_string Hrtf_Name;
567 const struct Hrtf *Hrtf;
568 ALCenum Hrtf_Status;
570 /* HRTF filter state for dry buffer content */
571 HrtfState Hrtf_State[MAX_OUTPUT_CHANNELS];
572 HrtfParams Hrtf_Params[MAX_OUTPUT_CHANNELS];
573 ALuint Hrtf_Offset;
575 /* UHJ encoder state */
576 struct Uhj2Encoder *Uhj_Encoder;
578 /* High quality Ambisonic decoder */
579 struct BFormatDec *AmbiDecoder;
581 // Stereo-to-binaural filter
582 struct bs2b *Bs2b;
584 /* Rendering mode. */
585 enum RenderMode Render_Mode;
587 // Device flags
588 ALuint Flags;
590 ALuint64 ClockBase;
591 ALuint SamplesDone;
593 /* Temp storage used for each source when mixing. */
594 alignas(16) ALfloat SourceData[BUFFERSIZE];
595 alignas(16) ALfloat ResampledData[BUFFERSIZE];
596 alignas(16) ALfloat FilteredData[BUFFERSIZE];
598 /* The "dry" path corresponds to the main output. */
599 struct {
600 union {
601 /* Ambisonic coefficients for mixing to the dry buffer. */
602 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
603 /* Coefficient channel mapping for mixing to the dry buffer. */
604 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
605 } Ambi;
606 /* Number of coefficients in each ChannelConfig to mix together (4 for
607 * first-order, 9 for second-order, etc). If the count is 0, the
608 * BFChannelConfig is used instead to map each output to a coefficient
609 * index.
611 ALuint CoeffCount;
613 /* Dry buffer will be aliased by the virtual or real output. */
614 ALfloat (*Buffer)[BUFFERSIZE];
615 ALuint NumChannels;
616 } Dry;
618 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
619 struct {
620 union {
621 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
622 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
623 } Ambi;
624 /* Will only be 4 or 0. */
625 ALuint CoeffCount;
627 ALfloat (*Buffer)[BUFFERSIZE];
628 ALuint NumChannels;
629 } FOAOut;
631 /* Virtual output, to be post-processed to the real output. */
632 struct {
633 ALfloat (*Buffer)[BUFFERSIZE];
634 ALuint NumChannels;
635 } VirtOut;
636 /* "Real" output, which will be written to the device buffer. */
637 struct {
638 enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
640 ALfloat (*Buffer)[BUFFERSIZE];
641 ALuint NumChannels;
642 } RealOut;
644 /* Running count of the mixer invocations, in 31.1 fixed point. This
645 * actually increments *twice* when mixing, first at the start and then at
646 * the end, so the bottom bit indicates if the device is currently mixing
647 * and the upper bits indicates how many mixes have been done.
649 RefCount MixCount;
651 /* Default effect slot */
652 struct ALeffectslot *DefaultSlot;
654 // Contexts created on this device
655 ATOMIC(ALCcontext*) ContextList;
657 almtx_t BackendLock;
658 struct ALCbackend *Backend;
660 void *ExtraData; // For the backend's use
662 ALCdevice *volatile next;
664 /* Memory space used by the default slot (Playback devices only) */
665 alignas(16) ALCbyte _slot_mem[];
668 // Frequency was requested by the app or config file
669 #define DEVICE_FREQUENCY_REQUEST (1<<1)
670 // Channel configuration was requested by the config file
671 #define DEVICE_CHANNELS_REQUEST (1<<2)
672 // Sample type was requested by the config file
673 #define DEVICE_SAMPLE_TYPE_REQUEST (1<<3)
675 // Specifies if the DSP is paused at user request
676 #define DEVICE_PAUSED (1<<30)
678 // Specifies if the device is currently running
679 #define DEVICE_RUNNING (1<<31)
682 /* Nanosecond resolution for the device clock time. */
683 #define DEVICE_CLOCK_RES U64(1000000000)
686 /* Must be less than 15 characters (16 including terminating null) for
687 * compatibility with pthread_setname_np limitations. */
688 #define MIXER_THREAD_NAME "alsoft-mixer"
690 #define RECORD_THREAD_NAME "alsoft-record"
693 struct ALCcontext_struct {
694 RefCount ref;
696 struct ALlistener *Listener;
698 UIntMap SourceMap;
699 UIntMap EffectSlotMap;
701 ATOMIC(ALenum) LastError;
703 volatile enum DistanceModel DistanceModel;
704 volatile ALboolean SourceDistanceModel;
706 volatile ALfloat DopplerFactor;
707 volatile ALfloat DopplerVelocity;
708 volatile ALfloat SpeedOfSound;
709 ATOMIC(ALenum) DeferUpdates;
711 RWLock PropLock;
713 /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
714 * indicates if updates are currently happening).
716 RefCount UpdateCount;
717 ATOMIC(ALenum) HoldUpdates;
719 struct ALvoice *Voices;
720 ALsizei VoiceCount;
721 ALsizei MaxVoices;
723 ATOMIC(struct ALeffectslot*) ActiveAuxSlotList;
725 ALCdevice *Device;
726 const ALCchar *ExtensionList;
728 ALCcontext *volatile next;
730 /* Memory space used by the listener */
731 alignas(16) ALCbyte _listener_mem[];
734 ALCcontext *GetContextRef(void);
736 void ALCcontext_IncRef(ALCcontext *context);
737 void ALCcontext_DecRef(ALCcontext *context);
739 void AppendAllDevicesList(const ALCchar *name);
740 void AppendCaptureDeviceList(const ALCchar *name);
742 void ALCdevice_Lock(ALCdevice *device);
743 void ALCdevice_Unlock(ALCdevice *device);
745 void ALCcontext_DeferUpdates(ALCcontext *context);
746 void ALCcontext_ProcessUpdates(ALCcontext *context);
748 inline void LockContext(ALCcontext *context)
749 { ALCdevice_Lock(context->Device); }
751 inline void UnlockContext(ALCcontext *context)
752 { ALCdevice_Unlock(context->Device); }
755 typedef struct {
756 #ifdef HAVE_FENV_H
757 DERIVE_FROM_TYPE(fenv_t);
758 #else
759 int state;
760 #endif
761 #ifdef HAVE_SSE
762 int sse_state;
763 #endif
764 } FPUCtl;
765 void SetMixerFPUMode(FPUCtl *ctl);
766 void RestoreFPUMode(const FPUCtl *ctl);
769 typedef struct ll_ringbuffer ll_ringbuffer_t;
770 typedef struct ll_ringbuffer_data {
771 char *buf;
772 size_t len;
773 } ll_ringbuffer_data_t;
774 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
775 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
776 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
777 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
778 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
779 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
780 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
781 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
782 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
783 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
784 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
785 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
786 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
788 void ReadALConfig(void);
789 void FreeALConfig(void);
790 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
791 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
792 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
793 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
794 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
795 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
796 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
797 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
799 void SetRTPriority(void);
801 void SetDefaultChannelOrder(ALCdevice *device);
802 void SetDefaultWFXChannelOrder(ALCdevice *device);
804 const ALCchar *DevFmtTypeString(enum DevFmtType type) DECL_CONST;
805 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans) DECL_CONST;
808 * GetChannelIdxByName
810 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
811 * doesn't exist.
813 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
815 ALint i;
816 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
818 if(names[i] == chan)
819 return i;
821 return -1;
823 #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
825 extern FILE *LogFile;
827 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
828 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
829 #else
830 void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
831 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
832 #endif
834 enum LogLevel {
835 NoLog,
836 LogError,
837 LogWarning,
838 LogTrace,
839 LogRef
841 extern enum LogLevel LogLevel;
843 #define TRACEREF(...) do { \
844 if(LogLevel >= LogRef) \
845 AL_PRINT("(--)", __VA_ARGS__); \
846 } while(0)
848 #define TRACE(...) do { \
849 if(LogLevel >= LogTrace) \
850 AL_PRINT("(II)", __VA_ARGS__); \
851 } while(0)
853 #define WARN(...) do { \
854 if(LogLevel >= LogWarning) \
855 AL_PRINT("(WW)", __VA_ARGS__); \
856 } while(0)
858 #define ERR(...) do { \
859 if(LogLevel >= LogError) \
860 AL_PRINT("(EE)", __VA_ARGS__); \
861 } while(0)
864 extern ALint RTPrioLevel;
867 extern ALuint CPUCapFlags;
868 enum {
869 CPU_CAP_SSE = 1<<0,
870 CPU_CAP_SSE2 = 1<<1,
871 CPU_CAP_SSE3 = 1<<2,
872 CPU_CAP_SSE4_1 = 1<<3,
873 CPU_CAP_NEON = 1<<4,
876 void FillCPUCaps(ALuint capfilter);
878 vector_al_string SearchDataFiles(const char *match, const char *subdir);
880 /* Small hack to use a pointer-to-array type as a normal argument type.
881 * Shouldn't be used directly. */
882 typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
885 #ifdef __cplusplus
887 #endif
889 #endif