Add a flag to specify when the low-pass filter needs to apply
[openal-soft.git] / OpenAL32 / Include / alu.h
blobdd13a293b2419705b0129a5b6cde6b38f9beff6e
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,
47 typedef struct HrtfState {
48 alignas(16) ALfloat History[SRC_HISTORY_LENGTH];
49 alignas(16) ALfloat Values[HRIR_LENGTH][2];
50 } HrtfState;
52 typedef struct HrtfParams {
53 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
54 alignas(16) ALfloat CoeffStep[HRIR_LENGTH][2];
55 ALuint Delay[2];
56 ALint DelayStep[2];
57 } HrtfParams;
59 typedef struct DirectParams {
60 ALfloat (*OutBuffer)[BUFFERSIZE];
62 enum ActiveFilters Filters[MAX_INPUT_CHANNELS];
63 ALfilterState LpFilter[MAX_INPUT_CHANNELS];
65 /* If not 'moving', gain/coefficients are set directly without fading. */
66 ALboolean Moving;
67 /* Stepping counter for gain/coefficient fading. */
68 ALuint Counter;
69 /* History/coefficient offset. */
70 ALuint Offset;
72 union {
73 struct {
74 HrtfParams Params[MAX_INPUT_CHANNELS];
75 HrtfState State[MAX_INPUT_CHANNELS];
76 ALuint IrSize;
77 ALfloat Gain;
78 ALfloat Dir[3];
79 } Hrtf;
81 /* A mixing matrix. First subscript is the channel number of the input
82 * data (regardless of channel configuration) and the second is the
83 * channel target (eg. FrontLeft). Not used with HRTF. */
84 struct {
85 ALfloat Current[MAX_INPUT_CHANNELS][MaxChannels];
86 ALfloat Step[MAX_INPUT_CHANNELS][MaxChannels];
87 ALfloat Target[MAX_INPUT_CHANNELS][MaxChannels];
88 } Gains;
89 } Mix;
90 } DirectParams;
92 typedef struct SendParams {
93 ALfloat (*OutBuffer)[BUFFERSIZE];
95 enum ActiveFilters Filters[MAX_INPUT_CHANNELS];
96 ALfilterState LpFilter[MAX_INPUT_CHANNELS];
98 ALboolean Moving;
99 ALuint Counter;
101 /* Gain control, which applies to all input channels to a single (mono)
102 * output buffer. */
103 struct {
104 ALfloat Current;
105 ALfloat Step;
106 ALfloat Target;
107 } Gain;
108 } SendParams;
111 typedef void (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
112 ALfloat *restrict dst, ALuint dstlen);
114 typedef ALvoid (*DryMixerFunc)(struct DirectParams *params,
115 const ALfloat *restrict data, ALuint srcchan,
116 ALuint OutPos, ALuint BufferSize);
117 typedef ALvoid (*WetMixerFunc)(struct SendParams *params,
118 const ALfloat *restrict data,
119 ALuint OutPos, ALuint BufferSize);
122 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
124 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
125 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
127 #define FRACTIONBITS (14)
128 #define FRACTIONONE (1<<FRACTIONBITS)
129 #define FRACTIONMASK (FRACTIONONE-1)
132 inline ALfloat minf(ALfloat a, ALfloat b)
133 { return ((a > b) ? b : a); }
134 inline ALfloat maxf(ALfloat a, ALfloat b)
135 { return ((a > b) ? a : b); }
136 inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
137 { return minf(max, maxf(min, val)); }
139 inline ALdouble mind(ALdouble a, ALdouble b)
140 { return ((a > b) ? b : a); }
141 inline ALdouble maxd(ALdouble a, ALdouble b)
142 { return ((a > b) ? a : b); }
143 inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
144 { return mind(max, maxd(min, val)); }
146 inline ALuint minu(ALuint a, ALuint b)
147 { return ((a > b) ? b : a); }
148 inline ALuint maxu(ALuint a, ALuint b)
149 { return ((a > b) ? a : b); }
150 inline ALuint clampu(ALuint val, ALuint min, ALuint max)
151 { return minu(max, maxu(min, val)); }
153 inline ALint mini(ALint a, ALint b)
154 { return ((a > b) ? b : a); }
155 inline ALint maxi(ALint a, ALint b)
156 { return ((a > b) ? a : b); }
157 inline ALint clampi(ALint val, ALint min, ALint max)
158 { return mini(max, maxi(min, val)); }
160 inline ALint64 mini64(ALint64 a, ALint64 b)
161 { return ((a > b) ? b : a); }
162 inline ALint64 maxi64(ALint64 a, ALint64 b)
163 { return ((a > b) ? a : b); }
164 inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
165 { return mini64(max, maxi64(min, val)); }
167 inline ALuint64 minu64(ALuint64 a, ALuint64 b)
168 { return ((a > b) ? b : a); }
169 inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
170 { return ((a > b) ? a : b); }
171 inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
172 { return minu64(max, maxu64(min, val)); }
175 inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
177 return val1 + (val2-val1)*mu;
179 inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
181 ALfloat mu2 = mu*mu;
182 ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
183 ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
184 ALfloat a2 = -0.5f*val0 + 0.5f*val2;
185 ALfloat a3 = val1;
187 return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
191 ALvoid aluInitPanning(ALCdevice *Device);
194 * ComputeAngleGains
196 * Sets channel gains based on a given source's angle and its half-width. The
197 * angle and hwidth parameters are in radians.
199 void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
202 * SetGains
204 * Helper to set the appropriate channels to the specified gain.
206 inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
208 ComputeAngleGains(device, 0.0f, F_PI, ingain, gains);
212 ALvoid CalcSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
213 ALvoid CalcNonAttnSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
215 ALvoid MixSource(struct ALactivesource *src, ALCdevice *Device, ALuint SamplesToDo);
217 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
218 /* Caller must lock the device. */
219 ALvoid aluHandleDisconnect(ALCdevice *device);
221 extern ALfloat ConeScale;
222 extern ALfloat ZScale;
224 #ifdef __cplusplus
226 #endif
228 #endif