diff options
author | Chris Robinson <[email protected]> | 2018-11-19 09:10:36 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-11-19 09:10:36 -0800 |
commit | 387a34ca002bdc25cc31fb8514c0fa2e44af6e1b (patch) | |
tree | 268ac0fff88d694d90502072e6fce2b38c831e10 /Alc/filters | |
parent | 07386e79de93988faccc98166a036b6234ff9fe1 (diff) |
Clean up the biquad filter a bit
Diffstat (limited to 'Alc/filters')
-rw-r--r-- | Alc/filters/defs.h | 43 | ||||
-rw-r--r-- | Alc/filters/filter.cpp | 24 |
2 files changed, 33 insertions, 34 deletions
diff --git a/Alc/filters/defs.h b/Alc/filters/defs.h index 58b7cc0a..19514b62 100644 --- a/Alc/filters/defs.h +++ b/Alc/filters/defs.h @@ -1,12 +1,11 @@ #ifndef ALC_FILTER_H #define ALC_FILTER_H +#include <cmath> + #include "AL/al.h" #include "math_defs.h" -#ifdef __cplusplus -extern "C" { -#endif /* Filters implementation is based on the "Cookbook formulae for audio * EQ biquad filter coefficients" by Robert Bristow-Johnson @@ -18,28 +17,30 @@ extern "C" { * the square root of the desired linear gain (or halve the dB gain). */ -typedef enum BiquadType { +enum class BiquadType { /** EFX-style low-pass filter, specifying a gain and reference frequency. */ - BiquadType_HighShelf, + HighShelf, /** EFX-style high-pass filter, specifying a gain and reference frequency. */ - BiquadType_LowShelf, + LowShelf, /** Peaking filter, specifying a gain and reference frequency. */ - BiquadType_Peaking, + Peaking, /** Low-pass cut-off filter, specifying a cut-off frequency. */ - BiquadType_LowPass, + LowPass, /** High-pass cut-off filter, specifying a cut-off frequency. */ - BiquadType_HighPass, + HighPass, /** Band-pass filter, specifying a center frequency. */ - BiquadType_BandPass, -} BiquadType; + BandPass, +}; -typedef struct BiquadFilter { - ALfloat z1, z2; /* Last two delayed components for direct form II. */ - ALfloat b0, b1, b2; /* Transfer function coefficients "b" (numerator) */ - ALfloat a1, a2; /* Transfer function coefficients "a" (denominator; a0 is - * pre-applied). */ -} BiquadFilter; +struct BiquadFilter { + /* Last two delayed components for direct form II. */ + ALfloat z1{0.0f}, z2{0.0f}; + /* Transfer function coefficients "b" (numerator) */ + ALfloat b0{1.0f}, b1{0.0f}, b2{0.0f}; + /* Transfer function coefficients "a" (denominator; a0 is pre-applied). */ + ALfloat a1{0.0f}, a2{0.0f}; +}; /* Currently only a C-based filter process method is implemented. */ #define BiquadFilter_process BiquadFilter_processC @@ -51,7 +52,7 @@ typedef struct BiquadFilter { */ inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope) { - return sqrtf((gain + 1.0f/gain)*(1.0f/slope - 1.0f) + 2.0f); + return std::sqrt((gain + 1.0f/gain)*(1.0f/slope - 1.0f) + 2.0f); } /** * Calculates the rcpQ (i.e. 1/Q) coefficient for filters, using the normalized @@ -62,7 +63,7 @@ inline ALfloat calc_rcpQ_from_slope(ALfloat gain, ALfloat slope) inline ALfloat calc_rcpQ_from_bandwidth(ALfloat f0norm, ALfloat bandwidth) { ALfloat w0 = F_TAU * f0norm; - return 2.0f*sinhf(logf(2.0f)/2.0f*bandwidth*w0/sinf(w0)); + return 2.0f*std::sinh(std::log(2.0f)/2.0f*bandwidth*w0/std::sin(w0)); } inline void BiquadFilter_clear(BiquadFilter *filter) @@ -113,8 +114,4 @@ inline void BiquadFilter_passthru(BiquadFilter *filter, ALsizei numsamples) } } -#ifdef __cplusplus -} // extern "C" -#endif - #endif /* ALC_FILTER_H */ diff --git a/Alc/filters/filter.cpp b/Alc/filters/filter.cpp index 838a9a5d..6099cf13 100644 --- a/Alc/filters/filter.cpp +++ b/Alc/filters/filter.cpp @@ -1,6 +1,8 @@ #include "config.h" +#include <cmath> + #include "AL/alc.h" #include "AL/al.h" @@ -19,15 +21,15 @@ void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, assert(gain > 0.00001f); w0 = F_TAU * f0norm; - sin_w0 = sinf(w0); - cos_w0 = cosf(w0); + sin_w0 = std::sin(w0); + cos_w0 = std::cos(w0); alpha = sin_w0/2.0f * rcpQ; /* Calculate filter coefficients depending on filter type */ switch(type) { - case BiquadType_HighShelf: - sqrtgain_alpha_2 = 2.0f * sqrtf(gain) * alpha; + case BiquadType::HighShelf: + sqrtgain_alpha_2 = 2.0f * std::sqrt(gain) * alpha; b[0] = gain*((gain+1.0f) + (gain-1.0f)*cos_w0 + sqrtgain_alpha_2); b[1] = -2.0f*gain*((gain-1.0f) + (gain+1.0f)*cos_w0 ); b[2] = gain*((gain+1.0f) + (gain-1.0f)*cos_w0 - sqrtgain_alpha_2); @@ -35,8 +37,8 @@ void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, a[1] = 2.0f* ((gain-1.0f) - (gain+1.0f)*cos_w0 ); a[2] = (gain+1.0f) - (gain-1.0f)*cos_w0 - sqrtgain_alpha_2; break; - case BiquadType_LowShelf: - sqrtgain_alpha_2 = 2.0f * sqrtf(gain) * alpha; + case BiquadType::LowShelf: + sqrtgain_alpha_2 = 2.0f * std::sqrt(gain) * alpha; b[0] = gain*((gain+1.0f) - (gain-1.0f)*cos_w0 + sqrtgain_alpha_2); b[1] = 2.0f*gain*((gain-1.0f) - (gain+1.0f)*cos_w0 ); b[2] = gain*((gain+1.0f) - (gain-1.0f)*cos_w0 - sqrtgain_alpha_2); @@ -44,8 +46,8 @@ void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, a[1] = -2.0f* ((gain-1.0f) + (gain+1.0f)*cos_w0 ); a[2] = (gain+1.0f) + (gain-1.0f)*cos_w0 - sqrtgain_alpha_2; break; - case BiquadType_Peaking: - gain = sqrtf(gain); + case BiquadType::Peaking: + gain = std::sqrt(gain); b[0] = 1.0f + alpha * gain; b[1] = -2.0f * cos_w0; b[2] = 1.0f - alpha * gain; @@ -54,7 +56,7 @@ void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, a[2] = 1.0f - alpha / gain; break; - case BiquadType_LowPass: + case BiquadType::LowPass: b[0] = (1.0f - cos_w0) / 2.0f; b[1] = 1.0f - cos_w0; b[2] = (1.0f - cos_w0) / 2.0f; @@ -62,7 +64,7 @@ void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, a[1] = -2.0f * cos_w0; a[2] = 1.0f - alpha; break; - case BiquadType_HighPass: + case BiquadType::HighPass: b[0] = (1.0f + cos_w0) / 2.0f; b[1] = -(1.0f + cos_w0); b[2] = (1.0f + cos_w0) / 2.0f; @@ -70,7 +72,7 @@ void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, a[1] = -2.0f * cos_w0; a[2] = 1.0f - alpha; break; - case BiquadType_BandPass: + case BiquadType::BandPass: b[0] = alpha; b[1] = 0; b[2] = -alpha; |