libsidplayfp  2.12.0
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 "sidcxx11.h"
33 
34 namespace reSIDfp
35 {
36 
40 class TwoPassSincResampler final : public Resampler
41 {
42 private:
43  std::unique_ptr<SincResampler> const s1;
44  std::unique_ptr<SincResampler> const s2;
45 
46 private:
47  TwoPassSincResampler(double clockFrequency, double samplingFrequency, double highestAccurateFrequency, double intermediateFrequency) :
48  s1(new SincResampler(clockFrequency, intermediateFrequency, highestAccurateFrequency)),
49  s2(new SincResampler(intermediateFrequency, samplingFrequency, highestAccurateFrequency))
50  {}
51 
52 public:
53  // Named constructor
54  static TwoPassSincResampler* create(double clockFrequency, double samplingFrequency)
55  {
56  // Set the passband frequency slightly below half sampling frequency
57  // pass_freq <= 0.9*sample_freq/2
58  //
59  // This constraint ensures that the FIR table is not overfilled.
60  // For higher sampling frequencies we're fine with 20KHz
61  const double halfFreq = (samplingFrequency > 44000.)
62  ? 20000. : samplingFrequency * 0.45;
63 
64  // Calculation according to Laurent Ganier.
65  // It evaluates to about 120 kHz at typical settings.
66  // Some testing around the chosen value seems to confirm that this does work.
67  double const intermediateFrequency = 2. * halfFreq
68  + std::sqrt(2. * halfFreq * clockFrequency
69  * (samplingFrequency - 2. * halfFreq) / samplingFrequency);
70 
71  return new TwoPassSincResampler(
72  clockFrequency, samplingFrequency, halfFreq, intermediateFrequency);
73  }
74 
75  bool input(int sample) override
76  {
77  return s1->input(sample) && s2->input(s1->output());
78  }
79 
80  int output() const override
81  {
82  return s2->output();
83  }
84 
85  void reset() override
86  {
87  s1->reset();
88  s2->reset();
89  }
90 };
91 
92 } // namespace reSIDfp
93 
94 #endif
Definition: Resampler.h:40
Definition: SincResampler.h:49
Definition: TwoPassSincResampler.h:41
bool input(int sample) override
Definition: TwoPassSincResampler.h:75