| 1 |
/* |
| 2 |
* This file is part of libsidplayfp, a SID player engine. |
| 3 |
* |
| 4 |
* Copyright 2011-2013 Leandro Nini <[email protected]> |
| 5 |
* Copyright 2007-2010 Antti Lankila |
| 6 |
* Copyright 2004 Dag Lem <[email protected]> |
| 7 |
* |
| 8 |
* This program is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU General Public License as published by |
| 10 |
* the Free Software Foundation; either version 2 of the License, or |
| 11 |
* (at your option) any later version. |
| 12 |
* |
| 13 |
* This program is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU General Public License |
| 19 |
* along with this program; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef SINCRESAMPLER_H |
| 24 |
#define SINCRESAMPLER_H |
| 25 |
|
| 26 |
#include "Resampler.h" |
| 27 |
|
| 28 |
#include <string> |
| 29 |
#include <map> |
| 30 |
|
| 31 |
#include "../array.h" |
| 32 |
|
| 33 |
#include "../sidcxx11.h" |
| 34 |
|
| 35 |
namespace reSIDfp |
| 36 |
{ |
| 37 |
|
| 38 |
/** |
| 39 |
* This is the theoretically correct (and computationally intensive) audio sample generation. |
| 40 |
* The samples are generated by resampling to the specified sampling frequency. |
| 41 |
* The work rate is inversely proportional to the percentage of the bandwidth |
| 42 |
* allocated to the filter transition band. |
| 43 |
* |
| 44 |
* This implementation is based on the paper "A Flexible Sampling-Rate Conversion Method", |
| 45 |
* by J. O. Smith and P. Gosset, or rather on the expanded tutorial on the |
| 46 |
* [Digital Audio Resampling Home Page](http://www-ccrma.stanford.edu/~jos/resample/). |
| 47 |
* |
| 48 |
* By building shifted FIR tables with samples according to the sampling frequency, |
| 49 |
* this implementation dramatically reduces the computational effort in the |
| 50 |
* filter convolutions, without any loss of accuracy. |
| 51 |
* The filter convolutions are also vectorizable on current hardware. |
| 52 |
*/ |
| 53 |
class SincResampler final : public Resampler |
| 54 |
{ |
| 55 |
private: |
| 56 |
/// Size of the ring buffer, must be a power of 2 |
| 57 |
static const int RINGSIZE = 2048; |
| 58 |
|
| 59 |
private: |
| 60 |
/// Table of the fir filter coefficients |
| 61 |
matrix_t* firTable; |
| 62 |
|
| 63 |
int sampleIndex; |
| 64 |
|
| 65 |
/// Filter resolution |
| 66 |
int firRES; |
| 67 |
|
| 68 |
/// Filter length |
| 69 |
int firN; |
| 70 |
|
| 71 |
const int cyclesPerSample; |
| 72 |
|
| 73 |
int sampleOffset; |
| 74 |
|
| 75 |
int outputValue; |
| 76 |
|
| 77 |
short sample[RINGSIZE * 2]; |
| 78 |
|
| 79 |
private: |
| 80 |
int fir(int subcycle); |
| 81 |
|
| 82 |
public: |
| 83 |
/** |
| 84 |
* Use a clock freqency of 985248Hz for PAL C64, 1022730Hz for NTSC C64. |
| 85 |
* The default end of passband frequency is pass_freq = 0.9*sample_freq/2 |
| 86 |
* for sample frequencies up to ~ 44.1kHz, and 20kHz for higher sample frequencies. |
| 87 |
* |
| 88 |
* For resampling, the ratio between the clock frequency and the sample frequency |
| 89 |
* is limited as follows: 125*clock_freq/sample_freq < 16384 |
| 90 |
* E.g. provided a clock frequency of ~ 1MHz, the sample frequency |
| 91 |
* can not be set lower than ~ 8kHz. |
| 92 |
* A lower sample frequency would make the resampling code overfill its 16k sample ring buffer. |
| 93 |
* |
| 94 |
* The end of passband frequency is also limited: pass_freq <= 0.9*sample_freq/2 |
| 95 |
* |
| 96 |
* E.g. for a 44.1kHz sampling rate the end of passband frequency is limited |
| 97 |
* to slightly below 20kHz. This constraint ensures that the FIR table is not overfilled. |
| 98 |
* |
| 99 |
* @param clockFrequency System clock frequency at Hz |
| 100 |
* @param samplingFrequency Desired output sampling rate |
| 101 |
* @param highestAccurateFrequency |
| 102 |
*/ |
| 103 |
SincResampler(double clockFrequency, double samplingFrequency, double highestAccurateFrequency); |
| 104 |
|
| 105 |
bool input(int input) override; |
| 106 |
|
| 107 |
int output() const override { return outputValue; } |
| 108 |
|
| 109 |
void reset() override; |
| 110 |
}; |
| 111 |
|
| 112 |
} // namespace reSIDfp |
| 113 |
|
| 114 |
#endif |