Use a macro to add backend include dirs
[openal-soft.git] / OpenAL32 / Include / alFilter.h
blob227d50cf33f79196917efed624500cc7cbd00fbd
1 #ifndef _AL_FILTER_H_
2 #define _AL_FILTER_H_
4 #include "alMain.h"
6 #include "math_defs.h"
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
12 #define LOWPASSFREQREF (5000.0f)
13 #define HIGHPASSFREQREF (250.0f)
16 /* Filters implementation is based on the "Cookbook formulae for audio
17 * EQ biquad filter coefficients" by Robert Bristow-Johnson
18 * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
20 /* Implementation note: For the shelf filters, the specified gain is for the
21 * reference frequency, which is the centerpoint of the transition band. This
22 * better matches EFX filter design. To set the gain for the shelf itself, use
23 * the square root of the desired linear gain (or halve the dB gain).
26 typedef enum ALfilterType {
27 /** EFX-style low-pass filter, specifying a gain and reference frequency. */
28 ALfilterType_HighShelf,
29 /** EFX-style high-pass filter, specifying a gain and reference frequency. */
30 ALfilterType_LowShelf,
31 /** Peaking filter, specifying a gain and reference frequency. */
32 ALfilterType_Peaking,
34 /** Low-pass cut-off filter, specifying a cut-off frequency. */
35 ALfilterType_LowPass,
36 /** High-pass cut-off filter, specifying a cut-off frequency. */
37 ALfilterType_HighPass,
38 /** Band-pass filter, specifying a center frequency. */
39 ALfilterType_BandPass,
40 } ALfilterType;
42 typedef struct ALfilterState {
43 ALfloat x[2]; /* History of two last input samples */
44 ALfloat y[2]; /* History of two last output samples */
45 ALfloat b0, b1, b2; /* Transfer function coefficients "b" */
46 ALfloat a1, a2; /* Transfer function coefficients "a" (a0 is pre-applied) */
47 } ALfilterState;
48 /* Currently only a C-based filter process method is implemented. */
49 #define ALfilterState_process ALfilterState_processC
51 /* Calculates the rcpQ (i.e. 1/Q) coefficient for shelving filters, using the
52 * reference gain and shelf slope parameter.
53 * 0 < gain
54 * 0 < slope <= 1
56 inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope)
58 return sqrtf((gain + 1.0f/gain)*(1.0f/slope - 1.0f) + 2.0f);
60 /* Calculates the rcpQ (i.e. 1/Q) coefficient for filters, using the frequency
61 * multiple (i.e. ref_freq / sampling_freq) and bandwidth.
62 * 0 < freq_mult < 0.5.
64 inline ALfloat calc_rcpQ_from_bandwidth(ALfloat freq_mult, ALfloat bandwidth)
66 ALfloat w0 = F_TAU * freq_mult;
67 return 2.0f*sinhf(logf(2.0f)/2.0f*bandwidth*w0/sinf(w0));
70 inline void ALfilterState_clear(ALfilterState *filter)
72 filter->x[0] = 0.0f;
73 filter->x[1] = 0.0f;
74 filter->y[0] = 0.0f;
75 filter->y[1] = 0.0f;
78 void ALfilterState_setParams(ALfilterState *filter, ALfilterType type, ALfloat gain, ALfloat freq_mult, ALfloat rcpQ);
80 inline void ALfilterState_copyParams(ALfilterState *restrict dst, const ALfilterState *restrict src)
82 dst->b0 = src->b0;
83 dst->b1 = src->b1;
84 dst->b2 = src->b2;
85 dst->a1 = src->a1;
86 dst->a2 = src->a2;
89 void ALfilterState_processC(ALfilterState *filter, ALfloat *restrict dst, const ALfloat *restrict src, ALsizei numsamples);
91 inline void ALfilterState_processPassthru(ALfilterState *filter, const ALfloat *restrict src, ALsizei numsamples)
93 if(numsamples >= 2)
95 filter->x[1] = src[numsamples-2];
96 filter->x[0] = src[numsamples-1];
97 filter->y[1] = src[numsamples-2];
98 filter->y[0] = src[numsamples-1];
100 else if(numsamples == 1)
102 filter->x[1] = filter->x[0];
103 filter->x[0] = src[0];
104 filter->y[1] = filter->y[0];
105 filter->y[0] = src[0];
110 typedef struct ALfilter {
111 // Filter type (AL_FILTER_NULL, ...)
112 ALenum type;
114 ALfloat Gain;
115 ALfloat GainHF;
116 ALfloat HFReference;
117 ALfloat GainLF;
118 ALfloat LFReference;
120 void (*SetParami)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALint val);
121 void (*SetParamiv)(struct ALfilter *filter, ALCcontext *context, ALenum param, const ALint *vals);
122 void (*SetParamf)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALfloat val);
123 void (*SetParamfv)(struct ALfilter *filter, ALCcontext *context, ALenum param, const ALfloat *vals);
125 void (*GetParami)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALint *val);
126 void (*GetParamiv)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALint *vals);
127 void (*GetParamf)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *val);
128 void (*GetParamfv)(struct ALfilter *filter, ALCcontext *context, ALenum param, ALfloat *vals);
130 /* Self ID */
131 ALuint id;
132 } ALfilter;
134 #define ALfilter_SetParami(x, c, p, v) ((x)->SetParami((x),(c),(p),(v)))
135 #define ALfilter_SetParamiv(x, c, p, v) ((x)->SetParamiv((x),(c),(p),(v)))
136 #define ALfilter_SetParamf(x, c, p, v) ((x)->SetParamf((x),(c),(p),(v)))
137 #define ALfilter_SetParamfv(x, c, p, v) ((x)->SetParamfv((x),(c),(p),(v)))
139 #define ALfilter_GetParami(x, c, p, v) ((x)->GetParami((x),(c),(p),(v)))
140 #define ALfilter_GetParamiv(x, c, p, v) ((x)->GetParamiv((x),(c),(p),(v)))
141 #define ALfilter_GetParamf(x, c, p, v) ((x)->GetParamf((x),(c),(p),(v)))
142 #define ALfilter_GetParamfv(x, c, p, v) ((x)->GetParamfv((x),(c),(p),(v)))
144 inline void LockFiltersRead(ALCdevice *device)
145 { LockUIntMapRead(&device->FilterMap); }
146 inline void UnlockFiltersRead(ALCdevice *device)
147 { UnlockUIntMapRead(&device->FilterMap); }
148 inline void LockFiltersWrite(ALCdevice *device)
149 { LockUIntMapWrite(&device->FilterMap); }
150 inline void UnlockFiltersWrite(ALCdevice *device)
151 { UnlockUIntMapWrite(&device->FilterMap); }
153 inline struct ALfilter *LookupFilter(ALCdevice *device, ALuint id)
154 { return (struct ALfilter*)LookupUIntMapKeyNoLock(&device->FilterMap, id); }
155 inline struct ALfilter *RemoveFilter(ALCdevice *device, ALuint id)
156 { return (struct ALfilter*)RemoveUIntMapKeyNoLock(&device->FilterMap, id); }
158 ALvoid ReleaseALFilters(ALCdevice *device);
160 #ifdef __cplusplus
162 #endif
164 #endif