diff options
author | Chris Robinson <[email protected]> | 2018-12-04 20:35:23 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2018-12-04 20:35:23 -0800 |
commit | 2a30ae3807134be37cf79139e7e7943df5b1bf64 (patch) | |
tree | 777e5e002ae29781faf79adb26d951914c4acb72 /Alc/filters | |
parent | 3866c9f9414e91e760f397d0d01514a411c77bcd (diff) |
Avoid some more explicit loops in the filters
Diffstat (limited to 'Alc/filters')
-rw-r--r-- | Alc/filters/filter.cpp | 13 | ||||
-rw-r--r-- | Alc/filters/nfc.cpp | 35 | ||||
-rw-r--r-- | Alc/filters/splitter.cpp | 66 |
3 files changed, 53 insertions, 61 deletions
diff --git a/Alc/filters/filter.cpp b/Alc/filters/filter.cpp index 6099cf13..5dc5d9b4 100644 --- a/Alc/filters/filter.cpp +++ b/Alc/filters/filter.cpp @@ -92,14 +92,13 @@ void BiquadFilter_setParams(BiquadFilter *filter, BiquadType type, ALfloat gain, void BiquadFilter_processC(BiquadFilter *filter, ALfloat *RESTRICT dst, const ALfloat *RESTRICT src, ALsizei numsamples) { - const ALfloat a1 = filter->a1; - const ALfloat a2 = filter->a2; const ALfloat b0 = filter->b0; const ALfloat b1 = filter->b1; const ALfloat b2 = filter->b2; + const ALfloat a1 = filter->a1; + const ALfloat a2 = filter->a2; ALfloat z1 = filter->z1; ALfloat z2 = filter->z2; - ALsizei i; ASSUME(numsamples > 0); @@ -111,14 +110,14 @@ void BiquadFilter_processC(BiquadFilter *filter, ALfloat *RESTRICT dst, const AL * * See: http://www.earlevel.com/main/2003/02/28/biquads/ */ - for(i = 0;i < numsamples;i++) + auto proc_sample = [b0,b1,b2,a1,a2,&z1,&z2](ALfloat input) noexcept -> ALfloat { - ALfloat input = src[i]; ALfloat output = input*b0 + z1; z1 = input*b1 - output*a1 + z2; z2 = input*b2 - output*a2; - dst[i] = output; - } + return output; + }; + std::transform<const ALfloat*RESTRICT>(src, src+numsamples, dst, proc_sample); filter->z1 = z1; filter->z2 = z2; diff --git a/Alc/filters/nfc.cpp b/Alc/filters/nfc.cpp index 8d61bb37..11a7b467 100644 --- a/Alc/filters/nfc.cpp +++ b/Alc/filters/nfc.cpp @@ -2,6 +2,9 @@ #include "config.h" #include "nfc.h" + +#include <algorithm> + #include "alMain.h" #include <string.h> @@ -228,18 +231,17 @@ void NfcFilterProcess1(NfcFilter *nfc, float *RESTRICT dst, const float *RESTRIC const float b1 = nfc->first.b1; const float a1 = nfc->first.a1; float z1 = nfc->first.z[0]; - int i; ASSUME(count > 0); - for(i = 0;i < count;i++) + auto proc_sample = [gain,b1,a1,&z1](float in) noexcept -> float { - float y = src[i]*gain - a1*z1; + float y = in*gain - a1*z1; float out = y + b1*z1; z1 += y; - - dst[i] = out; - } + return out; + }; + std::transform<const float*RESTRICT>(src, src+count, dst, proc_sample); nfc->first.z[0] = z1; } @@ -252,19 +254,18 @@ void NfcFilterProcess2(NfcFilter *nfc, float *RESTRICT dst, const float *RESTRIC const float a2 = nfc->second.a2; float z1 = nfc->second.z[0]; float z2 = nfc->second.z[1]; - int i; ASSUME(count > 0); - for(i = 0;i < count;i++) + auto proc_sample = [gain,b1,b2,a1,a2,&z1,&z2](float in) noexcept -> float { - float y = src[i]*gain - a1*z1 - a2*z2; + float y = in*gain - a1*z1 - a2*z2; float out = y + b1*z1 + b2*z2; z2 += z1; z1 += y; - - dst[i] = out; - } + return out; + }; + std::transform<const float*RESTRICT>(src, src+count, dst, proc_sample); nfc->second.z[0] = z1; nfc->second.z[1] = z2; } @@ -281,13 +282,12 @@ void NfcFilterProcess3(NfcFilter *nfc, float *RESTRICT dst, const float *RESTRIC float z1 = nfc->third.z[0]; float z2 = nfc->third.z[1]; float z3 = nfc->third.z[2]; - int i; ASSUME(count > 0); - for(i = 0;i < count;i++) + auto proc_sample = [gain,b1,b2,b3,a1,a2,a3,&z1,&z2,&z3](float in) noexcept -> float { - float y = src[i]*gain - a1*z1 - a2*z2; + float y = in*gain - a1*z1 - a2*z2; float out = y + b1*z1 + b2*z2; z2 += z1; z1 += y; @@ -296,8 +296,9 @@ void NfcFilterProcess3(NfcFilter *nfc, float *RESTRICT dst, const float *RESTRIC out = y + b3*z3; z3 += y; - dst[i] = out; - } + return out; + }; + std::transform<const float*RESTRICT>(src, src+count, dst, proc_sample); nfc->third.z[0] = z1; nfc->third.z[1] = z2; nfc->third.z[2] = z3; diff --git a/Alc/filters/splitter.cpp b/Alc/filters/splitter.cpp index 6aed7493..570f5e2c 100644 --- a/Alc/filters/splitter.cpp +++ b/Alc/filters/splitter.cpp @@ -3,15 +3,18 @@ #include "splitter.h" +#include <cmath> +#include <algorithm> + #include "math_defs.h" void bandsplit_init(BandSplitter *splitter, ALfloat f0norm) { ALfloat w = f0norm * F_TAU; - ALfloat cw = cosf(w); + ALfloat cw = std::cos(w); if(cw > FLT_EPSILON) - splitter->coeff = (sinf(w) - 1.0f) / cw; + splitter->coeff = (std::sin(w) - 1.0f) / cw; else splitter->coeff = cw * -0.5f; @@ -30,51 +33,46 @@ void bandsplit_clear(BandSplitter *splitter) void bandsplit_process(BandSplitter *splitter, ALfloat *RESTRICT hpout, ALfloat *RESTRICT lpout, const ALfloat *input, ALsizei count) { - ALfloat lp_coeff, hp_coeff, lp_y, hp_y, d; - ALfloat lp_z1, lp_z2, hp_z1; - ALsizei i; - ASSUME(count > 0); - hp_coeff = splitter->coeff; - lp_coeff = splitter->coeff*0.5f + 0.5f; - lp_z1 = splitter->lp_z1; - lp_z2 = splitter->lp_z2; - hp_z1 = splitter->hp_z1; - for(i = 0;i < count;i++) + const float ap_coeff{splitter->coeff}; + const float lp_coeff{splitter->coeff*0.5f + 0.5f}; + float lp_z1{splitter->lp_z1}; + float lp_z2{splitter->lp_z2}; + float ap_z1{splitter->hp_z1}; + auto proc_sample = [ap_coeff,lp_coeff,&lp_z1,&lp_z2,&ap_z1,&lpout](const float in) noexcept -> float { - ALfloat in = input[i]; - /* Low-pass sample processing. */ - d = (in - lp_z1) * lp_coeff; - lp_y = lp_z1 + d; + float d{(in - lp_z1) * lp_coeff}; + float lp_y{lp_z1 + d}; lp_z1 = lp_y + d; d = (lp_y - lp_z2) * lp_coeff; lp_y = lp_z2 + d; lp_z2 = lp_y + d; - lpout[i] = lp_y; + *(lpout++) = lp_y; /* All-pass sample processing. */ - hp_y = in*hp_coeff + hp_z1; - hp_z1 = in - hp_y*hp_coeff; + float ap_y{in*ap_coeff + ap_z1}; + ap_z1 = in - ap_y*ap_coeff; /* High-pass generated from removing low-passed output. */ - hpout[i] = hp_y - lp_y; - } + return ap_y - lp_y; + }; + std::transform<const float*RESTRICT>(input, input+count, hpout, proc_sample); splitter->lp_z1 = lp_z1; splitter->lp_z2 = lp_z2; - splitter->hp_z1 = hp_z1; + splitter->hp_z1 = ap_z1; } void splitterap_init(SplitterAllpass *splitter, ALfloat f0norm) { ALfloat w = f0norm * F_TAU; - ALfloat cw = cosf(w); + ALfloat cw = std::cos(w); if(cw > FLT_EPSILON) - splitter->coeff = (sinf(w) - 1.0f) / cw; + splitter->coeff = (std::sin(w) - 1.0f) / cw; else splitter->coeff = cw * -0.5f; @@ -88,22 +86,16 @@ void splitterap_clear(SplitterAllpass *splitter) void splitterap_process(SplitterAllpass *splitter, ALfloat *RESTRICT samples, ALsizei count) { - ALfloat coeff, in, out; - ALfloat z1; - ALsizei i; - ASSUME(count > 0); - coeff = splitter->coeff; - z1 = splitter->z1; - for(i = 0;i < count;i++) + const float coeff{splitter->coeff}; + float z1{splitter->z1}; + auto proc_sample = [coeff,&z1](const float in) noexcept -> float { - in = samples[i]; - - out = in*coeff + z1; + float out{in*coeff + z1}; z1 = in - out*coeff; - - samples[i] = out; - } + return out; + }; + std::transform(samples, samples+count, samples, proc_sample); splitter->z1 = z1; } |