libresidfp  1.2.3
ExternalFilter.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2025 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2004 Dag Lem <resid@nimrod.no>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef EXTERNALFILTER_H
24 #define EXTERNALFILTER_H
25 
26 #include "siddefs-fp.h"
27 
28 #include <cstdint>
29 
30 namespace reSIDfp
31 {
32 
87 {
88  friend class State;
89 
90 private:
92  int32_t Vlp;
93 
95  int32_t Vhp;
96 
97  int32_t w0lp_1_s7 = 0;
98 
99  int32_t w0hp_1_s17 = 0;
100 
101  double m_frequency;
102 
103  double m_ext_res = 10e3;
104 
105 private:
106  void recalcParams();
107 
108 public:
115  int32_t clock(int32_t input);
116 
120  ExternalFilter();
121 
127  void setClockFrequency(double frequency);
128 
134  void setExtResistance(double res);
135 
139  void reset();
140 };
141 
142 } // namespace reSIDfp
143 
144 #if RESIDFP_INLINING || defined(EXTERNALFILTER_CPP)
145 
146 namespace reSIDfp
147 {
148 
149 RESIDFP_INLINE
150 int32_t ExternalFilter::clock(int32_t input)
151 {
152 #if __cplusplus >= 202002L
153  const int32_t Vi = input << 11;
154 #else
155  // Left shift of negative values is undefined
156  const int32_t Vi = input * (1 << 11);
157 #endif
158  const int32_t dVlp = (w0lp_1_s7 * (Vi - Vlp)) >> 7;
159  const int32_t dVhp = (w0hp_1_s17 * (Vlp - Vhp)) >> 17;
160  Vlp += dVlp;
161  Vhp += dVhp;
162  return (Vlp - Vhp) >> 11;
163 }
164 
165 } // namespace reSIDfp
166 
167 #endif
168 
169 #endif
ExternalFilter()
Definition: ExternalFilter.cpp:41
Definition: Dac.cpp:27
Definition: ExternalFilter.h:86
void setExtResistance(double res)
Definition: ExternalFilter.cpp:52
void reset()
Definition: ExternalFilter.cpp:71
void setClockFrequency(double frequency)
Definition: ExternalFilter.cpp:46
Definition: State.h:45
int32_t clock(int32_t input)
Definition: ExternalFilter.h:150