| 1 | #cxx . . $ `<file>/src/pt2_rcfilters.c`/include <math.h> |
| 2 | #include "pt2_header.h"cxx . . $ `<file>/src/pt2_header.h`/ |
| 3 | #include "pt2_rcfilters.h"cxx . . $ `<file>/src/pt2_rcfilters.h`/ |
| 4 | |
| 5 | #define SMALL_NUMBERcxx . . $ `src/pt2_rcfilters.c:5:9`! (1E-4) |
| 6 | |
| 7 | // 1-pole 6dB/oct RC low-pass filter (Direct Form II) |
| 8 | void setupOnePoleFiltercxx . . $ setupOnePoleFilter(b75bf72749fa347e).(double audioRate, double cutOff, onePoleFilter_tcxx . . $ onePoleFilter_t# *f) |
| 9 | { |
| 10 | // this implementation should be OK as long as cutoff is below audioRate/4 |
| 11 | |
| 12 | if (cutOff >= audioRate/2.0) |
| 13 | cutOff = (audioRate/2.0) - SMALL_NUMBERcxx . . $ `src/pt2_rcfilters.c:5:9`!; |
| 14 | |
| 15 | const double b1 = expcxx . . $ exp(447c67c6676f75c3).((-2.0 * PIcxx . . $ `src/pt2_header.h:67:9`!) * cutOff / audioRate); |
| 16 | const double a0 = 1.0 - b1; |
| 17 | |
| 18 | f->b1cxx . . $ onePoleFilter_t#b1. = (float)b1; |
| 19 | f->a0cxx . . $ onePoleFilter_t#a0. = (float)a0; |
| 20 | } |
| 21 | |
| 22 | void clearOnePoleFilterStatecxx . . $ clearOnePoleFilterState(f6e1329b7fbf0464).(onePoleFilter_tcxx . . $ onePoleFilter_t# *f) |
| 23 | { |
| 24 | f->tmpLcxx . . $ onePoleFilter_t#tmpL. = f->tmpRcxx . . $ onePoleFilter_t#tmpR. = 0.0f; |
| 25 | } |
| 26 | |
| 27 | void onePoleLPFiltercxx . . $ onePoleLPFilter(6750ec94a31cf12d).(onePoleFilter_tcxx . . $ onePoleFilter_t# *f, const float in, float *out) |
| 28 | { |
| 29 | f->tmpLcxx . . $ onePoleFilter_t#tmpL. = (in * f->a0cxx . . $ onePoleFilter_t#a0.) + (f->tmpLcxx . . $ onePoleFilter_t#tmpL. * f->b1cxx . . $ onePoleFilter_t#b1.); |
| 30 | *out = f->tmpLcxx . . $ onePoleFilter_t#tmpL.; |
| 31 | } |
| 32 | |
| 33 | void onePoleLPFilterStereocxx . . $ onePoleLPFilterStereo(90467eb7f67c8a3c).(onePoleFilter_tcxx . . $ onePoleFilter_t# *f, const float *in, float *out) |
| 34 | { |
| 35 | // left channel |
| 36 | f->tmpLcxx . . $ onePoleFilter_t#tmpL. = (in[0] * f->a0cxx . . $ onePoleFilter_t#a0.) + (f->tmpLcxx . . $ onePoleFilter_t#tmpL. * f->b1cxx . . $ onePoleFilter_t#b1.); |
| 37 | out[0] = f->tmpLcxx . . $ onePoleFilter_t#tmpL.; |
| 38 | |
| 39 | // right channel |
| 40 | f->tmpRcxx . . $ onePoleFilter_t#tmpR. = (in[1] * f->a0cxx . . $ onePoleFilter_t#a0.) + (f->tmpRcxx . . $ onePoleFilter_t#tmpR. * f->b1cxx . . $ onePoleFilter_t#b1.); |
| 41 | out[1] = f->tmpRcxx . . $ onePoleFilter_t#tmpR.; |
| 42 | } |
| 43 | |
| 44 | void onePoleHPFiltercxx . . $ onePoleHPFilter(6750ec94a31cf12d).(onePoleFilter_tcxx . . $ onePoleFilter_t# *f, const float in, float *out) |
| 45 | { |
| 46 | f->tmpLcxx . . $ onePoleFilter_t#tmpL. = (in * f->a0cxx . . $ onePoleFilter_t#a0.) + (f->tmpLcxx . . $ onePoleFilter_t#tmpL. * f->b1cxx . . $ onePoleFilter_t#b1.); |
| 47 | *out = in - f->tmpLcxx . . $ onePoleFilter_t#tmpL.; |
| 48 | } |
| 49 | |
| 50 | void onePoleHPFilterStereocxx . . $ onePoleHPFilterStereo(90467eb7f67c8a3c).(onePoleFilter_tcxx . . $ onePoleFilter_t# *f, const float *in, float *out) |
| 51 | { |
| 52 | // left channel |
| 53 | f->tmpLcxx . . $ onePoleFilter_t#tmpL. = (in[0] * f->a0cxx . . $ onePoleFilter_t#a0.) + (f->tmpLcxx . . $ onePoleFilter_t#tmpL. * f->b1cxx . . $ onePoleFilter_t#b1.); |
| 54 | out[0] = in[0] - f->tmpLcxx . . $ onePoleFilter_t#tmpL.; |
| 55 | |
| 56 | // right channel |
| 57 | f->tmpRcxx . . $ onePoleFilter_t#tmpR. = (in[1] * f->a0cxx . . $ onePoleFilter_t#a0.) + (f->tmpRcxx . . $ onePoleFilter_t#tmpR. * f->b1cxx . . $ onePoleFilter_t#b1.); |
| 58 | out[1] = in[1] - f->tmpRcxx . . $ onePoleFilter_t#tmpR.; |
| 59 | } |
| 60 | |
| 61 | /* 2-pole RC low-pass filter with Q factor, based on: |
| 62 | ** https://www.musicdsp.org/en/latest/Filters/38-lp-and-hp-filter.html |
| 63 | */ |
| 64 | |
| 65 | void setupTwoPoleFiltercxx . . $ setupTwoPoleFilter(ae4e7863516b7826).(double audioRate, double cutOff, double qFactor, twoPoleFilter_tcxx . . $ twoPoleFilter_t# *f) |
| 66 | { |
| 67 | if (cutOff >= audioRate/2.0) |
| 68 | cutOff = (audioRate/2.0) - SMALL_NUMBERcxx . . $ `src/pt2_rcfilters.c:5:9`!; |
| 69 | |
| 70 | const double a = 1.0 / tancxx . . $ tan(447c67c6676f75c3).((PIcxx . . $ `src/pt2_header.h:67:9`! * cutOff) / audioRate); |
| 71 | const double b = 1.0 / qFactor; |
| 72 | |
| 73 | const double a1 = 1.0 / (1.0 + b * a + a * a); |
| 74 | const double a2 = (2.0 * a1); |
| 75 | const double b1 = 2.0 * (1.0 - a*a) * a1; |
| 76 | const double b2 = (1.0 - b * a + a * a) * a1; |
| 77 | |
| 78 | f->a1cxx . . $ twoPoleFilter_t#a1. = (float)a1; |
| 79 | f->a2cxx . . $ twoPoleFilter_t#a2. = (float)a2; |
| 80 | f->b1cxx . . $ twoPoleFilter_t#b1. = (float)b1; |
| 81 | f->b2cxx . . $ twoPoleFilter_t#b2. = (float)b2; |
| 82 | } |
| 83 | |
| 84 | void clearTwoPoleFilterStatecxx . . $ clearTwoPoleFilterState(1647a0868bd893a0).(twoPoleFilter_tcxx . . $ twoPoleFilter_t# *f) |
| 85 | { |
| 86 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[0] = f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[1] = f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[2] = f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[3] = 0.0f; |
| 87 | f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[0] = f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[1] = f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[2] = f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[3] = 0.0f; |
| 88 | } |
| 89 | |
| 90 | void twoPoleLPFiltercxx . . $ twoPoleLPFilter(ccd04d20cea2f819).(twoPoleFilter_tcxx . . $ twoPoleFilter_t# *f, const float in, float *out) |
| 91 | { |
| 92 | const float LOut = (in * f->a1cxx . . $ twoPoleFilter_t#a1.) + (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[0] * f->a2cxx . . $ twoPoleFilter_t#a2.) + (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[1] * f->a1cxx . . $ twoPoleFilter_t#a1.) - (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[2] * f->b1cxx . . $ twoPoleFilter_t#b1.) - (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[3] * f->b2cxx . . $ twoPoleFilter_t#b2.); |
| 93 | |
| 94 | // shift states |
| 95 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[1] = f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[0]; |
| 96 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[0] = in; |
| 97 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[3] = f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[2]; |
| 98 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[2] = LOut; |
| 99 | |
| 100 | // set output |
| 101 | *out = LOut; |
| 102 | } |
| 103 | |
| 104 | void twoPoleLPFilterStereocxx . . $ twoPoleLPFilterStereo(77050a5006a1adb7).(twoPoleFilter_tcxx . . $ twoPoleFilter_t# *f, const float *in, float *out) |
| 105 | { |
| 106 | const float LOut = (in[0] * f->a1cxx . . $ twoPoleFilter_t#a1.) + (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[0] * f->a2cxx . . $ twoPoleFilter_t#a2.) + (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[1] * f->a1cxx . . $ twoPoleFilter_t#a1.) - (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[2] * f->b1cxx . . $ twoPoleFilter_t#b1.) - (f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[3] * f->b2cxx . . $ twoPoleFilter_t#b2.); |
| 107 | const float ROut = (in[1] * f->a1cxx . . $ twoPoleFilter_t#a1.) + (f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[0] * f->a2cxx . . $ twoPoleFilter_t#a2.) + (f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[1] * f->a1cxx . . $ twoPoleFilter_t#a1.) - (f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[2] * f->b1cxx . . $ twoPoleFilter_t#b1.) - (f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[3] * f->b2cxx . . $ twoPoleFilter_t#b2.); |
| 108 | |
| 109 | // shift states |
| 110 | |
| 111 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[1] = f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[0]; |
| 112 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[0] = in[0]; |
| 113 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[3] = f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[2]; |
| 114 | f->tmpLcxx . . $ twoPoleFilter_t#tmpL.[2] = LOut; |
| 115 | |
| 116 | f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[1] = f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[0]; |
| 117 | f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[0] = in[1]; |
| 118 | f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[3] = f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[2]; |
| 119 | f->tmpRcxx . . $ twoPoleFilter_t#tmpR.[2] = ROut; |
| 120 | |
| 121 | // set outputs |
| 122 | |
| 123 | out[0] = LOut; |
| 124 | out[1] = ROut; |
| 125 | } |