alu.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef _ALU_H_
  2. #define _ALU_H_
  3. #include "alMain.h"
  4. #include <limits.h>
  5. #include <math.h>
  6. #ifdef HAVE_FLOAT_H
  7. #include <float.h>
  8. #endif
  9. #ifdef HAVE_IEEEFP_H
  10. #include <ieeefp.h>
  11. #endif
  12. #define F_PI (3.14159265358979323846f)
  13. #define F_PI_2 (1.57079632679489661923f)
  14. #define F_2PI (6.28318530717958647692f)
  15. #ifndef FLT_EPSILON
  16. #define FLT_EPSILON (1.19209290e-07f)
  17. #endif
  18. #define DEG2RAD(x) ((ALfloat)(x) * (F_PI/180.0f))
  19. #define RAD2DEG(x) ((ALfloat)(x) * (180.0f/F_PI))
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. struct ALsource;
  24. struct ALbuffer;
  25. struct DirectParams;
  26. struct SendParams;
  27. typedef void (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
  28. ALfloat *restrict dst, ALuint dstlen);
  29. typedef ALvoid (*DryMixerFunc)(const struct DirectParams *params,
  30. const ALfloat *restrict data, ALuint srcchan,
  31. ALuint OutPos, ALuint SamplesToDo,
  32. ALuint BufferSize);
  33. typedef ALvoid (*WetMixerFunc)(const struct SendParams *params,
  34. const ALfloat *restrict data,
  35. ALuint OutPos, ALuint SamplesToDo,
  36. ALuint BufferSize);
  37. #define GAIN_SILENCE_THRESHOLD (0.00001f)
  38. #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
  39. #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
  40. #define FRACTIONBITS (14)
  41. #define FRACTIONONE (1<<FRACTIONBITS)
  42. #define FRACTIONMASK (FRACTIONONE-1)
  43. inline ALfloat minf(ALfloat a, ALfloat b)
  44. { return ((a > b) ? b : a); }
  45. inline ALfloat maxf(ALfloat a, ALfloat b)
  46. { return ((a > b) ? a : b); }
  47. inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
  48. { return minf(max, maxf(min, val)); }
  49. inline ALdouble mind(ALdouble a, ALdouble b)
  50. { return ((a > b) ? b : a); }
  51. inline ALdouble maxd(ALdouble a, ALdouble b)
  52. { return ((a > b) ? a : b); }
  53. inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
  54. { return mind(max, maxd(min, val)); }
  55. inline ALuint minu(ALuint a, ALuint b)
  56. { return ((a > b) ? b : a); }
  57. inline ALuint maxu(ALuint a, ALuint b)
  58. { return ((a > b) ? a : b); }
  59. inline ALuint clampu(ALuint val, ALuint min, ALuint max)
  60. { return minu(max, maxu(min, val)); }
  61. inline ALint mini(ALint a, ALint b)
  62. { return ((a > b) ? b : a); }
  63. inline ALint maxi(ALint a, ALint b)
  64. { return ((a > b) ? a : b); }
  65. inline ALint clampi(ALint val, ALint min, ALint max)
  66. { return mini(max, maxi(min, val)); }
  67. inline ALint64 mini64(ALint64 a, ALint64 b)
  68. { return ((a > b) ? b : a); }
  69. inline ALint64 maxi64(ALint64 a, ALint64 b)
  70. { return ((a > b) ? a : b); }
  71. inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
  72. { return mini64(max, maxi64(min, val)); }
  73. inline ALuint64 minu64(ALuint64 a, ALuint64 b)
  74. { return ((a > b) ? b : a); }
  75. inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
  76. { return ((a > b) ? a : b); }
  77. inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
  78. { return minu64(max, maxu64(min, val)); }
  79. inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
  80. {
  81. return val1 + (val2-val1)*mu;
  82. }
  83. inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
  84. {
  85. ALfloat mu2 = mu*mu;
  86. ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
  87. ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
  88. ALfloat a2 = -0.5f*val0 + 0.5f*val2;
  89. ALfloat a3 = val1;
  90. return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
  91. }
  92. ALvoid aluInitPanning(ALCdevice *Device);
  93. /**
  94. * ComputeAngleGains
  95. *
  96. * Sets channel gains based on a given source's angle and its half-width. The
  97. * angle and hwidth parameters are in radians.
  98. */
  99. void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
  100. /**
  101. * SetGains
  102. *
  103. * Helper to set the appropriate channels to the specified gain.
  104. */
  105. inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
  106. {
  107. ComputeAngleGains(device, 0.0f, F_PI, ingain, gains);
  108. }
  109. ALvoid CalcSourceParams(struct ALsource *ALSource, const ALCcontext *ALContext);
  110. ALvoid CalcNonAttnSourceParams(struct ALsource *ALSource, const ALCcontext *ALContext);
  111. ALvoid MixSource(struct ALsource *Source, ALCdevice *Device, ALuint SamplesToDo);
  112. ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
  113. /* Caller must lock the device. */
  114. ALvoid aluHandleDisconnect(ALCdevice *device);
  115. extern ALfloat ConeScale;
  116. extern ALfloat ZScale;
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif