libsidplayfp  2.0.2
Filter6581.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2019 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  * Copyright 2004,2010 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 FILTER6581_H
24 #define FILTER6581_H
25 
26 #include "siddefs-fp.h"
27 
28 #include <memory>
29 
30 #include "Filter.h"
31 #include "FilterModelConfig.h"
32 
33 #include "sidcxx11.h"
34 
35 namespace reSIDfp
36 {
37 
38 class Integrator;
39 
322 class Filter6581 final : public Filter
323 {
324 private:
325  const unsigned short* f0_dac;
326 
327  unsigned short** mixer;
328  unsigned short** summer;
329  unsigned short** gain;
330 
331  const int voiceScaleS14;
332  const int voiceDC;
333 
335  std::unique_ptr<Integrator> const hpIntegrator;
336 
338  std::unique_ptr<Integrator> const bpIntegrator;
339 
340 protected:
344  void updatedCenterFrequency() override;
345 
351  void updateResonance(unsigned char res) override { currentResonance = gain[~res & 0xf]; }
352 
353  void updatedMixing() override;
354 
355 public:
356  Filter6581() :
357  f0_dac(FilterModelConfig::getInstance()->getDAC(0.5)),
358  mixer(FilterModelConfig::getInstance()->getMixer()),
359  summer(FilterModelConfig::getInstance()->getSummer()),
360  gain(FilterModelConfig::getInstance()->getGain()),
361  voiceScaleS14(FilterModelConfig::getInstance()->getVoiceScaleS14()),
362  voiceDC(FilterModelConfig::getInstance()->getVoiceDC()),
363  hpIntegrator(FilterModelConfig::getInstance()->buildIntegrator()),
364  bpIntegrator(FilterModelConfig::getInstance()->buildIntegrator())
365  {
366  input(0);
367  }
368 
369  ~Filter6581();
370 
371  int clock(int voice1, int voice2, int voice3) override;
372 
373  void input(int sample) override { ve = (sample * voiceScaleS14 * 3 >> 10) + mixer[0][0]; }
374 
380  void setFilterCurve(double curvePosition);
381 };
382 
383 } // namespace reSIDfp
384 
385 #if RESID_INLINING || defined(FILTER6581_CPP)
386 
387 #include "Integrator.h"
388 
389 namespace reSIDfp
390 {
391 
392 RESID_INLINE
393 int Filter6581::clock(int voice1, int voice2, int voice3)
394 {
395  voice1 = (voice1 * voiceScaleS14 >> 18) + voiceDC;
396  voice2 = (voice2 * voiceScaleS14 >> 18) + voiceDC;
397  // Voice 3 is silenced by voice3off if it is not routed through the filter.
398  voice3 = filt3 || !voice3off ? (voice3 * voiceScaleS14 >> 18) + voiceDC : 0;
399 
400  int Vi = 0;
401  int Vo = 0;
402 
403  (filt1 ? Vi : Vo) += voice1;
404  (filt2 ? Vi : Vo) += voice2;
405  (filt3 ? Vi : Vo) += voice3;
406  (filtE ? Vi : Vo) += ve;
407 
409  Vbp = hpIntegrator->solve(Vhp);
410  Vlp = bpIntegrator->solve(Vbp);
411 
412  if (lp) Vo += Vlp;
413  if (bp) Vo += Vbp;
414  if (hp) Vo += Vhp;
415 
416  return currentGain[currentMixer[Vo]] - (1 << 15);
417 }
418 
419 } // namespace reSIDfp
420 
421 #endif
422 
423 #endif
reSIDfp::Filter::ve
int ve
Filter external input.
Definition: Filter.h:57
reSIDfp::Filter6581::setFilterCurve
void setFilterCurve(double curvePosition)
Definition: Filter6581.cpp:68
reSIDfp::Filter::filt1
bool filt1
Routing to filter or outside filter.
Definition: Filter.h:63
reSIDfp::Filter::Vhp
int Vhp
Filter highpass state.
Definition: Filter.h:48
reSIDfp::Filter::Vlp
int Vlp
Filter lowpass state.
Definition: Filter.h:54
reSIDfp::Filter
Definition: Filter.h:33
reSIDfp::Filter::hp
bool hp
Highpass, bandpass, and lowpass filter modes.
Definition: Filter.h:69
reSIDfp::FilterModelConfig
Definition: FilterModelConfig.h:42
reSIDfp::Filter::currentSummer
unsigned short * currentSummer
Filter input summer setting.
Definition: Filter.h:42
reSIDfp::Filter6581::updatedMixing
void updatedMixing() override
Definition: Filter6581.cpp:44
reSIDfp::Filter6581::clock
int clock(int voice1, int voice2, int voice3) override
Definition: Filter6581.h:393
reSIDfp::Filter6581::updateResonance
void updateResonance(unsigned char res) override
Definition: Filter6581.h:351
reSIDfp::Filter::currentGain
unsigned short * currentGain
Current volume amplifier setting.
Definition: Filter.h:36
reSIDfp::Filter::voice3off
bool voice3off
Switch voice 3 off.
Definition: Filter.h:66
reSIDfp::Filter::Vbp
int Vbp
Filter bandpass state.
Definition: Filter.h:51
reSIDfp::Filter6581::updatedCenterFrequency
void updatedCenterFrequency() override
Definition: Filter6581.cpp:37
reSIDfp::Filter::currentMixer
unsigned short * currentMixer
Current filter/voice mixer setting.
Definition: Filter.h:39
reSIDfp::Filter6581
Definition: Filter6581.h:323
reSIDfp::Filter::currentResonance
unsigned short * currentResonance
Filter resonance value.
Definition: Filter.h:45