Debian Codegraph @

src/resid-fp/filterfp.h

raw symbols SCIP
1 // This file is part of reSID, a MOS6581 SID emulator engine.
2 // Copyright (C) 2004 Dag Lem <[email protected]>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // ---------------------------------------------------------------------------
18 // Filter distortion code written by Antti S. Lankila 2007 - 2009.
19
20 #ifndef __FILTERFP_H__
21 #define __FILTERFP_H__
22
23 #define M_LN2 0.69314718055994530942 /* log_e 2 */
24 #define M_PI 3.14159265358979323846 /* pi */
25
26 #include <math.h>
27 #include "siddefsfp.h"
28
29 // ----------------------------------------------------------------------------
30 // The SID filter is modeled with a two-integrator-loop biquadratic filter,
31 // which has been confirmed by Bob Yannes to be the actual circuit used in
32 // the SID chip.
33 //
34 // Measurements show that excellent emulation of the SID filter is achieved,
35 // except when high resonance is combined with high sustain levels.
36 // In this case the SID op-amps are performing less than ideally and are
37 // causing some peculiar behavior of the SID filter. This however seems to
38 // have more effect on the overall amplitude than on the color of the sound.
39 //
40 // The theory for the filter circuit can be found in "Microelectric Circuits"
41 // by Adel S. Sedra and Kenneth C. Smith.
42 // The circuit is modeled based on the explanation found there except that
43 // an additional inverter is used in the feedback from the bandpass output,
44 // allowing the summer op-amp to operate in single-ended mode. This yields
45 // inverted filter outputs with levels independent of Q, which corresponds with
46 // the results obtained from a real SID.
47 //
48 // We have been able to model the summer and the two integrators of the circuit
49 // to form components of an IIR filter.
50 // Vhp is the output of the summer, Vbp is the output of the first integrator,
51 // and Vlp is the output of the second integrator in the filter circuit.
52 //
53 // According to Bob Yannes, the active stages of the SID filter are not really
54 // op-amps. Rather, simple NMOS inverters are used. By biasing an inverter
55 // into its region of quasi-linear operation using a feedback resistor from
56 // input to output, a MOS inverter can be made to act like an op-amp for
57 // small signals centered around the switching threshold.
58 //
59 // Qualified guesses at SID filter schematics are depicted below.
60 //
61 // SID filter
62 // ----------
63 //
64 // -----------------------------------------------
65 // | |
66 // | ---Rq-- |
67 // | | | |
68 // | ------------<A]-----R1--------- |
69 // | | | |
70 // | | ---C---| ---C---|
71 // | | | | | |
72 // | --R1-- ---R1-- |---Rs--| |---Rs--|
73 // | | | | | | | |
74 // ----R1--|-----[A>--|--R-----[A>--|--R-----[A>--|
75 // | | | |
76 // vi -----R1-- | | |
77 //
78 // vhp vbp vlp
79 //
80 //
81 // vi - input voltage
82 // vhp - highpass output
83 // vbp - bandpass output
84 // vlp - lowpass output
85 // [A> - op-amp
86 // R1 - summer resistor
87 // Rq - resistor array controlling resonance (4 resistors)
88 // R - NMOS FET voltage controlled resistor controlling cutoff frequency
89 // Rs - shunt resitor
90 // C - capacitor
91 //
92 //
93 //
94 // SID integrator
95 // --------------
96 //
97 // V+
98 //
99 // |
100 // |
101 // -----|
102 // | |
103 // | ||--
104 // -||
105 // ---C--- ||->
106 // | | |
107 // |---Rs-----------|---- vo
108 // | |
109 // | ||--
110 // vi ---- -----|------------||
111 // | ^ | ||->
112 // |___| | |
113 // ----- | |
114 // | | |
115 // |---R2-- |
116 // |
117 // R1 V-
118 // |
119 // |
120 //
121 // Vw
122 //
123 // ----------------------------------------------------------------------------
124 class FilterFP
125 {
126 public:
127 FilterFP();
128
129 void enable_filter(bool enable);
130 void set_chip_model(chip_model model);
131 void set_distortion_properties(float, float, float);
132 void set_type3_properties(float, float, float, float);
133 void set_type4_properties(float, float);
134 void set_clock_frequency(float);
135 void set_nonlinearity(float);
136
137 inline
138 float clock(float voice1, float voice2, float voice3,
139 float ext_in);
140 void reset();
141
142 // Write registers.
143 void writeFC_LO(reg8);
144 void writeFC_HI(reg8);
145 void writeRES_FILT(reg8);
146 void writeMODE_VOL(reg8);
147
148 private:
149 void set_Q();
150 void set_w0();
151 float type3_w0(const float dist);
152 float type4_w0();
153 void calculate_helpers();
154 void nuke_denormals();
155 float waveshaper1(float value);
156 float waveshaper2(float value);
157
158 // Filter enabled.
159 bool enabled;
160
161 // 6581/8580 filter model (XXX: we should specialize in separate classes)
162 chip_model model;
163
164 // Filter cutoff frequency.
165 reg12 fc;
166
167 // Filter resonance.
168 reg8 res;
169
170 // Selects which inputs to route through filter.
171 reg8 filt;
172
173 // Switch voice 3 off.
174 reg8 voice3off;
175
176 // Highpass, bandpass, and lowpass filter modes.
177 reg8 hp_bp_lp;
178
179 // Output master volume.
180 reg4 vol;
181 float volf; /* avoid integer-to-float conversion at output */
182
183 // clock
184 float clock_frequency;
185
186 /* Distortion params for Type3 */
187 float attenuation, distortion_nonlinearity, intermixing_leaks;
188
189 /* Type3 params. */
190 float type3_baseresistance, type3_offset, type3_steepness, type3_minimumfetresistance;
191
192 /* Type4 params */
193 float type4_k, type4_b;
194
195 // State of filter.
196 float Vhp, Vbp, Vlp;
197
198 /* Resonance/Distortion/Type3/Type4 helpers. */
199 float type4_w0_cache, _1_div_Q, type3_fc_kink_exp, distortion_CT;
200
201 float nonlinearity;
202
203 friend class SIDFP;
204 };
205
206 // ----------------------------------------------------------------------------
207 // Inline functions.
208 // The following functions are defined inline because they are called every
209 // time a sample is calculated.
210 // ----------------------------------------------------------------------------
211
212 const float sidcaps_6581 = 470e-12f;
213 const float FC_TO_OSC = 512.f;
214
215 inline
216 static float fastexp(float val) {
217 typedef union {
218 int i;
219 float f;
220 } conv;
221
222 conv tmp;
223
224 /* single precision fp has 1 + 8 + 23 bits, exponent bias is 127.
225 * It therefore follows that we need to shift left by 23 bits, and to
226 * calculate exp(x) instead of pow(2, x) we divide the power by ln(2). */
227 const float a = static_cast<float>((1 << 23) / M_LN2);
228 /* The other factor corrects for the exponent bias so that 2^0 = 1. */
229 const float b = static_cast<float>((1 << 23) * 127);
230 /* According to "A Fast, Compact Approximation of the Exponential Function"
231 * by Nicol N. Schraudolph, 60801.48 yields the minimum RMS error for the
232 * piecewise-linear approximation when using doubles (20 bits residual).
233 * We have 23 bits, so we scale this value by 8. */
234 const float c = 60801.48f * 8.f;
235
236 /* Parenthesis are important: C standard disallows folding subtraction.
237 * Unfortunately GCC appears to generate a write to memory rather than
238 * handle this conversion entirely in registers. */
239 tmp.i = static_cast<int>(a * val + (b - c + 0.5f));
240 return tmp.f;
241 }
242
243 inline
244 float FilterFP::type3_w0(const float dist)
245 {
246 float fetresistance = type3_fc_kink_exp;
247 if (dist > 0) {
248 fetresistance *= fastexp(dist * type3_steepness);
249 }
250 const float dynamic_resistance = type3_minimumfetresistance + fetresistance;
251
252 /* 2 parallel resistors */
253 const float _1_div_resistance = (type3_baseresistance + dynamic_resistance) / (type3_baseresistance * dynamic_resistance);
254 /* 1.f / (clock * caps * resistance) */
255 return distortion_CT * _1_div_resistance;
256 }
257
258 inline
259 float FilterFP::type4_w0()
260 {
261 const float freq = type4_k * fc + type4_b;
262 return 2.f * static_cast<float>(M_PI) * freq / clock_frequency;
263 }
264
265 inline float FilterFP::waveshaper1(float value)
266 {
267 if (value > distortion_nonlinearity) {
268 value -= (value - distortion_nonlinearity) * 0.5f;
269 }
270 return value;
271 }
272
273 // ----------------------------------------------------------------------------
274 // SID clocking - 1 cycle.
275 // ----------------------------------------------------------------------------
276 inline
277 float FilterFP::clock(float voice1,
278 float voice2,
279 float voice3,
280 float ext_in)
281 {
282 float Vi = 0.f, Vf = 0.f;
283
284 // Route voices into or around filter.
285 ((filt & 1) ? Vi : Vf) += voice1;
286 ((filt & 2) ? Vi : Vf) += voice2;
287 // NB! Voice 3 is not silenced by voice3off if it is routed through
288 // the filter.
289 if (filt & 4) {
290 Vi += voice3;
291 } else if (! voice3off) {
292 Vf += voice3;
293 }
294 ((filt & 8) ? Vi : Vf) += ext_in;
295
296 if (hp_bp_lp & 1) {
297 Vf += Vlp;
298 }
299 if (hp_bp_lp & 2) {
300 Vf += Vbp;
301 }
302 if (hp_bp_lp & 4) {
303 Vf += Vhp;
304 }
305
306 if (model == MOS6581) {
307 Vlp -= Vbp * type3_w0(Vbp);
308 Vbp -= Vhp * type3_w0(Vhp);
309 Vhp = (Vbp * _1_div_Q - Vlp - Vi * 0.85f) * attenuation;
310
311 /* output strip mixing to filter state */
312 if (hp_bp_lp & 1) {
313 Vlp += Vf * intermixing_leaks;
314 }
315 if (hp_bp_lp & 2) {
316 Vbp += Vf * intermixing_leaks;
317 }
318 if (hp_bp_lp & 4) {
319 Vhp += Vf * intermixing_leaks;
320 }
321
322 /* saturate. This is likely the output inverter saturation. */
323 Vf *= volf;
324 Vf = waveshaper1(Vf);
325 } else {
326 /* On the 8580, BP appears mixed in phase with the rest. */
327 Vlp += Vbp * type4_w0_cache;
328 Vbp += Vhp * type4_w0_cache;
329 Vhp = -Vbp * _1_div_Q - Vlp - Vi;
330 Vf *= volf;
331 }
332
333 return Vf;
334 }
335
336 inline
337 void FilterFP::nuke_denormals()
338 {
339 /* We only need this for systems that don't do -msse and -mfpmath=sse */
340 if (Vbp > -1e-12f && Vbp < 1e-12f)
341 Vbp = 0;
342 if (Vlp > -1e-12f && Vlp < 1e-12f)
343 Vlp = 0;
344 }
345
346 #endif // not __FILTER_H__