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