alu.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #ifndef _ALU_H_
  2. #define _ALU_H_
  3. #include "AL/al.h"
  4. #include "AL/alc.h"
  5. #include "AL/alext.h"
  6. #include <limits.h>
  7. #include <math.h>
  8. #ifdef HAVE_FLOAT_H
  9. #include <float.h>
  10. /* HACK: Seems cross-compiling with MinGW includes the wrong float.h, which
  11. * doesn't define Windows' _controlfp and related macros */
  12. #if defined(__MINGW32__) && !defined(_RC_CHOP)
  13. /* Control word masks for unMask */
  14. #define _MCW_EM 0x0008001F /* Error masks */
  15. #define _MCW_IC 0x00040000 /* Infinity */
  16. #define _MCW_RC 0x00000300 /* Rounding */
  17. #define _MCW_PC 0x00030000 /* Precision */
  18. /* Control word values for unNew (use with related unMask above) */
  19. #define _EM_INVALID 0x00000010
  20. #define _EM_DENORMAL 0x00080000
  21. #define _EM_ZERODIVIDE 0x00000008
  22. #define _EM_OVERFLOW 0x00000004
  23. #define _EM_UNDERFLOW 0x00000002
  24. #define _EM_INEXACT 0x00000001
  25. #define _IC_AFFINE 0x00040000
  26. #define _IC_PROJECTIVE 0x00000000
  27. #define _RC_CHOP 0x00000300
  28. #define _RC_UP 0x00000200
  29. #define _RC_DOWN 0x00000100
  30. #define _RC_NEAR 0x00000000
  31. #define _PC_24 0x00020000
  32. #define _PC_53 0x00010000
  33. #define _PC_64 0x00000000
  34. _CRTIMP unsigned int __cdecl __MINGW_NOTHROW _controlfp (unsigned int unNew, unsigned int unMask);
  35. #endif
  36. #endif
  37. #ifdef HAVE_IEEEFP_H
  38. #include <ieeefp.h>
  39. #endif
  40. #define F_PI (3.14159265358979323846f) /* pi */
  41. #define F_PI_2 (1.57079632679489661923f) /* pi/2 */
  42. #ifdef HAVE_POWF
  43. #define aluPow(x,y) (powf((x),(y)))
  44. #else
  45. #define aluPow(x,y) ((ALfloat)pow((double)(x),(double)(y)))
  46. #endif
  47. #ifdef HAVE_SQRTF
  48. #define aluSqrt(x) (sqrtf((x)))
  49. #else
  50. #define aluSqrt(x) ((ALfloat)sqrt((double)(x)))
  51. #endif
  52. #ifdef HAVE_COSF
  53. #define aluCos(x) (cosf((x)))
  54. #else
  55. #define aluCos(x) ((ALfloat)cos((double)(x)))
  56. #endif
  57. #ifdef HAVE_SINF
  58. #define aluSin(x) (sinf((x)))
  59. #else
  60. #define aluSin(x) ((ALfloat)sin((double)(x)))
  61. #endif
  62. #ifdef HAVE_ACOSF
  63. #define aluAcos(x) (acosf((x)))
  64. #else
  65. #define aluAcos(x) ((ALfloat)acos((double)(x)))
  66. #endif
  67. #ifdef HAVE_ASINF
  68. #define aluAsin(x) (asinf((x)))
  69. #else
  70. #define aluAsin(x) ((ALfloat)asin((double)(x)))
  71. #endif
  72. #ifdef HAVE_ATANF
  73. #define aluAtan(x) (atanf((x)))
  74. #else
  75. #define aluAtan(x) ((ALfloat)atan((double)(x)))
  76. #endif
  77. #ifdef HAVE_ATAN2F
  78. #define aluAtan2(x,y) (atan2f((x),(y)))
  79. #else
  80. #define aluAtan2(x,y) ((ALfloat)atan2((double)(x),(double)(y)))
  81. #endif
  82. #ifdef HAVE_FABSF
  83. #define aluFabs(x) (fabsf((x)))
  84. #else
  85. #define aluFabs(x) ((ALfloat)fabs((double)(x)))
  86. #endif
  87. #ifdef HAVE_LOG10F
  88. #define aluLog10(x) (log10f((x)))
  89. #else
  90. #define aluLog10(x) ((ALfloat)log10((double)(x)))
  91. #endif
  92. #ifdef HAVE_FLOORF
  93. #define aluFloor(x) (floorf((x)))
  94. #else
  95. #define aluFloor(x) ((ALfloat)floor((double)(x)))
  96. #endif
  97. #define QUADRANT_NUM 128
  98. #define LUT_NUM (4 * QUADRANT_NUM)
  99. #ifdef __cplusplus
  100. extern "C" {
  101. #endif
  102. struct ALsource;
  103. struct ALbuffer;
  104. typedef ALvoid (*MixerFunc)(struct ALsource *self, ALCdevice *Device,
  105. const ALvoid *RESTRICT data,
  106. ALuint *DataPosInt, ALuint *DataPosFrac,
  107. ALuint OutPos, ALuint SamplesToDo,
  108. ALuint BufferSize);
  109. enum Resampler {
  110. PointResampler,
  111. LinearResampler,
  112. CubicResampler,
  113. ResamplerMax,
  114. };
  115. enum Channel {
  116. FRONT_LEFT = 0,
  117. FRONT_RIGHT,
  118. FRONT_CENTER,
  119. LFE,
  120. BACK_LEFT,
  121. BACK_RIGHT,
  122. BACK_CENTER,
  123. SIDE_LEFT,
  124. SIDE_RIGHT,
  125. MAXCHANNELS
  126. };
  127. enum DistanceModel {
  128. InverseDistanceClamped = AL_INVERSE_DISTANCE_CLAMPED,
  129. LinearDistanceClamped = AL_LINEAR_DISTANCE_CLAMPED,
  130. ExponentDistanceClamped = AL_EXPONENT_DISTANCE_CLAMPED,
  131. InverseDistance = AL_INVERSE_DISTANCE,
  132. LinearDistance = AL_LINEAR_DISTANCE,
  133. ExponentDistance = AL_EXPONENT_DISTANCE,
  134. DisableDistance = AL_NONE,
  135. DefaultDistanceModel = InverseDistanceClamped
  136. };
  137. #define BUFFERSIZE 4096
  138. #define FRACTIONBITS (14)
  139. #define FRACTIONONE (1<<FRACTIONBITS)
  140. #define FRACTIONMASK (FRACTIONONE-1)
  141. /* Size for temporary stack storage of buffer data. Must be a multiple of the
  142. * size of ALfloat, ie, 4. Larger values need more stack, while smaller values
  143. * may need more iterations. The value needs to be a sensible size, however, as
  144. * it constrains the max stepping value used for mixing.
  145. * The mixer requires being able to do two samplings per mixing loop. A 16KB
  146. * buffer can hold 512 sample frames for a 7.1 float buffer. With the cubic
  147. * resampler (which requires 3 padding sample frames), this limits the maximum
  148. * step to about 508. This means that buffer_freq*source_pitch cannot exceed
  149. * device_freq*508 for an 8-channel 32-bit buffer. */
  150. #ifndef STACK_DATA_SIZE
  151. #define STACK_DATA_SIZE 16384
  152. #endif
  153. static __inline ALfloat minf(ALfloat a, ALfloat b)
  154. { return ((a > b) ? b : a); }
  155. static __inline ALfloat maxf(ALfloat a, ALfloat b)
  156. { return ((a > b) ? a : b); }
  157. static __inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
  158. { return minf(max, maxf(min, val)); }
  159. static __inline ALuint minu(ALuint a, ALuint b)
  160. { return ((a > b) ? b : a); }
  161. static __inline ALuint maxu(ALuint a, ALuint b)
  162. { return ((a > b) ? a : b); }
  163. static __inline ALuint clampu(ALuint val, ALuint min, ALuint max)
  164. { return minu(max, maxu(min, val)); }
  165. static __inline ALint mini(ALint a, ALint b)
  166. { return ((a > b) ? b : a); }
  167. static __inline ALint maxi(ALint a, ALint b)
  168. { return ((a > b) ? a : b); }
  169. static __inline ALint clampi(ALint val, ALint min, ALint max)
  170. { return mini(max, maxi(min, val)); }
  171. static __inline ALint64 mini64(ALint64 a, ALint64 b)
  172. { return ((a > b) ? b : a); }
  173. static __inline ALint64 maxi64(ALint64 a, ALint64 b)
  174. { return ((a > b) ? a : b); }
  175. static __inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
  176. { return mini64(max, maxi64(min, val)); }
  177. static __inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
  178. {
  179. return val1 + (val2-val1)*mu;
  180. }
  181. static __inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
  182. {
  183. ALfloat mu2 = mu*mu;
  184. ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
  185. ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
  186. ALfloat a2 = -0.5f*val0 + 0.5f*val2;
  187. ALfloat a3 = val1;
  188. return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
  189. }
  190. static __inline int SetMixerFPUMode(void)
  191. {
  192. #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
  193. fpu_control_t fpuState, newState;
  194. _FPU_GETCW(fpuState);
  195. newState = fpuState&~(_FPU_EXTENDED|_FPU_DOUBLE|_FPU_SINGLE |
  196. _FPU_RC_NEAREST|_FPU_RC_DOWN|_FPU_RC_UP|_FPU_RC_ZERO);
  197. newState |= _FPU_SINGLE | _FPU_RC_ZERO;
  198. _FPU_SETCW(newState);
  199. #else
  200. int fpuState;
  201. #if defined(HAVE__CONTROLFP)
  202. fpuState = _controlfp(0, 0);
  203. (void)_controlfp(_RC_CHOP|_PC_24, _MCW_RC|_MCW_PC);
  204. #elif defined(HAVE_FESETROUND)
  205. fpuState = fegetround();
  206. fesetround(FE_TOWARDZERO);
  207. #endif
  208. #endif
  209. return fpuState;
  210. }
  211. static __inline void RestoreFPUMode(int state)
  212. {
  213. #if defined(_FPU_GETCW) && defined(_FPU_SETCW)
  214. fpu_control_t fpuState = state;
  215. _FPU_SETCW(fpuState);
  216. #elif defined(HAVE__CONTROLFP)
  217. _controlfp(state, _MCW_RC|_MCW_PC);
  218. #elif defined(HAVE_FESETROUND)
  219. fesetround(state);
  220. #endif
  221. }
  222. static __inline void aluCrossproduct(const ALfloat *inVector1, const ALfloat *inVector2, ALfloat *outVector)
  223. {
  224. outVector[0] = inVector1[1]*inVector2[2] - inVector1[2]*inVector2[1];
  225. outVector[1] = inVector1[2]*inVector2[0] - inVector1[0]*inVector2[2];
  226. outVector[2] = inVector1[0]*inVector2[1] - inVector1[1]*inVector2[0];
  227. }
  228. static __inline ALfloat aluDotproduct(const ALfloat *inVector1, const ALfloat *inVector2)
  229. {
  230. return inVector1[0]*inVector2[0] + inVector1[1]*inVector2[1] +
  231. inVector1[2]*inVector2[2];
  232. }
  233. static __inline void aluNormalize(ALfloat *inVector)
  234. {
  235. ALfloat length, inverse_length;
  236. length = aluSqrt(aluDotproduct(inVector, inVector));
  237. if(length > 0.0f)
  238. {
  239. inverse_length = 1.0f/length;
  240. inVector[0] *= inverse_length;
  241. inVector[1] *= inverse_length;
  242. inVector[2] *= inverse_length;
  243. }
  244. }
  245. ALvoid aluInitPanning(ALCdevice *Device);
  246. ALint aluCart2LUTpos(ALfloat re, ALfloat im);
  247. ALvoid CalcSourceParams(struct ALsource *ALSource, const ALCcontext *ALContext);
  248. ALvoid CalcNonAttnSourceParams(struct ALsource *ALSource, const ALCcontext *ALContext);
  249. MixerFunc SelectMixer(enum Resampler Resampler);
  250. MixerFunc SelectHrtfMixer(enum Resampler Resampler);
  251. ALvoid MixSource(struct ALsource *Source, ALCdevice *Device, ALuint SamplesToDo);
  252. ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
  253. ALvoid aluHandleDisconnect(ALCdevice *device);
  254. extern ALfloat ConeScale;
  255. extern ALfloat ZScale;
  256. #ifdef __cplusplus
  257. }
  258. #endif
  259. #endif