Combine the direct and send mixers
[openal-soft.git] / OpenAL32 / Include / alu.h
blob7727c666a64c3d8e2b974b7e587cd9ce87422870
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)
37 #define MAX_PITCH (10)
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
44 enum ActiveFilters {
45 AF_None = 0,
46 AF_LowPass = 1,
47 AF_HighPass = 2,
48 AF_BandPass = AF_LowPass | AF_HighPass
52 typedef struct HrtfState {
53 alignas(16) ALfloat History[SRC_HISTORY_LENGTH];
54 alignas(16) ALfloat Values[HRIR_LENGTH][2];
55 } HrtfState;
57 typedef struct HrtfParams {
58 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
59 alignas(16) ALfloat CoeffStep[HRIR_LENGTH][2];
60 ALuint Delay[2];
61 ALint DelayStep[2];
62 } HrtfParams;
65 typedef struct MixGains {
66 ALfloat Current;
67 ALfloat Step;
68 ALfloat Target;
69 } MixGains;
72 typedef struct DirectParams {
73 ALfloat (*OutBuffer)[BUFFERSIZE];
75 /* If not 'moving', gain/coefficients are set directly without fading. */
76 ALboolean Moving;
77 /* Stepping counter for gain/coefficient fading. */
78 ALuint Counter;
80 struct {
81 enum ActiveFilters ActiveType;
82 ALfilterState LowPass;
83 ALfilterState HighPass;
84 } Filters[MAX_INPUT_CHANNELS];
86 union {
87 struct {
88 HrtfParams Params[MAX_INPUT_CHANNELS];
89 HrtfState State[MAX_INPUT_CHANNELS];
90 ALuint IrSize;
91 ALfloat Gain;
92 ALfloat Dir[3];
93 } Hrtf;
95 MixGains Gains[MAX_INPUT_CHANNELS][MaxChannels];
96 } Mix;
97 } DirectParams;
99 typedef struct SendParams {
100 ALfloat (*OutBuffer)[BUFFERSIZE];
102 ALboolean Moving;
103 ALuint Counter;
105 struct {
106 enum ActiveFilters ActiveType;
107 ALfilterState LowPass;
108 ALfilterState HighPass;
109 } Filters[MAX_INPUT_CHANNELS];
111 /* Gain control, which applies to all input channels to a single (mono)
112 * output buffer. */
113 MixGains Gain;
114 } SendParams;
117 typedef const ALfloat* (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
118 ALfloat *restrict dst, ALuint dstlen);
120 typedef void (*MixerFunc)(const ALfloat *data, ALuint OutChans,
121 ALfloat (*restrict OutBuffer)[BUFFERSIZE], struct MixGains *Gains,
122 ALuint Counter, ALuint OutPos, ALuint BufferSize);
123 typedef void (*HrtfMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
124 ALuint Counter, ALuint Offset, ALuint OutPos,
125 const ALuint IrSize, const HrtfParams *hrtfparams,
126 HrtfState *hrtfstate, ALuint BufferSize);
129 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
131 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
132 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
134 #define FRACTIONBITS (14)
135 #define FRACTIONONE (1<<FRACTIONBITS)
136 #define FRACTIONMASK (FRACTIONONE-1)
139 inline ALfloat minf(ALfloat a, ALfloat b)
140 { return ((a > b) ? b : a); }
141 inline ALfloat maxf(ALfloat a, ALfloat b)
142 { return ((a > b) ? a : b); }
143 inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
144 { return minf(max, maxf(min, val)); }
146 inline ALdouble mind(ALdouble a, ALdouble b)
147 { return ((a > b) ? b : a); }
148 inline ALdouble maxd(ALdouble a, ALdouble b)
149 { return ((a > b) ? a : b); }
150 inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
151 { return mind(max, maxd(min, val)); }
153 inline ALuint minu(ALuint a, ALuint b)
154 { return ((a > b) ? b : a); }
155 inline ALuint maxu(ALuint a, ALuint b)
156 { return ((a > b) ? a : b); }
157 inline ALuint clampu(ALuint val, ALuint min, ALuint max)
158 { return minu(max, maxu(min, val)); }
160 inline ALint mini(ALint a, ALint b)
161 { return ((a > b) ? b : a); }
162 inline ALint maxi(ALint a, ALint b)
163 { return ((a > b) ? a : b); }
164 inline ALint clampi(ALint val, ALint min, ALint max)
165 { return mini(max, maxi(min, val)); }
167 inline ALint64 mini64(ALint64 a, ALint64 b)
168 { return ((a > b) ? b : a); }
169 inline ALint64 maxi64(ALint64 a, ALint64 b)
170 { return ((a > b) ? a : b); }
171 inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
172 { return mini64(max, maxi64(min, val)); }
174 inline ALuint64 minu64(ALuint64 a, ALuint64 b)
175 { return ((a > b) ? b : a); }
176 inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
177 { return ((a > b) ? a : b); }
178 inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
179 { return minu64(max, maxu64(min, val)); }
182 inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
184 return val1 + (val2-val1)*mu;
186 inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
188 ALfloat mu2 = mu*mu;
189 ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
190 ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
191 ALfloat a2 = -0.5f*val0 + 0.5f*val2;
192 ALfloat a3 = val1;
194 return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
198 ALvoid aluInitPanning(ALCdevice *Device);
201 * ComputeAngleGains
203 * Sets channel gains based on a given source's angle and its half-width. The
204 * angle and hwidth parameters are in radians.
206 void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
209 * SetGains
211 * Helper to set the appropriate channels to the specified gain.
213 inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
215 ComputeAngleGains(device, 0.0f, F_PI, ingain, gains);
219 ALvoid CalcSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
220 ALvoid CalcNonAttnSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
222 ALvoid MixSource(struct ALactivesource *src, ALCdevice *Device, ALuint SamplesToDo);
224 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
225 /* Caller must lock the device. */
226 ALvoid aluHandleDisconnect(ALCdevice *device);
228 extern ALfloat ConeScale;
229 extern ALfloat ZScale;
231 #ifdef __cplusplus
233 #endif
235 #endif