1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2017 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 #ifdef __LIBRETRO__
27 #include "../../../sidcxx11.h"
28 #endif
29 
30 namespace reSIDfp
31 {
32 
33 /**
34  * SID filter base class
35  */
36 class Filter
37 {
38 protected:
39     /// Current volume amplifier setting.
40     unsigned short* currentGain;
41 
42     /// Current filter/voice mixer setting.
43     unsigned short* currentMixer;
44 
45     /// Filter input summer setting.
46     unsigned short* currentSummer;
47 
48     /// Filter resonance value.
49     unsigned short* currentResonance;
50 
51     /// Filter highpass state.
52     int Vhp;
53 
54     /// Filter bandpass state.
55     int Vbp;
56 
57     /// Filter lowpass state.
58     int Vlp;
59 
60     /// Filter external input.
61     int ve;
62 
63     /// Filter cutoff frequency.
64     unsigned int fc;
65 
66     /// Routing to filter or outside filter
67     bool filt1, filt2, filt3, filtE;
68 
69     /// Switch voice 3 off.
70     bool voice3off;
71 
72     /// Highpass, bandpass, and lowpass filter modes.
73     bool hp, bp, lp;
74 
75     /// Current volume.
76     unsigned char vol;
77 
78 private:
79     /// Filter enabled.
80     bool enabled;
81 
82     /// Selects which inputs to route through filter.
83     unsigned char filt;
84 
85 protected:
86     /**
87      * Set filter cutoff frequency.
88      */
89     virtual void updatedCenterFrequency() = 0;
90 
91     /**
92      * Set filter resonance.
93      */
94     virtual void updateResonance(unsigned char res) = 0;
95 
96     /**
97      * Mixing configuration modified (offsets change)
98      */
99     virtual void updatedMixing() = 0;
100 
101 public:
Filter()102     Filter() :
103         currentGain(nullptr),
104         currentMixer(nullptr),
105         currentSummer(nullptr),
106         currentResonance(nullptr),
107         Vhp(0),
108         Vbp(0),
109         Vlp(0),
110         ve(0),
111         fc(0),
112         filt1(false),
113         filt2(false),
114         filt3(false),
115         filtE(false),
116         voice3off(false),
117         hp(false),
118         bp(false),
119         lp(false),
120         vol(0),
121         enabled(true),
122         filt(0) {}
123 
~Filter()124     virtual ~Filter() {}
125 
126     /**
127      * SID clocking - 1 cycle
128      *
129      * @param v1 voice 1 in
130      * @param v2 voice 2 in
131      * @param v3 voice 3 in
132      * @return filtered output
133      */
134     virtual unsigned short clock(int v1, int v2, int v3) = 0;
135 
136     /**
137      * Enable filter.
138      *
139      * @param enable
140      */
141     void enable(bool enable);
142 
143     /**
144      * SID reset.
145      */
146     void reset();
147 
148     /**
149      * Write Frequency Cutoff Low register.
150      *
151      * @param fc_lo Frequency Cutoff Low-Byte
152      */
153     void writeFC_LO(unsigned char fc_lo);
154 
155     /**
156      * Write Frequency Cutoff High register.
157      *
158      * @param fc_hi Frequency Cutoff High-Byte
159      */
160     void writeFC_HI(unsigned char fc_hi);
161 
162     /**
163      * Write Resonance/Filter register.
164      *
165      * @param res_filt Resonance/Filter
166      */
167     void writeRES_FILT(unsigned char res_filt);
168 
169     /**
170      * Write filter Mode/Volume register.
171      *
172      * @param mode_vol Filter Mode/Volume
173      */
174     void writeMODE_VOL(unsigned char mode_vol);
175 
176     virtual void input(int input) = 0;
177 };
178 
179 } // namespace reSIDfp
180 
181 #endif
182