Decode directly from B-Format to HRTF instead of a cube
[openal-soft.git] / OpenAL32 / Include / alMain.h
blob3d099ca283a247bae92dcfd890fe182269762036
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 typedef ALint64SOFT ALint64;
132 typedef ALuint64SOFT ALuint64;
134 #ifndef U64
135 #if defined(_MSC_VER)
136 #define U64(x) ((ALuint64)(x##ui64))
137 #elif SIZEOF_LONG == 8
138 #define U64(x) ((ALuint64)(x##ul))
139 #elif SIZEOF_LONG_LONG == 8
140 #define U64(x) ((ALuint64)(x##ull))
141 #endif
142 #endif
144 #ifndef UINT64_MAX
145 #define UINT64_MAX U64(18446744073709551615)
146 #endif
148 #ifndef UNUSED
149 #if defined(__cplusplus)
150 #define UNUSED(x)
151 #elif defined(__GNUC__)
152 #define UNUSED(x) UNUSED_##x __attribute__((unused))
153 #elif defined(__LCLINT__)
154 #define UNUSED(x) /*@unused@*/ x
155 #else
156 #define UNUSED(x) x
157 #endif
158 #endif
160 #ifdef __GNUC__
161 #define DECL_CONST __attribute__((const))
162 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
163 #else
164 #define DECL_CONST
165 #define DECL_FORMAT(x, y, z)
166 #endif
168 #if defined(__GNUC__) && defined(__i386__)
169 /* force_align_arg_pointer is required for proper function arguments aligning
170 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
171 * thread functions will be properly aligned on the stack, even though GCC may
172 * generate code with the assumption that it is. */
173 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
174 #else
175 #define FORCE_ALIGN
176 #endif
178 #ifdef HAVE_C99_VLA
179 #define DECL_VLA(T, _name, _size) T _name[(_size)]
180 #else
181 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
182 #endif
184 #ifndef PATH_MAX
185 #ifdef MAX_PATH
186 #define PATH_MAX MAX_PATH
187 #else
188 #define PATH_MAX 4096
189 #endif
190 #endif
193 static const union {
194 ALuint u;
195 ALubyte b[sizeof(ALuint)];
196 } EndianTest = { 1 };
197 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
199 #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
202 #define DERIVE_FROM_TYPE(t) t t##_parent
203 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
204 #ifdef __GNUC__
205 #define STATIC_UPCAST(to, from, obj) __extension__({ \
206 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
207 "Invalid upcast object from type"); \
208 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
210 #else
211 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
212 #endif
214 #define DECLARE_FORWARD(T1, T2, rettype, func) \
215 rettype T1##_##func(T1 *obj) \
216 { return T2##_##func(STATIC_CAST(T2, obj)); }
218 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
219 rettype T1##_##func(T1 *obj, argtype1 a) \
220 { return T2##_##func(STATIC_CAST(T2, obj), a); }
222 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
223 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
224 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
226 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
227 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
228 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
231 #define GET_VTABLE1(T1) (&(T1##_vtable))
232 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
234 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
235 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
237 #define DECLARE_THUNK(T1, T2, rettype, func) \
238 static rettype T1##_##T2##_##func(T2 *obj) \
239 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
241 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
242 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
243 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
245 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
246 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
247 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
249 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
250 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
251 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
253 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
254 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
255 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
257 #define DECLARE_DEFAULT_ALLOCATORS(T) \
258 static void* T##_New(size_t size) { return al_malloc(16, size); } \
259 static void T##_Delete(void *ptr) { al_free(ptr); }
261 /* Helper to extract an argument list for VCALL. Not used directly. */
262 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
264 /* Call a "virtual" method on an object, with arguments. */
265 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
266 /* Call a "virtual" method on an object, with no arguments. */
267 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
269 #define DELETE_OBJ(obj) do { \
270 if((obj) != NULL) \
272 V0((obj),Destruct)(); \
273 V0((obj),Delete)(); \
275 } while(0)
278 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
280 } while(0)
282 #define NEW_OBJ(_res, T) do { \
283 _res = T##_New(sizeof(T)); \
284 if(_res) \
286 memset(_res, 0, sizeof(T)); \
287 T##_Construct(_res, EXTRACT_NEW_ARGS
290 #ifdef __cplusplus
291 extern "C" {
292 #endif
294 struct Hrtf;
297 #define DEFAULT_OUTPUT_RATE (44100)
298 #define MIN_OUTPUT_RATE (8000)
301 /* Find the next power-of-2 for non-power-of-2 numbers. */
302 inline ALuint NextPowerOf2(ALuint value)
304 if(value > 0)
306 value--;
307 value |= value>>1;
308 value |= value>>2;
309 value |= value>>4;
310 value |= value>>8;
311 value |= value>>16;
313 return value+1;
316 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
317 * mode. */
318 inline ALint fastf2i(ALfloat f)
320 #ifdef HAVE_LRINTF
321 return lrintf(f);
322 #elif defined(_MSC_VER) && defined(_M_IX86)
323 ALint i;
324 __asm fld f
325 __asm fistp i
326 return i;
327 #else
328 return (ALint)f;
329 #endif
332 /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
333 * mode. */
334 inline ALuint fastf2u(ALfloat f)
335 { return fastf2i(f); }
338 enum DevProbe {
339 ALL_DEVICE_PROBE,
340 CAPTURE_DEVICE_PROBE
343 typedef struct {
344 ALCenum (*OpenPlayback)(ALCdevice*, const ALCchar*);
345 void (*ClosePlayback)(ALCdevice*);
346 ALCboolean (*ResetPlayback)(ALCdevice*);
347 ALCboolean (*StartPlayback)(ALCdevice*);
348 void (*StopPlayback)(ALCdevice*);
350 ALCenum (*OpenCapture)(ALCdevice*, const ALCchar*);
351 void (*CloseCapture)(ALCdevice*);
352 void (*StartCapture)(ALCdevice*);
353 void (*StopCapture)(ALCdevice*);
354 ALCenum (*CaptureSamples)(ALCdevice*, void*, ALCuint);
355 ALCuint (*AvailableSamples)(ALCdevice*);
356 } BackendFuncs;
358 ALCboolean alc_sndio_init(BackendFuncs *func_list);
359 void alc_sndio_deinit(void);
360 void alc_sndio_probe(enum DevProbe type);
361 ALCboolean alc_ca_init(BackendFuncs *func_list);
362 void alc_ca_deinit(void);
363 void alc_ca_probe(enum DevProbe type);
364 ALCboolean alc_opensl_init(BackendFuncs *func_list);
365 void alc_opensl_deinit(void);
366 void alc_opensl_probe(enum DevProbe type);
367 ALCboolean alc_qsa_init(BackendFuncs *func_list);
368 void alc_qsa_deinit(void);
369 void alc_qsa_probe(enum DevProbe type);
371 struct ALCbackend;
374 enum DistanceModel {
375 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
376 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
377 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
378 InverseDistance = AL_INVERSE_DISTANCE,
379 LinearDistance = AL_LINEAR_DISTANCE,
380 ExponentDistance = AL_EXPONENT_DISTANCE,
381 DisableDistance = AL_NONE,
383 DefaultDistanceModel = InverseDistanceClamped
386 enum Channel {
387 FrontLeft = 0,
388 FrontRight,
389 FrontCenter,
390 LFE,
391 BackLeft,
392 BackRight,
393 BackCenter,
394 SideLeft,
395 SideRight,
397 UpperFrontLeft,
398 UpperFrontRight,
399 UpperBackLeft,
400 UpperBackRight,
401 LowerFrontLeft,
402 LowerFrontRight,
403 LowerBackLeft,
404 LowerBackRight,
406 Aux0,
407 Aux1,
408 Aux2,
409 Aux3,
410 Aux4,
411 Aux5,
412 Aux6,
413 Aux7,
414 Aux8,
415 Aux9,
416 Aux10,
417 Aux11,
418 Aux12,
419 Aux13,
420 Aux14,
421 Aux15,
423 InvalidChannel
427 /* Device formats */
428 enum DevFmtType {
429 DevFmtByte = ALC_BYTE_SOFT,
430 DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
431 DevFmtShort = ALC_SHORT_SOFT,
432 DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
433 DevFmtInt = ALC_INT_SOFT,
434 DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
435 DevFmtFloat = ALC_FLOAT_SOFT,
437 DevFmtTypeDefault = DevFmtFloat
439 enum DevFmtChannels {
440 DevFmtMono = ALC_MONO_SOFT,
441 DevFmtStereo = ALC_STEREO_SOFT,
442 DevFmtQuad = ALC_QUAD_SOFT,
443 DevFmtX51 = ALC_5POINT1_SOFT,
444 DevFmtX61 = ALC_6POINT1_SOFT,
445 DevFmtX71 = ALC_7POINT1_SOFT,
447 /* Similar to 5.1, except using rear channels instead of sides */
448 DevFmtX51Rear = 0x80000000,
450 /* Ambisonic formats should be kept together */
451 DevFmtAmbi1,
452 DevFmtAmbi2,
453 DevFmtAmbi3,
455 DevFmtChannelsDefault = DevFmtStereo
457 #define MAX_OUTPUT_CHANNELS (16)
459 ALuint BytesFromDevFmt(enum DevFmtType type) DECL_CONST;
460 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans) DECL_CONST;
461 inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
463 return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
466 enum AmbiFormat {
467 AmbiFormat_FuMa, /* FuMa channel order and normalization */
468 AmbiFormat_ACN_SN3D, /* ACN channel order and SN3D normalization */
469 AmbiFormat_ACN_N3D, /* ACN channel order and N3D normalization */
471 AmbiFormat_Default = AmbiFormat_ACN_SN3D
475 extern const struct EffectList {
476 const char *name;
477 int type;
478 const char *ename;
479 ALenum val;
480 } EffectList[];
483 enum DeviceType {
484 Playback,
485 Capture,
486 Loopback
490 enum RenderMode {
491 NormalRender,
492 StereoPair,
493 HrtfRender
497 /* The maximum number of Ambisonics coefficients. For a given order (o), the
498 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
499 * second-order has 9, third-order has 16, and fourth-order has 25.
501 #define MAX_AMBI_ORDER 3
502 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
504 /* A bitmask of ambisonic channels with height information. If none of these
505 * channels are used/needed, there's no height (e.g. with most surround sound
506 * speaker setups). This only specifies up to 4th order, which is the highest
507 * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
508 * order). This is ACN ordering, with bit 0 being ACN 0, etc.
510 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
512 /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
513 * representation. This is 2 per each order above zero-order, plus 1 for zero-
514 * order. Or simply, o*2 + 1.
516 #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
519 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
520 typedef struct BFChannelConfig {
521 ALfloat Scale;
522 ALuint Index;
523 } BFChannelConfig;
525 typedef union AmbiConfig {
526 /* Ambisonic coefficients for mixing to the dry buffer. */
527 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
528 /* Coefficient channel mapping for mixing to the dry buffer. */
529 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
530 } AmbiConfig;
533 #define HRTF_HISTORY_BITS (6)
534 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
535 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
537 typedef struct HrtfState {
538 alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
539 alignas(16) ALfloat Values[HRIR_LENGTH][2];
540 } HrtfState;
542 typedef struct HrtfParams {
543 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
544 ALuint Delay[2];
545 } HrtfParams;
548 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
549 * more memory, while smaller values may need more iterations. The value needs
550 * to be a sensible size, however, as it constrains the max stepping value used
551 * for mixing, as well as the maximum number of samples per mixing iteration.
553 #define BUFFERSIZE (2048u)
555 struct ALCdevice_struct
557 RefCount ref;
559 ALCboolean Connected;
560 enum DeviceType Type;
562 ALuint Frequency;
563 ALuint UpdateSize;
564 ALuint NumUpdates;
565 enum DevFmtChannels FmtChans;
566 enum DevFmtType FmtType;
567 ALboolean IsHeadphones;
568 /* For DevFmtAmbi* output only, specifies the channel order and
569 * normalization.
571 enum AmbiFormat AmbiFmt;
573 al_string DeviceName;
575 ATOMIC(ALCenum) LastError;
577 // Maximum number of sources that can be created
578 ALuint SourcesMax;
579 // Maximum number of slots that can be created
580 ALuint AuxiliaryEffectSlotMax;
582 ALCuint NumMonoSources;
583 ALCuint NumStereoSources;
584 ALuint NumAuxSends;
586 // Map of Buffers for this device
587 UIntMap BufferMap;
589 // Map of Effects for this device
590 UIntMap EffectMap;
592 // Map of Filters for this device
593 UIntMap FilterMap;
595 /* HRTF filter tables */
596 vector_HrtfEntry Hrtf_List;
597 al_string Hrtf_Name;
598 const struct Hrtf *Hrtf;
599 ALCenum Hrtf_Status;
601 /* HRTF filter state for dry buffer content */
602 HrtfState Hrtf_State[4];
603 HrtfParams Hrtf_Params[4];
604 ALuint Hrtf_Offset;
605 ALuint Hrtf_IrSize;
607 /* UHJ encoder state */
608 struct Uhj2Encoder *Uhj_Encoder;
610 /* High quality Ambisonic decoder */
611 struct BFormatDec *AmbiDecoder;
613 /* Stereo-to-binaural filter */
614 struct bs2b *Bs2b;
616 /* First-order ambisonic upsampler for higher-order output */
617 struct AmbiUpsampler *AmbiUp;
619 /* Rendering mode. */
620 enum RenderMode Render_Mode;
622 // Device flags
623 ALuint Flags;
625 ALuint64 ClockBase;
626 ALuint SamplesDone;
628 /* Temp storage used for each source when mixing. */
629 alignas(16) ALfloat SourceData[BUFFERSIZE];
630 alignas(16) ALfloat ResampledData[BUFFERSIZE];
631 alignas(16) ALfloat FilteredData[BUFFERSIZE];
633 /* The "dry" path corresponds to the main output. */
634 struct {
635 AmbiConfig Ambi;
636 /* Number of coefficients in each Ambi.Coeffs to mix together (4 for
637 * first-order, 9 for second-order, etc). If the count is 0, Ambi.Map
638 * is used instead to map each output to a coefficient index.
640 ALuint CoeffCount;
642 ALfloat (*Buffer)[BUFFERSIZE];
643 ALuint NumChannels;
644 } Dry;
646 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
647 struct {
648 AmbiConfig Ambi;
649 /* Will only be 4 or 0. */
650 ALuint CoeffCount;
652 ALfloat (*Buffer)[BUFFERSIZE];
653 ALuint NumChannels;
654 } FOAOut;
656 /* "Real" output, which will be written to the device buffer. May alias the
657 * dry buffer.
659 struct {
660 enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
662 ALfloat (*Buffer)[BUFFERSIZE];
663 ALuint NumChannels;
664 } RealOut;
666 /* Running count of the mixer invocations, in 31.1 fixed point. This
667 * actually increments *twice* when mixing, first at the start and then at
668 * the end, so the bottom bit indicates if the device is currently mixing
669 * and the upper bits indicates how many mixes have been done.
671 RefCount MixCount;
673 /* Default effect slot */
674 struct ALeffectslot *DefaultSlot;
676 // Contexts created on this device
677 ATOMIC(ALCcontext*) ContextList;
679 almtx_t BackendLock;
680 struct ALCbackend *Backend;
682 void *ExtraData; // For the backend's use
684 ALCdevice *volatile next;
686 /* Memory space used by the default slot (Playback devices only) */
687 alignas(16) ALCbyte _slot_mem[];
690 // Frequency was requested by the app or config file
691 #define DEVICE_FREQUENCY_REQUEST (1<<1)
692 // Channel configuration was requested by the config file
693 #define DEVICE_CHANNELS_REQUEST (1<<2)
694 // Sample type was requested by the config file
695 #define DEVICE_SAMPLE_TYPE_REQUEST (1<<3)
697 // Specifies if the DSP is paused at user request
698 #define DEVICE_PAUSED (1<<30)
700 // Specifies if the device is currently running
701 #define DEVICE_RUNNING (1<<31)
704 /* Nanosecond resolution for the device clock time. */
705 #define DEVICE_CLOCK_RES U64(1000000000)
708 /* Must be less than 15 characters (16 including terminating null) for
709 * compatibility with pthread_setname_np limitations. */
710 #define MIXER_THREAD_NAME "alsoft-mixer"
712 #define RECORD_THREAD_NAME "alsoft-record"
715 struct ALCcontext_struct {
716 RefCount ref;
718 struct ALlistener *Listener;
720 UIntMap SourceMap;
721 UIntMap EffectSlotMap;
723 ATOMIC(ALenum) LastError;
725 volatile enum DistanceModel DistanceModel;
726 volatile ALboolean SourceDistanceModel;
728 volatile ALfloat DopplerFactor;
729 volatile ALfloat DopplerVelocity;
730 volatile ALfloat SpeedOfSound;
731 ATOMIC(ALenum) DeferUpdates;
733 RWLock PropLock;
735 /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
736 * indicates if updates are currently happening).
738 RefCount UpdateCount;
739 ATOMIC(ALenum) HoldUpdates;
741 struct ALvoice *Voices;
742 ALsizei VoiceCount;
743 ALsizei MaxVoices;
745 ATOMIC(struct ALeffectslot*) ActiveAuxSlotList;
747 ALCdevice *Device;
748 const ALCchar *ExtensionList;
750 ALCcontext *volatile next;
752 /* Memory space used by the listener */
753 alignas(16) ALCbyte _listener_mem[];
756 ALCcontext *GetContextRef(void);
758 void ALCcontext_IncRef(ALCcontext *context);
759 void ALCcontext_DecRef(ALCcontext *context);
761 void AppendAllDevicesList(const ALCchar *name);
762 void AppendCaptureDeviceList(const ALCchar *name);
764 void ALCdevice_Lock(ALCdevice *device);
765 void ALCdevice_Unlock(ALCdevice *device);
767 void ALCcontext_DeferUpdates(ALCcontext *context);
768 void ALCcontext_ProcessUpdates(ALCcontext *context);
770 inline void LockContext(ALCcontext *context)
771 { ALCdevice_Lock(context->Device); }
773 inline void UnlockContext(ALCcontext *context)
774 { ALCdevice_Unlock(context->Device); }
777 typedef struct {
778 #ifdef HAVE_FENV_H
779 DERIVE_FROM_TYPE(fenv_t);
780 #else
781 int state;
782 #endif
783 #ifdef HAVE_SSE
784 int sse_state;
785 #endif
786 } FPUCtl;
787 void SetMixerFPUMode(FPUCtl *ctl);
788 void RestoreFPUMode(const FPUCtl *ctl);
791 typedef struct ll_ringbuffer ll_ringbuffer_t;
792 typedef struct ll_ringbuffer_data {
793 char *buf;
794 size_t len;
795 } ll_ringbuffer_data_t;
796 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
797 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
798 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
799 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
800 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
801 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
802 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
803 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
804 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
805 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
806 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
807 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
808 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
810 void ReadALConfig(void);
811 void FreeALConfig(void);
812 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
813 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
814 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
815 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
816 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
817 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
818 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
819 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
821 void SetRTPriority(void);
823 void SetDefaultChannelOrder(ALCdevice *device);
824 void SetDefaultWFXChannelOrder(ALCdevice *device);
826 const ALCchar *DevFmtTypeString(enum DevFmtType type) DECL_CONST;
827 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans) DECL_CONST;
830 * GetChannelIdxByName
832 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
833 * doesn't exist.
835 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
837 ALint i;
838 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
840 if(names[i] == chan)
841 return i;
843 return -1;
845 #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
847 extern FILE *LogFile;
849 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
850 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
851 #else
852 void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
853 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
854 #endif
856 enum LogLevel {
857 NoLog,
858 LogError,
859 LogWarning,
860 LogTrace,
861 LogRef
863 extern enum LogLevel LogLevel;
865 #define TRACEREF(...) do { \
866 if(LogLevel >= LogRef) \
867 AL_PRINT("(--)", __VA_ARGS__); \
868 } while(0)
870 #define TRACE(...) do { \
871 if(LogLevel >= LogTrace) \
872 AL_PRINT("(II)", __VA_ARGS__); \
873 } while(0)
875 #define WARN(...) do { \
876 if(LogLevel >= LogWarning) \
877 AL_PRINT("(WW)", __VA_ARGS__); \
878 } while(0)
880 #define ERR(...) do { \
881 if(LogLevel >= LogError) \
882 AL_PRINT("(EE)", __VA_ARGS__); \
883 } while(0)
886 extern ALint RTPrioLevel;
889 extern ALuint CPUCapFlags;
890 enum {
891 CPU_CAP_SSE = 1<<0,
892 CPU_CAP_SSE2 = 1<<1,
893 CPU_CAP_SSE3 = 1<<2,
894 CPU_CAP_SSE4_1 = 1<<3,
895 CPU_CAP_NEON = 1<<4,
898 void FillCPUCaps(ALuint capfilter);
900 vector_al_string SearchDataFiles(const char *match, const char *subdir);
902 /* Small hack to use a pointer-to-array type as a normal argument type.
903 * Shouldn't be used directly. */
904 typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
907 #ifdef __cplusplus
909 #endif
911 #endif