Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 2 additions & 39 deletions src/FilterModelConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,11 @@ class FilterModelConfig
};

protected:
static inline uint16_t to_uint16_dither(double x, double d_noise)
{
const int tmp = static_cast<int>(x + d_noise);
assert((tmp >= 0) && (tmp <= USHRT_MAX));
return static_cast<uint16_t>(tmp);
}

static inline uint16_t to_uint16(double x)
{
return to_uint16_dither(x, 0.5);
return static_cast<uint16_t>(x);
}

private:
/*
* Hack to add quick dither when converting values from float to int
* and avoid quantization noise.
* Hopefully this can be removed the day we move all the analog part
* processing to floats.
*
* Not sure about the effect of using such small buffer of numbers
* since the random sequence repeats every 1024 values but for
* now it seems to do the job.
*/
class Randomnoise
{
private:
double buffer[1024];
mutable int index = 0;
public:
Randomnoise()
{
std::uniform_real_distribution<double> unif(0., 1.);
std::default_random_engine re;
for (int i=0; i<1024; i++)
buffer[i] = unif(re);
}
double getNoise() const { index = (index + 1) & 0x3ff; return buffer[index]; }
};

protected:
/// Capacitor value.
const double C;
Expand Down Expand Up @@ -134,9 +100,6 @@ class FilterModelConfig
/// Reverse op-amp transfer function.
uint16_t opamp_rev[1 << 16]; //-V730_NOINIT this is initialized in the derived class constructor

private:
Randomnoise rnd;

private:
FilterModelConfig(const FilterModelConfig&) = delete;
FilterModelConfig& operator= (const FilterModelConfig&) = delete;
Expand Down Expand Up @@ -296,7 +259,7 @@ class FilterModelConfig

inline uint16_t getNormalizedValue(double value) const
{
return to_uint16_dither(N16 * (value - vmin), rnd.getNoise());
return to_uint16(N16 * (value - vmin));
}

template<int N>
Expand Down
17 changes: 16 additions & 1 deletion src/resample/Resampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ class Resampler

Resampler() {}

mutable uint32_t m_wnoise = 34653463u;
mutable int32_t m_bnoise = 0;

int32_t bnoise() const
{
// white noise
m_wnoise = m_wnoise * 1664525u + 1013904223u;

// low-passed noise
int32_t n = (int32_t)((m_wnoise >> 20) & 0x7f) - 0x80;
m_bnoise = m_bnoise + ((0x06*(n - m_bnoise)) >> 7);

return m_bnoise;
}

public:
virtual ~Resampler() = default;

Expand All @@ -59,7 +74,7 @@ class Resampler
inline int16_t getOutput(int32_t scaleFactor) const
{
const int32_t out = (scaleFactor * output()) / 2;
return Limiter::softClip(out);
return Limiter::softClip(out + bnoise());
}

virtual void reset() = 0;
Expand Down
Loading