diff --git a/src/FilterModelConfig.h b/src/FilterModelConfig.h index d875653..c9dc940 100644 --- a/src/FilterModelConfig.h +++ b/src/FilterModelConfig.h @@ -54,45 +54,11 @@ class FilterModelConfig }; protected: - static inline uint16_t to_uint16_dither(double x, double d_noise) - { - const int tmp = static_cast(x + d_noise); - assert((tmp >= 0) && (tmp <= USHRT_MAX)); - return static_cast(tmp); - } - static inline uint16_t to_uint16(double x) { - return to_uint16_dither(x, 0.5); + return static_cast(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 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; @@ -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; @@ -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 diff --git a/src/resample/Resampler.h b/src/resample/Resampler.h index aa68412..31b87da 100644 --- a/src/resample/Resampler.h +++ b/src/resample/Resampler.h @@ -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; @@ -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;