2 ==============================================================================
3 This file is part of Tal-Reverb by Patrick Kunz.
5 Copyright(c) 2005-2009 Patrick Kunz, TAL
9 This file may be licensed under the terms of of the
10 GNU General Public License Version 2 (the ``GPL'').
12 Software distributed under the License is distributed
13 on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14 express or implied. See the GPL for the specific language
15 governing rights and limitations.
17 You should have received a copy of the GPL along with this
18 program. If not, go to http://www.gnu.org/licenses/gpl.html
19 or write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 ==============================================================================
25 #if !defined(__Filter_h)
33 float f
, k
, p
, scale
, r
, x
;
34 float y1
, y2
, y3
, y4
, oldx
, oldy1
, oldy2
, oldy3
;
35 float fCutoff
, fActualCutoff
;
38 float sampleRateFactor
;
42 Filter(float sampleRate
)
45 y1
= y2
= y3
= y4
= oldx
= oldy1
= oldy2
= oldy3
= 0.0f
;
47 if (sampleRate
<= 0.0f
) sampleRate
= 44100.0f
;
49 sampleRateFactor
= 44100.0f
/ sampleRate
;
50 if (sampleRateFactor
> 1.0f
)
51 sampleRateFactor
= 1.0f
;
53 fActualCutoff
= -1.0f
;
56 inline float process(float input
, float fCutoff
, float fRes
, bool highPass
)
58 if (fCutoff
!= fActualCutoff
)
60 fActualCutoff
= fCutoff
;
61 fCutoff
*= sampleRateFactor
;
62 updateValues(fCutoff
* fCutoff
);
66 // Four cascaded onepole filters (bilinear transform)
67 y1
= (x
+ oldx
) * p
- k
* y1
;
68 y2
= (y1
+ oldy1
) * p
- k
* y2
;
73 if (highPass
) input
= input
- y2
;
78 void updateValues(float fCutoff
)
80 // Filter section MOOG VCF
81 // CSound source code, Stilson/Smith CCRMA paper.
82 f
= fCutoff
; // [0 - 1]
83 k
= 3.6f
* f
- 1.6f
* f
* f
- 1.0f
; // (Empirical tunning) /// !!! original (convex)
85 p
= (k
+ 1.0f
) * 0.5f
; // scale [0, 1]
86 scale
= expf((1.0f
- p
) * 1.386249f
); // original