| 1 |
/* Copyright (C) 2015-2022 Sergey V. Mikayev |
| 2 |
* |
| 3 |
* This program is free software: you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU Lesser General Public License as published by |
| 5 |
* the Free Software Foundation, either version 2.1 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef SRCTOOLS_IIR_2X_RESAMPLER_H |
| 18 |
#define SRCTOOLS_IIR_2X_RESAMPLER_H |
| 19 |
|
| 20 |
#include "ResamplerStage.h" |
| 21 |
|
| 22 |
namespace SRCTools { |
| 23 |
|
| 24 |
static const unsigned int IIR_RESAMPER_CHANNEL_COUNT = 2; |
| 25 |
static const unsigned int IIR_SECTION_ORDER = 2; |
| 26 |
|
| 27 |
typedef FloatSample IIRCoefficient; |
| 28 |
typedef FloatSample BufferedSample; |
| 29 |
|
| 30 |
typedef BufferedSample SectionBuffer[IIR_SECTION_ORDER]; |
| 31 |
|
| 32 |
// Non-trivial coefficients of a 2nd-order section of a parallel bank |
| 33 |
// (zero-order numerator coefficient is always zero, zero-order denominator coefficient is always unity) |
| 34 |
struct IIRSection { |
| 35 |
IIRCoefficient num1; |
| 36 |
IIRCoefficient num2; |
| 37 |
IIRCoefficient den1; |
| 38 |
IIRCoefficient den2; |
| 39 |
}; |
| 40 |
|
| 41 |
class IIRResampler : public ResamplerStage { |
| 42 |
public: |
| 43 |
enum Quality { |
| 44 |
// Used when providing custom IIR filter coefficients. |
| 45 |
CUSTOM, |
| 46 |
// Use fast elliptic filter with symmetric ripple: N=8, Ap=As=-99 dB, fp=0.125, fs = 0.25 (in terms of sample rate) |
| 47 |
FAST, |
| 48 |
// Use average elliptic filter with symmetric ripple: N=12, Ap=As=-106 dB, fp=0.193, fs = 0.25 (in terms of sample rate) |
| 49 |
GOOD, |
| 50 |
// Use sharp elliptic filter with symmetric ripple: N=18, Ap=As=-106 dB, fp=0.238, fs = 0.25 (in terms of sample rate) |
| 51 |
BEST |
| 52 |
}; |
| 53 |
|
| 54 |
// Returns the retained fraction of the passband for the given standard quality value |
| 55 |
static double getPassbandFractionForQuality(Quality quality); |
| 56 |
|
| 57 |
protected: |
| 58 |
explicit IIRResampler(const Quality quality); |
| 59 |
explicit IIRResampler(const unsigned int useSectionsCount, const IIRCoefficient useFIR, const IIRSection useSections[]); |
| 60 |
~IIRResampler(); |
| 61 |
|
| 62 |
const struct Constants { |
| 63 |
// Coefficient of the 0-order FIR part |
| 64 |
IIRCoefficient fir; |
| 65 |
// 2nd-order sections that comprise a parallel bank |
| 66 |
const IIRSection *sections; |
| 67 |
// Number of 2nd-order sections |
| 68 |
unsigned int sectionsCount; |
| 69 |
// Delay line per channel per section |
| 70 |
SectionBuffer *buffer; |
| 71 |
|
| 72 |
Constants(const unsigned int useSectionsCount, const IIRCoefficient useFIR, const IIRSection useSections[], const Quality quality); |
| 73 |
} constants; |
| 74 |
}; // class IIRResampler |
| 75 |
|
| 76 |
class IIR2xInterpolator : public IIRResampler { |
| 77 |
public: |
| 78 |
explicit IIR2xInterpolator(const Quality quality); |
| 79 |
explicit IIR2xInterpolator(const unsigned int useSectionsCount, const IIRCoefficient useFIR, const IIRSection useSections[]); |
| 80 |
|
| 81 |
void process(const FloatSample *&inSamples, unsigned int &inLength, FloatSample *&outSamples, unsigned int &outLength); |
| 82 |
unsigned int estimateInLength(const unsigned int outLength) const; |
| 83 |
|
| 84 |
private: |
| 85 |
FloatSample lastInputSamples[IIR_RESAMPER_CHANNEL_COUNT]; |
| 86 |
unsigned int phase; |
| 87 |
}; |
| 88 |
|
| 89 |
class IIR2xDecimator : public IIRResampler { |
| 90 |
public: |
| 91 |
explicit IIR2xDecimator(const Quality quality); |
| 92 |
explicit IIR2xDecimator(const unsigned int useSectionsCount, const IIRCoefficient useFIR, const IIRSection useSections[]); |
| 93 |
|
| 94 |
void process(const FloatSample *&inSamples, unsigned int &inLength, FloatSample *&outSamples, unsigned int &outLength); |
| 95 |
unsigned int estimateInLength(const unsigned int outLength) const; |
| 96 |
}; |
| 97 |
|
| 98 |
} // namespace SRCTools |
| 99 |
|
| 100 |
#endif // SRCTOOLS_IIR_2X_RESAMPLER_H |