| 1 | // ---------------------------------------------------------------------------cxx . . $ `<file>/src/resid/extfilt.h`/ |
| 2 | // This file is part of reSID, a MOS6581 SID emulator engine. |
| 3 | // Copyright (C) 2004 Dag Lem <[email protected]> |
| 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 __EXTFILT_H__ |
| 21 | #define __EXTFILT_H__cxx . . $ `src/resid/extfilt.h:21:9`! |
| 22 | |
| 23 | #include "siddefs.h"cxx . . $ `<file>/src/resid/siddefs.h`/ |
| 24 | |
| 25 | // ---------------------------------------------------------------------------- |
| 26 | // The audio output stage in a Commodore 64 consists of two STC networks, |
| 27 | // a low-pass filter with 3-dB frequency 16kHz followed by a high-pass |
| 28 | // filter with 3-dB frequency 16Hz (the latter provided an audio equipment |
| 29 | // input impedance of 1kOhm). |
| 30 | // The STC networks are connected with a BJT supposedly meant to act as |
| 31 | // a unity gain buffer, which is not really how it works. A more elaborate |
| 32 | // model would include the BJT, however DC circuit analysis yields BJT |
| 33 | // base-emitter and emitter-base impedances sufficiently low to produce |
| 34 | // additional low-pass and high-pass 3dB-frequencies in the order of hundreds |
| 35 | // of kHz. This calls for a sampling frequency of several MHz, which is far |
| 36 | // too high for practical use. |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | class ExternalFiltercxx . . $ ExternalFilter# |
| 39 | { |
| 40 | public: |
| 41 | ExternalFiltercxx . . $ ExternalFilter#ExternalFilter(49f6e7a06ebc5aa8).(); |
| 42 | |
| 43 | void enable_filtercxx . . $ ExternalFilter#enable_filter(b45ddcbc8423892c).(bool enable); |
| 44 | void set_chip_modelcxx . . $ ExternalFilter#set_chip_model(cea143c94d31b99f).(chip_modelcxx . . $ chip_model# model); |
| 45 | |
| 46 | RESID_INLINEcxx . . $ `src/resid/siddefs.h:70:9`! void clockcxx . . $ ExternalFilter#clock(d4f767463ce0a6b3).(sound_samplecxx . . $ sound_sample# Vi); |
| 47 | RESID_INLINEcxx . . $ `src/resid/siddefs.h:70:9`! void clockcxx . . $ ExternalFilter#clock(3e454b8ccf442868).(cycle_countcxx . . $ cycle_count# delta_t, sound_samplecxx . . $ sound_sample# Vi); |
| 48 | void resetcxx . . $ ExternalFilter#reset(49f6e7a06ebc5aa8).(); |
| 49 | |
| 50 | // Audio output (20 bits). |
| 51 | RESID_INLINEcxx . . $ `src/resid/siddefs.h:70:9`! sound_samplecxx . . $ sound_sample# outputcxx . . $ ExternalFilter#output(b126dc7c1de90089).(); |
| 52 | |
| 53 | protected: |
| 54 | // Filter enabled. |
| 55 | bool enabledcxx . . $ ExternalFilter#enabled.; |
| 56 | |
| 57 | // Maximum mixer DC offset. |
| 58 | sound_samplecxx . . $ sound_sample# mixer_DCcxx . . $ ExternalFilter#mixer_DC.; |
| 59 | |
| 60 | // State of filters. |
| 61 | sound_samplecxx . . $ sound_sample# Vlpcxx . . $ ExternalFilter#Vlp.; // lowpass |
| 62 | sound_samplecxx . . $ sound_sample# Vhpcxx . . $ ExternalFilter#Vhp.; // highpass |
| 63 | sound_samplecxx . . $ sound_sample# Vocxx . . $ ExternalFilter#Vo.; |
| 64 | |
| 65 | // Cutoff frequencies. |
| 66 | sound_samplecxx . . $ sound_sample# w0lpcxx . . $ ExternalFilter#w0lp.; |
| 67 | sound_samplecxx . . $ sound_sample# w0hpcxx . . $ ExternalFilter#w0hp.; |
| 68 | |
| 69 | friend class SIDcxx . . $ SID#; |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | // ---------------------------------------------------------------------------- |
| 74 | // Inline functions. |
| 75 | // The following functions are defined inline because they are called every |
| 76 | // time a sample is calculated. |
| 77 | // ---------------------------------------------------------------------------- |
| 78 | |
| 79 | #if RESID_INLININGcxx . . $ `src/resid/siddefs.h:69:9`! || defined(__EXTFILT_CC__cxx . . $ `src/resid/extfilt.cpp:20:9`!) |
| 80 | |
| 81 | // ---------------------------------------------------------------------------- |
| 82 | // SID clocking - 1 cycle. |
| 83 | // ---------------------------------------------------------------------------- |
| 84 | RESID_INLINEcxx . . $ `src/resid/siddefs.h:70:9`! |
| 85 | void ExternalFiltercxx . . $ ExternalFilter#::clockcxx . . $ ExternalFilter#clock(d4f767463ce0a6b3).(sound_samplecxx . . $ sound_sample# Vi) |
| 86 | { |
| 87 | // This is handy for testing. |
| 88 | if (!enabledcxx . . $ ExternalFilter#enabled.) { |
| 89 | // Remove maximum DC level since there is no filter to do it. |
| 90 | Vlpcxx . . $ ExternalFilter#Vlp. = Vhpcxx . . $ ExternalFilter#Vhp. = 0; |
| 91 | Vocxx . . $ ExternalFilter#Vo. = Vi - mixer_DCcxx . . $ ExternalFilter#mixer_DC.; |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | // delta_t is converted to seconds given a 1MHz clock by dividing |
| 96 | // with 1 000 000. |
| 97 | |
| 98 | // Calculate filter outputs. |
| 99 | // Vo = Vlp - Vhp; |
| 100 | // Vlp = Vlp + w0lp*(Vi - Vlp)*delta_t; |
| 101 | // Vhp = Vhp + w0hp*(Vlp - Vhp)*delta_t; |
| 102 | |
| 103 | sound_samplecxx . . $ sound_sample# dVlp = (w0lpcxx . . $ ExternalFilter#w0lp. >> 8)*(Vi - Vlpcxx . . $ ExternalFilter#Vlp.) >> 12; |
| 104 | sound_samplecxx . . $ sound_sample# dVhp = w0hpcxx . . $ ExternalFilter#w0hp.*(Vlpcxx . . $ ExternalFilter#Vlp. - Vhpcxx . . $ ExternalFilter#Vhp.) >> 20; |
| 105 | Vocxx . . $ ExternalFilter#Vo. = Vlpcxx . . $ ExternalFilter#Vlp. - Vhpcxx . . $ ExternalFilter#Vhp.; |
| 106 | Vlpcxx . . $ ExternalFilter#Vlp. += dVlp; |
| 107 | Vhpcxx . . $ ExternalFilter#Vhp. += dVhp; |
| 108 | } |
| 109 | |
| 110 | // ---------------------------------------------------------------------------- |
| 111 | // SID clocking - delta_t cycles. |
| 112 | // ---------------------------------------------------------------------------- |
| 113 | RESID_INLINEcxx . . $ `src/resid/siddefs.h:70:9`! |
| 114 | void ExternalFiltercxx . . $ ExternalFilter#::clockcxx . . $ ExternalFilter#clock(3e454b8ccf442868).(cycle_countcxx . . $ cycle_count# delta_t, |
| 115 | sound_samplecxx . . $ sound_sample# Vi) |
| 116 | { |
| 117 | // This is handy for testing. |
| 118 | if (!enabledcxx . . $ ExternalFilter#enabled.) { |
| 119 | // Remove maximum DC level since there is no filter to do it. |
| 120 | Vlpcxx . . $ ExternalFilter#Vlp. = Vhpcxx . . $ ExternalFilter#Vhp. = 0; |
| 121 | Vocxx . . $ ExternalFilter#Vo. = Vi - mixer_DCcxx . . $ ExternalFilter#mixer_DC.; |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // Maximum delta cycles for the external filter to work satisfactorily |
| 126 | // is approximately 8. |
| 127 | cycle_countcxx . . $ cycle_count# delta_t_flt = 8; |
| 128 | |
| 129 | while (delta_t) { |
| 130 | if (delta_t < delta_t_flt) { |
| 131 | delta_t_flt = delta_t; |
| 132 | } |
| 133 | |
| 134 | // delta_t is converted to seconds given a 1MHz clock by dividing |
| 135 | // with 1 000 000. |
| 136 | |
| 137 | // Calculate filter outputs. |
| 138 | // Vo = Vlp - Vhp; |
| 139 | // Vlp = Vlp + w0lp*(Vi - Vlp)*delta_t; |
| 140 | // Vhp = Vhp + w0hp*(Vlp - Vhp)*delta_t; |
| 141 | |
| 142 | sound_samplecxx . . $ sound_sample# dVlp = (w0lpcxx . . $ ExternalFilter#w0lp.*delta_t_flt >> 8)*(Vi - Vlpcxx . . $ ExternalFilter#Vlp.) >> 12; |
| 143 | sound_samplecxx . . $ sound_sample# dVhp = w0hpcxx . . $ ExternalFilter#w0hp.*delta_t_flt*(Vlpcxx . . $ ExternalFilter#Vlp. - Vhpcxx . . $ ExternalFilter#Vhp.) >> 20; |
| 144 | Vocxx . . $ ExternalFilter#Vo. = Vlpcxx . . $ ExternalFilter#Vlp. - Vhpcxx . . $ ExternalFilter#Vhp.; |
| 145 | Vlpcxx . . $ ExternalFilter#Vlp. += dVlp; |
| 146 | Vhpcxx . . $ ExternalFilter#Vhp. += dVhp; |
| 147 | |
| 148 | delta_t -= delta_t_flt; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | |
| 153 | // ---------------------------------------------------------------------------- |
| 154 | // Audio output (19.5 bits). |
| 155 | // ---------------------------------------------------------------------------- |
| 156 | RESID_INLINEcxx . . $ `src/resid/siddefs.h:70:9`! |
| 157 | sound_samplecxx . . $ sound_sample# ExternalFiltercxx . . $ ExternalFilter#::outputcxx . . $ ExternalFilter#output(b126dc7c1de90089).() |
| 158 | { |
| 159 | return Vocxx . . $ ExternalFilter#Vo.; |
| 160 | } |
| 161 | |
| 162 | #endif // RESID_INLINING || defined(__EXTFILT_CC__) |
| 163 | |
| 164 | #endif // not __EXTFILT_H__ |