Rename activesource to voice
[openal-soft.git] / OpenAL32 / Include / alu.h
blob55d339889b8d5e9afb2928bb9531867fdf80baa2
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 struct ALsource;
45 struct ALvoice;
48 enum ActiveFilters {
49 AF_None = 0,
50 AF_LowPass = 1,
51 AF_HighPass = 2,
52 AF_BandPass = AF_LowPass | AF_HighPass
56 typedef struct HrtfState {
57 alignas(16) ALfloat History[SRC_HISTORY_LENGTH];
58 alignas(16) ALfloat Values[HRIR_LENGTH][2];
59 } HrtfState;
61 typedef struct HrtfParams {
62 alignas(16) ALfloat Coeffs[HRIR_LENGTH][2];
63 alignas(16) ALfloat CoeffStep[HRIR_LENGTH][2];
64 ALuint Delay[2];
65 ALint DelayStep[2];
66 } HrtfParams;
69 typedef struct MixGains {
70 ALfloat Current;
71 ALfloat Step;
72 ALfloat Target;
73 } MixGains;
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;
84 struct {
85 enum ActiveFilters ActiveType;
86 ALfilterState LowPass;
87 ALfilterState HighPass;
88 } Filters[MAX_INPUT_CHANNELS];
90 union {
91 struct {
92 HrtfParams Params[MAX_INPUT_CHANNELS];
93 HrtfState State[MAX_INPUT_CHANNELS];
94 ALuint IrSize;
95 ALfloat Gain;
96 ALfloat Dir[3];
97 } Hrtf;
99 MixGains Gains[MAX_INPUT_CHANNELS][MaxChannels];
100 } Mix;
101 } DirectParams;
103 typedef struct SendParams {
104 ALfloat (*OutBuffer)[BUFFERSIZE];
106 ALboolean Moving;
107 ALuint Counter;
109 struct {
110 enum ActiveFilters ActiveType;
111 ALfilterState LowPass;
112 ALfilterState HighPass;
113 } Filters[MAX_INPUT_CHANNELS];
115 /* Gain control, which applies to all input channels to a single (mono)
116 * output buffer. */
117 MixGains Gain;
118 } SendParams;
121 typedef const ALfloat* (*ResamplerFunc)(const ALfloat *src, ALuint frac, ALuint increment,
122 ALfloat *restrict dst, ALuint dstlen);
124 typedef void (*MixerFunc)(const ALfloat *data, ALuint OutChans,
125 ALfloat (*restrict OutBuffer)[BUFFERSIZE], struct MixGains *Gains,
126 ALuint Counter, ALuint OutPos, ALuint BufferSize);
127 typedef void (*HrtfMixerFunc)(ALfloat (*restrict OutBuffer)[BUFFERSIZE], const ALfloat *data,
128 ALuint Counter, ALuint Offset, ALuint OutPos,
129 const ALuint IrSize, const HrtfParams *hrtfparams,
130 HrtfState *hrtfstate, ALuint BufferSize);
133 #define GAIN_SILENCE_THRESHOLD (0.00001f) /* -100dB */
135 #define SPEEDOFSOUNDMETRESPERSEC (343.3f)
136 #define AIRABSORBGAINHF (0.99426f) /* -0.05dB */
138 #define FRACTIONBITS (14)
139 #define FRACTIONONE (1<<FRACTIONBITS)
140 #define FRACTIONMASK (FRACTIONONE-1)
143 inline ALfloat minf(ALfloat a, ALfloat b)
144 { return ((a > b) ? b : a); }
145 inline ALfloat maxf(ALfloat a, ALfloat b)
146 { return ((a > b) ? a : b); }
147 inline ALfloat clampf(ALfloat val, ALfloat min, ALfloat max)
148 { return minf(max, maxf(min, val)); }
150 inline ALdouble mind(ALdouble a, ALdouble b)
151 { return ((a > b) ? b : a); }
152 inline ALdouble maxd(ALdouble a, ALdouble b)
153 { return ((a > b) ? a : b); }
154 inline ALdouble clampd(ALdouble val, ALdouble min, ALdouble max)
155 { return mind(max, maxd(min, val)); }
157 inline ALuint minu(ALuint a, ALuint b)
158 { return ((a > b) ? b : a); }
159 inline ALuint maxu(ALuint a, ALuint b)
160 { return ((a > b) ? a : b); }
161 inline ALuint clampu(ALuint val, ALuint min, ALuint max)
162 { return minu(max, maxu(min, val)); }
164 inline ALint mini(ALint a, ALint b)
165 { return ((a > b) ? b : a); }
166 inline ALint maxi(ALint a, ALint b)
167 { return ((a > b) ? a : b); }
168 inline ALint clampi(ALint val, ALint min, ALint max)
169 { return mini(max, maxi(min, val)); }
171 inline ALint64 mini64(ALint64 a, ALint64 b)
172 { return ((a > b) ? b : a); }
173 inline ALint64 maxi64(ALint64 a, ALint64 b)
174 { return ((a > b) ? a : b); }
175 inline ALint64 clampi64(ALint64 val, ALint64 min, ALint64 max)
176 { return mini64(max, maxi64(min, val)); }
178 inline ALuint64 minu64(ALuint64 a, ALuint64 b)
179 { return ((a > b) ? b : a); }
180 inline ALuint64 maxu64(ALuint64 a, ALuint64 b)
181 { return ((a > b) ? a : b); }
182 inline ALuint64 clampu64(ALuint64 val, ALuint64 min, ALuint64 max)
183 { return minu64(max, maxu64(min, val)); }
186 inline ALfloat lerp(ALfloat val1, ALfloat val2, ALfloat mu)
188 return val1 + (val2-val1)*mu;
190 inline ALfloat cubic(ALfloat val0, ALfloat val1, ALfloat val2, ALfloat val3, ALfloat mu)
192 ALfloat mu2 = mu*mu;
193 ALfloat a0 = -0.5f*val0 + 1.5f*val1 + -1.5f*val2 + 0.5f*val3;
194 ALfloat a1 = val0 + -2.5f*val1 + 2.0f*val2 + -0.5f*val3;
195 ALfloat a2 = -0.5f*val0 + 0.5f*val2;
196 ALfloat a3 = val1;
198 return a0*mu*mu2 + a1*mu2 + a2*mu + a3;
202 ALvoid aluInitPanning(ALCdevice *Device);
205 * ComputeAngleGains
207 * Sets channel gains based on a given source's angle and its half-width. The
208 * angle and hwidth parameters are in radians.
210 void ComputeAngleGains(const ALCdevice *device, ALfloat angle, ALfloat hwidth, ALfloat ingain, ALfloat gains[MaxChannels]);
213 * SetGains
215 * Helper to set the appropriate channels to the specified gain.
217 inline void SetGains(const ALCdevice *device, ALfloat ingain, ALfloat gains[MaxChannels])
219 ComputeAngleGains(device, 0.0f, F_PI, ingain, gains);
223 ALvoid CalcSourceParams(struct ALvoice *voice, const struct ALsource *source, const ALCcontext *ALContext);
224 ALvoid CalcNonAttnSourceParams(struct ALvoice *voice, const struct ALsource *source, const ALCcontext *ALContext);
226 ALvoid MixSource(struct ALvoice *voice, struct ALsource *source, ALCdevice *Device, ALuint SamplesToDo);
228 ALvoid aluMixData(ALCdevice *device, ALvoid *buffer, ALsizei size);
229 /* Caller must lock the device. */
230 ALvoid aluHandleDisconnect(ALCdevice *device);
232 extern ALfloat ConeScale;
233 extern ALfloat ZScale;
235 #ifdef __cplusplus
237 #endif
239 #endif