alMain.h 23 KB

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