Don't pass the DirectParams to the dry-path mixer
[openal-soft.git] / OpenAL32 / Include / alu.h
blob432e9bd396d15803c8b75a11e56bfce8fc296270
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;
70 typedef struct DirectParams {
71 ALfloat (*OutBuffer)[BUFFERSIZE];
73 enum ActiveFilters Filters[MAX_INPUT_CHANNELS];
74 ALfilterState LpFilter[MAX_INPUT_CHANNELS];
75 ALfilterState HpFilter[MAX_INPUT_CHANNELS];
77 /* If not 'moving', gain/coefficients are set directly without fading. */
78 ALboolean Moving;
79 /* Stepping counter for gain/coefficient fading. */
80 ALuint Counter;
81 /* History/coefficient offset. */
82 ALuint Offset;
84 union {
85 struct {
86 HrtfParams Params[MAX_INPUT_CHANNELS];
87 HrtfState State[MAX_INPUT_CHANNELS];
88 ALuint IrSize;
89 ALfloat Gain;
90 ALfloat Dir[3];
91 } Hrtf;
93 MixGains Gains[MAX_INPUT_CHANNELS];
94 } Mix;
95 } DirectParams;
97 typedef struct SendParams {
98 ALfloat (*OutBuffer)[BUFFERSIZE];
100 enum ActiveFilters Filters[MAX_INPUT_CHANNELS];
101 ALfilterState LpFilter[MAX_INPUT_CHANNELS];
102 ALfilterState HpFilter[MAX_INPUT_CHANNELS];
104 ALboolean Moving;
105 ALuint Counter;
107 /* Gain control, which applies to all input channels to a single (mono)
108 * output buffer. */
109 struct {
110 ALfloat Current;
111 ALfloat Step;
112 ALfloat Target;
113 } Gain;
114 } SendParams;
117 typedef void (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
118 ALfloat *restrict dst, ALuint dstlen);
120 typedef ALvoid (*DryMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
121 MixGains *Gains, ALuint Counter, ALuint OutPos,
122 ALuint BufferSize);
123 typedef void (*HrtfMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
124 ALuint Counter, ALuint Offset, const ALuint IrSize,
125 const HrtfParams *hrtfparams, HrtfState *hrtfstate,
126 ALuint OutPos, ALuint BufferSize);
127 typedef ALvoid (*WetMixerFunc)(struct SendParams *params,
128 const ALfloat *restrict data,
129 ALuint OutPos, ALuint BufferSize);
132 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
134 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
135 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
137 #define FRACTIONBITS (14)
138 #define FRACTIONONE (1<<FRACTIONBITS)
139 #define FRACTIONMASK (FRACTIONONE-1)
142 inline ALfloat minf(ALfloat a, ALfloat b)
143 { return ((a > b) ? b : a); }
144 inline ALfloat maxf(ALfloat a, ALfloat b)
145 { return ((a > b) ? a : b); }
146 inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
147 { return minf(max, maxf(min, val)); }
149 inline ALdouble mind(ALdouble a, ALdouble b)
150 { return ((a > b) ? b : a); }
151 inline ALdouble maxd(ALdouble a, ALdouble b)
152 { return ((a > b) ? a : b); }
153 inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
154 { return mind(max, maxd(min, val)); }
156 inline ALuint minu(ALuint a, ALuint b)
157 { return ((a > b) ? b : a); }
158 inline ALuint maxu(ALuint a, ALuint b)
159 { return ((a > b) ? a : b); }
160 inline ALuint clampu(ALuint val, ALuint min, ALuint max)
161 { return minu(max, maxu(min, val)); }
163 inline ALint mini(ALint a, ALint b)
164 { return ((a > b) ? b : a); }
165 inline ALint maxi(ALint a, ALint b)
166 { return ((a > b) ? a : b); }
167 inline ALint clampi(ALint val, ALint min, ALint max)
168 { return mini(max, maxi(min, val)); }
170 inline ALint64 mini64(ALint64 a, ALint64 b)
171 { return ((a > b) ? b : a); }
172 inline ALint64 maxi64(ALint64 a, ALint64 b)
173 { return ((a > b) ? a : b); }
174 inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
175 { return mini64(max, maxi64(min, val)); }
177 inline ALuint64 minu64(ALuint64 a, ALuint64 b)
178 { return ((a > b) ? b : a); }
179 inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
180 { return ((a > b) ? a : b); }
181 inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
182 { return minu64(max, maxu64(min, val)); }
185 inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
187 return val1 + (val2-val1)*mu;
189 inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
191 ALfloat mu2 = mu*mu;
192 ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
193 ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
194 ALfloat a2 = -0.5f*val0 + 0.5f*val2;
195 ALfloat a3 = val1;
197 return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
201 ALvoid aluInitPanning(ALCdevice *Device);
204 * ComputeAngleGains
206 * Sets channel gains based on a given source's angle and its half-width. The
207 * angle and hwidth parameters are in radians.
209 void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
212 * SetGains
214 * Helper to set the appropriate channels to the specified gain.
216 inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
218 ComputeAngleGains(device, 0.0f, F_PI, ingain, gains);
222 ALvoid CalcSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
223 ALvoid CalcNonAttnSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
225 ALvoid MixSource(struct ALactivesource *src, ALCdevice *Device, ALuint SamplesToDo);
227 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
228 /* Caller must lock the device. */
229 ALvoid aluHandleDisconnect(ALCdevice *device);
231 extern ALfloat ConeScale;
232 extern ALfloat ZScale;
234 #ifdef __cplusplus
236 #endif
238 #endif