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. */
23 /** EFX-style high-pass filter, specifying a gain and reference frequency. */
25 /** Peaking filter, specifying a gain and reference frequency. */
28 /** Low-pass cut-off filter, specifying a cut-off frequency. */
30 /** High-pass cut-off filter, specifying a cut-off frequency. */
32 /** Band-pass filter, specifying a center frequency. */
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
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
);
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
)
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
)
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))
110 else if(numsamples
== 1)
112 filter
->z1
= filter
->z2
;
117 #endif /* ALC_FILTER_H */