alMain.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. #ifndef AL_MAIN_H
  2. #define AL_MAIN_H
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <assert.h>
  7. #include <math.h>
  8. #include <limits.h>
  9. #ifdef HAVE_STRINGS_H
  10. #include <strings.h>
  11. #endif
  12. #ifdef HAVE_FENV_H
  13. #include <fenv.h>
  14. #endif
  15. #include "AL/al.h"
  16. #include "AL/alc.h"
  17. #include "AL/alext.h"
  18. #if defined(_WIN64)
  19. #define SZFMT "%I64u"
  20. #elif defined(_WIN32)
  21. #define SZFMT "%u"
  22. #else
  23. #define SZFMT "%zu"
  24. #endif
  25. #include "static_assert.h"
  26. #include "align.h"
  27. #include "atomic.h"
  28. #include "uintmap.h"
  29. #include "vector.h"
  30. #include "alstring.h"
  31. #include "almalloc.h"
  32. #include "threads.h"
  33. #include "hrtf.h"
  34. #ifndef ALC_SOFT_device_clock
  35. #define ALC_SOFT_device_clock 1
  36. typedef int64_t ALCint64SOFT;
  37. typedef uint64_t ALCuint64SOFT;
  38. #define ALC_DEVICE_CLOCK_SOFT 0x1600
  39. #define ALC_DEVICE_LATENCY_SOFT 0x1601
  40. #define ALC_DEVICE_CLOCK_LATENCY_SOFT 0x1602
  41. typedef void (ALC_APIENTRY*LPALCGETINTEGER64VSOFT)(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
  42. #ifdef AL_ALEXT_PROTOTYPES
  43. ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALsizei size, ALCint64SOFT *values);
  44. #endif
  45. #endif
  46. #ifndef AL_SOFT_buffer_samples2
  47. #define AL_SOFT_buffer_samples2 1
  48. /* Channel configurations */
  49. #define AL_MONO_SOFT 0x1500
  50. #define AL_STEREO_SOFT 0x1501
  51. #define AL_REAR_SOFT 0x1502
  52. #define AL_QUAD_SOFT 0x1503
  53. #define AL_5POINT1_SOFT 0x1504
  54. #define AL_6POINT1_SOFT 0x1505
  55. #define AL_7POINT1_SOFT 0x1506
  56. #define AL_BFORMAT2D_SOFT 0x1507
  57. #define AL_BFORMAT3D_SOFT 0x1508
  58. /* Sample types */
  59. #define AL_BYTE_SOFT 0x1400
  60. #define AL_UNSIGNED_BYTE_SOFT 0x1401
  61. #define AL_SHORT_SOFT 0x1402
  62. #define AL_UNSIGNED_SHORT_SOFT 0x1403
  63. #define AL_INT_SOFT 0x1404
  64. #define AL_UNSIGNED_INT_SOFT 0x1405
  65. #define AL_FLOAT_SOFT 0x1406
  66. #define AL_DOUBLE_SOFT 0x1407
  67. #define AL_BYTE3_SOFT 0x1408
  68. #define AL_UNSIGNED_BYTE3_SOFT 0x1409
  69. #define AL_MULAW_SOFT 0x140A
  70. /* Storage formats */
  71. #define AL_MONO8_SOFT 0x1100
  72. #define AL_MONO16_SOFT 0x1101
  73. #define AL_MONO32F_SOFT 0x10010
  74. #define AL_STEREO8_SOFT 0x1102
  75. #define AL_STEREO16_SOFT 0x1103
  76. #define AL_STEREO32F_SOFT 0x10011
  77. #define AL_QUAD8_SOFT 0x1204
  78. #define AL_QUAD16_SOFT 0x1205
  79. #define AL_QUAD32F_SOFT 0x1206
  80. #define AL_REAR8_SOFT 0x1207
  81. #define AL_REAR16_SOFT 0x1208
  82. #define AL_REAR32F_SOFT 0x1209
  83. #define AL_5POINT1_8_SOFT 0x120A
  84. #define AL_5POINT1_16_SOFT 0x120B
  85. #define AL_5POINT1_32F_SOFT 0x120C
  86. #define AL_6POINT1_8_SOFT 0x120D
  87. #define AL_6POINT1_16_SOFT 0x120E
  88. #define AL_6POINT1_32F_SOFT 0x120F
  89. #define AL_7POINT1_8_SOFT 0x1210
  90. #define AL_7POINT1_16_SOFT 0x1211
  91. #define AL_7POINT1_32F_SOFT 0x1212
  92. #define AL_BFORMAT2D_8_SOFT 0x20021
  93. #define AL_BFORMAT2D_16_SOFT 0x20022
  94. #define AL_BFORMAT2D_32F_SOFT 0x20023
  95. #define AL_BFORMAT3D_8_SOFT 0x20031
  96. #define AL_BFORMAT3D_16_SOFT 0x20032
  97. #define AL_BFORMAT3D_32F_SOFT 0x20033
  98. /* Buffer attributes */
  99. #define AL_INTERNAL_FORMAT_SOFT 0x2008
  100. #define AL_BYTE_LENGTH_SOFT 0x2009
  101. #define AL_SAMPLE_LENGTH_SOFT 0x200A
  102. #define AL_SEC_LENGTH_SOFT 0x200B
  103. #if 0
  104. typedef void (AL_APIENTRY*LPALBUFFERSAMPLESSOFT)(ALuint,ALuint,ALenum,ALsizei,ALenum,ALenum,const ALvoid*);
  105. typedef void (AL_APIENTRY*LPALGETBUFFERSAMPLESSOFT)(ALuint,ALsizei,ALsizei,ALenum,ALenum,ALvoid*);
  106. typedef ALboolean (AL_APIENTRY*LPALISBUFFERFORMATSUPPORTEDSOFT)(ALenum);
  107. #ifdef AL_ALEXT_PROTOTYPES
  108. AL_API void AL_APIENTRY alBufferSamplesSOFT(ALuint buffer, ALuint samplerate, ALenum internalformat, ALsizei samples, ALenum channels, ALenum type, const ALvoid *data);
  109. AL_API void AL_APIENTRY alGetBufferSamplesSOFT(ALuint buffer, ALsizei offset, ALsizei samples, ALenum channels, ALenum type, ALvoid *data);
  110. AL_API ALboolean AL_APIENTRY alIsBufferFormatSupportedSOFT(ALenum format);
  111. #endif
  112. #endif
  113. #endif
  114. #ifdef __GNUC__
  115. /* Because of a long-standing deficiency in C, you're not allowed to implicitly
  116. * cast a pointer-to-type-array to a pointer-to-const-type-array. For example,
  117. *
  118. * int (*ptr)[10];
  119. * const int (*cptr)[10] = ptr;
  120. *
  121. * is not allowed and most compilers will generate noisy warnings about
  122. * incompatible types, even though it just makes the array elements const.
  123. * Clang will allow it if you make the array type a typedef, like this:
  124. *
  125. * typedef int int10[10];
  126. * int10 *ptr;
  127. * const int10 *cptr = ptr;
  128. *
  129. * however GCC does not and still issues the incompatible type warning. The
  130. * "proper" way to fix it is to add an explicit cast for the constified type,
  131. * but that removes the vast majority of otherwise useful type-checking you'd
  132. * get, and runs the risk of improper casts if types are later changed. Leaving
  133. * it non-const can also be an issue if you use it as a function parameter, and
  134. * happen to have a const type as input (and also reduce the capabilities of
  135. * the compiler to better optimize the function).
  136. *
  137. * So to work around the problem, we use a macro. The macro first assigns the
  138. * incoming variable to the specified non-const type to ensure it's the correct
  139. * type, then casts the variable as the desired constified type. Very ugly, but
  140. * I'd rather not have hundreds of lines of warnings because I want to tell the
  141. * compiler that some array(s) can't be changed by the code, or have lots of
  142. * error-prone casts.
  143. */
  144. #define SAFE_CONST(T, var) __extension__({ \
  145. T _tmp = (var); \
  146. (const T)_tmp; \
  147. })
  148. #else
  149. /* Non-GNU-compatible compilers have to use a straight cast with no extra
  150. * checks, due to the lack of multi-statement expressions.
  151. */
  152. #define SAFE_CONST(T, var) ((const T)(var))
  153. #endif
  154. typedef ALint64SOFT ALint64;
  155. typedef ALuint64SOFT ALuint64;
  156. #ifndef U64
  157. #if defined(_MSC_VER)
  158. #define U64(x) ((ALuint64)(x##ui64))
  159. #elif SIZEOF_LONG == 8
  160. #define U64(x) ((ALuint64)(x##ul))
  161. #elif SIZEOF_LONG_LONG == 8
  162. #define U64(x) ((ALuint64)(x##ull))
  163. #endif
  164. #endif
  165. #ifndef UINT64_MAX
  166. #define UINT64_MAX U64(18446744073709551615)
  167. #endif
  168. #ifndef UNUSED
  169. #if defined(__cplusplus)
  170. #define UNUSED(x)
  171. #elif defined(__GNUC__)
  172. #define UNUSED(x) UNUSED_##x __attribute__((unused))
  173. #elif defined(__LCLINT__)
  174. #define UNUSED(x) /*@unused@*/ x
  175. #else
  176. #define UNUSED(x) x
  177. #endif
  178. #endif
  179. #ifdef __GNUC__
  180. #define DECL_FORMAT(x, y, z) __attribute__((format(x, (y), (z))))
  181. #else
  182. #define DECL_FORMAT(x, y, z)
  183. #endif
  184. #if defined(__GNUC__) && defined(__i386__)
  185. /* force_align_arg_pointer is required for proper function arguments aligning
  186. * when SSE code is used. Some systems (Windows, QNX) do not guarantee our
  187. * thread functions will be properly aligned on the stack, even though GCC may
  188. * generate code with the assumption that it is. */
  189. #define FORCE_ALIGN __attribute__((force_align_arg_pointer))
  190. #else
  191. #define FORCE_ALIGN
  192. #endif
  193. #ifdef HAVE_C99_VLA
  194. #define DECL_VLA(T, _name, _size) T _name[(_size)]
  195. #else
  196. #define DECL_VLA(T, _name, _size) T *_name = alloca((_size) * sizeof(T))
  197. #endif
  198. #ifndef PATH_MAX
  199. #ifdef MAX_PATH
  200. #define PATH_MAX MAX_PATH
  201. #else
  202. #define PATH_MAX 4096
  203. #endif
  204. #endif
  205. static const union {
  206. ALuint u;
  207. ALubyte b[sizeof(ALuint)];
  208. } EndianTest = { 1 };
  209. #define IS_LITTLE_ENDIAN (EndianTest.b[0] == 1)
  210. #define COUNTOF(x) (sizeof((x))/sizeof((x)[0]))
  211. #define DERIVE_FROM_TYPE(t) t t##_parent
  212. #define STATIC_CAST(to, obj) (&(obj)->to##_parent)
  213. #ifdef __GNUC__
  214. #define STATIC_UPCAST(to, from, obj) __extension__({ \
  215. static_assert(__builtin_types_compatible_p(from, __typeof(*(obj))), \
  216. "Invalid upcast object from type"); \
  217. (to*)((char*)(obj) - offsetof(to, from##_parent)); \
  218. })
  219. #else
  220. #define STATIC_UPCAST(to, from, obj) ((to*)((char*)(obj) - offsetof(to, from##_parent)))
  221. #endif
  222. #define DECLARE_FORWARD(T1, T2, rettype, func) \
  223. rettype T1##_##func(T1 *obj) \
  224. { return T2##_##func(STATIC_CAST(T2, obj)); }
  225. #define DECLARE_FORWARD1(T1, T2, rettype, func, argtype1) \
  226. rettype T1##_##func(T1 *obj, argtype1 a) \
  227. { return T2##_##func(STATIC_CAST(T2, obj), a); }
  228. #define DECLARE_FORWARD2(T1, T2, rettype, func, argtype1, argtype2) \
  229. rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b) \
  230. { return T2##_##func(STATIC_CAST(T2, obj), a, b); }
  231. #define DECLARE_FORWARD3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
  232. rettype T1##_##func(T1 *obj, argtype1 a, argtype2 b, argtype3 c) \
  233. { return T2##_##func(STATIC_CAST(T2, obj), a, b, c); }
  234. #define GET_VTABLE1(T1) (&(T1##_vtable))
  235. #define GET_VTABLE2(T1, T2) (&(T1##_##T2##_vtable))
  236. #define SET_VTABLE1(T1, obj) ((obj)->vtbl = GET_VTABLE1(T1))
  237. #define SET_VTABLE2(T1, T2, obj) (STATIC_CAST(T2, obj)->vtbl = GET_VTABLE2(T1, T2))
  238. #define DECLARE_THUNK(T1, T2, rettype, func) \
  239. static rettype T1##_##T2##_##func(T2 *obj) \
  240. { return T1##_##func(STATIC_UPCAST(T1, T2, obj)); }
  241. #define DECLARE_THUNK1(T1, T2, rettype, func, argtype1) \
  242. static rettype T1##_##T2##_##func(T2 *obj, argtype1 a) \
  243. { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a); }
  244. #define DECLARE_THUNK2(T1, T2, rettype, func, argtype1, argtype2) \
  245. static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b) \
  246. { return T1##_##func(STATIC_UPCAST(T1, T2, obj), a, b); }
  247. #define DECLARE_THUNK3(T1, T2, rettype, func, argtype1, argtype2, argtype3) \
  248. static rettype T1##_##T2##_##func(T2 *obj, argtype1 a, argtype2 b, argtype3 c) \
  249. { 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); }
  253. #define DECLARE_DEFAULT_ALLOCATORS(T) \
  254. static void* T##_New(size_t size) { return al_malloc(16, size); } \
  255. static void T##_Delete(void *ptr) { al_free(ptr); }
  256. /* Helper to extract an argument list for VCALL. Not used directly. */
  257. #define EXTRACT_VCALL_ARGS(...) __VA_ARGS__))
  258. /* Call a "virtual" method on an object, with arguments. */
  259. #define V(obj, func) ((obj)->vtbl->func((obj), EXTRACT_VCALL_ARGS
  260. /* Call a "virtual" method on an object, with no arguments. */
  261. #define V0(obj, func) ((obj)->vtbl->func((obj) EXTRACT_VCALL_ARGS
  262. #define DELETE_OBJ(obj) do { \
  263. if((obj) != NULL) \
  264. { \
  265. V0((obj),Destruct)(); \
  266. V0((obj),Delete)(); \
  267. } \
  268. } while(0)
  269. #define EXTRACT_NEW_ARGS(...) __VA_ARGS__); \
  270. } \
  271. } while(0)
  272. #define NEW_OBJ(_res, T) do { \
  273. _res = T##_New(sizeof(T)); \
  274. if(_res) \
  275. { \
  276. memset(_res, 0, sizeof(T)); \
  277. T##_Construct(_res, EXTRACT_NEW_ARGS
  278. #define NEW_OBJ0(_res, T) do { \
  279. _res = T##_New(sizeof(T)); \
  280. if(_res) \
  281. { \
  282. memset(_res, 0, sizeof(T)); \
  283. T##_Construct(_res EXTRACT_NEW_ARGS
  284. #ifdef __cplusplus
  285. extern "C" {
  286. #endif
  287. struct Hrtf;
  288. #define DEFAULT_OUTPUT_RATE (44100)
  289. #define MIN_OUTPUT_RATE (8000)
  290. /* Find the next power-of-2 for non-power-of-2 numbers. */
  291. inline ALuint NextPowerOf2(ALuint value)
  292. {
  293. if(value > 0)
  294. {
  295. value--;
  296. value |= value>>1;
  297. value |= value>>2;
  298. value |= value>>4;
  299. value |= value>>8;
  300. value |= value>>16;
  301. }
  302. return value+1;
  303. }
  304. /* Fast float-to-int conversion. Assumes the FPU is already in round-to-zero
  305. * mode. */
  306. inline ALint fastf2i(ALfloat f)
  307. {
  308. #ifdef HAVE_LRINTF
  309. return lrintf(f);
  310. #elif defined(_MSC_VER) && defined(_M_IX86)
  311. ALint i;
  312. __asm fld f
  313. __asm fistp i
  314. return i;
  315. #else
  316. return (ALint)f;
  317. #endif
  318. }
  319. /* Fast float-to-uint conversion. Assumes the FPU is already in round-to-zero
  320. * mode. */
  321. inline ALuint fastf2u(ALfloat f)
  322. { return fastf2i(f); }
  323. enum DevProbe {
  324. ALL_DEVICE_PROBE,
  325. CAPTURE_DEVICE_PROBE
  326. };
  327. typedef struct {
  328. ALCenum (*OpenPlayback)(ALCdevice*, const ALCchar*);
  329. void (*ClosePlayback)(ALCdevice*);
  330. ALCboolean (*ResetPlayback)(ALCdevice*);
  331. ALCboolean (*StartPlayback)(ALCdevice*);
  332. void (*StopPlayback)(ALCdevice*);
  333. ALCenum (*OpenCapture)(ALCdevice*, const ALCchar*);
  334. void (*CloseCapture)(ALCdevice*);
  335. void (*StartCapture)(ALCdevice*);
  336. void (*StopCapture)(ALCdevice*);
  337. ALCenum (*CaptureSamples)(ALCdevice*, void*, ALCuint);
  338. ALCuint (*AvailableSamples)(ALCdevice*);
  339. } BackendFuncs;
  340. ALCboolean alc_sndio_init(BackendFuncs *func_list);
  341. void alc_sndio_deinit(void);
  342. void alc_sndio_probe(enum DevProbe type);
  343. ALCboolean alc_ca_init(BackendFuncs *func_list);
  344. void alc_ca_deinit(void);
  345. void alc_ca_probe(enum DevProbe type);
  346. ALCboolean alc_opensl_init(BackendFuncs *func_list);
  347. void alc_opensl_deinit(void);
  348. void alc_opensl_probe(enum DevProbe type);
  349. ALCboolean alc_qsa_init(BackendFuncs *func_list);
  350. void alc_qsa_deinit(void);
  351. void alc_qsa_probe(enum DevProbe type);
  352. struct ALCbackend;
  353. enum DistanceModel {
  354. InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
  355. LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
  356. ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
  357. InverseDistance = AL_INVERSE_DISTANCE,
  358. LinearDistance = AL_LINEAR_DISTANCE,
  359. ExponentDistance = AL_EXPONENT_DISTANCE,
  360. DisableDistance = AL_NONE,
  361. DefaultDistanceModel = InverseDistanceClamped
  362. };
  363. enum Channel {
  364. FrontLeft = 0,
  365. FrontRight,
  366. FrontCenter,
  367. LFE,
  368. BackLeft,
  369. BackRight,
  370. BackCenter,
  371. SideLeft,
  372. SideRight,
  373. UpperFrontLeft,
  374. UpperFrontRight,
  375. UpperBackLeft,
  376. UpperBackRight,
  377. LowerFrontLeft,
  378. LowerFrontRight,
  379. LowerBackLeft,
  380. LowerBackRight,
  381. Aux0,
  382. Aux1,
  383. Aux2,
  384. Aux3,
  385. Aux4,
  386. Aux5,
  387. Aux6,
  388. Aux7,
  389. Aux8,
  390. Aux9,
  391. Aux10,
  392. Aux11,
  393. Aux12,
  394. Aux13,
  395. Aux14,
  396. Aux15,
  397. InvalidChannel
  398. };
  399. /* Device formats */
  400. enum DevFmtType {
  401. DevFmtByte = ALC_BYTE_SOFT,
  402. DevFmtUByte = ALC_UNSIGNED_BYTE_SOFT,
  403. DevFmtShort = ALC_SHORT_SOFT,
  404. DevFmtUShort = ALC_UNSIGNED_SHORT_SOFT,
  405. DevFmtInt = ALC_INT_SOFT,
  406. DevFmtUInt = ALC_UNSIGNED_INT_SOFT,
  407. DevFmtFloat = ALC_FLOAT_SOFT,
  408. DevFmtTypeDefault = DevFmtFloat
  409. };
  410. enum DevFmtChannels {
  411. DevFmtMono = ALC_MONO_SOFT,
  412. DevFmtStereo = ALC_STEREO_SOFT,
  413. DevFmtQuad = ALC_QUAD_SOFT,
  414. DevFmtX51 = ALC_5POINT1_SOFT,
  415. DevFmtX61 = ALC_6POINT1_SOFT,
  416. DevFmtX71 = ALC_7POINT1_SOFT,
  417. /* Similar to 5.1, except using rear channels instead of sides */
  418. DevFmtX51Rear = 0x80000000,
  419. /* Ambisonic formats should be kept together */
  420. DevFmtAmbi1,
  421. DevFmtAmbi2,
  422. DevFmtAmbi3,
  423. DevFmtChannelsDefault = DevFmtStereo
  424. };
  425. #define MAX_OUTPUT_CHANNELS (16)
  426. ALuint BytesFromDevFmt(enum DevFmtType type);
  427. ALuint ChannelsFromDevFmt(enum DevFmtChannels chans);
  428. inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type)
  429. {
  430. return ChannelsFromDevFmt(chans) * BytesFromDevFmt(type);
  431. }
  432. enum AmbiFormat {
  433. AmbiFormat_FuMa, /* FuMa channel order and normalization */
  434. AmbiFormat_ACN_SN3D, /* ACN channel order and SN3D normalization */
  435. AmbiFormat_ACN_N3D, /* ACN channel order and N3D normalization */
  436. AmbiFormat_Default = AmbiFormat_ACN_SN3D
  437. };
  438. extern const struct EffectList {
  439. const char *name;
  440. int type;
  441. const char *ename;
  442. ALenum val;
  443. } EffectList[];
  444. enum DeviceType {
  445. Playback,
  446. Capture,
  447. Loopback
  448. };
  449. enum RenderMode {
  450. NormalRender,
  451. StereoPair,
  452. HrtfRender
  453. };
  454. /* The maximum number of Ambisonics coefficients. For a given order (o), the
  455. * size needed will be (o+1)**2, thus zero-order has 1, first-order has 4,
  456. * second-order has 9, third-order has 16, and fourth-order has 25.
  457. */
  458. #define MAX_AMBI_ORDER 3
  459. #define MAX_AMBI_COEFFS ((MAX_AMBI_ORDER+1) * (MAX_AMBI_ORDER+1))
  460. /* A bitmask of ambisonic channels with height information. If none of these
  461. * channels are used/needed, there's no height (e.g. with most surround sound
  462. * speaker setups). This only specifies up to 4th order, which is the highest
  463. * order a 32-bit mask value can specify (a 64-bit mask could handle up to 7th
  464. * order). This is ACN ordering, with bit 0 being ACN 0, etc.
  465. */
  466. #define AMBI_PERIPHONIC_MASK (0xfe7ce4)
  467. /* The maximum number of Ambisonic coefficients for 2D (non-periphonic)
  468. * representation. This is 2 per each order above zero-order, plus 1 for zero-
  469. * order. Or simply, o*2 + 1.
  470. */
  471. #define MAX_AMBI2D_COEFFS (MAX_AMBI_ORDER*2 + 1)
  472. typedef ALfloat ChannelConfig[MAX_AMBI_COEFFS];
  473. typedef struct BFChannelConfig {
  474. ALfloat Scale;
  475. ALuint Index;
  476. } BFChannelConfig;
  477. typedef union AmbiConfig {
  478. /* Ambisonic coefficients for mixing to the dry buffer. */
  479. ChannelConfig Coeffs[MAX_OUTPUT_CHANNELS];
  480. /* Coefficient channel mapping for mixing to the dry buffer. */
  481. BFChannelConfig Map[MAX_OUTPUT_CHANNELS];
  482. } AmbiConfig;
  483. #define HRTF_HISTORY_BITS (6)
  484. #define HRTF_HISTORY_LENGTH (1<<HRTF_HISTORY_BITS)
  485. #define HRTF_HISTORY_MASK (HRTF_HISTORY_LENGTH-1)
  486. typedef struct HrtfState {
  487. alignas(16) ALfloat History[HRTF_HISTORY_LENGTH];
  488. alignas(16) ALfloat Values[HRIR_LENGTH][2];
  489. } HrtfState;
  490. typedef struct HrtfParams {
  491. alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
  492. ALuint Delay[2];
  493. } HrtfParams;
  494. /* Size for temporary storage of buffer data, in ALfloats. Larger values need
  495. * more memory, while smaller values may need more iterations. The value needs
  496. * to be a sensible size, however, as it constrains the max stepping value used
  497. * for mixing, as well as the maximum number of samples per mixing iteration.
  498. */
  499. #define BUFFERSIZE (2048u)
  500. struct ALCdevice_struct
  501. {
  502. RefCount ref;
  503. ALCboolean Connected;
  504. enum DeviceType Type;
  505. ALuint Frequency;
  506. ALuint UpdateSize;
  507. ALuint NumUpdates;
  508. enum DevFmtChannels FmtChans;
  509. enum DevFmtType FmtType;
  510. ALboolean IsHeadphones;
  511. /* For DevFmtAmbi* output only, specifies the channel order and
  512. * normalization.
  513. */
  514. enum AmbiFormat AmbiFmt;
  515. al_string DeviceName;
  516. ATOMIC(ALCenum) LastError;
  517. // Maximum number of sources that can be created
  518. ALuint SourcesMax;
  519. // Maximum number of slots that can be created
  520. ALuint AuxiliaryEffectSlotMax;
  521. ALCuint NumMonoSources;
  522. ALCuint NumStereoSources;
  523. ALuint NumAuxSends;
  524. // Map of Buffers for this device
  525. UIntMap BufferMap;
  526. // Map of Effects for this device
  527. UIntMap EffectMap;
  528. // Map of Filters for this device
  529. UIntMap FilterMap;
  530. /* HRTF filter tables */
  531. struct {
  532. vector_HrtfEntry List;
  533. al_string Name;
  534. ALCenum Status;
  535. const struct Hrtf *Handle;
  536. /* HRTF filter state for dry buffer content */
  537. alignas(16) ALfloat Values[4][HRIR_LENGTH][2];
  538. alignas(16) ALfloat Coeffs[4][HRIR_LENGTH][2];
  539. ALuint Offset;
  540. ALuint IrSize;
  541. } Hrtf;
  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. /* Temp storage used for each source when mixing. */
  557. alignas(16) ALfloat SourceData[BUFFERSIZE];
  558. alignas(16) ALfloat ResampledData[BUFFERSIZE];
  559. alignas(16) ALfloat FilteredData[BUFFERSIZE];
  560. /* The "dry" path corresponds to the main output. */
  561. struct {
  562. AmbiConfig Ambi;
  563. /* Number of coefficients in each Ambi.Coeffs to mix together (4 for
  564. * first-order, 9 for second-order, etc). If the count is 0, Ambi.Map
  565. * is used instead to map each output to a coefficient index.
  566. */
  567. ALuint CoeffCount;
  568. ALfloat (*Buffer)[BUFFERSIZE];
  569. ALuint NumChannels;
  570. } Dry;
  571. /* First-order ambisonics output, to be upsampled to the dry buffer if different. */
  572. struct {
  573. AmbiConfig Ambi;
  574. /* Will only be 4 or 0. */
  575. ALuint CoeffCount;
  576. ALfloat (*Buffer)[BUFFERSIZE];
  577. ALuint NumChannels;
  578. } FOAOut;
  579. /* "Real" output, which will be written to the device buffer. May alias the
  580. * dry buffer.
  581. */
  582. struct {
  583. enum Channel ChannelName[MAX_OUTPUT_CHANNELS];
  584. ALfloat (*Buffer)[BUFFERSIZE];
  585. ALuint NumChannels;
  586. } RealOut;
  587. /* Running count of the mixer invocations, in 31.1 fixed point. This
  588. * actually increments *twice* when mixing, first at the start and then at
  589. * the end, so the bottom bit indicates if the device is currently mixing
  590. * and the upper bits indicates how many mixes have been done.
  591. */
  592. RefCount MixCount;
  593. /* Default effect slot */
  594. struct ALeffectslot *DefaultSlot;
  595. // Contexts created on this device
  596. ATOMIC(ALCcontext*) ContextList;
  597. almtx_t BackendLock;
  598. struct ALCbackend *Backend;
  599. void *ExtraData; // For the backend's use
  600. ALCdevice *volatile next;
  601. /* Memory space used by the default slot (Playback devices only) */
  602. alignas(16) ALCbyte _slot_mem[];
  603. };
  604. // Frequency was requested by the app or config file
  605. #define DEVICE_FREQUENCY_REQUEST (1u<<1)
  606. // Channel configuration was requested by the config file
  607. #define DEVICE_CHANNELS_REQUEST (1u<<2)
  608. // Sample type was requested by the config file
  609. #define DEVICE_SAMPLE_TYPE_REQUEST (1u<<3)
  610. // Specifies if the DSP is paused at user request
  611. #define DEVICE_PAUSED (1u<<30)
  612. // Specifies if the device is currently running
  613. #define DEVICE_RUNNING (1u<<31)
  614. /* Nanosecond resolution for the device clock time. */
  615. #define DEVICE_CLOCK_RES U64(1000000000)
  616. /* Must be less than 15 characters (16 including terminating null) for
  617. * compatibility with pthread_setname_np limitations. */
  618. #define MIXER_THREAD_NAME "alsoft-mixer"
  619. #define RECORD_THREAD_NAME "alsoft-record"
  620. struct ALCcontext_struct {
  621. RefCount ref;
  622. struct ALlistener *Listener;
  623. UIntMap SourceMap;
  624. UIntMap EffectSlotMap;
  625. ATOMIC(ALenum) LastError;
  626. enum DistanceModel DistanceModel;
  627. ALboolean SourceDistanceModel;
  628. ALfloat DopplerFactor;
  629. ALfloat DopplerVelocity;
  630. ALfloat SpeedOfSound;
  631. ATOMIC(ALenum) DeferUpdates;
  632. RWLock PropLock;
  633. /* Counter for the pre-mixing updates, in 31.1 fixed point (lowest bit
  634. * indicates if updates are currently happening).
  635. */
  636. RefCount UpdateCount;
  637. ATOMIC(ALenum) HoldUpdates;
  638. ALfloat GainBoost;
  639. struct ALvoice *Voices;
  640. ALsizei VoiceCount;
  641. ALsizei MaxVoices;
  642. ATOMIC(struct ALeffectslot*) ActiveAuxSlotList;
  643. ALCdevice *Device;
  644. const ALCchar *ExtensionList;
  645. ALCcontext *volatile next;
  646. /* Memory space used by the listener */
  647. alignas(16) ALCbyte _listener_mem[];
  648. };
  649. ALCcontext *GetContextRef(void);
  650. void ALCcontext_IncRef(ALCcontext *context);
  651. void ALCcontext_DecRef(ALCcontext *context);
  652. void AppendAllDevicesList(const ALCchar *name);
  653. void AppendCaptureDeviceList(const ALCchar *name);
  654. void ALCdevice_Lock(ALCdevice *device);
  655. void ALCdevice_Unlock(ALCdevice *device);
  656. void ALCcontext_DeferUpdates(ALCcontext *context, ALenum type);
  657. void ALCcontext_ProcessUpdates(ALCcontext *context);
  658. inline void LockContext(ALCcontext *context)
  659. { ALCdevice_Lock(context->Device); }
  660. inline void UnlockContext(ALCcontext *context)
  661. { ALCdevice_Unlock(context->Device); }
  662. enum {
  663. DeferOff = AL_FALSE,
  664. DeferAll,
  665. DeferAllowPlay
  666. };
  667. typedef struct {
  668. #ifdef HAVE_FENV_H
  669. DERIVE_FROM_TYPE(fenv_t);
  670. #else
  671. int state;
  672. #endif
  673. #ifdef HAVE_SSE
  674. int sse_state;
  675. #endif
  676. } FPUCtl;
  677. void SetMixerFPUMode(FPUCtl *ctl);
  678. void RestoreFPUMode(const FPUCtl *ctl);
  679. typedef struct ll_ringbuffer ll_ringbuffer_t;
  680. typedef struct ll_ringbuffer_data {
  681. char *buf;
  682. size_t len;
  683. } ll_ringbuffer_data_t;
  684. ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz);
  685. void ll_ringbuffer_free(ll_ringbuffer_t *rb);
  686. void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
  687. void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec);
  688. size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt);
  689. size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt);
  690. void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt);
  691. size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb);
  692. int ll_ringbuffer_mlock(ll_ringbuffer_t *rb);
  693. void ll_ringbuffer_reset(ll_ringbuffer_t *rb);
  694. size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt);
  695. void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt);
  696. size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb);
  697. void ReadALConfig(void);
  698. void FreeALConfig(void);
  699. int ConfigValueExists(const char *devName, const char *blockName, const char *keyName);
  700. const char *GetConfigValue(const char *devName, const char *blockName, const char *keyName, const char *def);
  701. int GetConfigValueBool(const char *devName, const char *blockName, const char *keyName, int def);
  702. int ConfigValueStr(const char *devName, const char *blockName, const char *keyName, const char **ret);
  703. int ConfigValueInt(const char *devName, const char *blockName, const char *keyName, int *ret);
  704. int ConfigValueUInt(const char *devName, const char *blockName, const char *keyName, unsigned int *ret);
  705. int ConfigValueFloat(const char *devName, const char *blockName, const char *keyName, float *ret);
  706. int ConfigValueBool(const char *devName, const char *blockName, const char *keyName, int *ret);
  707. void SetRTPriority(void);
  708. void SetDefaultChannelOrder(ALCdevice *device);
  709. void SetDefaultWFXChannelOrder(ALCdevice *device);
  710. const ALCchar *DevFmtTypeString(enum DevFmtType type);
  711. const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans);
  712. /**
  713. * GetChannelIdxByName
  714. *
  715. * Returns the index for the given channel name (e.g. FrontCenter), or -1 if it
  716. * doesn't exist.
  717. */
  718. inline ALint GetChannelIndex(const enum Channel names[MAX_OUTPUT_CHANNELS], enum Channel chan)
  719. {
  720. ALint i;
  721. for(i = 0;i < MAX_OUTPUT_CHANNELS;i++)
  722. {
  723. if(names[i] == chan)
  724. return i;
  725. }
  726. return -1;
  727. }
  728. #define GetChannelIdxByName(x, c) GetChannelIndex((x).ChannelName, (c))
  729. extern FILE *LogFile;
  730. #if defined(__GNUC__) && !defined(_WIN32) && !defined(IN_IDE_PARSER)
  731. #define AL_PRINT(T, MSG, ...) fprintf(LogFile, "AL lib: %s %s: "MSG, T, __FUNCTION__ , ## __VA_ARGS__)
  732. #else
  733. void al_print(const char *type, const char *func, const char *fmt, ...) DECL_FORMAT(printf, 3,4);
  734. #define AL_PRINT(T, ...) al_print((T), __FUNCTION__, __VA_ARGS__)
  735. #endif
  736. enum LogLevel {
  737. NoLog,
  738. LogError,
  739. LogWarning,
  740. LogTrace,
  741. LogRef
  742. };
  743. extern enum LogLevel LogLevel;
  744. #define TRACEREF(...) do { \
  745. if(LogLevel >= LogRef) \
  746. AL_PRINT("(--)", __VA_ARGS__); \
  747. } while(0)
  748. #define TRACE(...) do { \
  749. if(LogLevel >= LogTrace) \
  750. AL_PRINT("(II)", __VA_ARGS__); \
  751. } while(0)
  752. #define WARN(...) do { \
  753. if(LogLevel >= LogWarning) \
  754. AL_PRINT("(WW)", __VA_ARGS__); \
  755. } while(0)
  756. #define ERR(...) do { \
  757. if(LogLevel >= LogError) \
  758. AL_PRINT("(EE)", __VA_ARGS__); \
  759. } while(0)
  760. extern ALint RTPrioLevel;
  761. extern ALuint CPUCapFlags;
  762. enum {
  763. CPU_CAP_SSE = 1<<0,
  764. CPU_CAP_SSE2 = 1<<1,
  765. CPU_CAP_SSE3 = 1<<2,
  766. CPU_CAP_SSE4_1 = 1<<3,
  767. CPU_CAP_NEON = 1<<4,
  768. };
  769. void FillCPUCaps(ALuint capfilter);
  770. vector_al_string SearchDataFiles(const char *match, const char *subdir);
  771. /* Small hack to use a pointer-to-array type as a normal argument type.
  772. * Shouldn't be used directly. */
  773. typedef ALfloat ALfloatBUFFERSIZE[BUFFERSIZE];
  774. #ifdef __cplusplus
  775. }
  776. #endif
  777. #endif