Clean up the biquad filter a bit
[openal-soft.git] / Alc / filters / defs.h
blob19514b6280cad4d961a9bea698386cd3936c82b6
1 #ifndef ALC_FILTER_H
2 #define ALC_FILTER_H
4 #include <cmath>
6 #include "AL/al.h"
7 #include "math_defs.h"
10 /* Filters implementation is based on the "Cookbook formulae for audio
11 * EQ biquad filter coefficients" by Robert Bristow-Johnson
12 * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
14 /* Implementation note: For the shelf filters, the specified gain is for the
15 * reference frequency, which is the centerpoint of the transition band. This
16 * better matches EFX filter design. To set the gain for the shelf itself, use
17 * the square root of the desired linear gain (or halve the dB gain).
20 enum class BiquadType {
21 /** EFX-style low-pass filter, specifying a gain and reference frequency. */
22 HighShelf,
23 /** EFX-style high-pass filter, specifying a gain and reference frequency. */
24 LowShelf,
25 /** Peaking filter, specifying a gain and reference frequency. */
26 Peaking,
28 /** Low-pass cut-off filter, specifying a cut-off frequency. */
29 LowPass,
30 /** High-pass cut-off filter, specifying a cut-off frequency. */
31 HighPass,
32 /** Band-pass filter, specifying a center frequency. */
33 BandPass,
36 struct BiquadFilter {
37 /* Last two delayed components for direct form II. */
38 ALfloat z1{0.0f}, z2{0.0f};
39 /* Transfer function coefficients "b" (numerator) */
40 ALfloat b0{1.0f}, b1{0.0f}, b2{0.0f};
41 /* Transfer function coefficients "a" (denominator; a0 is pre-applied). */
42 ALfloat a1{0.0f}, a2{0.0f};
44 /* Currently only a C-based filter process method is implemented. */
45 #define BiquadFilter_process BiquadFilter_processC
47 /**
48 * Calculates the rcpQ (i.e. 1/Q) coefficient for shelving filters, using the
49 * reference gain and shelf slope parameter.
50 * \param gain 0 < gain
51 * \param slope 0 < slope <= 1
53 inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope)
55 return std::sqrt((gain + 1.0f/gain)*(1.0f/slope - 1.0f) + 2.0f);
57 /**
58 * Calculates the rcpQ (i.e. 1/Q) coefficient for filters, using the normalized
59 * reference frequency and bandwidth.
60 * \param f0norm 0 < f0norm < 0.5.
61 * \param bandwidth 0 < bandwidth
63 inline ALfloat calc_rcpQ_from_bandwidth(ALfloat f0norm, ALfloat bandwidth)
65 ALfloat w0 = F_TAU * f0norm;
66 return 2.0f*std::sinh(std::log(2.0f)/2.0f*bandwidth*w0/std::sin(w0));
69 inline void BiquadFilter_clear(BiquadFilter *filter)
71 filter->z1 = 0.0f;
72 filter->z2 = 0.0f;
75 /**
76 * Sets up the filter state for the specified filter type and its parameters.
78 * \param filter The filter object to prepare.
79 * \param type The type of filter for the object to apply.
80 * \param gain The gain for the reference frequency response. Only used by the
81 * Shelf and Peaking filter types.
82 * \param f0norm The normalized reference frequency (ref_freq / sample_rate).
83 * This is the center point for the Shelf, Peaking, and BandPass
84 * filter types, or the cutoff frequency for the LowPass and
85 * HighPass filter types.
86 * \param rcpQ The reciprocal of the Q coefficient for the filter's transition
87 * band. Can be generated from calc_rcpQ_from_slope or
88 * calc_rcpQ_from_bandwidth depending on the available data.
90 void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, ALfloat f0norm, ALfloat rcpQ);
92 inline void BiquadFilter_copyParams(BiquadFilter *RESTRICT dst, const BiquadFilter *RESTRICT src)
94 dst->b0 = src->b0;
95 dst->b1 = src->b1;
96 dst->b2 = src->b2;
97 dst->a1 = src->a1;
98 dst->a2 = src->a2;
101 void BiquadFilter_processC(BiquadFilter *filter, ALfloat *RESTRICT dst, const ALfloat *RESTRICT src, ALsizei numsamples);
103 inline void BiquadFilter_passthru(BiquadFilter *filter, ALsizei numsamples)
105 if(LIKELY(numsamples >= 2))
107 filter->z1 = 0.0f;
108 filter->z2 = 0.0f;
110 else if(numsamples == 1)
112 filter->z1 = filter->z2;
113 filter->z2 = 0.0f;
117 #endif /* ALC_FILTER_H */