libresidfp  1.2.3
TwoPassSincResampler.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2015 Leandro Nini <drfiemost@users.sourceforge.net>
5  * Copyright 2007-2010 Antti Lankila
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #ifndef TWOPASSSINCRESAMPLER_H
23 #define TWOPASSSINCRESAMPLER_H
24 
25 #include <cmath>
26 
27 #include <memory>
28 
29 #include "Resampler.h"
30 #include "SincResampler.h"
31 
32 #include "siddefs-fp.h"
33 
34 namespace reSIDfp
35 {
36 
40 class TwoPassSincResampler final : public Resampler
41 {
42  friend class State;
43 
44 private:
45  std::unique_ptr<SincResampler> const s1;
46  std::unique_ptr<SincResampler> const s2;
47 
48 private:
49  TwoPassSincResampler(double clockFrequency, double samplingFrequency, double highestAccurateFrequency, double intermediateFrequency) :
50  s1(new SincResampler(clockFrequency, intermediateFrequency, highestAccurateFrequency)),
51  s2(new SincResampler(intermediateFrequency, samplingFrequency, highestAccurateFrequency))
52  {}
53 
54 public:
55  // Named constructor
56  static TwoPassSincResampler* create(double clockFrequency, double samplingFrequency)
57  {
58  // Set the passband frequency slightly below half sampling frequency
59  // pass_freq <= 0.9*sample_freq/2
60  //
61  // This constraint ensures that the FIR table is not overfilled.
62  // For higher sampling frequencies we're fine with 20KHz
63  const double halfFreq = (samplingFrequency > 44000.)
64  ? 20000. : samplingFrequency * 0.45;
65 
66  // Calculation according to Laurent Ganier.
67  // It evaluates to about 120 kHz at typical settings.
68  // Some testing around the chosen value seems to confirm that this does work.
69  double const intermediateFrequency = 2. * halfFreq
70  + std::sqrt(2. * halfFreq * clockFrequency
71  * (samplingFrequency - 2. * halfFreq) / samplingFrequency);
72 
73  return new TwoPassSincResampler(
74  clockFrequency, samplingFrequency, halfFreq, intermediateFrequency);
75  }
76 
77  bool input(int32_t sample) override
78  {
79  return s1->input(sample) && s2->input(s1->output());
80  }
81 
82  int32_t output() const override
83  {
84  return s2->output();
85  }
86 
87  void reset() override
88  {
89  s1->reset();
90  s2->reset();
91  }
92 };
93 
94 } // namespace reSIDfp
95 
96 #endif
Definition: SincResampler.h:48
bool input(int32_t sample) override
Definition: TwoPassSincResampler.h:77
Definition: Dac.cpp:27
Definition: TwoPassSincResampler.h:40
Definition: Resampler.h:36
Definition: State.h:45