| 1 |
/* |
| 2 |
* This file is part of libsidplayfp, a SID player engine. |
| 3 |
* |
| 4 |
* Copyright 2011-2020 Leandro Nini <[email protected]> |
| 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 |
|
| 34 |
/** |
| 35 |
* Abstraction of a resampling process. Given enough input, produces output. |
| 36 |
* Constructors take additional arguments that configure these objects. |
| 37 |
*/ |
| 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 |
|
| 63 |
/** |
| 64 |
* Input a sample into resampler. Output "true" when resampler is ready with new sample. |
| 65 |
* |
| 66 |
* @param sample input sample |
| 67 |
* @return true when a sample is ready |
| 68 |
*/ |
| 69 |
virtual bool input(int sample) = 0; |
| 70 |
|
| 71 |
/** |
| 72 |
* Output a sample from resampler. |
| 73 |
* |
| 74 |
* @return resampled sample |
| 75 |
*/ |
| 76 |
short getOutput() const |
| 77 |
{ |
| 78 |
return softClip(output()); |
| 79 |
} |
| 80 |
|
| 81 |
virtual void reset() = 0; |
| 82 |
}; |
| 83 |
|
| 84 |
} // namespace reSIDfp |
| 85 |
|
| 86 |
#endif |