| 1 |
/* |
| 2 |
* Copyright (C) 2018-2023 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include "chip_defs.h" |
| 29 |
#include <cmath> |
| 30 |
#include <cstddef> |
| 31 |
|
| 32 |
struct blip_t; |
| 33 |
|
| 34 |
namespace chip |
| 35 |
{ |
| 36 |
enum class ResamplerType : int |
| 37 |
{ |
| 38 |
Linear = 0, |
| 39 |
BlipBuf, |
| 40 |
FastBlipBuf, |
| 41 |
}; |
| 42 |
|
| 43 |
class AbstractResampler |
| 44 |
{ |
| 45 |
public: |
| 46 |
AbstractResampler(); |
| 47 |
virtual ~AbstractResampler(); |
| 48 |
virtual void init(int srcRate, int destRate, size_t maxDuration); |
| 49 |
virtual void reset() {} |
| 50 |
|
| 51 |
virtual void setDestributionRate(int destRate) |
| 52 |
{ |
| 53 |
init(srcRate_, destRate, maxDuration_); |
| 54 |
} |
| 55 |
|
| 56 |
virtual void setMaxDuration(size_t maxDuration) noexcept; |
| 57 |
virtual sample** interpolate(sample** src, size_t nSamples, size_t intrSize) = 0; |
| 58 |
|
| 59 |
/** |
| 60 |
* @brief calculateInternalSampleSize |
| 61 |
* @param nSamples number of samples after resampling |
| 62 |
* @param ok false is stored if something went wrong during calculation, otherwise true |
| 63 |
* @return number of samples to resmple |
| 64 |
*/ |
| 65 |
virtual size_t calculateInternalSampleSize(size_t nSamples, bool& ok) |
| 66 |
{ |
| 67 |
ok = true; |
| 68 |
return static_cast<size_t>(std::ceil(nSamples * rateRatio_)); |
| 69 |
} |
| 70 |
|
| 71 |
protected: |
| 72 |
int srcRate_, destRate_; |
| 73 |
size_t maxDuration_; |
| 74 |
float rateRatio_; |
| 75 |
sample* destBuf_[2]; |
| 76 |
|
| 77 |
void updateRateRatio() |
| 78 |
{ |
| 79 |
rateRatio_ = static_cast<float>(srcRate_) / destRate_; |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
class LinearResampler final : public AbstractResampler |
| 84 |
{ |
| 85 |
public: |
| 86 |
sample** interpolate(sample** src, size_t nSamples, size_t) override; |
| 87 |
}; |
| 88 |
|
| 89 |
class BlipResampler final : public AbstractResampler |
| 90 |
{ |
| 91 |
public: |
| 92 |
BlipResampler(bool fast = false); |
| 93 |
~BlipResampler(); |
| 94 |
void init(int srcRate, int destRate, size_t maxDuration) override; |
| 95 |
void reset() override; |
| 96 |
|
| 97 |
void setDestributionRate(int destRate) override |
| 98 |
{ |
| 99 |
init(srcRate_, destRate, maxDuration_); |
| 100 |
} |
| 101 |
|
| 102 |
size_t calculateInternalSampleSize(size_t nSamples, bool& ok) override; |
| 103 |
sample** interpolate(sample** src, size_t nSamples, size_t intrSize) override; |
| 104 |
|
| 105 |
private: |
| 106 |
struct Channel |
| 107 |
{ |
| 108 |
blip_t* blipBuf_; |
| 109 |
short prevSample_; |
| 110 |
} ch_[2]; |
| 111 |
|
| 112 |
void (*addDelta)(blip_t*, unsigned int, int); |
| 113 |
}; |
| 114 |
} |