Return a sample pointer from resamplers
[openal-soft.git] / OpenAL32 / Include / alu.h
blobdf0e923db7cdb8c94fd210d1ab9cdd76fabc4eb5
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 /* If not 'moving', gain/coefficients are set directly without fading. */
80 ALboolean Moving;
81 /* Stepping counter for gain/coefficient fading. */
82 ALuint Counter;
83 /* History/coefficient offset. */
84 ALuint Offset;
86 struct {
87 enum ActiveFilters ActiveType;
88 ALfilterState LowPass;
89 ALfilterState HighPass;
90 } Filters[MAX_INPUT_CHANNELS];
92 union {
93 struct {
94 HrtfParams Params[MAX_INPUT_CHANNELS];
95 HrtfState State[MAX_INPUT_CHANNELS];
96 ALuint IrSize;
97 ALfloat Gain;
98 ALfloat Dir[3];
99 } Hrtf;
101 MixGains Gains[MAX_INPUT_CHANNELS];
102 } Mix;
103 } DirectParams;
105 typedef struct SendParams {
106 ALfloat (*OutBuffer)[BUFFERSIZE];
108 ALboolean Moving;
109 ALuint Counter;
111 struct {
112 enum ActiveFilters ActiveType;
113 ALfilterState LowPass;
114 ALfilterState HighPass;
115 } Filters[MAX_INPUT_CHANNELS];
117 /* Gain control, which applies to all input channels to a single (mono)
118 * output buffer. */
119 MixGainMono Gain;
120 } SendParams;
123 typedef const ALfloat* (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
124 ALfloat *restrict dst, ALuint dstlen);
126 typedef void (*DryMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
127 MixGains *Gains, ALuint Counter, ALuint OutPos,
128 ALuint BufferSize);
129 typedef void (*HrtfMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
130 ALuint Counter, ALuint Offset, ALuint OutPos,
131 const ALuint IrSize, const HrtfParams *hrtfparams,
132 HrtfState *hrtfstate, ALuint BufferSize);
133 typedef void (*WetMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
134 MixGainMono *Gain, ALuint Counter, ALuint OutPos,
135 ALuint BufferSize);
138 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
140 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
141 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
143 #define FRACTIONBITS (14)
144 #define FRACTIONONE (1<<FRACTIONBITS)
145 #define FRACTIONMASK (FRACTIONONE-1)
148 inline ALfloat minf(ALfloat a, ALfloat b)
149 { return ((a > b) ? b : a); }
150 inline ALfloat maxf(ALfloat a, ALfloat b)
151 { return ((a > b) ? a : b); }
152 inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
153 { return minf(max, maxf(min, val)); }
155 inline ALdouble mind(ALdouble a, ALdouble b)
156 { return ((a > b) ? b : a); }
157 inline ALdouble maxd(ALdouble a, ALdouble b)
158 { return ((a > b) ? a : b); }
159 inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
160 { return mind(max, maxd(min, val)); }
162 inline ALuint minu(ALuint a, ALuint b)
163 { return ((a > b) ? b : a); }
164 inline ALuint maxu(ALuint a, ALuint b)
165 { return ((a > b) ? a : b); }
166 inline ALuint clampu(ALuint val, ALuint min, ALuint max)
167 { return minu(max, maxu(min, val)); }
169 inline ALint mini(ALint a, ALint b)
170 { return ((a > b) ? b : a); }
171 inline ALint maxi(ALint a, ALint b)
172 { return ((a > b) ? a : b); }
173 inline ALint clampi(ALint val, ALint min, ALint max)
174 { return mini(max, maxi(min, val)); }
176 inline ALint64 mini64(ALint64 a, ALint64 b)
177 { return ((a > b) ? b : a); }
178 inline ALint64 maxi64(ALint64 a, ALint64 b)
179 { return ((a > b) ? a : b); }
180 inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
181 { return mini64(max, maxi64(min, val)); }
183 inline ALuint64 minu64(ALuint64 a, ALuint64 b)
184 { return ((a > b) ? b : a); }
185 inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
186 { return ((a > b) ? a : b); }
187 inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
188 { return minu64(max, maxu64(min, val)); }
191 inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
193 return val1 + (val2-val1)*mu;
195 inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
197 ALfloat mu2 = mu*mu;
198 ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
199 ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
200 ALfloat a2 = -0.5f*val0 + 0.5f*val2;
201 ALfloat a3 = val1;
203 return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
207 ALvoid aluInitPanning(ALCdevice *Device);
210 * ComputeAngleGains
212 * Sets channel gains based on a given source's angle and its half-width. The
213 * angle and hwidth parameters are in radians.
215 void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
218 * SetGains
220 * Helper to set the appropriate channels to the specified gain.
222 inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
224 ComputeAngleGains(device, 0.0f, F_PI, ingain, gains);
228 ALvoid CalcSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
229 ALvoid CalcNonAttnSourceParams(struct ALactivesource *src, const ALCcontext *ALContext);
231 ALvoid MixSource(struct ALactivesource *src, ALCdevice *Device, ALuint SamplesToDo);
233 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
234 /* Caller must lock the device. */
235 ALvoid aluHandleDisconnect(ALCdevice *device);
237 extern ALfloat ConeScale;
238 extern ALfloat ZScale;
240 #ifdef __cplusplus
242 #endif
244 #endif