Value_Smoothing_Filter.cpp (2447B)
1 2 /*******************************************************************************/ 3 /* Copyright (C) 2008-2020 Jonathan Moore Liles */ 4 /* */ 5 /* This program is free software; you can redistribute it and/or modify it */ 6 /* under the terms of the GNU General Public License as published by the */ 7 /* Free Software Foundation; either version 2 of the License, or (at your */ 8 /* option) any later version. */ 9 /* */ 10 /* This program is distributed in the hope that it will be useful, but WITHOUT */ 11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */ 12 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for */ 13 /* more details. */ 14 /* */ 15 /* You should have received a copy of the GNU General Public License along */ 16 /* with This program; see the file COPYING. If not,write to the Free Software */ 17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 /*******************************************************************************/ 19 20 #include "Value_Smoothing_Filter.h" 21 #include <math.h> 22 23 /* compensate for missing nonlib macro */ 24 #define assume_aligned(x) (x) 25 26 void 27 Value_Smoothing_Filter::sample_rate ( nframes_t n ) 28 { 29 const float FS = n; 30 const float T = 0.05f; 31 32 w = _cutoff / (FS * T); 33 } 34 35 bool 36 Value_Smoothing_Filter::apply( sample_t * __restrict__ dst, nframes_t nframes, float gt ) 37 { 38 if ( _reset_on_next_apply ) 39 { 40 reset( gt ); 41 _reset_on_next_apply = false; 42 return false; 43 } 44 45 if ( target_reached(gt) ) 46 return false; 47 48 sample_t * dst_ = (sample_t*) assume_aligned(dst); 49 50 const float a = 0.07f; 51 const float b = 1 + a; 52 53 const float gm = b * gt; 54 55 float g1 = this->g1; 56 float g2 = this->g2; 57 58 for (nframes_t i = 0; i < nframes; i++) 59 { 60 g1 += w * (gm - g1 - a * g2); 61 g2 += w * (g1 - g2); 62 dst_[i] = g2; 63 } 64 65 g2 += 1e-10f; /* denormal protection */ 66 67 if ( fabsf( gt - g2 ) < t ) 68 g2 = gt; 69 70 this->g1 = g1; 71 this->g2 = g2; 72 73 return true; 74 }