libsidplayfp  2.2.2
Resampler.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2011-2020 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 RESAMPLER_H
23 #define RESAMPLER_H
24 
25 #include <cmath>
26 
27 #include "sidcxx11.h"
28 
29 #include "siddefs-fp.h"
30 
31 namespace reSIDfp
32 {
33 
38 class Resampler
39 {
40 protected:
41  inline short softClip(int x) const
42  {
43  constexpr int threshold = 28000;
44  if (likely(x < threshold))
45  return x;
46 
47  constexpr double t = threshold / 32768.;
48  constexpr double a = 1. - t;
49  constexpr double b = 1. / a;
50 
51  double value = static_cast<double>(x - threshold) / 32768.;
52  value = t + a * tanh(b * value);
53  return static_cast<short>(value * 32768.);
54  }
55 
56  virtual int output() const = 0;
57 
58  Resampler() {}
59 
60 public:
61  virtual ~Resampler() {}
62 
69  virtual bool input(int sample) = 0;
70 
76  short getOutput() const
77  {
78  return softClip(output());
79  }
80 
81  virtual void reset() = 0;
82 };
83 
84 } // namespace reSIDfp
85 
86 #endif
Definition: Resampler.h:39
short getOutput() const
Definition: Resampler.h:76
virtual bool input(int sample)=0