Add a comment about CoeffCount being 0
[openal-soft.git] / OpenAL32 / Include / alMain.h
blobe818245b615afc0318927fa96602aad661b868c6
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"
41 #include "hrtf.h"
43 #ifndef ALC_SOFT_device_clock
44 #define ALC_SOFT_device_clock 1
45 typedef int64_t ALCint64SOFT;
46 typedef uint64_t ALCuint64SOFT;
47 #define ALC_DEVICE_CLOCK_SOFT 0x1600
48 typedef void (ALC_APIENTRY*LPALCGETINTEGER64VSOFT)(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
49 #ifdef AL_ALEXT_PROTOTYPES
50 ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
51 #endif
52 #endif
54 #ifndef AL_SOFT_buffer_samples2
55 #define AL_SOFT_buffer_samples2 1
56 /* Channel configurations */
57 #define AL_MONO_SOFT 0x1500
58 #define AL_STEREO_SOFT 0x1501
59 #define AL_REAR_SOFT 0x1502
60 #define AL_QUAD_SOFT 0x1503
61 #define AL_5POINT1_SOFT 0x1504
62 #define AL_6POINT1_SOFT 0x1505
63 #define AL_7POINT1_SOFT 0x1506
64 #define AL_BFORMAT2D_SOFT 0x1507
65 #define AL_BFORMAT3D_SOFT 0x1508
67 /* Sample types */
68 #define AL_BYTE_SOFT 0x1400
69 #define AL_UNSIGNED_BYTE_SOFT 0x1401
70 #define AL_SHORT_SOFT 0x1402
71 #define AL_UNSIGNED_SHORT_SOFT 0x1403
72 #define AL_INT_SOFT 0x1404
73 #define AL_UNSIGNED_INT_SOFT 0x1405
74 #define AL_FLOAT_SOFT 0x1406
75 #define AL_DOUBLE_SOFT 0x1407
76 #define AL_BYTE3_SOFT 0x1408
77 #define AL_UNSIGNED_BYTE3_SOFT 0x1409
78 #define AL_MULAW_SOFT 0x140A
80 /* Storage formats */
81 #define AL_MONO8_SOFT 0x1100
82 #define AL_MONO16_SOFT 0x1101
83 #define AL_MONO32F_SOFT 0x10010
84 #define AL_STEREO8_SOFT 0x1102
85 #define AL_STEREO16_SOFT 0x1103
86 #define AL_STEREO32F_SOFT 0x10011
87 #define AL_QUAD8_SOFT 0x1204
88 #define AL_QUAD16_SOFT 0x1205
89 #define AL_QUAD32F_SOFT 0x1206
90 #define AL_REAR8_SOFT 0x1207
91 #define AL_REAR16_SOFT 0x1208
92 #define AL_REAR32F_SOFT 0x1209
93 #define AL_5POINT1_8_SOFT 0x120A
94 #define AL_5POINT1_16_SOFT 0x120B
95 #define AL_5POINT1_32F_SOFT 0x120C
96 #define AL_6POINT1_8_SOFT 0x120D
97 #define AL_6POINT1_16_SOFT 0x120E
98 #define AL_6POINT1_32F_SOFT 0x120F
99 #define AL_7POINT1_8_SOFT 0x1210
100 #define AL_7POINT1_16_SOFT 0x1211
101 #define AL_7POINT1_32F_SOFT 0x1212
102 #define AL_BFORMAT2D_8_SOFT 0x20021
103 #define AL_BFORMAT2D_16_SOFT 0x20022
104 #define AL_BFORMAT2D_32F_SOFT 0x20023
105 #define AL_BFORMAT3D_8_SOFT 0x20031
106 #define AL_BFORMAT3D_16_SOFT 0x20032
107 #define AL_BFORMAT3D_32F_SOFT 0x20033
109 /* Buffer attributes */
110 #define AL_INTERNAL_FORMAT_SOFT 0x2008
111 #define AL_BYTE_LENGTH_SOFT 0x2009
112 #define AL_SAMPLE_LENGTH_SOFT 0x200A
113 #define AL_SEC_LENGTH_SOFT 0x200B
115 #if 0
116 typedef void (AL_APIENTRY*LPALBUFFERSAMPLESSOFT)(ALuint,ALuint,ALenum,ALsizei,ALenum,ALenum,const ALvoid*);
117 typedef void (AL_APIENTRY*LPALGETBUFFERSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,ALvoid*);
118 typedef ALboolean (AL_APIENTRY*LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum);
119 #ifdef AL_ALEXT_PROTOTYPES
120 AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer, ALuint samplerate, ALenum internalformat, ALsizei samples, ALenum channels, ALenum type, const ALvoid *data);
121 AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer, ALsizei offset, ALsizei samples, ALenum channels, ALenum type, ALvoid *data);
122 AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format);
123 #endif
124 #endif
125 #endif
128 typedef ALint64SOFT ALint64;
129 typedef ALuint64SOFT ALuint64;
131 #ifndef U64
132 #if defined(_MSC_VER)
133 #define U64(x) ((ALuint64)(x##ui64))
134 #elif SIZEOF_LONG == 8
135 #define U64(x) ((ALuint64)(x##ul))
136 #elif SIZEOF_LONG_LONG == 8
137 #define U64(x) ((ALuint64)(x##ull))
138 #endif
139 #endif
141 #ifndef UINT64_MAX
142 #define UINT64_MAX U64(18446744073709551615)
143 #endif
145 #ifndef UNUSED
146 #if defined(__cplusplus)
147 #define UNUSED(x)
148 #elif defined(__GNUC__)
149 #define UNUSED(x) UNUSED_##x __attribute__((unused))
150 #elif defined(__LCLINT__)
151 #define UNUSED(x) /*@unused@*/ x
152 #else
153 #define UNUSED(x) x
154 #endif
155 #endif
157 #ifdef __GNUC__
158 #define DECL_CONST __attribute__((const))
159 #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
160 #else
161 #define DECL_CONST
162 #define DECL_FORMAT(x, y, z)
163 #endif
165 #if defined(__GNUC__) && defined(__i386__)
166 /* force_align_arg_pointer is required for proper function arguments aligning
167 * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
168 * thread functions will be properly aligned on the stack, even though GCC may
169 * generate code with the assumption that it is. */
170 #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
171 #else
172 #define FORCE_ALIGN
173 #endif
175 #ifdef HAVE_C99_VLA
176 #define DECL_VLA(T, _name, _size) T _name[(_size)]
177 #else
178 #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
179 #endif
181 #ifndef PATH_MAX
182 #ifdef MAX_PATH
183 #define PATH_MAX MAX_PATH
184 #else
185 #define PATH_MAX 4096
186 #endif
187 #endif
190 static const union {
191 ALuint u;
192 ALubyte b[sizeof(ALuint)];
193 } EndianTest = { 1 };
194 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
196 #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
199 #define DERIVE_FROM_TYPE(t) t t##_parent
200 #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
201 #ifdef __GNUC__
202 #define STATIC_UPCAST(to, from, obj) __extension__({ \
203 static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
204 "Invalid upcast object from type"); \
205 (to*)((char*)(obj) - offsetof(to, from##_parent)); \
207 #else
208 #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
209 #endif
211 #define DECLARE_FORWARD(T1, T2, rettype, func) \
212 rettype T1##_##func(T1 *obj) \
213 { return T2##_##func(STATIC_CAST(T2, obj)); }
215 #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
216 rettype T1##_##func(T1 *obj, argtype1 a) \
217 { return T2##_##func(STATIC_CAST(T2, obj), a); }
219 #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
220 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
221 { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
223 #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
224 rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
225 { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
228 #define GET_VTABLE1(T1) (&(T1##_vtable))
229 #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
231 #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
232 #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
234 #define DECLARE_THUNK(T1, T2, rettype, func) \
235 static rettype T1##_##T2##_##func(T2 *obj) \
236 { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
238 #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
239 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
240 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
242 #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
243 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
244 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
246 #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
247 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
248 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c); }
250 #define DECLARE_THUNK4(T1, T2, rettype, func, argtype1, argtype2, argtype3, argtype4) \
251 static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c, argtype4 d) \
252 { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b, c, d); }
254 #define DECLARE_DEFAULT_ALLOCATORS(T) \
255 static void* T##_New(size_t size) { return al_malloc(16, size); } \
256 static void T##_Delete(void *ptr) { al_free(ptr); }
258 /* Helper to extract an argument list for VCALL. Not used directly. */
259 #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
261 /* Call a "virtual" method on an object, with arguments. */
262 #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
263 /* Call a "virtual" method on an object, with no arguments. */
264 #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
266 #define DELETE_OBJ(obj) do { \
267 if((obj) != NULL) \
269 V0((obj),Destruct)(); \
270 V0((obj),Delete)(); \
272 } while(0)
275 #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
277 } while(0)
279 #define NEW_OBJ(_res, T) do { \
280 _res = T##_New(sizeof(T)); \
281 if(_res) \
283 memset(_res, 0, sizeof(T)); \
284 T##_Construct(_res, EXTRACT_NEW_ARGS
287 #ifdef __cplusplus
288 extern "C" {
289 #endif
291 struct Hrtf;
294 #define DEFAULT_OUTPUT_RATE (44100)
295 #define MIN_OUTPUT_RATE (8000)
298 /* Find the next power-of-2 for non-power-of-2 numbers. */
299 inline ALuint NextPowerOf2(ALuint value)
301 if(value > 0)
303 value--;
304 value |= value>>1;
305 value |= value>>2;
306 value |= value>>4;
307 value |= value>>8;
308 value |= value>>16;
310 return value+1;
313 /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
314 * mode. */
315 inline ALint fastf2i(ALfloat f)
317 #ifdef HAVE_LRINTF
318 return lrintf(f);
319 #elif defined(_MSC_VER) && defined(_M_IX86)
320 ALint i;
321 __asm fld f
322 __asm fistp i
323 return i;
324 #else
325 return (ALint)f;
326 #endif
329 /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
330 * mode. */
331 inline ALuint fastf2u(ALfloat f)
332 { return fastf2i(f); }
335 enum DevProbe {
336 ALL_DEVICE_PROBE,
337 CAPTURE_DEVICE_PROBE
340 typedef struct {
341 ALCenum (*OpenPlayback)(ALCdevice*, const ALCchar*);
342 void (*ClosePlayback)(ALCdevice*);
343 ALCboolean (*ResetPlayback)(ALCdevice*);
344 ALCboolean (*StartPlayback)(ALCdevice*);
345 void (*StopPlayback)(ALCdevice*);
347 ALCenum (*OpenCapture)(ALCdevice*, const ALCchar*);
348 void (*CloseCapture)(ALCdevice*);
349 void (*StartCapture)(ALCdevice*);
350 void (*StopCapture)(ALCdevice*);
351 ALCenum (*CaptureSamples)(ALCdevice*, void*, ALCuint);
352 ALCuint (*AvailableSamples)(ALCdevice*);
353 } BackendFuncs;
355 ALCboolean alc_sndio_init(BackendFuncs *func_list);
356 void alc_sndio_deinit(void);
357 void alc_sndio_probe(enum DevProbe type);
358 ALCboolean alc_ca_init(BackendFuncs *func_list);
359 void alc_ca_deinit(void);
360 void alc_ca_probe(enum DevProbe type);
361 ALCboolean alc_opensl_init(BackendFuncs *func_list);
362 void alc_opensl_deinit(void);
363 void alc_opensl_probe(enum DevProbe type);
364 ALCboolean alc_qsa_init(BackendFuncs *func_list);
365 void alc_qsa_deinit(void);
366 void alc_qsa_probe(enum DevProbe type);
368 struct ALCbackend;
371 enum DistanceModel {
372 InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
373 LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
374 ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
375 InverseDistance = AL_INVERSE_DISTANCE,
376 LinearDistance = AL_LINEAR_DISTANCE,
377 ExponentDistance = AL_EXPONENT_DISTANCE,
378 DisableDistance = AL_NONE,
380 DefaultDistanceModel = InverseDistanceClamped
383 enum Channel {
384 FrontLeft = 0,
385 FrontRight,
386 FrontCenter,
387 LFE,
388 BackLeft,
389 BackRight,
390 BackCenter,
391 SideLeft,
392 SideRight,
394 UpperFrontLeft,
395 UpperFrontRight,
396 UpperBackLeft,
397 UpperBackRight,
398 LowerFrontLeft,
399 LowerFrontRight,
400 LowerBackLeft,
401 LowerBackRight,
403 Aux0,
404 Aux1,
405 Aux2,
406 Aux3,
407 Aux4,
408 Aux5,
409 Aux6,
410 Aux7,
411 Aux8,
412 Aux9,
413 Aux10,
414 Aux11,
415 Aux12,
416 Aux13,
417 Aux14,
418 Aux15,
420 InvalidChannel
424 /* Device formats */
425 enum DevFmtType {
426 DevFmtByte = ALC_BYTE_SOFT,
427 DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
428 DevFmtShort = ALC_SHORT_SOFT,
429 DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
430 DevFmtInt = ALC_INT_SOFT,
431 DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
432 DevFmtFloat = ALC_FLOAT_SOFT,
434 DevFmtTypeDefault = DevFmtFloat
436 enum DevFmtChannels {
437 DevFmtMono = ALC_MONO_SOFT,
438 DevFmtStereo = ALC_STEREO_SOFT,
439 DevFmtQuad = ALC_QUAD_SOFT,
440 DevFmtX51 = ALC_5POINT1_SOFT,
441 DevFmtX61 = ALC_6POINT1_SOFT,
442 DevFmtX71 = ALC_7POINT1_SOFT,
444 /* Similar to 5.1, except using rear channels instead of sides */
445 DevFmtX51Rear = 0x80000000,
447 DevFmtBFormat3D,
449 DevFmtChannelsDefault = DevFmtStereo
451 #define MAX_OUTPUT_CHANNELS (16)
453 ALuint BytesFromDevFmt(enum DevFmtType type) DECL_CONST;
454 ALuint ChannelsFromDevFmt(enum DevFmtChannels chans) DECL_CONST;
455 inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
457 return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
461 extern const struct EffectList {
462 const char *name;
463 int type;
464 const char *ename;
465 ALenum val;
466 } EffectList[];
469 enum DeviceType {
470 Playback,
471 Capture,
472 Loopback
476 enum RenderMode {
477 NormalRender,
478 StereoPair,
479 HrtfRender
483 /* The maximum number of Ambisonics coefficients. For a given order (o), the
484 * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
485 * second-order has 9, and third-order has 16. */
486 #define MAX_AMBI_ORDER 3
487 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
489 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
490 typedef struct BFChannelConfig {
491 ALfloat Scale;
492 ALuint Index;
493 } BFChannelConfig;
496 #define HRTF_HISTORY_BITS (6)
497 #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
498 #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
500 typedef struct HrtfState {
501 alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
502 alignas(16) ALfloat Values[HRIR_LENGTH][2];
503 } HrtfState;
505 typedef struct HrtfParams {
506 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
507 ALuint Delay[2];
508 } HrtfParams;
511 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
512 * more memory, while smaller values may need more iterations. The value needs
513 * to be a sensible size, however, as it constrains the max stepping value used
514 * for mixing, as well as the maximum number of samples per mixing iteration.
516 #define BUFFERSIZE (2048u)
518 struct ALCdevice_struct
520 RefCount ref;
522 ALCboolean Connected;
523 enum DeviceType Type;
525 ALuint Frequency;
526 ALuint UpdateSize;
527 ALuint NumUpdates;
528 enum DevFmtChannels FmtChans;
529 enum DevFmtType FmtType;
530 ALboolean IsHeadphones;
532 al_string DeviceName;
534 ATOMIC(ALCenum) LastError;
536 // Maximum number of sources that can be created
537 ALuint MaxNoOfSources;
538 // Maximum number of slots that can be created
539 ALuint AuxiliaryEffectSlotMax;
541 ALCuint NumMonoSources;
542 ALCuint NumStereoSources;
543 ALuint NumAuxSends;
545 // Map of Buffers for this device
546 UIntMap BufferMap;
548 // Map of Effects for this device
549 UIntMap EffectMap;
551 // Map of Filters for this device
552 UIntMap FilterMap;
554 /* HRTF filter tables */
555 vector_HrtfEntry Hrtf_List;
556 al_string Hrtf_Name;
557 const struct Hrtf *Hrtf;
558 ALCenum Hrtf_Status;
560 /* HRTF filter state for dry buffer content */
561 HrtfState Hrtf_State[MAX_OUTPUT_CHANNELS];
562 HrtfParams Hrtf_Params[MAX_OUTPUT_CHANNELS];
563 ALuint Hrtf_Offset;
565 /* UHJ encoder state */
566 struct Uhj2Encoder *Uhj_Encoder;
568 /* High quality Ambisonic decoder */
569 struct BFormatDec *AmbiDecoder;
571 // Stereo-to-binaural filter
572 struct bs2b *Bs2b;
574 /* Rendering mode. */
575 enum RenderMode Render_Mode;
577 // Device flags
578 ALuint Flags;
580 ALuint64 ClockBase;
581 ALuint SamplesDone;
583 /* Temp storage used for each source when mixing. */
584 alignas(16) ALfloat SourceData[BUFFERSIZE];
585 alignas(16) ALfloat ResampledData[BUFFERSIZE];
586 alignas(16) ALfloat FilteredData[BUFFERSIZE];
588 /* The "dry" path corresponds to the main output. */
589 struct {
590 union {
591 /* Ambisonic coefficients for mixing to the dry buffer. */
592 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
593 /* Coefficient channel mapping for mixing to the dry buffer. */
594 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
595 } Ambi;
596 /* Number of coefficients in each ChannelConfig to mix together (4 for
597 * first-order, 9 for second-order, etc). If the count is 0, the
598 * BFChannelConfig is used instead to map each output to a coefficient
599 * index.
601 ALuint CoeffCount;
603 /* Dry buffer will be aliased by the virtual or real output. */
604 ALfloat (*Buffer)[BUFFERSIZE];
605 ALuint NumChannels;
606 } Dry;
608 /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
609 struct {
610 union {
611 ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
612 BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
613 } Ambi;
614 /* Will only be 4 or 0. */
615 ALuint CoeffCount;
617 ALfloat (*Buffer)[BUFFERSIZE];
618 ALuint NumChannels;
619 } FOAOut;
621 /* Virtual output, to be post-processed to the real output. */
622 struct {
623 ALfloat (*Buffer)[BUFFERSIZE];
624 ALuint NumChannels;
625 } VirtOut;
626 /* "Real" output, which will be written to the device buffer. */
627 struct {
628 enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
630 ALfloat (*Buffer)[BUFFERSIZE];
631 ALuint NumChannels;
632 } RealOut;
634 /* Running count of the mixer invocations, in 31.1 fixed point. This
635 * actually increments *twice* when mixing, first at the start and then at
636 * the end, so the bottom bit indicates if the device is currently mixing
637 * and the upper bits indicates how many mixes have been done.
639 RefCount MixCount;
641 /* Default effect slot */
642 struct ALeffectslot *DefaultSlot;
644 // Contexts created on this device
645 ATOMIC(ALCcontext*) ContextList;
647 struct ALCbackend *Backend;
649 void *ExtraData; // For the backend's use
651 ALCdevice *volatile next;
653 /* Memory space used by the default slot (Playback devices only) */
654 alignas(16) ALCbyte _slot_mem[];
657 // Frequency was requested by the app or config file
658 #define DEVICE_FREQUENCY_REQUEST (1<<1)
659 // Channel configuration was requested by the config file
660 #define DEVICE_CHANNELS_REQUEST (1<<2)
661 // Sample type was requested by the config file
662 #define DEVICE_SAMPLE_TYPE_REQUEST (1<<3)
664 // Specifies if the DSP is paused at user request
665 #define DEVICE_PAUSED (1<<30)
667 // Specifies if the device is currently running
668 #define DEVICE_RUNNING (1<<31)
671 /* Nanosecond resolution for the device clock time. */
672 #define DEVICE_CLOCK_RES U64(1000000000)
675 /* Must be less than 15 characters (16 including terminating null) for
676 * compatibility with pthread_setname_np limitations. */
677 #define MIXER_THREAD_NAME "alsoft-mixer"
679 #define RECORD_THREAD_NAME "alsoft-record"
682 struct ALCcontext_struct
684 RefCount ref;
686 struct ALlistener *Listener;
688 UIntMap SourceMap;
689 UIntMap EffectSlotMap;
691 ATOMIC(ALenum) LastError;
693 ATOMIC(ALenum) UpdateSources;
695 volatile enum DistanceModel DistanceModel;
696 volatile ALboolean SourceDistanceModel;
698 volatile ALfloat DopplerFactor;
699 volatile ALfloat DopplerVelocity;
700 volatile ALfloat SpeedOfSound;
701 volatile ALenum DeferUpdates;
703 struct ALvoice *Voices;
704 ALsizei VoiceCount;
705 ALsizei MaxVoices;
707 VECTOR(struct ALeffectslot*) ActiveAuxSlots;
709 ALCdevice *Device;
710 const ALCchar *ExtensionList;
712 ALCcontext *volatile next;
714 /* Memory space used by the listener */
715 alignas(16) ALCbyte _listener_mem[];
718 ALCcontext *GetContextRef(void);
720 void ALCcontext_IncRef(ALCcontext *context);
721 void ALCcontext_DecRef(ALCcontext *context);
723 void AppendAllDevicesList(const ALCchar *name);
724 void AppendCaptureDeviceList(const ALCchar *name);
726 void ALCdevice_Lock(ALCdevice *device);
727 void ALCdevice_Unlock(ALCdevice *device);
729 void ALCcontext_DeferUpdates(ALCcontext *context);
730 void ALCcontext_ProcessUpdates(ALCcontext *context);
732 inline void LockContext(ALCcontext *context)
733 { ALCdevice_Lock(context->Device); }
735 inline void UnlockContext(ALCcontext *context)
736 { ALCdevice_Unlock(context->Device); }
739 typedef struct {
740 #ifdef HAVE_FENV_H
741 DERIVE_FROM_TYPE(fenv_t);
742 #else
743 int state;
744 #endif
745 #ifdef HAVE_SSE
746 int sse_state;
747 #endif
748 } FPUCtl;
749 void SetMixerFPUMode(FPUCtl *ctl);
750 void RestoreFPUMode(const FPUCtl *ctl);
753 typedef struct ll_ringbuffer ll_ringbuffer_t;
754 typedef struct ll_ringbuffer_data {
755 char *buf;
756 size_t len;
757 } ll_ringbuffer_data_t;
758 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
759 void ll_ringbuffer_free(ll_ringbuffer_t *rb);
760 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
761 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
762 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
763 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
764 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
765 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
766 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
767 void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
768 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
769 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
770 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
772 void ReadALConfig(void);
773 void FreeALConfig(void);
774 int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
775 const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
776 int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
777 int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
778 int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
779 int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
780 int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
781 int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
783 void SetRTPriority(void);
785 void SetDefaultChannelOrder(ALCdevice *device);
786 void SetDefaultWFXChannelOrder(ALCdevice *device);
788 const ALCchar *DevFmtTypeString(enum DevFmtType type) DECL_CONST;
789 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans) DECL_CONST;
792 * GetChannelIdxByName
794 * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
795 * doesn't exist.
797 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
799 ALint i;
800 for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
802 if(names[i] == chan)
803 return i;
805 return -1;
807 #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
809 extern FILE *LogFile;
811 #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
812 #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
813 #else
814 void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
815 #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
816 #endif
818 enum LogLevel {
819 NoLog,
820 LogError,
821 LogWarning,
822 LogTrace,
823 LogRef
825 extern enum LogLevel LogLevel;
827 #define TRACEREF(...) do { \
828 if(LogLevel >= LogRef) \
829 AL_PRINT("(--)", __VA_ARGS__); \
830 } while(0)
832 #define TRACE(...) do { \
833 if(LogLevel >= LogTrace) \
834 AL_PRINT("(II)", __VA_ARGS__); \
835 } while(0)
837 #define WARN(...) do { \
838 if(LogLevel >= LogWarning) \
839 AL_PRINT("(WW)", __VA_ARGS__); \
840 } while(0)
842 #define ERR(...) do { \
843 if(LogLevel >= LogError) \
844 AL_PRINT("(EE)", __VA_ARGS__); \
845 } while(0)
848 extern ALint RTPrioLevel;
851 extern ALuint CPUCapFlags;
852 enum {
853 CPU_CAP_SSE = 1<<0,
854 CPU_CAP_SSE2 = 1<<1,
855 CPU_CAP_SSE3 = 1<<2,
856 CPU_CAP_SSE4_1 = 1<<3,
857 CPU_CAP_NEON = 1<<4,
860 void FillCPUCaps(ALuint capfilter);
862 vector_al_string SearchDataFiles(const char *match, const char *subdir);
864 /* Small hack to use a pointer-to-array type as a normal argument type.
865 * Shouldn't be used directly. */
866 typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
869 #ifdef __cplusplus
871 #endif
873 #endif