Move an HRTF mixer parameter and shorten a couple variable names
[openal-soft.git] / OpenAL32 / Include / alu.h
blob254b15e3f6e6eb00d1326621de6570e2df297c8f
1 #ifndef _ALU_H_
2 #define _ALU_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
13 #include "alMain.h"
14 #include "alBuffer.h"
15 #include "alFilter.h"
17 #include "hrtf.h"
18 #include "align.h"
21 #define F_PI (3.14159265358979323846f)
22 #define F_PI_2 (1.57079632679489661923f)
23 #define F_2PI (6.28318530717958647692f)
25 #ifndef FLT_EPSILON
26 #define FLT_EPSILON (1.19209290e-07f)
27 #endif
29 #define DEG2RAD(x) ((ALfloat)(x) * (F_PI/180.0f))
30 #define RAD2DEG(x) ((ALfloat)(x) * (180.0f/F_PI))
33 #define SRC_HISTORY_BITS (6)
34 #define SRC_HISTORY_LENGTH (1<<SRC_HISTORY_BITS)
35 #define SRC_HISTORY_MASK (SRC_HISTORY_LENGTH-1)
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 enum ActiveFilters {
43 AF_None = 0,
44 AF_LowPass = 1,
45 AF_HighPass = 2,
46 AF_BandPass = AF_LowPass | AF_HighPass
50 typedef struct HrtfState {
51 alignas(16) ALfloat History[SRC_HISTORY_LENGTH];
52 alignas(16) ALfloat Values[HRIR_LENGTH][2];
53 } HrtfState;
55 typedef struct HrtfParams {
56 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
57 alignas(16) ALfloat CoeffStep[HRIR_LENGTH][2];
58 ALuint Delay[2];
59 ALint DelayStep[2];
60 } HrtfParams;
63 typedef struct MixGains {
64 ALfloat Current[MaxChannels];
65 ALfloat Step[MaxChannels];
66 ALfloat Target[MaxChannels];
67 } MixGains;
69 typedef struct MixGainMono {
70 ALfloat Current;
71 ALfloat Step;
72 ALfloat Target;
73 } MixGainMono;
76 typedef struct DirectParams {
77 ALfloat (*OutBuffer)[BUFFERSIZE];
79 enum ActiveFilters Filters[MAX_INPUT_CHANNELS];
80 ALfilterState LpFilter[MAX_INPUT_CHANNELS];
81 ALfilterState HpFilter[MAX_INPUT_CHANNELS];
83 /* If not 'moving', gain/coefficients are set directly without fading. */
84 ALboolean Moving;
85 /* Stepping counter for gain/coefficient fading. */
86 ALuint Counter;
87 /* History/coefficient offset. */
88 ALuint Offset;
90 union {
91 struct {
92 HrtfParams Params[MAX_INPUT_CHANNELS];
93 HrtfState State[MAX_INPUT_CHANNELS];
94 ALuint IrSize;
95 ALfloat Gain;
96 ALfloat Dir[3];
97 } Hrtf;
99 MixGains Gains[MAX_INPUT_CHANNELS];
100 } Mix;
101 } DirectParams;
103 typedef struct SendParams {
104 ALfloat (*OutBuffer)[BUFFERSIZE];
106 enum ActiveFilters Filters[MAX_INPUT_CHANNELS];
107 ALfilterState LpFilter[MAX_INPUT_CHANNELS];
108 ALfilterState HpFilter[MAX_INPUT_CHANNELS];
110 ALboolean Moving;
111 ALuint Counter;
113 /* Gain control, which applies to all input channels to a single (mono)
114 * output buffer. */
115 MixGainMono Gain;
116 } SendParams;
119 typedef void (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
120 ALfloat *restrict dst, ALuint dstlen);
122 typedef void (*DryMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
123 MixGains *Gains, ALuint Counter, ALuint OutPos,
124 ALuint BufferSize);
125 typedef void (*HrtfMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
126 ALuint Counter, ALuint Offset, ALuint OutPos,
127 const ALuint IrSize, const HrtfParams *hrtfparams,
128 HrtfState *hrtfstate, ALuint BufferSize);
129 typedef void (*WetMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
130 MixGainMono *Gain, ALuint Counter, ALuint OutPos,
131 ALuint BufferSize);
134 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
136 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
137 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
139 #define FRACTIONBITS (14)
140 #define FRACTIONONE (1<<FRACTIONBITS)
141 #define FRACTIONMASK (FRACTIONONE-1)
144 inline ALfloat minf(ALfloat a, ALfloat b)
145 { return ((a > b) ? b : a); }
146 inline ALfloat maxf(ALfloat a, ALfloat b)
147 { return ((a > b) ? a : b); }
148 inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
149 { return minf(max, maxf(min, val)); }
151 inline ALdouble mind(ALdouble a, ALdouble b)
152 { return ((a > b) ? b : a); }
153 inline ALdouble maxd(ALdouble a, ALdouble b)
154 { return ((a > b) ? a : b); }
155 inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
156 { return mind(max, maxd(min, val)); }
158 inline ALuint minu(ALuint a, ALuint b)
159 { return ((a > b) ? b : a); }
160 inline ALuint maxu(ALuint a, ALuint b)
161 { return ((a > b) ? a : b); }
162 inline ALuint clampu(ALuint val, ALuint min, ALuint max)
163 { return minu(max, maxu(min, val)); }
165 inline ALint mini(ALint a, ALint b)
166 { return ((a > b) ? b : a); }
167 inline ALint maxi(ALint a, ALint b)
168 { return ((a > b) ? a : b); }
169 inline ALint clampi(ALint val, ALint min, ALint max)
170 { return mini(max, maxi(min, val)); }
172 inline ALint64 mini64(ALint64 a, ALint64 b)
173 { return ((a > b) ? b : a); }
174 inline ALint64 maxi64(ALint64 a, ALint64 b)
175 { return ((a > b) ? a : b); }
176 inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
177 { return mini64(max, maxi64(min, val)); }
179 inline ALuint64 minu64(ALuint64 a, ALuint64 b)
180 { return ((a > b) ? b : a); }
181 inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
182 { return ((a > b) ? a : b); }
183 inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
184 { return minu64(max, maxu64(min, val)); }
187 inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
189 return val1 + (val2-val1)*mu;
191 inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
193 ALfloat mu2 = mu*mu;
194 ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
195 ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
196 ALfloat a2 = -0.5f*val0 + 0.5f*val2;
197 ALfloat a3 = val1;
199 return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
203 ALvoid aluInitPanning(ALCdevice *Device);
206 * ComputeAngleGains
208 * Sets channel gains based on a given source's angle and its half-width. The
209 * angle and hwidth parameters are in radians.
211 void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
214 * SetGains
216 * Helper to set the appropriate channels to the specified gain.
218 inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
220 ComputeAngleGains(device, 0.0f, F_PI, ingain, gains);
224 ALvoid CalcSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
225 ALvoid CalcNonAttnSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
227 ALvoid MixSource(struct ALactivesource *src, ALCdevice *Device, ALuint SamplesToDo);
229 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
230 /* Caller must lock the device. */
231 ALvoid aluHandleDisconnect(ALCdevice *device);
233 extern ALfloat ConeScale;
234 extern ALfloat ZScale;
236 #ifdef __cplusplus
238 #endif
240 #endif