alMain.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. /* Define a CTZ64 macro (count trailing zeros, for 64-bit integers). The result
  101. * is *UNDEFINED* if the value is 0.
  102. */
  103. #ifdef __GNUC__
  104. #if SIZEOF_LONG == 8
  105. #define CTZ64(x) __builtin_ctzl(x)
  106. #else
  107. #define CTZ64(x) __builtin_ctzll(x)
  108. #endif
  109. #elif defined(HAVE_BITSCANFORWARD64_INTRINSIC)
  110. inline int msvc64_ctz64(ALuint64 v)
  111. {
  112. unsigned long idx = 64;
  113. _BitScanForward64(&idx, v);
  114. return (int)idx;
  115. }
  116. #define CTZ64(x) msvc64_ctz64(x)
  117. #elif defined(HAVE_BITSCANFORWARD_INTRINSIC)
  118. inline int msvc_ctz64(ALuint64 v)
  119. {
  120. unsigned long idx = 64;
  121. if(!_BitScanForward(&idx, v&0xffffffff))
  122. {
  123. if(_BitScanForward(&idx, v>>32))
  124. idx += 32;
  125. }
  126. return (int)idx;
  127. }
  128. #define CTZ64(x) msvc_ctz64(x)
  129. #else
  130. /* There be black magics here. The popcnt64 method is derived from
  131. * https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
  132. * while the ctz-utilizing-popcnt algorithm is shown here
  133. * http://www.hackersdelight.org/hdcodetxt/ntz.c.txt
  134. * as the ntz2 variant. These likely aren't the most efficient methods, but
  135. * they're good enough if the GCC or MSVC intrinsics aren't available.
  136. */
  137. inline int fallback_popcnt64(ALuint64 v)
  138. {
  139. v = v - ((v >> 1) & U64(0x5555555555555555));
  140. v = (v & U64(0x3333333333333333)) + ((v >> 2) & U64(0x3333333333333333));
  141. v = (v + (v >> 4)) & U64(0x0f0f0f0f0f0f0f0f);
  142. return (int)((v * U64(0x0101010101010101)) >> 56);
  143. }
  144. inline int fallback_ctz64(ALuint64 value)
  145. {
  146. return fallback_popcnt64(~value & (value - 1));
  147. }
  148. #define CTZ64(x) fallback_ctz64(x)
  149. #endif
  150. static const union {
  151. ALuint u;
  152. ALubyte b[sizeof(ALuint)];
  153. } EndianTest = { 1 };
  154. #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
  155. #define COUNTOF(x) (sizeof(x) / sizeof(0[x]))
  156. struct ll_ringbuffer;
  157. struct Hrtf;
  158. struct HrtfEntry;
  159. struct DirectHrtfState;
  160. struct FrontStablizer;
  161. struct Compressor;
  162. struct ALCbackend;
  163. struct ALbuffer;
  164. struct ALeffect;
  165. struct ALfilter;
  166. struct ALsource;
  167. struct ALcontextProps;
  168. struct ALlistenerProps;
  169. struct ALvoiceProps;
  170. struct ALeffectslotProps;
  171. #define DEFAULT_OUTPUT_RATE (44100)
  172. #define MIN_OUTPUT_RATE (8000)
  173. /* Find the next power-of-2 for non-power-of-2 numbers. */
  174. inline ALuint NextPowerOf2(ALuint value)
  175. {
  176. if(value > 0)
  177. {
  178. value--;
  179. value |= value>>1;
  180. value |= value>>2;
  181. value |= value>>4;
  182. value |= value>>8;
  183. value |= value>>16;
  184. }
  185. return value+1;
  186. }
  187. /** Round up a value to the next multiple. */
  188. inline size_t RoundUp(size_t value, size_t r)
  189. {
  190. value += r-1;
  191. return value - (value%r);
  192. }
  193. /* Fast float-to-int conversion. No particular rounding mode is assumed; the
  194. * IEEE-754 default is round-to-nearest with ties-to-even, though an app could
  195. * change it on its own threads. On some systems, a truncating conversion may
  196. * always be the fastest method.
  197. */
  198. inline ALint fastf2i(ALfloat f)
  199. {
  200. #if defined(HAVE_INTRIN_H) && ((defined(_M_IX86_FP) && (_M_IX86_FP > 0)) || defined(_M_X64))
  201. return _mm_cvt_ss2si(_mm_set1_ps(f));
  202. #elif defined(_MSC_VER) && defined(_M_IX86_FP)
  203. ALint i;
  204. __asm fld f
  205. __asm fistp i
  206. return i;
  207. #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
  208. ALint i;
  209. #ifdef __SSE_MATH__
  210. __asm__("cvtss2si %1, %0" : "=r"(i) : "x"(f));
  211. #else
  212. __asm__("flds %1\n fistps %0" : "=m"(i) : "m"(f));
  213. #endif
  214. return i;
  215. /* On GCC when compiling with -fno-math-errno, lrintf can be inlined to
  216. * some simple instructions. Clang does not inline it, always generating a
  217. * libc call, while MSVC's implementation is horribly slow, so always fall
  218. * back to a normal integer conversion for them.
  219. */
  220. #elif defined(HAVE_LRINTF) && !defined(_MSC_VER) && !defined(__clang__)
  221. return lrintf(f);
  222. #else
  223. return (ALint)f;
  224. #endif
  225. }
  226. /* Converts float-to-int using standard behavior (truncation). */
  227. inline int float2int(float f)
  228. {
  229. /* TODO: Make a more efficient method for x87. */
  230. return (ALint)f;
  231. }
  232. enum DevProbe {
  233. ALL_DEVICE_PROBE,
  234. CAPTURE_DEVICE_PROBE
  235. };
  236. enum DistanceModel {
  237. InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
  238. LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
  239. ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
  240. InverseDistance = AL_INVERSE_DISTANCE,
  241. LinearDistance = AL_LINEAR_DISTANCE,
  242. ExponentDistance = AL_EXPONENT_DISTANCE,
  243. DisableDistance = AL_NONE,
  244. DefaultDistanceModel = InverseDistanceClamped
  245. };
  246. enum Channel {
  247. FrontLeft = 0,
  248. FrontRight,
  249. FrontCenter,
  250. LFE,
  251. BackLeft,
  252. BackRight,
  253. BackCenter,
  254. SideLeft,
  255. SideRight,
  256. UpperFrontLeft,
  257. UpperFrontRight,
  258. UpperBackLeft,
  259. UpperBackRight,
  260. LowerFrontLeft,
  261. LowerFrontRight,
  262. LowerBackLeft,
  263. LowerBackRight,
  264. Aux0,
  265. Aux1,
  266. Aux2,
  267. Aux3,
  268. Aux4,
  269. Aux5,
  270. Aux6,
  271. Aux7,
  272. Aux8,
  273. Aux9,
  274. Aux10,
  275. Aux11,
  276. Aux12,
  277. Aux13,
  278. Aux14,
  279. Aux15,
  280. InvalidChannel
  281. };
  282. /* Device formats */
  283. enum DevFmtType {
  284. DevFmtByte = ALC_BYTE_SOFT,
  285. DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
  286. DevFmtShort = ALC_SHORT_SOFT,
  287. DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
  288. DevFmtInt = ALC_INT_SOFT,
  289. DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
  290. DevFmtFloat = ALC_FLOAT_SOFT,
  291. DevFmtTypeDefault = DevFmtFloat
  292. };
  293. enum DevFmtChannels {
  294. DevFmtMono = ALC_MONO_SOFT,
  295. DevFmtStereo = ALC_STEREO_SOFT,
  296. DevFmtQuad = ALC_QUAD_SOFT,
  297. DevFmtX51 = ALC_5POINT1_SOFT,
  298. DevFmtX61 = ALC_6POINT1_SOFT,
  299. DevFmtX71 = ALC_7POINT1_SOFT,
  300. DevFmtAmbi3D = ALC_BFORMAT3D_SOFT,
  301. /* Similar to 5.1, except using rear channels instead of sides */
  302. DevFmtX51Rear = 0x80000000,
  303. DevFmtChannelsDefault = DevFmtStereo
  304. };
  305. #define MAX_OUTPUT_CHANNELS (16)
  306. ALsizei BytesFromDevFmt(enum DevFmtType type);
  307. ALsizei ChannelsFromDevFmt(enum DevFmtChannels chans, ALsizei ambiorder);
  308. inline ALsizei FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type, ALsizei ambiorder)
  309. {
  310. return ChannelsFromDevFmt(chans, ambiorder) * BytesFromDevFmt(type);
  311. }
  312. enum AmbiLayout {
  313. AmbiLayout_FuMa = ALC_FUMA_SOFT, /* FuMa channel order */
  314. AmbiLayout_ACN = ALC_ACN_SOFT, /* ACN channel order */
  315. AmbiLayout_Default = AmbiLayout_ACN
  316. };
  317. enum AmbiNorm {
  318. AmbiNorm_FuMa = ALC_FUMA_SOFT, /* FuMa normalization */
  319. AmbiNorm_SN3D = ALC_SN3D_SOFT, /* SN3D normalization */
  320. AmbiNorm_N3D = ALC_N3D_SOFT, /* N3D normalization */
  321. AmbiNorm_Default = AmbiNorm_SN3D
  322. };
  323. enum DeviceType {
  324. Playback,
  325. Capture,
  326. Loopback
  327. };
  328. enum RenderMode {
  329. NormalRender,
  330. StereoPair,
  331. HrtfRender
  332. };
  333. /* The maximum number of Ambisonics coefficients. For a given order (o), the
  334. * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
  335. * second-order has 9, third-order has 16, and fourth-order has 25.
  336. */
  337. #define MAX_AMBI_ORDER 3
  338. #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
  339. /* A bitmask of ambisonic channels with height information. If none of these
  340. * channels are used/needed, there's no height (e.g. with most surround sound
  341. * speaker setups). This only specifies up to 4th order, which is the highest
  342. * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
  343. * order). This is ACN ordering, with bit 0 being ACN 0, etc.
  344. */
  345. #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
  346. /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
  347. * representation. This is 2 per each order above zero-order, plus 1 for zero-
  348. * order. Or simply, o*2 + 1.
  349. */
  350. #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
  351. typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
  352. typedef struct BFChannelConfig {
  353. ALfloat Scale;
  354. ALsizei Index;
  355. } BFChannelConfig;
  356. typedef union AmbiConfig {
  357. /* Ambisonic coefficients for mixing to the dry buffer. */
  358. ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
  359. /* Coefficient channel mapping for mixing to the dry buffer. */
  360. BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
  361. } AmbiConfig;
  362. typedef struct BufferSubList {
  363. ALuint64 FreeMask;
  364. struct ALbuffer *Buffers; /* 64 */
  365. } BufferSubList;
  366. TYPEDEF_VECTOR(BufferSubList, vector_BufferSubList)
  367. typedef struct EffectSubList {
  368. ALuint64 FreeMask;
  369. struct ALeffect *Effects; /* 64 */
  370. } EffectSubList;
  371. TYPEDEF_VECTOR(EffectSubList, vector_EffectSubList)
  372. typedef struct FilterSubList {
  373. ALuint64 FreeMask;
  374. struct ALfilter *Filters; /* 64 */
  375. } FilterSubList;
  376. TYPEDEF_VECTOR(FilterSubList, vector_FilterSubList)
  377. typedef struct SourceSubList {
  378. ALuint64 FreeMask;
  379. struct ALsource *Sources; /* 64 */
  380. } SourceSubList;
  381. TYPEDEF_VECTOR(SourceSubList, vector_SourceSubList)
  382. /* Effect slots are rather large, and apps aren't likely to have more than one
  383. * or two (let alone 64), so hold them individually.
  384. */
  385. typedef struct ALeffectslot *ALeffectslotPtr;
  386. TYPEDEF_VECTOR(ALeffectslotPtr, vector_ALeffectslotPtr)
  387. typedef struct EnumeratedHrtf {
  388. al_string name;
  389. struct HrtfEntry *hrtf;
  390. } EnumeratedHrtf;
  391. TYPEDEF_VECTOR(EnumeratedHrtf, vector_EnumeratedHrtf)
  392. /* Maximum delay in samples for speaker distance compensation. */
  393. #define MAX_DELAY_LENGTH 1024
  394. typedef struct DistanceComp {
  395. ALfloat Gain;
  396. ALsizei Length; /* Valid range is [0...MAX_DELAY_LENGTH). */
  397. ALfloat *Buffer;
  398. } DistanceComp;
  399. /* Size for temporary storage of buffer data, in ALfloats. Larger values need
  400. * more memory, while smaller values may need more iterations. The value needs
  401. * to be a sensible size, however, as it constrains the max stepping value used
  402. * for mixing, as well as the maximum number of samples per mixing iteration.
  403. */
  404. #define BUFFERSIZE 2048
  405. typedef struct DryMixParams {
  406. AmbiConfig Ambi;
  407. /* Number of coefficients in each Ambi.Coeffs to mix together (4 for first-
  408. * order, 9 for second-order, etc). If the count is 0, Ambi.Map is used
  409. * instead to map each output to a coefficient index.
  410. */
  411. ALsizei CoeffCount;
  412. ALfloat (*Buffer)[BUFFERSIZE];
  413. ALsizei NumChannels;
  414. ALsizei NumChannelsPerOrder[MAX_AMBI_ORDER+1];
  415. } DryMixParams;
  416. typedef struct BFMixParams {
  417. AmbiConfig Ambi;
  418. /* Will only be 4 or 0. */
  419. ALsizei CoeffCount;
  420. ALfloat (*Buffer)[BUFFERSIZE];
  421. ALsizei NumChannels;
  422. } BFMixParams;
  423. typedef struct RealMixParams {
  424. enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
  425. ALfloat (*Buffer)[BUFFERSIZE];
  426. ALsizei NumChannels;
  427. } RealMixParams;
  428. typedef void (*POSTPROCESS)(ALCdevice *device, ALsizei SamplesToDo);
  429. struct ALCdevice_struct {
  430. RefCount ref;
  431. ATOMIC(ALenum) Connected;
  432. enum DeviceType Type;
  433. ALuint Frequency;
  434. ALuint UpdateSize;
  435. ALuint NumUpdates;
  436. enum DevFmtChannels FmtChans;
  437. enum DevFmtType FmtType;
  438. ALboolean IsHeadphones;
  439. ALsizei AmbiOrder;
  440. /* For DevFmtAmbi* output only, specifies the channel order and
  441. * normalization.
  442. */
  443. enum AmbiLayout AmbiLayout;
  444. enum AmbiNorm AmbiScale;
  445. al_string DeviceName;
  446. ATOMIC(ALCenum) LastError;
  447. // Maximum number of sources that can be created
  448. ALuint SourcesMax;
  449. // Maximum number of slots that can be created
  450. ALuint AuxiliaryEffectSlotMax;
  451. ALCuint NumMonoSources;
  452. ALCuint NumStereoSources;
  453. ALsizei NumAuxSends;
  454. // Map of Buffers for this device
  455. vector_BufferSubList BufferList;
  456. almtx_t BufferLock;
  457. // Map of Effects for this device
  458. vector_EffectSubList EffectList;
  459. almtx_t EffectLock;
  460. // Map of Filters for this device
  461. vector_FilterSubList FilterList;
  462. almtx_t FilterLock;
  463. POSTPROCESS PostProcess;
  464. /* HRTF state and info */
  465. struct DirectHrtfState *Hrtf;
  466. al_string HrtfName;
  467. struct Hrtf *HrtfHandle;
  468. vector_EnumeratedHrtf HrtfList;
  469. ALCenum HrtfStatus;
  470. /* UHJ encoder state */
  471. struct Uhj2Encoder *Uhj_Encoder;
  472. /* High quality Ambisonic decoder */
  473. struct BFormatDec *AmbiDecoder;
  474. /* Stereo-to-binaural filter */
  475. struct bs2b *Bs2b;
  476. /* First-order ambisonic upsampler for higher-order output */
  477. struct AmbiUpsampler *AmbiUp;
  478. /* Rendering mode. */
  479. enum RenderMode Render_Mode;
  480. // Device flags
  481. ALuint Flags;
  482. ALuint64 ClockBase;
  483. ALuint SamplesDone;
  484. /* Temp storage used for mixer processing. */
  485. alignas(16) ALfloat TempBuffer[4][BUFFERSIZE];
  486. /* The "dry" path corresponds to the main output. */
  487. DryMixParams Dry;
  488. /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
  489. BFMixParams FOAOut;
  490. /* "Real" output, which will be written to the device buffer. May alias the
  491. * dry buffer.
  492. */
  493. RealMixParams RealOut;
  494. struct FrontStablizer *Stablizer;
  495. struct Compressor *Limiter;
  496. /* The average speaker distance as determined by the ambdec configuration
  497. * (or alternatively, by the NFC-HOA reference delay). Only used for NFC.
  498. */
  499. ALfloat AvgSpeakerDist;
  500. /* Delay buffers used to compensate for speaker distances. */
  501. DistanceComp ChannelDelay[MAX_OUTPUT_CHANNELS];
  502. /* Dithering control. */
  503. ALfloat DitherDepth;
  504. ALuint DitherSeed;
  505. /* Running count of the mixer invocations, in 31.1 fixed point. This
  506. * actually increments *twice* when mixing, first at the start and then at
  507. * the end, so the bottom bit indicates if the device is currently mixing
  508. * and the upper bits indicates how many mixes have been done.
  509. */
  510. RefCount MixCount;
  511. // Contexts created on this device
  512. ATOMIC(ALCcontext*) ContextList;
  513. almtx_t BackendLock;
  514. struct ALCbackend *Backend;
  515. ATOMIC(ALCdevice*) next;
  516. };
  517. // Frequency was requested by the app or config file
  518. #define DEVICE_FREQUENCY_REQUEST (1u<<1)
  519. // Channel configuration was requested by the config file
  520. #define DEVICE_CHANNELS_REQUEST (1u<<2)
  521. // Sample type was requested by the config file
  522. #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
  523. // Specifies if the DSP is paused at user request
  524. #define DEVICE_PAUSED (1u<<30)
  525. // Specifies if the device is currently running
  526. #define DEVICE_RUNNING (1u<<31)
  527. /* Nanosecond resolution for the device clock time. */
  528. #define DEVICE_CLOCK_RES U64(1000000000)
  529. /* Must be less than 15 characters (16 including terminating null) for
  530. * compatibility with pthread_setname_np limitations. */
  531. #define MIXER_THREAD_NAME "alsoft-mixer"
  532. #define RECORD_THREAD_NAME "alsoft-record"
  533. enum {
  534. EventType_SourceStateChange = 1<<0,
  535. EventType_BufferCompleted = 1<<1,
  536. EventType_Error = 1<<2,
  537. EventType_Performance = 1<<3,
  538. EventType_Deprecated = 1<<4,
  539. EventType_Disconnected = 1<<5,
  540. };
  541. typedef struct AsyncEvent {
  542. unsigned int EnumType;
  543. ALenum Type;
  544. ALuint ObjectId;
  545. ALuint Param;
  546. ALchar Message[1008];
  547. } AsyncEvent;
  548. struct ALCcontext_struct {
  549. RefCount ref;
  550. struct ALlistener *Listener;
  551. vector_SourceSubList SourceList;
  552. ALuint NumSources;
  553. almtx_t SourceLock;
  554. vector_ALeffectslotPtr EffectSlotList;
  555. almtx_t EffectSlotLock;
  556. ATOMIC(ALenum) LastError;
  557. enum DistanceModel DistanceModel;
  558. ALboolean SourceDistanceModel;
  559. ALfloat DopplerFactor;
  560. ALfloat DopplerVelocity;
  561. ALfloat SpeedOfSound;
  562. ALfloat MetersPerUnit;
  563. ATOMIC_FLAG PropsClean;
  564. ATOMIC(ALenum) DeferUpdates;
  565. almtx_t PropLock;
  566. /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
  567. * indicates if updates are currently happening).
  568. */
  569. RefCount UpdateCount;
  570. ATOMIC(ALenum) HoldUpdates;
  571. ALfloat GainBoost;
  572. ATOMIC(struct ALcontextProps*) Update;
  573. /* Linked lists of unused property containers, free to use for future
  574. * updates.
  575. */
  576. ATOMIC(struct ALcontextProps*) FreeContextProps;
  577. ATOMIC(struct ALlistenerProps*) FreeListenerProps;
  578. ATOMIC(struct ALvoiceProps*) FreeVoiceProps;
  579. ATOMIC(struct ALeffectslotProps*) FreeEffectslotProps;
  580. struct ALvoice **Voices;
  581. ALsizei VoiceCount;
  582. ALsizei MaxVoices;
  583. ATOMIC(struct ALeffectslotArray*) ActiveAuxSlots;
  584. almtx_t EventThrdLock;
  585. althrd_t EventThread;
  586. alsem_t EventSem;
  587. struct ll_ringbuffer *AsyncEvents;
  588. ATOMIC(ALbitfieldSOFT) EnabledEvts;
  589. almtx_t EventCbLock;
  590. ALEVENTPROCSOFT EventCb;
  591. void *EventParam;
  592. /* Default effect slot */
  593. struct ALeffectslot *DefaultSlot;
  594. ALCdevice *Device;
  595. const ALCchar *ExtensionList;
  596. ATOMIC(ALCcontext*) next;
  597. /* Memory space used by the listener (and possibly default effect slot) */
  598. alignas(16) ALCbyte _listener_mem[];
  599. };
  600. ALCcontext *GetContextRef(void);
  601. void ALCcontext_DecRef(ALCcontext *context);
  602. void ALCcontext_DeferUpdates(ALCcontext *context);
  603. void ALCcontext_ProcessUpdates(ALCcontext *context);
  604. void AllocateVoices(ALCcontext *context, ALsizei num_voices, ALsizei old_sends);
  605. void AppendAllDevicesList(const ALCchar *name);
  606. void AppendCaptureDeviceList(const ALCchar *name);
  607. extern ALint RTPrioLevel;
  608. void SetRTPriority(void);
  609. void SetDefaultChannelOrder(ALCdevice *device);
  610. void SetDefaultWFXChannelOrder(ALCdevice *device);
  611. const ALCchar *DevFmtTypeString(enum DevFmtType type);
  612. const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
  613. inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
  614. {
  615. ALint i;
  616. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  617. {
  618. if(names[i] == chan)
  619. return i;
  620. }
  621. return -1;
  622. }
  623. /**
  624. * GetChannelIdxByName
  625. *
  626. * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
  627. * doesn't exist.
  628. */
  629. inline ALint GetChannelIdxByName(const RealMixParams *real, enum Channel chan)
  630. { return GetChannelIndex(real->ChannelName, chan); }
  631. inline void LockBufferList(ALCdevice *device) { almtx_lock(&device->BufferLock); }
  632. inline void UnlockBufferList(ALCdevice *device) { almtx_unlock(&device->BufferLock); }
  633. inline void LockEffectList(ALCdevice *device) { almtx_lock(&device->EffectLock); }
  634. inline void UnlockEffectList(ALCdevice *device) { almtx_unlock(&device->EffectLock); }
  635. inline void LockFilterList(ALCdevice *device) { almtx_lock(&device->FilterLock); }
  636. inline void UnlockFilterList(ALCdevice *device) { almtx_unlock(&device->FilterLock); }
  637. inline void LockEffectSlotList(ALCcontext *context)
  638. { almtx_lock(&context->EffectSlotLock); }
  639. inline void UnlockEffectSlotList(ALCcontext *context)
  640. { almtx_unlock(&context->EffectSlotLock); }
  641. vector_al_string SearchDataFiles(const char *match, const char *subdir);
  642. #ifdef __cplusplus
  643. }
  644. #endif
  645. #endif