alu.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. #ifndef _ALU_H_
  2. #define _ALU_H_
  3. #include <limits.h>
  4. #include <math.h>
  5. #ifdef HAVE_FLOAT_H
  6. #include <float.h>
  7. #endif
  8. #ifdef HAVE_IEEEFP_H
  9. #include <ieeefp.h>
  10. #endif
  11. #include "alMain.h"
  12. #include "alBuffer.h"
  13. #include "alFilter.h"
  14. #include "alAuxEffectSlot.h"
  15. #include "hrtf.h"
  16. #include "align.h"
  17. #include "nfcfilter.h"
  18. #include "math_defs.h"
  19. #define MAX_PITCH (255)
  20. /* Maximum number of buffer samples before the current pos needed for resampling. */
  21. #define MAX_PRE_SAMPLES 12
  22. /* Maximum number of buffer samples after the current pos needed for resampling. */
  23. #define MAX_POST_SAMPLES 12
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. struct ALsource;
  28. struct ALbufferlistitem;
  29. struct ALvoice;
  30. struct ALeffectslot;
  31. #define DITHER_RNG_SEED 22222
  32. enum SpatializeMode {
  33. SpatializeOff = AL_FALSE,
  34. SpatializeOn = AL_TRUE,
  35. SpatializeAuto = AL_AUTO_SOFT
  36. };
  37. enum Resampler {
  38. PointResampler,
  39. LinearResampler,
  40. FIR4Resampler,
  41. BSincResampler,
  42. ResamplerMax = BSincResampler
  43. };
  44. extern enum Resampler ResamplerDefault;
  45. /* The number of distinct scale and phase intervals within the filter table. */
  46. #define BSINC_SCALE_BITS 4
  47. #define BSINC_SCALE_COUNT (1<<BSINC_SCALE_BITS)
  48. #define BSINC_PHASE_BITS 4
  49. #define BSINC_PHASE_COUNT (1<<BSINC_PHASE_BITS)
  50. /* Interpolator state. Kind of a misnomer since the interpolator itself is
  51. * stateless. This just keeps it from having to recompute scale-related
  52. * mappings for every sample.
  53. */
  54. typedef struct BsincState {
  55. ALfloat sf; /* Scale interpolation factor. */
  56. ALuint m; /* Coefficient count. */
  57. ALint l; /* Left coefficient offset. */
  58. struct {
  59. const ALfloat *filter; /* Filter coefficients. */
  60. const ALfloat *scDelta; /* Scale deltas. */
  61. const ALfloat *phDelta; /* Phase deltas. */
  62. const ALfloat *spDelta; /* Scale-phase deltas. */
  63. } coeffs[BSINC_PHASE_COUNT];
  64. } BsincState;
  65. typedef union InterpState {
  66. BsincState bsinc;
  67. } InterpState;
  68. ALboolean BsincPrepare(const ALuint increment, BsincState *state);
  69. typedef const ALfloat* (*ResamplerFunc)(const InterpState *state,
  70. const ALfloat *restrict src, ALsizei frac, ALint increment,
  71. ALfloat *restrict dst, ALsizei dstlen
  72. );
  73. typedef union aluVector {
  74. alignas(16) ALfloat v[4];
  75. } aluVector;
  76. inline void aluVectorSet(aluVector *vector, ALfloat x, ALfloat y, ALfloat z, ALfloat w)
  77. {
  78. vector->v[0] = x;
  79. vector->v[1] = y;
  80. vector->v[2] = z;
  81. vector->v[3] = w;
  82. }
  83. typedef union aluMatrixf {
  84. alignas(16) ALfloat m[4][4];
  85. } aluMatrixf;
  86. extern const aluMatrixf IdentityMatrixf;
  87. inline void aluMatrixfSetRow(aluMatrixf *matrix, ALuint row,
  88. ALfloat m0, ALfloat m1, ALfloat m2, ALfloat m3)
  89. {
  90. matrix->m[row][0] = m0;
  91. matrix->m[row][1] = m1;
  92. matrix->m[row][2] = m2;
  93. matrix->m[row][3] = m3;
  94. }
  95. inline void aluMatrixfSet(aluMatrixf *matrix, ALfloat m00, ALfloat m01, ALfloat m02, ALfloat m03,
  96. ALfloat m10, ALfloat m11, ALfloat m12, ALfloat m13,
  97. ALfloat m20, ALfloat m21, ALfloat m22, ALfloat m23,
  98. ALfloat m30, ALfloat m31, ALfloat m32, ALfloat m33)
  99. {
  100. aluMatrixfSetRow(matrix, 0, m00, m01, m02, m03);
  101. aluMatrixfSetRow(matrix, 1, m10, m11, m12, m13);
  102. aluMatrixfSetRow(matrix, 2, m20, m21, m22, m23);
  103. aluMatrixfSetRow(matrix, 3, m30, m31, m32, m33);
  104. }
  105. enum ActiveFilters {
  106. AF_None = 0,
  107. AF_LowPass = 1,
  108. AF_HighPass = 2,
  109. AF_BandPass = AF_LowPass | AF_HighPass
  110. };
  111. typedef struct MixHrtfParams {
  112. const ALfloat (*Coeffs)[2];
  113. ALsizei Delay[2];
  114. ALfloat Gain;
  115. ALfloat GainStep;
  116. } MixHrtfParams;
  117. typedef struct DirectParams {
  118. ALfilterState LowPass;
  119. ALfilterState HighPass;
  120. NfcFilter NFCtrlFilter[MAX_AMBI_ORDER];
  121. struct {
  122. HrtfParams Old;
  123. HrtfParams Target;
  124. HrtfState State;
  125. } Hrtf;
  126. struct {
  127. ALfloat Current[MAX_OUTPUT_CHANNELS];
  128. ALfloat Target[MAX_OUTPUT_CHANNELS];
  129. } Gains;
  130. } DirectParams;
  131. typedef struct SendParams {
  132. ALfilterState LowPass;
  133. ALfilterState HighPass;
  134. struct {
  135. ALfloat Current[MAX_OUTPUT_CHANNELS];
  136. ALfloat Target[MAX_OUTPUT_CHANNELS];
  137. } Gains;
  138. } SendParams;
  139. struct ALvoiceProps {
  140. ATOMIC(struct ALvoiceProps*) next;
  141. ALfloat Pitch;
  142. ALfloat Gain;
  143. ALfloat OuterGain;
  144. ALfloat MinGain;
  145. ALfloat MaxGain;
  146. ALfloat InnerAngle;
  147. ALfloat OuterAngle;
  148. ALfloat RefDistance;
  149. ALfloat MaxDistance;
  150. ALfloat RolloffFactor;
  151. ALfloat Position[3];
  152. ALfloat Velocity[3];
  153. ALfloat Direction[3];
  154. ALfloat Orientation[2][3];
  155. ALboolean HeadRelative;
  156. enum DistanceModel DistanceModel;
  157. enum Resampler Resampler;
  158. ALboolean DirectChannels;
  159. enum SpatializeMode SpatializeMode;
  160. ALboolean DryGainHFAuto;
  161. ALboolean WetGainAuto;
  162. ALboolean WetGainHFAuto;
  163. ALfloat OuterGainHF;
  164. ALfloat AirAbsorptionFactor;
  165. ALfloat RoomRolloffFactor;
  166. ALfloat DopplerFactor;
  167. ALfloat StereoPan[2];
  168. ALfloat Radius;
  169. /** Direct filter and auxiliary send info. */
  170. struct {
  171. ALfloat Gain;
  172. ALfloat GainHF;
  173. ALfloat HFReference;
  174. ALfloat GainLF;
  175. ALfloat LFReference;
  176. } Direct;
  177. struct {
  178. struct ALeffectslot *Slot;
  179. ALfloat Gain;
  180. ALfloat GainHF;
  181. ALfloat HFReference;
  182. ALfloat GainLF;
  183. ALfloat LFReference;
  184. } Send[];
  185. };
  186. /* If not 'fading', gain targets are used directly without fading. */
  187. #define VOICE_IS_FADING (1<<0)
  188. #define VOICE_HAS_HRTF (1<<1)
  189. #define VOICE_HAS_NFC (1<<2)
  190. typedef struct ALvoice {
  191. struct ALvoiceProps *Props;
  192. ATOMIC(struct ALvoiceProps*) Update;
  193. ATOMIC(struct ALvoiceProps*) FreeList;
  194. ATOMIC(struct ALsource*) Source;
  195. ATOMIC(bool) Playing;
  196. /**
  197. * Source offset in samples, relative to the currently playing buffer, NOT
  198. * the whole queue, and the fractional (fixed-point) offset to the next
  199. * sample.
  200. */
  201. ATOMIC(ALuint) position;
  202. ATOMIC(ALsizei) position_fraction;
  203. /* Current buffer queue item being played. */
  204. ATOMIC(struct ALbufferlistitem*) current_buffer;
  205. /* Buffer queue item to loop to at end of queue (will be NULL for non-
  206. * looping voices).
  207. */
  208. ATOMIC(struct ALbufferlistitem*) loop_buffer;
  209. /**
  210. * Number of channels and bytes-per-sample for the attached source's
  211. * buffer(s).
  212. */
  213. ALsizei NumChannels;
  214. ALsizei SampleSize;
  215. /** Current target parameters used for mixing. */
  216. ALint Step;
  217. ResamplerFunc Resampler;
  218. ALuint Flags;
  219. ALuint Offset; /* Number of output samples mixed since starting. */
  220. alignas(16) ALfloat PrevSamples[MAX_INPUT_CHANNELS][MAX_PRE_SAMPLES];
  221. InterpState ResampleState;
  222. struct {
  223. enum ActiveFilters FilterType;
  224. DirectParams Params[MAX_INPUT_CHANNELS];
  225. ALfloat (*Buffer)[BUFFERSIZE];
  226. ALsizei Channels;
  227. ALsizei ChannelsPerOrder[MAX_AMBI_ORDER+1];
  228. } Direct;
  229. struct {
  230. enum ActiveFilters FilterType;
  231. SendParams Params[MAX_INPUT_CHANNELS];
  232. ALfloat (*Buffer)[BUFFERSIZE];
  233. ALsizei Channels;
  234. } Send[];
  235. } ALvoice;
  236. void DeinitVoice(ALvoice *voice);
  237. typedef void (*MixerFunc)(const ALfloat *data, ALsizei OutChans,
  238. ALfloat (*restrict OutBuffer)[BUFFERSIZE], ALfloat *CurrentGains,
  239. const ALfloat *TargetGains, ALsizei Counter, ALsizei OutPos,
  240. ALsizei BufferSize);
  241. typedef void (*RowMixerFunc)(ALfloat *OutBuffer, const ALfloat *gains,
  242. const ALfloat (*restrict data)[BUFFERSIZE], ALsizei InChans,
  243. ALsizei InPos, ALsizei BufferSize);
  244. typedef void (*HrtfMixerFunc)(ALfloat *restrict LeftOut, ALfloat *restrict RightOut,
  245. const ALfloat *data, ALsizei Offset, ALsizei OutPos,
  246. const ALsizei IrSize, MixHrtfParams *hrtfparams,
  247. HrtfState *hrtfstate, ALsizei BufferSize);
  248. typedef void (*HrtfMixerBlendFunc)(ALfloat *restrict LeftOut, ALfloat *restrict RightOut,
  249. const ALfloat *data, ALsizei Offset, ALsizei OutPos,
  250. const ALsizei IrSize, const HrtfParams *oldparams,
  251. MixHrtfParams *newparams, HrtfState *hrtfstate,
  252. ALsizei BufferSize);
  253. typedef void (*HrtfDirectMixerFunc)(ALfloat *restrict LeftOut, ALfloat *restrict RightOut,
  254. const ALfloat *data, ALsizei Offset, const ALsizei IrSize,
  255. const ALfloat (*restrict Coeffs)[2],
  256. ALfloat (*restrict Values)[2], ALsizei BufferSize);
  257. #define GAIN_MIX_MAX (16.0f) /* +24dB */
  258. #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
  259. #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
  260. #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
  261. /* Target gain for the reverb decay feedback reaching the decay time. */
  262. #define REVERB_DECAY_GAIN (0.001f) /* -60 dB */
  263. #define FRACTIONBITS (12)
  264. #define FRACTIONONE (1<<FRACTIONBITS)
  265. #define FRACTIONMASK (FRACTIONONE-1)
  266. inline ALfloat minf(ALfloat a, ALfloat b)
  267. { return ((a > b) ? b : a); }
  268. inline ALfloat maxf(ALfloat a, ALfloat b)
  269. { return ((a > b) ? a : b); }
  270. inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
  271. { return minf(max, maxf(min, val)); }
  272. inline ALdouble mind(ALdouble a, ALdouble b)
  273. { return ((a > b) ? b : a); }
  274. inline ALdouble maxd(ALdouble a, ALdouble b)
  275. { return ((a > b) ? a : b); }
  276. inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
  277. { return mind(max, maxd(min, val)); }
  278. inline ALuint minu(ALuint a, ALuint b)
  279. { return ((a > b) ? b : a); }
  280. inline ALuint maxu(ALuint a, ALuint b)
  281. { return ((a > b) ? a : b); }
  282. inline ALuint clampu(ALuint val, ALuint min, ALuint max)
  283. { return minu(max, maxu(min, val)); }
  284. inline ALint mini(ALint a, ALint b)
  285. { return ((a > b) ? b : a); }
  286. inline ALint maxi(ALint a, ALint b)
  287. { return ((a > b) ? a : b); }
  288. inline ALint clampi(ALint val, ALint min, ALint max)
  289. { return mini(max, maxi(min, val)); }
  290. inline ALint64 mini64(ALint64 a, ALint64 b)
  291. { return ((a > b) ? b : a); }
  292. inline ALint64 maxi64(ALint64 a, ALint64 b)
  293. { return ((a > b) ? a : b); }
  294. inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
  295. { return mini64(max, maxi64(min, val)); }
  296. inline ALuint64 minu64(ALuint64 a, ALuint64 b)
  297. { return ((a > b) ? b : a); }
  298. inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
  299. { return ((a > b) ? a : b); }
  300. inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
  301. { return minu64(max, maxu64(min, val)); }
  302. extern alignas(16) const ALfloat bsincTab[18840];
  303. extern alignas(16) const ALfloat sinc4Tab[FRACTIONONE][4];
  304. inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
  305. {
  306. return val1 + (val2-val1)*mu;
  307. }
  308. inline ALfloat resample_fir4(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALsizei frac)
  309. {
  310. return sinc4Tab[frac][0]*val0 + sinc4Tab[frac][1]*val1 +
  311. sinc4Tab[frac][2]*val2 + sinc4Tab[frac][3]*val3;
  312. }
  313. enum HrtfRequestMode {
  314. Hrtf_Default = 0,
  315. Hrtf_Enable = 1,
  316. Hrtf_Disable = 2,
  317. };
  318. void aluInitMixer(void);
  319. MixerFunc SelectMixer(void);
  320. RowMixerFunc SelectRowMixer(void);
  321. ResamplerFunc SelectResampler(enum Resampler resampler);
  322. /* aluInitRenderer
  323. *
  324. * Set up the appropriate panning method and mixing method given the device
  325. * properties.
  326. */
  327. void aluInitRenderer(ALCdevice *device, ALint hrtf_id, enum HrtfRequestMode hrtf_appreq, enum HrtfRequestMode hrtf_userreq);
  328. void aluInitEffectPanning(struct ALeffectslot *slot);
  329. /**
  330. * CalcDirectionCoeffs
  331. *
  332. * Calculates ambisonic coefficients based on a direction vector. The vector
  333. * must be normalized (unit length), and the spread is the angular width of the
  334. * sound (0...tau).
  335. */
  336. void CalcDirectionCoeffs(const ALfloat dir[3], ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
  337. /**
  338. * CalcAngleCoeffs
  339. *
  340. * Calculates ambisonic coefficients based on azimuth and elevation. The
  341. * azimuth and elevation parameters are in radians, going right and up
  342. * respectively.
  343. */
  344. inline void CalcAngleCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS])
  345. {
  346. ALfloat dir[3] = {
  347. sinf(azimuth) * cosf(elevation),
  348. sinf(elevation),
  349. -cosf(azimuth) * cosf(elevation)
  350. };
  351. CalcDirectionCoeffs(dir, spread, coeffs);
  352. }
  353. /**
  354. * CalcAnglePairwiseCoeffs
  355. *
  356. * Calculates ambisonic coefficients based on azimuth and elevation. The
  357. * azimuth and elevation parameters are in radians, going right and up
  358. * respectively. This pairwise variant warps the result such that +30 azimuth
  359. * is full right, and -30 azimuth is full left.
  360. */
  361. void CalcAnglePairwiseCoeffs(ALfloat azimuth, ALfloat elevation, ALfloat spread, ALfloat coeffs[MAX_AMBI_COEFFS]);
  362. /**
  363. * ComputeAmbientGains
  364. *
  365. * Computes channel gains for ambient, omni-directional sounds.
  366. */
  367. #define ComputeAmbientGains(b, g, o) do { \
  368. if((b).CoeffCount > 0) \
  369. ComputeAmbientGainsMC((b).Ambi.Coeffs, (b).NumChannels, g, o); \
  370. else \
  371. ComputeAmbientGainsBF((b).Ambi.Map, (b).NumChannels, g, o); \
  372. } while (0)
  373. void ComputeAmbientGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
  374. void ComputeAmbientGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
  375. /**
  376. * ComputePanningGains
  377. *
  378. * Computes panning gains using the given channel decoder coefficients and the
  379. * pre-calculated direction or angle coefficients.
  380. */
  381. #define ComputePanningGains(b, c, g, o) do { \
  382. if((b).CoeffCount > 0) \
  383. ComputePanningGainsMC((b).Ambi.Coeffs, (b).NumChannels, (b).CoeffCount, c, g, o);\
  384. else \
  385. ComputePanningGainsBF((b).Ambi.Map, (b).NumChannels, c, g, o); \
  386. } while (0)
  387. void ComputePanningGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, ALsizei numcoeffs, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
  388. void ComputePanningGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat coeffs[MAX_AMBI_COEFFS], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
  389. /**
  390. * ComputeFirstOrderGains
  391. *
  392. * Sets channel gains for a first-order ambisonics input channel. The matrix is
  393. * a 1x4 'slice' of a transform matrix for the input channel, used to scale and
  394. * orient the sound samples.
  395. */
  396. #define ComputeFirstOrderGains(b, m, g, o) do { \
  397. if((b).CoeffCount > 0) \
  398. ComputeFirstOrderGainsMC((b).Ambi.Coeffs, (b).NumChannels, m, g, o); \
  399. else \
  400. ComputeFirstOrderGainsBF((b).Ambi.Map, (b).NumChannels, m, g, o); \
  401. } while (0)
  402. void ComputeFirstOrderGainsMC(const ChannelConfig *chancoeffs, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
  403. void ComputeFirstOrderGainsBF(const BFChannelConfig *chanmap, ALsizei numchans, const ALfloat mtx[4], ALfloat ingain, ALfloat gains[MAX_OUTPUT_CHANNELS]);
  404. ALboolean MixSource(struct ALvoice *voice, struct ALsource *Source, ALCdevice *Device, ALsizei SamplesToDo);
  405. void aluMixData(ALCdevice *device, ALvoid *OutBuffer, ALsizei NumSamples);
  406. /* Caller must lock the device. */
  407. void aluHandleDisconnect(ALCdevice *device);
  408. extern ALfloat ConeScale;
  409. extern ALfloat ZScale;
  410. #ifdef __cplusplus
  411. }
  412. #endif
  413. #endif