libsidplayfp  2.15.0
Filter.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 FILTER_H
24 #define FILTER_H
25 
26 #include "FilterModelConfig.h"
27 #include "Voice.h"
28 
29 #include "siddefs-fp.h"
30 
31 namespace reSIDfp
32 {
33 
37 class Filter
38 {
39 private:
40  unsigned short* mixer;
41  unsigned short* summer;
42  unsigned short* resonance;
43  unsigned short* volume;
44 
45  FilterModelConfig& fmc;
46 
48  unsigned short* currentMixer = nullptr;
49 
51  unsigned short* currentSummer = nullptr;
52 
54  unsigned short* currentResonance = nullptr;
55 
57  unsigned short* currentVolume = nullptr;
58 
59 protected:
61  int Vhp = 0;
62 
64  int Vbp = 0;
65 
67  int Vlp = 0;
68 
69 private:
71  int Ve = 0;
72 
74  unsigned int fc = 0;
75 
77 
78  bool filt1 = false;
79  bool filt2 = false;
80  bool filt3 = false;
81  bool filtE = false;
83 
85  bool voice3off = false;
86 
87 protected:
89 
90  bool hp = false;
91  bool bp = false;
92  bool lp = false;
94 
95 private:
97  unsigned char vol = 0;
98 
100  bool enabled = true;
101 
103  unsigned char filt = 0;
104 
105 private:
106  inline int getNormalizedVoice(Voice& v) const
107  {
108  return fmc.getNormalizedVoice(v.output(), v.envelope()->output());
109  }
110 
111 protected:
115  virtual void updateCenterFrequency() = 0;
116 
122  void updateResonance(unsigned char res) { currentResonance = resonance + (res * (1<<16)); }
123 
127  void updateMixing();
128 
132  inline unsigned int getFC() const { return fc; }
133 
134  virtual int solveIntegrators() = 0;
135 
136 public:
138 
139  virtual ~Filter() = default;
140 
149  unsigned short clock(Voice& v1, Voice& v2, Voice& v3);
150 
156  void enable(bool enable);
157 
161  void reset();
162 
168  void writeFC_LO(unsigned char fc_lo);
169 
175  void writeFC_HI(unsigned char fc_hi);
176 
182  void writeRES_FILT(unsigned char res_filt);
183 
189  void writeMODE_VOL(unsigned char mode_vol);
190 
196  void input(short input) { Ve = fmc.getNormalizedVoice(input/32768.f, 0); }
197 };
198 
199 } // namespace reSIDfp
200 
201 #if RESID_INLINING || defined(FILTER_CPP)
202 
203 namespace reSIDfp
204 {
205 
206 RESID_INLINE
207 unsigned short Filter::clock(Voice& voice1, Voice& voice2, Voice& voice3)
208 {
209  const int V1 = getNormalizedVoice(voice1);
210  const int V2 = getNormalizedVoice(voice2);
211  // Voice 3 is silenced by voice3off if it is not routed through the filter.
212  const int V3 = (filt3 || !voice3off) ? getNormalizedVoice(voice3) : 0;
213 
214  int Vsum = 0;
215  int Vmix = 0;
216 
217  (filt1 ? Vsum : Vmix) += V1;
218  (filt2 ? Vsum : Vmix) += V2;
219  (filt3 ? Vsum : Vmix) += V3;
220  (filtE ? Vsum : Vmix) += Ve;
221 
222  Vhp = currentSummer[currentResonance[Vbp] + Vlp + Vsum];
223 
224  Vmix += solveIntegrators();
225 
226  return currentVolume[currentMixer[Vmix]];
227 }
228 
229 } // namespace reSIDfp
230 
231 #endif
232 
233 #endif
unsigned int output() const
Definition: EnvelopeGenerator.h:130
Definition: FilterModelConfig.h:40
Definition: Filter.h:38
bool hp
Highpass, bandpass, and lowpass filter modes.
Definition: Filter.h:90
int Vbp
Filter bandpass state.
Definition: Filter.h:64
void updateResonance(unsigned char res)
Definition: Filter.h:122
void writeFC_LO(unsigned char fc_lo)
Definition: Filter.cpp:75
void writeRES_FILT(unsigned char res_filt)
Definition: Filter.cpp:87
void writeFC_HI(unsigned char fc_hi)
Definition: Filter.cpp:81
int Vhp
Filter highpass state.
Definition: Filter.h:61
unsigned short clock(Voice &v1, Voice &v2, Voice &v3)
Definition: Filter.h:207
int Vlp
Filter lowpass state.
Definition: Filter.h:67
void enable(bool enable)
Definition: Filter.cpp:125
void reset()
Definition: Filter.cpp:139
virtual void updateCenterFrequency()=0
void updateMixing()
Definition: Filter.cpp:51
void input(short input)
Definition: Filter.h:196
void writeMODE_VOL(unsigned char mode_vol)
Definition: Filter.cpp:104
unsigned int getFC() const
Definition: Filter.h:132
Definition: Voice.h:37
RESID_INLINE float output()
Definition: Voice.h:65