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. */
34 /** Low-pass cut-off filter, specifying a cut-off frequency. */
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
,
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 a1
, a2
; /* Transfer function coefficients "a" (a0 is pre-applied) */
46 ALfloat b1
, b2
; /* Transfer function coefficients "b" (b0 is input_gain) */
49 void (*process
)(struct ALfilterState
*self
, ALfloat
*restrict dst
, const ALfloat
*src
, ALuint numsamples
);
51 #define ALfilterState_process(a, ...) ((a)->process((a), __VA_ARGS__))
53 /* Calculates the rcpQ (i.e. 1/Q) coefficient for shelving filters, using the
54 * reference gain and shelf slope parameter.
58 inline ALfloat
calc_rcpQ_from_slope(ALfloat gain
, ALfloat slope
)
60 return sqrtf((gain
+ 1.0f
/gain
)*(1.0f
/slope
- 1.0f
) + 2.0f
);
62 /* Calculates the rcpQ (i.e. 1/Q) coefficient for filters, using the frequency
63 * multiple (i.e. ref_freq / sampling_freq) and bandwidth.
64 * 0 < freq_mult < 0.5.
66 inline ALfloat
calc_rcpQ_from_bandwidth(ALfloat freq_mult
, ALfloat bandwidth
)
68 ALfloat w0
= F_TAU
* freq_mult
;
69 return 2.0f
*sinhf(logf(2.0f
)/2.0f
*bandwidth
*w0
/sinf(w0
));
72 inline void ALfilterState_clear(ALfilterState
*filter
)
80 void ALfilterState_setParams(ALfilterState
*filter
, ALfilterType type
, ALfloat gain
, ALfloat freq_mult
, ALfloat rcpQ
);
82 inline ALfloat
ALfilterState_processSingle(ALfilterState
*filter
, ALfloat sample
)
86 outsmp
= filter
->input_gain
* sample
+
87 filter
->b1
* filter
->x
[0] +
88 filter
->b2
* filter
->x
[1] -
89 filter
->a1
* filter
->y
[0] -
90 filter
->a2
* filter
->y
[1];
91 filter
->x
[1] = filter
->x
[0];
92 filter
->x
[0] = sample
;
93 filter
->y
[1] = filter
->y
[0];
94 filter
->y
[0] = outsmp
;
99 void ALfilterState_processC(ALfilterState
*filter
, ALfloat
*restrict dst
, const ALfloat
*src
, ALuint numsamples
);
101 inline void ALfilterState_processPassthru(ALfilterState
*filter
, const ALfloat
*src
, ALuint numsamples
)
105 filter
->x
[1] = src
[numsamples
-2];
106 filter
->x
[0] = src
[numsamples
-1];
107 filter
->y
[1] = src
[numsamples
-2];
108 filter
->y
[0] = src
[numsamples
-1];
110 else if(numsamples
== 1)
112 filter
->x
[1] = filter
->x
[0];
113 filter
->x
[0] = src
[0];
114 filter
->y
[1] = filter
->y
[0];
115 filter
->y
[0] = src
[0];
120 typedef struct ALfilter
{
121 // Filter type (AL_FILTER_NULL, ...)
130 void (*SetParami
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, ALint val
);
131 void (*SetParamiv
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, const ALint
*vals
);
132 void (*SetParamf
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, ALfloat val
);
133 void (*SetParamfv
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
);
135 void (*GetParami
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, ALint
*val
);
136 void (*GetParamiv
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, ALint
*vals
);
137 void (*GetParamf
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, ALfloat
*val
);
138 void (*GetParamfv
)(struct ALfilter
*filter
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
);
144 #define ALfilter_SetParami(x, c, p, v) ((x)->SetParami((x),(c),(p),(v)))
145 #define ALfilter_SetParamiv(x, c, p, v) ((x)->SetParamiv((x),(c),(p),(v)))
146 #define ALfilter_SetParamf(x, c, p, v) ((x)->SetParamf((x),(c),(p),(v)))
147 #define ALfilter_SetParamfv(x, c, p, v) ((x)->SetParamfv((x),(c),(p),(v)))
149 #define ALfilter_GetParami(x, c, p, v) ((x)->GetParami((x),(c),(p),(v)))
150 #define ALfilter_GetParamiv(x, c, p, v) ((x)->GetParamiv((x),(c),(p),(v)))
151 #define ALfilter_GetParamf(x, c, p, v) ((x)->GetParamf((x),(c),(p),(v)))
152 #define ALfilter_GetParamfv(x, c, p, v) ((x)->GetParamfv((x),(c),(p),(v)))
154 inline struct ALfilter
*LookupFilter(ALCdevice
*device
, ALuint id
)
155 { return (struct ALfilter
*)LookupUIntMapKey(&device
->FilterMap
, id
); }
156 inline struct ALfilter
*RemoveFilter(ALCdevice
*device
, ALuint id
)
157 { return (struct ALfilter
*)RemoveUIntMapKey(&device
->FilterMap
, id
); }
159 ALvoid
ReleaseALFilters(ALCdevice
*device
);