Use blended HRIRs for the B-Format decode
[openal-soft.git] / alc / filters / splitter.cpp
blobc6218e70c5671800825ad1e494b2922db69774fe
2 #include "config.h"
4 #include "splitter.h"
6 #include <algorithm>
7 #include <cmath>
8 #include <limits>
10 #include "math_defs.h"
11 #include "opthelpers.h"
14 template<typename Real>
15 void BandSplitterR<Real>::init(Real f0norm)
17 const Real w{f0norm * al::MathDefs<Real>::Tau()};
18 const Real cw{std::cos(w)};
19 if(cw > std::numeric_limits<float>::epsilon())
20 mCoeff = (std::sin(w) - 1.0f) / cw;
21 else
22 mCoeff = cw * -0.5f;
24 mLpZ1 = 0.0f;
25 mLpZ2 = 0.0f;
26 mApZ1 = 0.0f;
29 template<typename Real>
30 void BandSplitterR<Real>::process(Real *hpout, Real *lpout, const Real *input, const size_t count)
32 ASSUME(count > 0);
34 const Real ap_coeff{mCoeff};
35 const Real lp_coeff{mCoeff*0.5f + 0.5f};
36 Real lp_z1{mLpZ1};
37 Real lp_z2{mLpZ2};
38 Real ap_z1{mApZ1};
39 auto proc_sample = [ap_coeff,lp_coeff,&lp_z1,&lp_z2,&ap_z1,&lpout](const Real in) noexcept -> Real
41 /* Low-pass sample processing. */
42 Real d{(in - lp_z1) * lp_coeff};
43 Real lp_y{lp_z1 + d};
44 lp_z1 = lp_y + d;
46 d = (lp_y - lp_z2) * lp_coeff;
47 lp_y = lp_z2 + d;
48 lp_z2 = lp_y + d;
50 *(lpout++) = lp_y;
52 /* All-pass sample processing. */
53 Real ap_y{in*ap_coeff + ap_z1};
54 ap_z1 = in - ap_y*ap_coeff;
56 /* High-pass generated from removing low-passed output. */
57 return ap_y - lp_y;
59 std::transform(input, input+count, hpout, proc_sample);
60 mLpZ1 = lp_z1;
61 mLpZ2 = lp_z2;
62 mApZ1 = ap_z1;
65 template<typename Real>
66 void BandSplitterR<Real>::applyHfScale(Real *samples, const Real hfscale, const size_t count)
68 ASSUME(count > 0);
70 const Real ap_coeff{mCoeff};
71 const Real lp_coeff{mCoeff*0.5f + 0.5f};
72 Real lp_z1{mLpZ1};
73 Real lp_z2{mLpZ2};
74 Real ap_z1{mApZ1};
75 auto proc_sample = [hfscale,ap_coeff,lp_coeff,&lp_z1,&lp_z2,&ap_z1](const Real in) noexcept -> Real
77 /* Low-pass sample processing. */
78 Real d{(in - lp_z1) * lp_coeff};
79 Real lp_y{lp_z1 + d};
80 lp_z1 = lp_y + d;
82 d = (lp_y - lp_z2) * lp_coeff;
83 lp_y = lp_z2 + d;
84 lp_z2 = lp_y + d;
86 /* All-pass sample processing. */
87 Real ap_y{in*ap_coeff + ap_z1};
88 ap_z1 = in - ap_y*ap_coeff;
90 /* High-pass generated from removing low-passed output. */
91 return (ap_y-lp_y)*hfscale + lp_y;
93 std::transform(samples, samples+count, samples, proc_sample);
94 mLpZ1 = lp_z1;
95 mLpZ2 = lp_z2;
96 mApZ1 = ap_z1;
99 template<typename Real>
100 void BandSplitterR<Real>::applyAllpass(Real *samples, const size_t count) const
102 ASSUME(count > 0);
104 const Real coeff{mCoeff};
105 Real z1{0.0f};
106 auto proc_sample = [coeff,&z1](const Real in) noexcept -> Real
108 const Real out{in*coeff + z1};
109 z1 = in - out*coeff;
110 return out;
112 std::transform(samples, samples+count, samples, proc_sample);
116 template class BandSplitterR<float>;
117 template class BandSplitterR<double>;