Free Electron
alMain.h
1 #ifndef AL_MAIN_H
2 #define AL_MAIN_H
3 
4 #include <string.h>
5 #include <stdio.h>
6 #include <stddef.h>
7 #include <stdarg.h>
8 #include <assert.h>
9 #include <math.h>
10 #include <limits.h>
11 
12 #ifdef HAVE_STRINGS_H
13 #include <strings.h>
14 #endif
15 #ifdef HAVE_INTRIN_H
16 #include <intrin.h>
17 #endif
18 
19 #include "AL/al.h"
20 #include "AL/alc.h"
21 #include "AL/alext.h"
22 
23 #include "inprogext.h"
24 #include "logging.h"
25 #include "polymorphism.h"
26 #include "static_assert.h"
27 #include "align.h"
28 #include "atomic.h"
29 #include "vector.h"
30 #include "alstring.h"
31 #include "almalloc.h"
32 #include "threads.h"
33 
34 
35 #if defined(_WIN64)
36 #define SZFMT "%I64u"
37 #elif defined(_WIN32)
38 #define SZFMT "%u"
39 #else
40 #define SZFMT "%zu"
41 #endif
42 
43 #ifdef __has_builtin
44 #define HAS_BUILTIN __has_builtin
45 #else
46 #define HAS_BUILTIN(x) (0)
47 #endif
48 
49 #ifdef __GNUC__
50 /* LIKELY optimizes the case where the condition is true. The condition is not
51  * required to be true, but it can result in more optimal code for the true
52  * path at the expense of a less optimal false path.
53  */
54 #define LIKELY(x) __builtin_expect(!!(x), !0)
55 /* The opposite of LIKELY, optimizing the case where the condition is false. */
56 #define UNLIKELY(x) __builtin_expect(!!(x), 0)
57 /* Unlike LIKELY, ASSUME requires the condition to be true or else it invokes
58  * undefined behavior. It's essentially an assert without actually checking the
59  * condition at run-time, allowing for stronger optimizations than LIKELY.
60  */
61 #if HAS_BUILTIN(__builtin_assume)
62 #define ASSUME __builtin_assume
63 #else
64 #define ASSUME(x) do { if(!(x)) __builtin_unreachable(); } while(0)
65 #endif
66 
67 #else
68 
69 #define LIKELY(x) (!!(x))
70 #define UNLIKELY(x) (!!(x))
71 #ifdef _MSC_VER
72 #define ASSUME __assume
73 #else
74 #define ASSUME(x) ((void)0)
75 #endif
76 #endif
77 
78 #ifndef UINT64_MAX
79 #define UINT64_MAX U64(18446744073709551615)
80 #endif
81 
82 #ifndef UNUSED
83 #if defined(__cplusplus)
84 #define UNUSED(x)
85 #elif defined(__GNUC__)
86 #define UNUSED(x) UNUSED_##x __attribute__((unused))
87 #elif defined(__LCLINT__)
88 #define UNUSED(x) /*@unused@*/ x
89 #else
90 #define UNUSED(x) x
91 #endif
92 #endif
93 
94 /* Calculates the size of a struct with N elements of a flexible array member.
95  * GCC and Clang allow offsetof(Type, fam[N]) for this, but MSVC seems to have
96  * trouble, so a bit more verbose workaround is needed.
97  */
98 #define FAM_SIZE(T, M, N) (offsetof(T, M) + sizeof(((T*)NULL)->M[0])*(N))
99 
100 
101 #ifdef __cplusplus
102 extern "C" {
103 #endif
104 
105 typedef ALint64SOFT ALint64;
106 typedef ALuint64SOFT ALuint64;
107 
108 #ifndef U64
109 #if defined(_MSC_VER)
110 #define U64(x) ((ALuint64)(x##ui64))
111 #elif SIZEOF_LONG == 8
112 #define U64(x) ((ALuint64)(x##ul))
113 #elif SIZEOF_LONG_LONG == 8
114 #define U64(x) ((ALuint64)(x##ull))
115 #endif
116 #endif
117 
118 #ifndef I64
119 #if defined(_MSC_VER)
120 #define I64(x) ((ALint64)(x##i64))
121 #elif SIZEOF_LONG == 8
122 #define I64(x) ((ALint64)(x##l))
123 #elif SIZEOF_LONG_LONG == 8
124 #define I64(x) ((ALint64)(x##ll))
125 #endif
126 #endif
127 
128 /* Define a CTZ64 macro (count trailing zeros, for 64-bit integers). The result
129  * is *UNDEFINED* if the value is 0.
130  */
131 #ifdef __GNUC__
132 
133 #if SIZEOF_LONG == 8
134 #define CTZ64 __builtin_ctzl
135 #else
136 #define CTZ64 __builtin_ctzll
137 #endif
138 
139 #elif defined(HAVE_BITSCANFORWARD64_INTRINSIC)
140 
141 inline int msvc64_ctz64(ALuint64 v)
142 {
143  unsigned long idx = 64;
144  _BitScanForward64(&idx, v);
145  return (int)idx;
146 }
147 #define CTZ64 msvc64_ctz64
148 
149 #elif defined(HAVE_BITSCANFORWARD_INTRINSIC)
150 
151 inline int msvc_ctz64(ALuint64 v)
152 {
153  unsigned long idx = 64;
154  if(!_BitScanForward(&idx, v&0xffffffff))
155  {
156  if(_BitScanForward(&idx, v>>32))
157  idx += 32;
158  }
159  return (int)idx;
160 }
161 #define CTZ64 msvc_ctz64
162 
163 #else
164 
165 /* There be black magics here. The popcnt64 method is derived from
166  * https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
167  * while the ctz-utilizing-popcnt algorithm is shown here
168  * http://www.hackersdelight.org/hdcodetxt/ntz.c.txt
169  * as the ntz2 variant. These likely aren't the most efficient methods, but
170  * they're good enough if the GCC or MSVC intrinsics aren't available.
171  */
172 inline int fallback_popcnt64(ALuint64 v)
173 {
174  v = v - ((v >> 1) & U64(0x5555555555555555));
175  v = (v & U64(0x3333333333333333)) + ((v >> 2) & U64(0x3333333333333333));
176  v = (v + (v >> 4)) & U64(0x0f0f0f0f0f0f0f0f);
177  return (int)((v * U64(0x0101010101010101)) >> 56);
178 }
179 
180 inline int fallback_ctz64(ALuint64 value)
181 {
182  return fallback_popcnt64(~value & (value - 1));
183 }
184 #define CTZ64 fallback_ctz64
185 #endif
186 
187 #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__)
188 #define IS_LITTLE_ENDIAN (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
189 #else
190 static const union {
191  ALuint u;
192  ALubyte b[sizeof(ALuint)];
193 } EndianTest = { 1 };
194 #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
195 #endif
196 
197 #define COUNTOF(x) (sizeof(x) / sizeof(0[x]))
198 
199 
200 struct ll_ringbuffer;
201 struct Hrtf;
202 struct HrtfEntry;
203 struct DirectHrtfState;
204 struct FrontStablizer;
205 struct Compressor;
206 struct ALCbackend;
207 struct ALbuffer;
208 struct ALeffect;
209 struct ALfilter;
210 struct ALsource;
211 struct ALcontextProps;
212 struct ALlistenerProps;
213 struct ALvoiceProps;
214 struct ALeffectslotProps;
215 
216 
217 #define DEFAULT_OUTPUT_RATE (44100)
218 #define MIN_OUTPUT_RATE (8000)
219 
220 
221 /* Find the next power-of-2 for non-power-of-2 numbers. */
222 inline ALuint NextPowerOf2(ALuint value)
223 {
224  if(value > 0)
225  {
226  value--;
227  value |= value>>1;
228  value |= value>>2;
229  value |= value>>4;
230  value |= value>>8;
231  value |= value>>16;
232  }
233  return value+1;
234 }
235 
236 /** Round up a value to the next multiple. */
237 inline size_t RoundUp(size_t value, size_t r)
238 {
239  value += r-1;
240  return value - (value%r);
241 }
242 
243 /* Fast float-to-int conversion. No particular rounding mode is assumed; the
244  * IEEE-754 default is round-to-nearest with ties-to-even, though an app could
245  * change it on its own threads. On some systems, a truncating conversion may
246  * always be the fastest method.
247  */
248 inline ALint fastf2i(ALfloat f)
249 {
250 #if defined(HAVE_INTRIN_H) && ((defined(_M_IX86_FP) && (_M_IX86_FP > 0)) || defined(_M_X64))
251  return _mm_cvt_ss2si(_mm_set1_ps(f));
252 
253 #elif defined(_MSC_VER) && defined(_M_IX86_FP)
254 
255  ALint i;
256  __asm fld f
257  __asm fistp i
258  return i;
259 
260 #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
261 
262  ALint i;
263 #ifdef __SSE_MATH__
264  __asm__("cvtss2si %1, %0" : "=r"(i) : "x"(f));
265 #else
266  __asm__ __volatile__("fistpl %0" : "=m"(i) : "t"(f) : "st");
267 #endif
268  return i;
269 
270  /* On GCC when compiling with -fno-math-errno, lrintf can be inlined to
271  * some simple instructions. Clang does not inline it, always generating a
272  * libc call, while MSVC's implementation is horribly slow, so always fall
273  * back to a normal integer conversion for them.
274  */
275 #elif defined(HAVE_LRINTF) && !defined(_MSC_VER) && !defined(__clang__)
276 
277  return lrintf(f);
278 
279 #else
280 
281  return (ALint)f;
282 #endif
283 }
284 
285 /* Converts float-to-int using standard behavior (truncation). */
286 inline int float2int(float f)
287 {
288 #if ((defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) && \
289  !defined(__SSE_MATH__)) || (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 0)
290  ALint sign, shift, mant;
291  union {
292  ALfloat f;
293  ALint i;
294  } conv;
295 
296  conv.f = f;
297  sign = (conv.i>>31) | 1;
298  shift = ((conv.i>>23)&0xff) - (127+23);
299 
300  /* Over/underflow */
301  if(UNLIKELY(shift >= 31 || shift < -23))
302  return 0;
303 
304  mant = (conv.i&0x7fffff) | 0x800000;
305  if(LIKELY(shift < 0))
306  return (mant >> -shift) * sign;
307  return (mant << shift) * sign;
308 
309 #else
310 
311  return (ALint)f;
312 #endif
313 }
314 
315 /* Rounds a float to the nearest integral value, according to the current
316  * rounding mode. This is essentially an inlined version of rintf, although
317  * makes fewer promises (e.g. -0 or -0.25 rounded to 0 may result in +0).
318  */
319 inline float fast_roundf(float f)
320 {
321 #if (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__)) && \
322  !defined(__SSE_MATH__)
323 
324  float out;
325  __asm__ __volatile__("frndint" : "=t"(out) : "0"(f));
326  return out;
327 
328 #else
329 
330  /* Integral limit, where sub-integral precision is not available for
331  * floats.
332  */
333  static const float ilim[2] = {
334  8388608.0f /* 0x1.0p+23 */,
335  -8388608.0f /* -0x1.0p+23 */
336  };
337  ALuint sign, expo;
338  union {
339  ALfloat f;
340  ALuint i;
341  } conv;
342 
343  conv.f = f;
344  sign = (conv.i>>31)&0x01;
345  expo = (conv.i>>23)&0xff;
346 
347  if(UNLIKELY(expo >= 150/*+23*/))
348  {
349  /* An exponent (base-2) of 23 or higher is incapable of sub-integral
350  * precision, so it's already an integral value. We don't need to worry
351  * about infinity or NaN here.
352  */
353  return f;
354  }
355  /* Adding the integral limit to the value (with a matching sign) forces a
356  * result that has no sub-integral precision, and is consequently forced to
357  * round to an integral value. Removing the integral limit then restores
358  * the initial value rounded to the integral. The compiler should not
359  * optimize this out because of non-associative rules on floating-point
360  * math (as long as you don't use -fassociative-math,
361  * -funsafe-math-optimizations, -ffast-math, or -Ofast, in which case this
362  * may break).
363  */
364  f += ilim[sign];
365  return f - ilim[sign];
366 #endif
367 }
368 
369 
370 enum DevProbe {
371  ALL_DEVICE_PROBE,
372  CAPTURE_DEVICE_PROBE
373 };
374 
375 
376 enum DistanceModel {
377  InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
378  LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
379  ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
380  InverseDistance = AL_INVERSE_DISTANCE,
381  LinearDistance = AL_LINEAR_DISTANCE,
382  ExponentDistance = AL_EXPONENT_DISTANCE,
383  DisableDistance = AL_NONE,
384 
385  DefaultDistanceModel = InverseDistanceClamped
386 };
387 
388 enum Channel {
389  FrontLeft = 0,
390  FrontRight,
391  FrontCenter,
392  LFE,
393  BackLeft,
394  BackRight,
395  BackCenter,
396  SideLeft,
397  SideRight,
398 
399  UpperFrontLeft,
400  UpperFrontRight,
401  UpperBackLeft,
402  UpperBackRight,
403  LowerFrontLeft,
404  LowerFrontRight,
405  LowerBackLeft,
406  LowerBackRight,
407 
408  Aux0,
409  Aux1,
410  Aux2,
411  Aux3,
412  Aux4,
413  Aux5,
414  Aux6,
415  Aux7,
416  Aux8,
417  Aux9,
418  Aux10,
419  Aux11,
420  Aux12,
421  Aux13,
422  Aux14,
423  Aux15,
424 
425  InvalidChannel
426 };
427 
428 
429 /* Device formats */
430 enum DevFmtType {
431  DevFmtByte = ALC_BYTE_SOFT,
432  DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
433  DevFmtShort = ALC_SHORT_SOFT,
434  DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
435  DevFmtInt = ALC_INT_SOFT,
436  DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
437  DevFmtFloat = ALC_FLOAT_SOFT,
438 
439  DevFmtTypeDefault = DevFmtFloat
440 };
441 enum DevFmtChannels {
442  DevFmtMono = ALC_MONO_SOFT,
443  DevFmtStereo = ALC_STEREO_SOFT,
444  DevFmtQuad = ALC_QUAD_SOFT,
445  DevFmtX51 = ALC_5POINT1_SOFT,
446  DevFmtX61 = ALC_6POINT1_SOFT,
447  DevFmtX71 = ALC_7POINT1_SOFT,
448  DevFmtAmbi3D = ALC_BFORMAT3D_SOFT,
449 
450  /* Similar to 5.1, except using rear channels instead of sides */
451  DevFmtX51Rear = 0x80000000,
452 
453  DevFmtChannelsDefault = DevFmtStereo
454 };
455 #define MAX_OUTPUT_CHANNELS (16)
456 
457 ALsizei BytesFromDevFmt(enum DevFmtType type);
458 ALsizei ChannelsFromDevFmt(enum DevFmtChannels chans, ALsizei ambiorder);
459 inline ALsizei FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type, ALsizei ambiorder)
460 {
461  return ChannelsFromDevFmt(chans, ambiorder) * BytesFromDevFmt(type);
462 }
463 
464 enum AmbiLayout {
465  AmbiLayout_FuMa = ALC_FUMA_SOFT, /* FuMa channel order */
466  AmbiLayout_ACN = ALC_ACN_SOFT, /* ACN channel order */
467 
468  AmbiLayout_Default = AmbiLayout_ACN
469 };
470 
471 enum AmbiNorm {
472  AmbiNorm_FuMa = ALC_FUMA_SOFT, /* FuMa normalization */
473  AmbiNorm_SN3D = ALC_SN3D_SOFT, /* SN3D normalization */
474  AmbiNorm_N3D = ALC_N3D_SOFT, /* N3D normalization */
475 
476  AmbiNorm_Default = AmbiNorm_SN3D
477 };
478 
479 
480 enum DeviceType {
481  Playback,
482  Capture,
483  Loopback
484 };
485 
486 
487 enum RenderMode {
488  NormalRender,
489  StereoPair,
490  HrtfRender
491 };
492 
493 
494 /* The maximum number of Ambisonics coefficients. For a given order (o), the
495  * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
496  * second-order has 9, third-order has 16, and fourth-order has 25.
497  */
498 #define MAX_AMBI_ORDER 3
499 #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
500 
501 /* A bitmask of ambisonic channels with height information. If none of these
502  * channels are used/needed, there's no height (e.g. with most surround sound
503  * speaker setups). This only specifies up to 4th order, which is the highest
504  * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
505  * order). This is ACN ordering, with bit 0 being ACN 0, etc.
506  */
507 #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
508 
509 /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
510  * representation. This is 2 per each order above zero-order, plus 1 for zero-
511  * order. Or simply, o*2 + 1.
512  */
513 #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
514 
515 
516 typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
517 typedef struct BFChannelConfig {
518  ALfloat Scale;
519  ALsizei Index;
520 } BFChannelConfig;
521 
522 typedef union AmbiConfig {
523  /* Ambisonic coefficients for mixing to the dry buffer. */
524  ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
525  /* Coefficient channel mapping for mixing to the dry buffer. */
526  BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
527 } AmbiConfig;
528 
529 
530 typedef struct BufferSubList {
531  ALuint64 FreeMask;
532  struct ALbuffer *Buffers; /* 64 */
533 } BufferSubList;
534 TYPEDEF_VECTOR(BufferSubList, vector_BufferSubList)
535 
536 typedef struct EffectSubList {
537  ALuint64 FreeMask;
538  struct ALeffect *Effects; /* 64 */
539 } EffectSubList;
540 TYPEDEF_VECTOR(EffectSubList, vector_EffectSubList)
541 
542 typedef struct FilterSubList {
543  ALuint64 FreeMask;
544  struct ALfilter *Filters; /* 64 */
545 } FilterSubList;
546 TYPEDEF_VECTOR(FilterSubList, vector_FilterSubList)
547 
548 typedef struct SourceSubList {
549  ALuint64 FreeMask;
550  struct ALsource *Sources; /* 64 */
551 } SourceSubList;
552 TYPEDEF_VECTOR(SourceSubList, vector_SourceSubList)
553 
554 /* Effect slots are rather large, and apps aren't likely to have more than one
555  * or two (let alone 64), so hold them individually.
556  */
557 typedef struct ALeffectslot *ALeffectslotPtr;
558 TYPEDEF_VECTOR(ALeffectslotPtr, vector_ALeffectslotPtr)
559 
560 
561 typedef struct EnumeratedHrtf {
562  al_string name;
563 
564  struct HrtfEntry *hrtf;
565 } EnumeratedHrtf;
566 TYPEDEF_VECTOR(EnumeratedHrtf, vector_EnumeratedHrtf)
567 
568 
569 /* Maximum delay in samples for speaker distance compensation. */
570 #define MAX_DELAY_LENGTH 1024
571 
572 typedef struct DistanceComp {
573  ALfloat Gain;
574  ALsizei Length; /* Valid range is [0...MAX_DELAY_LENGTH). */
575  ALfloat *Buffer;
576 } DistanceComp;
577 
578 /* Size for temporary storage of buffer data, in ALfloats. Larger values need
579  * more memory, while smaller values may need more iterations. The value needs
580  * to be a sensible size, however, as it constrains the max stepping value used
581  * for mixing, as well as the maximum number of samples per mixing iteration.
582  */
583 #define BUFFERSIZE 2048
584 
585 typedef struct MixParams {
586  AmbiConfig Ambi;
587  /* Number of coefficients in each Ambi.Coeffs to mix together (4 for first-
588  * order, 9 for second-order, etc). If the count is 0, Ambi.Map is used
589  * instead to map each output to a coefficient index.
590  */
591  ALsizei CoeffCount;
592 
593  ALfloat (*Buffer)[BUFFERSIZE];
594  ALsizei NumChannels;
595 } MixParams;
596 
597 typedef struct RealMixParams {
598  enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
599 
600  ALfloat (*Buffer)[BUFFERSIZE];
601  ALsizei NumChannels;
602 } RealMixParams;
603 
604 typedef void (*POSTPROCESS)(ALCdevice *device, ALsizei SamplesToDo);
605 
606 struct ALCdevice_struct {
607  RefCount ref;
608 
609  ATOMIC(ALenum) Connected;
610  enum DeviceType Type;
611 
612  ALuint Frequency;
613  ALuint UpdateSize;
614  ALuint NumUpdates;
615  enum DevFmtChannels FmtChans;
616  enum DevFmtType FmtType;
617  ALboolean IsHeadphones;
618  ALsizei AmbiOrder;
619  /* For DevFmtAmbi* output only, specifies the channel order and
620  * normalization.
621  */
622  enum AmbiLayout AmbiLayout;
623  enum AmbiNorm AmbiScale;
624 
625  ALCenum LimiterState;
626 
627  al_string DeviceName;
628 
629  ATOMIC(ALCenum) LastError;
630 
631  // Maximum number of sources that can be created
632  ALuint SourcesMax;
633  // Maximum number of slots that can be created
634  ALuint AuxiliaryEffectSlotMax;
635 
636  ALCuint NumMonoSources;
637  ALCuint NumStereoSources;
638  ALsizei NumAuxSends;
639 
640  // Map of Buffers for this device
641  vector_BufferSubList BufferList;
642  almtx_t BufferLock;
643 
644  // Map of Effects for this device
645  vector_EffectSubList EffectList;
646  almtx_t EffectLock;
647 
648  // Map of Filters for this device
649  vector_FilterSubList FilterList;
650  almtx_t FilterLock;
651 
652  POSTPROCESS PostProcess;
653 
654  /* HRTF state and info */
655  struct DirectHrtfState *Hrtf;
656  al_string HrtfName;
657  struct Hrtf *HrtfHandle;
658  vector_EnumeratedHrtf HrtfList;
659  ALCenum HrtfStatus;
660 
661  /* UHJ encoder state */
662  struct Uhj2Encoder *Uhj_Encoder;
663 
664  /* High quality Ambisonic decoder */
665  struct BFormatDec *AmbiDecoder;
666 
667  /* Stereo-to-binaural filter */
668  struct bs2b *Bs2b;
669 
670  /* First-order ambisonic upsampler for higher-order output */
671  struct AmbiUpsampler *AmbiUp;
672 
673  /* Rendering mode. */
674  enum RenderMode Render_Mode;
675 
676  // Device flags
677  ALuint Flags;
678 
679  ALuint64 ClockBase;
680  ALuint SamplesDone;
681  ALuint FixedLatency;
682 
683  /* Temp storage used for mixer processing. */
684  alignas(16) ALfloat TempBuffer[4][BUFFERSIZE];
685 
686  /* The "dry" path corresponds to the main output. */
687  MixParams Dry;
688  ALsizei NumChannelsPerOrder[MAX_AMBI_ORDER+1];
689 
690  /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
691  MixParams FOAOut;
692 
693  /* "Real" output, which will be written to the device buffer. May alias the
694  * dry buffer.
695  */
696  RealMixParams RealOut;
697 
698  struct FrontStablizer *Stablizer;
699 
700  struct Compressor *Limiter;
701 
702  /* The average speaker distance as determined by the ambdec configuration
703  * (or alternatively, by the NFC-HOA reference delay). Only used for NFC.
704  */
705  ALfloat AvgSpeakerDist;
706 
707  /* Delay buffers used to compensate for speaker distances. */
708  DistanceComp ChannelDelay[MAX_OUTPUT_CHANNELS];
709 
710  /* Dithering control. */
711  ALfloat DitherDepth;
712  ALuint DitherSeed;
713 
714  /* Running count of the mixer invocations, in 31.1 fixed point. This
715  * actually increments *twice* when mixing, first at the start and then at
716  * the end, so the bottom bit indicates if the device is currently mixing
717  * and the upper bits indicates how many mixes have been done.
718  */
719  RefCount MixCount;
720 
721  // Contexts created on this device
722  ATOMIC(ALCcontext*) ContextList;
723 
724  almtx_t BackendLock;
725  struct ALCbackend *Backend;
726 
727  ATOMIC(ALCdevice*) next;
728 };
729 
730 // Frequency was requested by the app or config file
731 #define DEVICE_FREQUENCY_REQUEST (1u<<1)
732 // Channel configuration was requested by the config file
733 #define DEVICE_CHANNELS_REQUEST (1u<<2)
734 // Sample type was requested by the config file
735 #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
736 
737 // Specifies if the DSP is paused at user request
738 #define DEVICE_PAUSED (1u<<30)
739 
740 // Specifies if the device is currently running
741 #define DEVICE_RUNNING (1u<<31)
742 
743 
744 /* Nanosecond resolution for the device clock time. */
745 #define DEVICE_CLOCK_RES U64(1000000000)
746 
747 
748 /* Must be less than 15 characters (16 including terminating null) for
749  * compatibility with pthread_setname_np limitations. */
750 #define MIXER_THREAD_NAME "alsoft-mixer"
751 
752 #define RECORD_THREAD_NAME "alsoft-record"
753 
754 
755 enum {
756  /* End event thread processing. */
757  EventType_KillThread = 0,
758 
759  /* User event types. */
760  EventType_SourceStateChange = 1<<0,
761  EventType_BufferCompleted = 1<<1,
762  EventType_Error = 1<<2,
763  EventType_Performance = 1<<3,
764  EventType_Deprecated = 1<<4,
765  EventType_Disconnected = 1<<5,
766 
767  /* Internal events. */
768  EventType_ReleaseEffectState = 65536,
769 };
770 
771 typedef struct AsyncEvent {
772  unsigned int EnumType;
773  union {
774  char dummy;
775  struct {
776  ALenum type;
777  ALuint id;
778  ALuint param;
779  ALchar msg[1008];
780  } user;
781  struct ALeffectState *EffectState;
782  } u;
783 } AsyncEvent;
784 #define ASYNC_EVENT(t) { t, { 0 } }
785 
786 struct ALCcontext_struct {
787  RefCount ref;
788 
789  struct ALlistener *Listener;
790 
791  vector_SourceSubList SourceList;
792  ALuint NumSources;
793  almtx_t SourceLock;
794 
795  vector_ALeffectslotPtr EffectSlotList;
796  almtx_t EffectSlotLock;
797 
798  ATOMIC(ALenum) LastError;
799 
800  enum DistanceModel DistanceModel;
801  ALboolean SourceDistanceModel;
802 
803  ALfloat DopplerFactor;
804  ALfloat DopplerVelocity;
805  ALfloat SpeedOfSound;
806  ALfloat MetersPerUnit;
807 
808  ATOMIC_FLAG PropsClean;
809  ATOMIC(ALenum) DeferUpdates;
810 
811  almtx_t PropLock;
812 
813  /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
814  * indicates if updates are currently happening).
815  */
816  RefCount UpdateCount;
817  ATOMIC(ALenum) HoldUpdates;
818 
819  ALfloat GainBoost;
820 
821  ATOMIC(struct ALcontextProps*) Update;
822 
823  /* Linked lists of unused property containers, free to use for future
824  * updates.
825  */
826  ATOMIC(struct ALcontextProps*) FreeContextProps;
827  ATOMIC(struct ALlistenerProps*) FreeListenerProps;
828  ATOMIC(struct ALvoiceProps*) FreeVoiceProps;
829  ATOMIC(struct ALeffectslotProps*) FreeEffectslotProps;
830 
831  struct ALvoice **Voices;
832  ALsizei VoiceCount;
833  ALsizei MaxVoices;
834 
835  ATOMIC(struct ALeffectslotArray*) ActiveAuxSlots;
836 
837  althrd_t EventThread;
838  alsem_t EventSem;
839  struct ll_ringbuffer *AsyncEvents;
840  ATOMIC(ALbitfieldSOFT) EnabledEvts;
841  almtx_t EventCbLock;
842  ALEVENTPROCSOFT EventCb;
843  void *EventParam;
844 
845  /* Default effect slot */
846  struct ALeffectslot *DefaultSlot;
847 
848  ALCdevice *Device;
849  const ALCchar *ExtensionList;
850 
851  ATOMIC(ALCcontext*) next;
852 
853  /* Memory space used by the listener (and possibly default effect slot) */
854  alignas(16) ALCbyte _listener_mem[];
855 };
856 
857 ALCcontext *GetContextRef(void);
858 
859 void ALCcontext_DecRef(ALCcontext *context);
860 
861 void ALCcontext_DeferUpdates(ALCcontext *context);
862 void ALCcontext_ProcessUpdates(ALCcontext *context);
863 
864 void AllocateVoices(ALCcontext *context, ALsizei num_voices, ALsizei old_sends);
865 
866 
867 extern ALint RTPrioLevel;
868 void SetRTPriority(void);
869 
870 void SetDefaultChannelOrder(ALCdevice *device);
871 void SetDefaultWFXChannelOrder(ALCdevice *device);
872 
873 const ALCchar *DevFmtTypeString(enum DevFmtType type);
874 const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
875 
876 inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
877 {
878  ALint i;
879  for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
880  {
881  if(names[i] == chan)
882  return i;
883  }
884  return -1;
885 }
886 /**
887  * GetChannelIdxByName
888  *
889  * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
890  * doesn't exist.
891  */
892 inline ALint GetChannelIdxByName(const RealMixParams *real, enum Channel chan)
893 { return GetChannelIndex(real->ChannelName, chan); }
894 
895 
896 inline void LockBufferList(ALCdevice *device) { almtx_lock(&device->BufferLock); }
897 inline void UnlockBufferList(ALCdevice *device) { almtx_unlock(&device->BufferLock); }
898 
899 inline void LockEffectList(ALCdevice *device) { almtx_lock(&device->EffectLock); }
900 inline void UnlockEffectList(ALCdevice *device) { almtx_unlock(&device->EffectLock); }
901 
902 inline void LockFilterList(ALCdevice *device) { almtx_lock(&device->FilterLock); }
903 inline void UnlockFilterList(ALCdevice *device) { almtx_unlock(&device->FilterLock); }
904 
905 inline void LockEffectSlotList(ALCcontext *context)
906 { almtx_lock(&context->EffectSlotLock); }
907 inline void UnlockEffectSlotList(ALCcontext *context)
908 { almtx_unlock(&context->EffectSlotLock); }
909 
910 
911 int EventThread(void *arg);
912 
913 
914 vector_al_string SearchDataFiles(const char *match, const char *subdir);
915 
916 #ifdef __cplusplus
917 }
918 #endif
919 
920 #endif