Add a volume-adjust config option to adjust the source output volume
[openal-soft.git] / OpenAL32 / Include / alMain.h
blob6320bc98ef834499d103b69e2617269d4c29fab7
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
130 #ifndef AL_SOFT_gain_clamp_ex
131 #define AL_SOFT_gain_clamp_ex 1
132 #define AL_GAIN_LIMIT_SOFT 0x200E
133 #endif
136 typedef ALint64SOFT ALint64;
137 typedef ALuint64SOFT ALuint64;
139 #ifndef U64
140 #if defined(_MSC_VER)
141 #define U64(x) ((ALuint64)(x##ui64))
142 #elif SIZEOF_LONG == 8
143 #define U64(x) ((ALuint64)(x##ul))
144 #elif SIZEOF_LONG_LONG == 8
145 #define U64(x) ((ALuint64)(x##ull))
146 #endif
147 #endif
149 #ifndef UINT64_MAX
150 #define UINT64_MAX U64(18446744073709551615)
151 #endif
153 #ifndef UNUSED
154 #if defined(__cplusplus)
155 #define UNUSED(x)
156 #elif defined(__GNUC__)
157 #define UNUSED(x) UNUSED_##x __attribute__((unused))
158 #elif defined(__LCLINT__)
159 #define UNUSED(x) /*@unused@*/ x
160 #else
161 #define UNUSED(x) x
162 #endif
163 #endif
165 #ifdef __GNUC__
166 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
167 #else
168 #define DECL_FORMAT(x, y, z)
169 #endif
171 #if defined(__GNUC__) && defined(__i386__)
172 /* force_align_arg_pointer is required for proper function arguments aligning
173 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
174 * thread functions will be properly aligned on the stack, even though GCC may
175 * generate code with the assumption that it is. */
176 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
177 #else
178 #define FORCE_ALIGN
179 #endif
181 #ifdef HAVE_C99_VLA
182 #define DECL_VLA(T, _name, _size) T _name[(_size)]
183 #else
184 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
185 #endif
187 #ifndef PATH_MAX
188 #ifdef MAX_PATH
189 #define PATH_MAX MAX_PATH
190 #else
191 #define PATH_MAX 4096
192 #endif
193 #endif
196 static const union {
197 ALuint u;
198 ALubyte b[sizeof(ALuint)];
199 } EndianTest = { 1 };
200 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
202 #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
205 #define DERIVE_FROM_TYPE(t) t t##_parent
206 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
207 #ifdef __GNUC__
208 #define STATIC_UPCAST(to, from, obj) __extension__({ \
209 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
210 "Invalid upcast object from type"); \
211 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
213 #else
214 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
215 #endif
217 #define DECLARE_FORWARD(T1, T2, rettype, func) \
218 rettype T1##_##func(T1 *obj) \
219 { return T2##_##func(STATIC_CAST(T2, obj)); }
221 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
222 rettype T1##_##func(T1 *obj, argtype1 a) \
223 { return T2##_##func(STATIC_CAST(T2, obj), a); }
225 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
226 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
227 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
229 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
230 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
231 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
234 #define GET_VTABLE1(T1) (&(T1##_vtable))
235 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
237 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
238 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
240 #define DECLARE_THUNK(T1, T2, rettype, func) \
241 static rettype T1##_##T2##_##func(T2 *obj) \
242 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
244 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
245 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
246 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
248 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
249 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
250 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
252 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
253 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
254 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
256 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
257 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
258 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
260 #define DECLARE_DEFAULT_ALLOCATORS(T) \
261 static void* T##_New(size_t size) { return al_malloc(16, size); } \
262 static void T##_Delete(void *ptr) { al_free(ptr); }
264 /* Helper to extract an argument list for VCALL. Not used directly. */
265 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
267 /* Call a "virtual" method on an object, with arguments. */
268 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
269 /* Call a "virtual" method on an object, with no arguments. */
270 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
272 #define DELETE_OBJ(obj) do { \
273 if((obj) != NULL) \
275 V0((obj),Destruct)(); \
276 V0((obj),Delete)(); \
278 } while(0)
281 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
283 } while(0)
285 #define NEW_OBJ(_res, T) do { \
286 _res = T##_New(sizeof(T)); \
287 if(_res) \
289 memset(_res, 0, sizeof(T)); \
290 T##_Construct(_res, EXTRACT_NEW_ARGS
291 #define NEW_OBJ0(_res, T) do { \
292 _res = T##_New(sizeof(T)); \
293 if(_res) \
295 memset(_res, 0, sizeof(T)); \
296 T##_Construct(_res EXTRACT_NEW_ARGS
299 #ifdef __cplusplus
300 extern "C" {
301 #endif
303 struct Hrtf;
306 #define DEFAULT_OUTPUT_RATE (44100)
307 #define MIN_OUTPUT_RATE (8000)
310 /* Find the next power-of-2 for non-power-of-2 numbers. */
311 inline ALuint NextPowerOf2(ALuint value)
313 if(value > 0)
315 value--;
316 value |= value>>1;
317 value |= value>>2;
318 value |= value>>4;
319 value |= value>>8;
320 value |= value>>16;
322 return value+1;
325 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
326 * mode. */
327 inline ALint fastf2i(ALfloat f)
329 #ifdef HAVE_LRINTF
330 return lrintf(f);
331 #elif defined(_MSC_VER) && defined(_M_IX86)
332 ALint i;
333 __asm fld f
334 __asm fistp i
335 return i;
336 #else
337 return (ALint)f;
338 #endif
341 /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
342 * mode. */
343 inline ALuint fastf2u(ALfloat f)
344 { return fastf2i(f); }
347 enum DevProbe {
348 ALL_DEVICE_PROBE,
349 CAPTURE_DEVICE_PROBE
352 typedef struct {
353 ALCenum (*OpenPlayback)(ALCdevice*, const ALCchar*);
354 void (*ClosePlayback)(ALCdevice*);
355 ALCboolean (*ResetPlayback)(ALCdevice*);
356 ALCboolean (*StartPlayback)(ALCdevice*);
357 void (*StopPlayback)(ALCdevice*);
359 ALCenum (*OpenCapture)(ALCdevice*, const ALCchar*);
360 void (*CloseCapture)(ALCdevice*);
361 void (*StartCapture)(ALCdevice*);
362 void (*StopCapture)(ALCdevice*);
363 ALCenum (*CaptureSamples)(ALCdevice*, void*, ALCuint);
364 ALCuint (*AvailableSamples)(ALCdevice*);
365 } BackendFuncs;
367 ALCboolean alc_sndio_init(BackendFuncs *func_list);
368 void alc_sndio_deinit(void);
369 void alc_sndio_probe(enum DevProbe type);
370 ALCboolean alc_ca_init(BackendFuncs *func_list);
371 void alc_ca_deinit(void);
372 void alc_ca_probe(enum DevProbe type);
373 ALCboolean alc_opensl_init(BackendFuncs *func_list);
374 void alc_opensl_deinit(void);
375 void alc_opensl_probe(enum DevProbe type);
376 ALCboolean alc_qsa_init(BackendFuncs *func_list);
377 void alc_qsa_deinit(void);
378 void alc_qsa_probe(enum DevProbe type);
380 struct ALCbackend;
383 enum DistanceModel {
384 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
385 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
386 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
387 InverseDistance = AL_INVERSE_DISTANCE,
388 LinearDistance = AL_LINEAR_DISTANCE,
389 ExponentDistance = AL_EXPONENT_DISTANCE,
390 DisableDistance = AL_NONE,
392 DefaultDistanceModel = InverseDistanceClamped
395 enum Channel {
396 FrontLeft = 0,
397 FrontRight,
398 FrontCenter,
399 LFE,
400 BackLeft,
401 BackRight,
402 BackCenter,
403 SideLeft,
404 SideRight,
406 UpperFrontLeft,
407 UpperFrontRight,
408 UpperBackLeft,
409 UpperBackRight,
410 LowerFrontLeft,
411 LowerFrontRight,
412 LowerBackLeft,
413 LowerBackRight,
415 Aux0,
416 Aux1,
417 Aux2,
418 Aux3,
419 Aux4,
420 Aux5,
421 Aux6,
422 Aux7,
423 Aux8,
424 Aux9,
425 Aux10,
426 Aux11,
427 Aux12,
428 Aux13,
429 Aux14,
430 Aux15,
432 InvalidChannel
436 /* Device formats */
437 enum DevFmtType {
438 DevFmtByte = ALC_BYTE_SOFT,
439 DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
440 DevFmtShort = ALC_SHORT_SOFT,
441 DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
442 DevFmtInt = ALC_INT_SOFT,
443 DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
444 DevFmtFloat = ALC_FLOAT_SOFT,
446 DevFmtTypeDefault = DevFmtFloat
448 enum DevFmtChannels {
449 DevFmtMono = ALC_MONO_SOFT,
450 DevFmtStereo = ALC_STEREO_SOFT,
451 DevFmtQuad = ALC_QUAD_SOFT,
452 DevFmtX51 = ALC_5POINT1_SOFT,
453 DevFmtX61 = ALC_6POINT1_SOFT,
454 DevFmtX71 = ALC_7POINT1_SOFT,
456 /* Similar to 5.1, except using rear channels instead of sides */
457 DevFmtX51Rear = 0x80000000,
459 /* Ambisonic formats should be kept together */
460 DevFmtAmbi1,
461 DevFmtAmbi2,
462 DevFmtAmbi3,
464 DevFmtChannelsDefault = DevFmtStereo
466 #define MAX_OUTPUT_CHANNELS (16)
468 ALuint BytesFromDevFmt(enum DevFmtType type);
469 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans);
470 inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
472 return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
475 enum AmbiFormat {
476 AmbiFormat_FuMa, /* FuMa channel order and normalization */
477 AmbiFormat_ACN_SN3D, /* ACN channel order and SN3D normalization */
478 AmbiFormat_ACN_N3D, /* ACN channel order and N3D normalization */
480 AmbiFormat_Default = AmbiFormat_ACN_SN3D
484 extern const struct EffectList {
485 const char *name;
486 int type;
487 const char *ename;
488 ALenum val;
489 } EffectList[];
492 enum DeviceType {
493 Playback,
494 Capture,
495 Loopback
499 enum RenderMode {
500 NormalRender,
501 StereoPair,
502 HrtfRender
506 /* The maximum number of Ambisonics coefficients. For a given order (o), the
507 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
508 * second-order has 9, third-order has 16, and fourth-order has 25.
510 #define MAX_AMBI_ORDER 3
511 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
513 /* A bitmask of ambisonic channels with height information. If none of these
514 * channels are used/needed, there's no height (e.g. with most surround sound
515 * speaker setups). This only specifies up to 4th order, which is the highest
516 * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
517 * order). This is ACN ordering, with bit 0 being ACN 0, etc.
519 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
521 /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
522 * representation. This is 2 per each order above zero-order, plus 1 for zero-
523 * order. Or simply, o*2 + 1.
525 #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
528 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
529 typedef struct BFChannelConfig {
530 ALfloat Scale;
531 ALuint Index;
532 } BFChannelConfig;
534 typedef union AmbiConfig {
535 /* Ambisonic coefficients for mixing to the dry buffer. */
536 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
537 /* Coefficient channel mapping for mixing to the dry buffer. */
538 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
539 } AmbiConfig;
542 #define HRTF_HISTORY_BITS (6)
543 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
544 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
546 typedef struct HrtfState {
547 alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
548 alignas(16) ALfloat Values[HRIR_LENGTH][2];
549 } HrtfState;
551 typedef struct HrtfParams {
552 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
553 ALuint Delay[2];
554 } HrtfParams;
557 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
558 * more memory, while smaller values may need more iterations. The value needs
559 * to be a sensible size, however, as it constrains the max stepping value used
560 * for mixing, as well as the maximum number of samples per mixing iteration.
562 #define BUFFERSIZE (2048u)
564 struct ALCdevice_struct
566 RefCount ref;
568 ALCboolean Connected;
569 enum DeviceType Type;
571 ALuint Frequency;
572 ALuint UpdateSize;
573 ALuint NumUpdates;
574 enum DevFmtChannels FmtChans;
575 enum DevFmtType FmtType;
576 ALboolean IsHeadphones;
577 /* For DevFmtAmbi* output only, specifies the channel order and
578 * normalization.
580 enum AmbiFormat AmbiFmt;
582 al_string DeviceName;
584 ATOMIC(ALCenum) LastError;
586 // Maximum number of sources that can be created
587 ALuint SourcesMax;
588 // Maximum number of slots that can be created
589 ALuint AuxiliaryEffectSlotMax;
591 ALCuint NumMonoSources;
592 ALCuint NumStereoSources;
593 ALuint NumAuxSends;
595 // Map of Buffers for this device
596 UIntMap BufferMap;
598 // Map of Effects for this device
599 UIntMap EffectMap;
601 // Map of Filters for this device
602 UIntMap FilterMap;
604 /* HRTF filter tables */
605 struct {
606 vector_HrtfEntry List;
607 al_string Name;
608 ALCenum Status;
609 const struct Hrtf *Handle;
611 /* HRTF filter state for dry buffer content */
612 alignas(16) ALfloat Values[4][HRIR_LENGTH][2];
613 alignas(16) ALfloat Coeffs[4][HRIR_LENGTH][2];
614 ALuint Offset;
615 ALuint IrSize;
616 } Hrtf;
618 /* UHJ encoder state */
619 struct Uhj2Encoder *Uhj_Encoder;
621 /* High quality Ambisonic decoder */
622 struct BFormatDec *AmbiDecoder;
624 /* Stereo-to-binaural filter */
625 struct bs2b *Bs2b;
627 /* First-order ambisonic upsampler for higher-order output */
628 struct AmbiUpsampler *AmbiUp;
630 /* Rendering mode. */
631 enum RenderMode Render_Mode;
633 // Device flags
634 ALuint Flags;
636 ALuint64 ClockBase;
637 ALuint SamplesDone;
639 /* Temp storage used for each source when mixing. */
640 alignas(16) ALfloat SourceData[BUFFERSIZE];
641 alignas(16) ALfloat ResampledData[BUFFERSIZE];
642 alignas(16) ALfloat FilteredData[BUFFERSIZE];
644 /* The "dry" path corresponds to the main output. */
645 struct {
646 AmbiConfig Ambi;
647 /* Number of coefficients in each Ambi.Coeffs to mix together (4 for
648 * first-order, 9 for second-order, etc). If the count is 0, Ambi.Map
649 * is used instead to map each output to a coefficient index.
651 ALuint CoeffCount;
653 ALfloat (*Buffer)[BUFFERSIZE];
654 ALuint NumChannels;
655 } Dry;
657 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
658 struct {
659 AmbiConfig Ambi;
660 /* Will only be 4 or 0. */
661 ALuint CoeffCount;
663 ALfloat (*Buffer)[BUFFERSIZE];
664 ALuint NumChannels;
665 } FOAOut;
667 /* "Real" output, which will be written to the device buffer. May alias the
668 * dry buffer.
670 struct {
671 enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
673 ALfloat (*Buffer)[BUFFERSIZE];
674 ALuint NumChannels;
675 } RealOut;
677 /* Running count of the mixer invocations, in 31.1 fixed point. This
678 * actually increments *twice* when mixing, first at the start and then at
679 * the end, so the bottom bit indicates if the device is currently mixing
680 * and the upper bits indicates how many mixes have been done.
682 RefCount MixCount;
684 /* Default effect slot */
685 struct ALeffectslot *DefaultSlot;
687 // Contexts created on this device
688 ATOMIC(ALCcontext*) ContextList;
690 almtx_t BackendLock;
691 struct ALCbackend *Backend;
693 void *ExtraData; // For the backend's use
695 ALCdevice *volatile next;
697 /* Memory space used by the default slot (Playback devices only) */
698 alignas(16) ALCbyte _slot_mem[];
701 // Frequency was requested by the app or config file
702 #define DEVICE_FREQUENCY_REQUEST (1u<<1)
703 // Channel configuration was requested by the config file
704 #define DEVICE_CHANNELS_REQUEST (1u<<2)
705 // Sample type was requested by the config file
706 #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
708 // Specifies if the DSP is paused at user request
709 #define DEVICE_PAUSED (1u<<30)
711 // Specifies if the device is currently running
712 #define DEVICE_RUNNING (1u<<31)
715 /* Nanosecond resolution for the device clock time. */
716 #define DEVICE_CLOCK_RES U64(1000000000)
719 /* Must be less than 15 characters (16 including terminating null) for
720 * compatibility with pthread_setname_np limitations. */
721 #define MIXER_THREAD_NAME "alsoft-mixer"
723 #define RECORD_THREAD_NAME "alsoft-record"
726 struct ALCcontext_struct {
727 RefCount ref;
729 struct ALlistener *Listener;
731 UIntMap SourceMap;
732 UIntMap EffectSlotMap;
734 ATOMIC(ALenum) LastError;
736 enum DistanceModel DistanceModel;
737 ALboolean SourceDistanceModel;
739 ALfloat DopplerFactor;
740 ALfloat DopplerVelocity;
741 ALfloat SpeedOfSound;
742 ATOMIC(ALenum) DeferUpdates;
744 RWLock PropLock;
746 /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
747 * indicates if updates are currently happening).
749 RefCount UpdateCount;
750 ATOMIC(ALenum) HoldUpdates;
752 ALfloat GainBoost;
754 struct ALvoice *Voices;
755 ALsizei VoiceCount;
756 ALsizei MaxVoices;
758 ATOMIC(struct ALeffectslot*) ActiveAuxSlotList;
760 ALCdevice *Device;
761 const ALCchar *ExtensionList;
763 ALCcontext *volatile next;
765 /* Memory space used by the listener */
766 alignas(16) ALCbyte _listener_mem[];
769 ALCcontext *GetContextRef(void);
771 void ALCcontext_IncRef(ALCcontext *context);
772 void ALCcontext_DecRef(ALCcontext *context);
774 void AppendAllDevicesList(const ALCchar *name);
775 void AppendCaptureDeviceList(const ALCchar *name);
777 void ALCdevice_Lock(ALCdevice *device);
778 void ALCdevice_Unlock(ALCdevice *device);
780 void ALCcontext_DeferUpdates(ALCcontext *context, ALenum type);
781 void ALCcontext_ProcessUpdates(ALCcontext *context);
783 inline void LockContext(ALCcontext *context)
784 { ALCdevice_Lock(context->Device); }
786 inline void UnlockContext(ALCcontext *context)
787 { ALCdevice_Unlock(context->Device); }
789 enum {
790 DeferOff = AL_FALSE,
791 DeferAll,
792 DeferAllowPlay
796 typedef struct {
797 #ifdef HAVE_FENV_H
798 DERIVE_FROM_TYPE(fenv_t);
799 #else
800 int state;
801 #endif
802 #ifdef HAVE_SSE
803 int sse_state;
804 #endif
805 } FPUCtl;
806 void SetMixerFPUMode(FPUCtl *ctl);
807 void RestoreFPUMode(const FPUCtl *ctl);
810 typedef struct ll_ringbuffer ll_ringbuffer_t;
811 typedef struct ll_ringbuffer_data {
812 char *buf;
813 size_t len;
814 } ll_ringbuffer_data_t;
815 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
816 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
817 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
818 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
819 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
820 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
821 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
822 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
823 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
824 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
825 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
826 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
827 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
829 void ReadALConfig(void);
830 void FreeALConfig(void);
831 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
832 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
833 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
834 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
835 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
836 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
837 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
838 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
840 void SetRTPriority(void);
842 void SetDefaultChannelOrder(ALCdevice *device);
843 void SetDefaultWFXChannelOrder(ALCdevice *device);
845 const ALCchar *DevFmtTypeString(enum DevFmtType type);
846 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
849 * GetChannelIdxByName
851 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
852 * doesn't exist.
854 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
856 ALint i;
857 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
859 if(names[i] == chan)
860 return i;
862 return -1;
864 #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
866 extern FILE *LogFile;
868 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
869 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
870 #else
871 void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
872 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
873 #endif
875 enum LogLevel {
876 NoLog,
877 LogError,
878 LogWarning,
879 LogTrace,
880 LogRef
882 extern enum LogLevel LogLevel;
884 #define TRACEREF(...) do { \
885 if(LogLevel >= LogRef) \
886 AL_PRINT("(--)", __VA_ARGS__); \
887 } while(0)
889 #define TRACE(...) do { \
890 if(LogLevel >= LogTrace) \
891 AL_PRINT("(II)", __VA_ARGS__); \
892 } while(0)
894 #define WARN(...) do { \
895 if(LogLevel >= LogWarning) \
896 AL_PRINT("(WW)", __VA_ARGS__); \
897 } while(0)
899 #define ERR(...) do { \
900 if(LogLevel >= LogError) \
901 AL_PRINT("(EE)", __VA_ARGS__); \
902 } while(0)
905 extern ALint RTPrioLevel;
908 extern ALuint CPUCapFlags;
909 enum {
910 CPU_CAP_SSE = 1<<0,
911 CPU_CAP_SSE2 = 1<<1,
912 CPU_CAP_SSE3 = 1<<2,
913 CPU_CAP_SSE4_1 = 1<<3,
914 CPU_CAP_NEON = 1<<4,
917 void FillCPUCaps(ALuint capfilter);
919 vector_al_string SearchDataFiles(const char *match, const char *subdir);
921 /* Small hack to use a pointer-to-array type as a normal argument type.
922 * Shouldn't be used directly. */
923 typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
926 #ifdef __cplusplus
928 #endif
930 #endif