libresidfp 0.9.1
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
31namespace reSIDfp
32{
33
37class Filter
38{
39private:
40 unsigned short* mixer;
41 unsigned short* summer;
42 unsigned short* resonance;
43 unsigned short* volume;
44
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
59protected:
61 int Vhp = 0;
62
64 int Vbp = 0;
65
67 int Vlp = 0;
68
69private:
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
87protected:
89
90 bool hp = false;
91 bool bp = false;
92 bool lp = false;
94
95private:
97 unsigned char vol = 0;
98
100 bool enabled = true;
101
103 unsigned char filt = 0;
104
105private:
106 inline int getNormalizedVoice(Voice& v) const
107 {
108 return fmc.getNormalizedVoice(v.output(), v.envelope()->output());
109 }
110
111 // If voice 3 is off we still need to clock the waveform generator
112 inline int getSilentVoice(Voice& v) const
113 {
114 v.wave()->output();
115 return 0;
116 }
117
118protected:
122 virtual void updateCenterFrequency() = 0;
123
129 void updateResonance(unsigned char res) { currentResonance = resonance + (res * (1<<16)); }
130
134 void updateMixing();
135
139 inline unsigned int getFC() const { return fc; }
140
141 virtual int solveIntegrators() = 0;
142
143public:
145
146 virtual ~Filter() = default;
147
156 unsigned short clock(Voice& v1, Voice& v2, Voice& v3);
157
163 void enable(bool enable);
164
168 void reset();
169
175 void writeFC_LO(unsigned char fc_lo);
176
182 void writeFC_HI(unsigned char fc_hi);
183
189 void writeRES_FILT(unsigned char res_filt);
190
196 void writeMODE_VOL(unsigned char mode_vol);
197
203 void input(short input) { Ve = fmc.getNormalizedVoice(input/32768.f, 0); }
204};
205
206} // namespace reSIDfp
207
208#if RESIDFP_INLINING || defined(FILTER_CPP)
209
210namespace reSIDfp
211{
212
213RESIDFP_INLINE
214unsigned short Filter::clock(Voice& voice1, Voice& voice2, Voice& voice3)
215{
216 const int V1 = getNormalizedVoice(voice1);
217 const int V2 = getNormalizedVoice(voice2);
218 // Voice 3 is silenced by voice3off if it is not routed through the filter.
219 const int V3 = (filt3 || !voice3off) ? getNormalizedVoice(voice3) : getSilentVoice(voice3);
220
221 int Vsum = 0;
222 int Vmix = 0;
223
224 (filt1 ? Vsum : Vmix) += V1;
225 (filt2 ? Vsum : Vmix) += V2;
226 (filt3 ? Vsum : Vmix) += V3;
227 (filtE ? Vsum : Vmix) += Ve;
228
229 Vhp = currentSummer[currentResonance[Vbp] + Vlp + Vsum];
230
231 Vmix += solveIntegrators();
232
233 return currentVolume[currentMixer[Vmix]];
234}
235
236} // namespace reSIDfp
237
238#endif
239
240#endif
unsigned int output() const
Definition EnvelopeGenerator.h:130
Definition FilterModelConfig.h:40
Definition Filter.h:38
unsigned short clock(Voice &v1, Voice &v2, Voice &v3)
Definition Filter.h:214
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:129
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
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:203
void writeMODE_VOL(unsigned char mode_vol)
Definition Filter.cpp:104
unsigned int getFC() const
Definition Filter.h:139
Definition Voice.h:37
RESIDFP_INLINE float output()
Definition Voice.h:65
unsigned int output()
Definition WaveformGenerator.h:350