libsidplayfp  2.2.2
extfilt.h
1 // ---------------------------------------------------------------------------
2 // This file is part of reSID, a MOS6581 SID emulator engine.
3 // Copyright (C) 2010 Dag Lem <resid@nimrod.no>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // ---------------------------------------------------------------------------
19 
20 #ifndef RESID_EXTFILT_H
21 #define RESID_EXTFILT_H
22 
23 #include "resid-config.h"
24 
25 namespace reSID
26 {
27 
28 // ----------------------------------------------------------------------------
29 // The audio output stage in a Commodore 64 consists of two STC networks,
30 // a low-pass filter with 3-dB frequency 16kHz followed by a high-pass
31 // filter with 3-dB frequency 16Hz (the latter provided an audio equipment
32 // input impedance of 1kOhm).
33 // The STC networks are connected with a BJT supposedly meant to act as
34 // a unity gain buffer, which is not really how it works. A more elaborate
35 // model would include the BJT, however DC circuit analysis yields BJT
36 // base-emitter and emitter-base impedances sufficiently low to produce
37 // additional low-pass and high-pass 3dB-frequencies in the order of hundreds
38 // of kHz. This calls for a sampling frequency of several MHz, which is far
39 // too high for practical use.
40 //
41 // 9/12V
42 // -----+
43 // audio| 10k |
44 // +----+---R---+--------+-----(K) +-----
45 // out | | | | | |audio
46 // -----+ R 1k C 1000 | | 10 uF |
47 // | | pF +-C----+-----C-----+ 1K
48 // 470 | |
49 // GND GND pF R 1K | amp
50 // | +-----
51 //
52 // GND
53 //
54 // ----------------------------------------------------------------------------
56 {
57 public:
59 
60  void enable_filter(bool enable);
61 
62  void clock(short Vi);
63  void clock(cycle_count delta_t, short Vi);
64  void reset();
65 
66  // Audio output (16 bits).
67  int output();
68 
69 protected:
70  // Filter enabled.
71  bool enabled;
72 
73  // State of filters (27 bits).
74  int Vlp; // lowpass
75  int Vhp; // highpass
76 
77  // Cutoff frequencies.
78  int w0lp_1_s7;
79  int w0hp_1_s17;
80 
81 friend class SID;
82 };
83 
84 
85 // ----------------------------------------------------------------------------
86 // Inline functions.
87 // The following functions are defined inline because they are called every
88 // time a sample is calculated.
89 // ----------------------------------------------------------------------------
90 
91 #if RESID_INLINING || defined(RESID_EXTFILT_CC)
92 
93 // ----------------------------------------------------------------------------
94 // SID clocking - 1 cycle.
95 // ----------------------------------------------------------------------------
96 RESID_INLINE
97 void ExternalFilter::clock(short Vi)
98 {
99  // This is handy for testing.
100  if (unlikely(!enabled)) {
101  // Vo = Vlp - Vhp;
102  Vlp = Vi << 11;
103  Vhp = 0;
104  return;
105  }
106 
107  // Calculate filter outputs.
108  // Vlp = Vlp + w0lp*(Vi - Vlp)*delta_t;
109  // Vhp = Vhp + w0hp*(Vlp - Vhp)*delta_t;
110  // Vo = Vlp - Vhp;
111 
112  int dVlp = w0lp_1_s7*int((unsigned(Vi) << 11) - unsigned(Vlp)) >> 7;
113  int dVhp = w0hp_1_s17*(Vlp - Vhp) >> 17;
114  Vlp += dVlp;
115  Vhp += dVhp;
116 }
117 
118 // ----------------------------------------------------------------------------
119 // SID clocking - delta_t cycles.
120 // ----------------------------------------------------------------------------
121 RESID_INLINE
122 void ExternalFilter::clock(cycle_count delta_t, short Vi)
123 {
124  // This is handy for testing.
125  if (unlikely(!enabled)) {
126  // Vo = Vlp - Vhp;
127  Vlp = Vi << 11;
128  Vhp = 0;
129  return;
130  }
131 
132  // Maximum delta cycles for the external filter to work satisfactorily
133  // is approximately 8.
134  cycle_count delta_t_flt = 8;
135 
136  while (delta_t) {
137  if (unlikely(delta_t < delta_t_flt)) {
138  delta_t_flt = delta_t;
139  }
140 
141  // Calculate filter outputs.
142  // Vlp = Vlp + w0lp*(Vi - Vlp)*delta_t;
143  // Vhp = Vhp + w0hp*(Vlp - Vhp)*delta_t;
144  // Vo = Vlp - Vhp;
145 
146  int dVlp = (w0lp_1_s7*delta_t_flt >> 3)*((Vi << 11) - Vlp) >> 4;
147  int dVhp = (w0hp_1_s17*delta_t_flt >> 3)*(Vlp - Vhp) >> 14;
148  Vlp += dVlp;
149  Vhp += dVhp;
150 
151  delta_t -= delta_t_flt;
152  }
153 }
154 
155 
156 // ----------------------------------------------------------------------------
157 // Audio output (16 bits).
158 // ----------------------------------------------------------------------------
159 RESID_INLINE
160 int ExternalFilter::output()
161 {
162  return (Vlp - Vhp) >> 11;
163 }
164 
165 #endif // RESID_INLINING || defined(RESID_EXTFILT_CC)
166 
167 } // namespace reSID
168 
169 #endif // not RESID_EXTFILT_H
Definition: extfilt.h:56
Definition: sid.h:39