21 #define F_PI (3.14159265358979323846f)
22 #define F_PI_2 (1.57079632679489661923f)
23 #define F_2PI (6.28318530717958647692f)
26 #define FLT_EPSILON (1.19209290e-07f)
29 #define DEG2RAD(x) ((ALfloat)(x) * (F_PI/180.0f))
30 #define RAD2DEG(x) ((ALfloat)(x) * (180.0f/F_PI))
33 #define MAX_PITCH (10)
44 typedef union aluVector
{
45 alignas(16) ALfloat v
[4];
48 inline void aluVectorSet(aluVector
*restrict vector
, ALfloat x
, ALfloat y
, ALfloat z
, ALfloat w
)
57 typedef union aluMatrix
{
58 alignas(16) ALfloat m
[4][4];
61 inline void aluMatrixSetRow(aluMatrix
*restrict matrix
, ALuint row
,
62 ALfloat m0
, ALfloat m1
, ALfloat m2
, ALfloat m3
)
64 matrix
->m
[row
][0] = m0
;
65 matrix
->m
[row
][1] = m1
;
66 matrix
->m
[row
][2] = m2
;
67 matrix
->m
[row
][3] = m3
;
70 inline void aluMatrixSet(aluMatrix
*restrict matrix
, ALfloat m00
, ALfloat m01
, ALfloat m02
, ALfloat m03
,
71 ALfloat m10
, ALfloat m11
, ALfloat m12
, ALfloat m13
,
72 ALfloat m20
, ALfloat m21
, ALfloat m22
, ALfloat m23
,
73 ALfloat m30
, ALfloat m31
, ALfloat m32
, ALfloat m33
)
75 aluMatrixSetRow(matrix
, 0, m00
, m01
, m02
, m03
);
76 aluMatrixSetRow(matrix
, 1, m10
, m11
, m12
, m13
);
77 aluMatrixSetRow(matrix
, 2, m20
, m21
, m22
, m23
);
78 aluMatrixSetRow(matrix
, 3, m30
, m31
, m32
, m33
);
86 AF_BandPass
= AF_LowPass
| AF_HighPass
90 typedef struct MixGains
{
97 typedef struct DirectParams
{
98 ALfloat (*OutBuffer
)[BUFFERSIZE
];
101 /* If not 'moving', gain/coefficients are set directly without fading. */
103 /* Stepping counter for gain/coefficient fading. */
105 /* Last direction (relative to listener) and gain of a moving source. */
110 enum ActiveFilters ActiveType
;
111 ALfilterState LowPass
;
112 ALfilterState HighPass
;
113 } Filters
[MAX_INPUT_CHANNELS
];
116 HrtfParams Params
[MAX_INPUT_CHANNELS
];
117 HrtfState State
[MAX_INPUT_CHANNELS
];
119 MixGains Gains
[MAX_INPUT_CHANNELS
][MAX_OUTPUT_CHANNELS
];
122 typedef struct SendParams
{
123 ALfloat (*OutBuffer
)[BUFFERSIZE
];
129 enum ActiveFilters ActiveType
;
130 ALfilterState LowPass
;
131 ALfilterState HighPass
;
132 } Filters
[MAX_INPUT_CHANNELS
];
134 /* Gain control, which applies to all input channels to a single (mono)
140 typedef const ALfloat
* (*ResamplerFunc
)(const ALfloat
*src
, ALuint frac
, ALuint increment
,
141 ALfloat
*restrict dst
, ALuint dstlen
);
143 typedef void (*MixerFunc
)(const ALfloat
*data
, ALuint OutChans
,
144 ALfloat (*restrict OutBuffer
)[BUFFERSIZE
], struct MixGains
*Gains
,
145 ALuint Counter
, ALuint OutPos
, ALuint BufferSize
);
146 typedef void (*HrtfMixerFunc
)(ALfloat (*restrict OutBuffer
)[BUFFERSIZE
], const ALfloat
*data
,
147 ALuint Counter
, ALuint Offset
, ALuint OutPos
,
148 const ALuint IrSize
, const HrtfParams
*hrtfparams
,
149 HrtfState
*hrtfstate
, ALuint BufferSize
);
152 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
154 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
155 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
157 #define FRACTIONBITS (12)
158 #define FRACTIONONE (1<<FRACTIONBITS)
159 #define FRACTIONMASK (FRACTIONONE-1)
162 inline ALfloat
minf(ALfloat a
, ALfloat b
)
163 { return ((a
> b
) ? b
: a
); }
164 inline ALfloat
maxf(ALfloat a
, ALfloat b
)
165 { return ((a
> b
) ? a
: b
); }
166 inline ALfloat
clampf(ALfloat val
, ALfloat min
, ALfloat max
)
167 { return minf(max
, maxf(min
, val
)); }
169 inline ALdouble
mind(ALdouble a
, ALdouble b
)
170 { return ((a
> b
) ? b
: a
); }
171 inline ALdouble
maxd(ALdouble a
, ALdouble b
)
172 { return ((a
> b
) ? a
: b
); }
173 inline ALdouble
clampd(ALdouble val
, ALdouble min
, ALdouble max
)
174 { return mind(max
, maxd(min
, val
)); }
176 inline ALuint
minu(ALuint a
, ALuint b
)
177 { return ((a
> b
) ? b
: a
); }
178 inline ALuint
maxu(ALuint a
, ALuint b
)
179 { return ((a
> b
) ? a
: b
); }
180 inline ALuint
clampu(ALuint val
, ALuint min
, ALuint max
)
181 { return minu(max
, maxu(min
, val
)); }
183 inline ALint
mini(ALint a
, ALint b
)
184 { return ((a
> b
) ? b
: a
); }
185 inline ALint
maxi(ALint a
, ALint b
)
186 { return ((a
> b
) ? a
: b
); }
187 inline ALint
clampi(ALint val
, ALint min
, ALint max
)
188 { return mini(max
, maxi(min
, val
)); }
190 inline ALint64
mini64(ALint64 a
, ALint64 b
)
191 { return ((a
> b
) ? b
: a
); }
192 inline ALint64
maxi64(ALint64 a
, ALint64 b
)
193 { return ((a
> b
) ? a
: b
); }
194 inline ALint64
clampi64(ALint64 val
, ALint64 min
, ALint64 max
)
195 { return mini64(max
, maxi64(min
, val
)); }
197 inline ALuint64
minu64(ALuint64 a
, ALuint64 b
)
198 { return ((a
> b
) ? b
: a
); }
199 inline ALuint64
maxu64(ALuint64 a
, ALuint64 b
)
200 { return ((a
> b
) ? a
: b
); }
201 inline ALuint64
clampu64(ALuint64 val
, ALuint64 min
, ALuint64 max
)
202 { return minu64(max
, maxu64(min
, val
)); }
205 extern ALfloat CubicLUT
[FRACTIONONE
][4];
208 inline ALfloat
lerp(ALfloat val1
, ALfloat val2
, ALfloat mu
)
210 return val1
+ (val2
-val1
)*mu
;
212 inline ALfloat
cubic(ALfloat val0
, ALfloat val1
, ALfloat val2
, ALfloat val3
, ALuint frac
)
214 const ALfloat
*k
= CubicLUT
[frac
];
215 return k
[0]*val0
+ k
[1]*val1
+ k
[2]*val2
+ k
[3]*val3
;
219 void aluInitResamplers(void);
221 ALvoid
aluInitPanning(ALCdevice
*Device
);
224 * ComputeDirectionalGains
226 * Sets channel gains based on a direction. The direction must be a 3-component
227 * vector no longer than 1 unit.
229 void ComputeDirectionalGains(const ALCdevice
*device
, const ALfloat dir
[3], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
234 * Sets channel gains based on angle and elevation. The angle and elevation
235 * parameters are in radians, going right and up respectively.
237 void ComputeAngleGains(const ALCdevice
*device
, ALfloat angle
, ALfloat elevation
, ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
240 * ComputeAmbientGains
242 * Sets channel gains for ambient, omni-directional sounds.
244 void ComputeAmbientGains(const ALCdevice
*device
, ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
247 * ComputeBFormatGains
249 * Sets channel gains for a given (first-order) B-Format channel. The matrix is
250 * a 1x4 'slice' of the rotation matrix for a given channel used to orient the
253 void ComputeBFormatGains(const ALCdevice
*device
, const ALfloat mtx
[4], ALfloat ingain
, ALfloat gains
[MAX_OUTPUT_CHANNELS
]);
256 ALvoid
CalcSourceParams(struct ALvoice
*voice
, const struct ALsource
*source
, const ALCcontext
*ALContext
);
257 ALvoid
CalcNonAttnSourceParams(struct ALvoice
*voice
, const struct ALsource
*source
, const ALCcontext
*ALContext
);
259 ALvoid
MixSource(struct ALvoice
*voice
, struct ALsource
*source
, ALCdevice
*Device
, ALuint SamplesToDo
);
261 ALvoid
aluMixData(ALCdevice
*device
, ALvoid
*buffer
, ALsizei size
);
262 /* Caller must lock the device. */
263 ALvoid
aluHandleDisconnect(ALCdevice
*device
);
265 extern ALfloat ConeScale
;
266 extern ALfloat ZScale
;