70 static_assert(std::is_floating_point_v<T>,
"Dither requires a floating-point sample type");
89 explicit Dither(
int targetBits = 16,
bool noiseShaping =
false) noexcept
90 : noiseShaping_(noiseShaping)
92 static std::atomic<uint32_t> seedGenerator{ 0x193A6B54u };
94 rngState_ = seedGenerator.fetch_add(0x9E3779B9u, std::memory_order_relaxed);
97 if (rngState_ == 0) rngState_ = 1;
109 constexpr int maxBits = (
sizeof(T) == 4) ? 24 : 32;
110 targetBits_ = std::clamp(bits, 8, maxBits);
114 int64_t levels = int64_t(1) << (targetBits_ - 1);
116 quantScale_ =
static_cast<T
>(levels);
117 quantStep_ = T(1) / quantScale_;
118 loLevel_ = -quantScale_;
119 hiLevel_ = quantScale_ - T(1);
123 errorCap_ = T(2) * quantStep_;
141 for (
auto& h : errorState_) h.fill(T(0));
153 if (noiseShaping_ && channel >= 0 && channel < kMaxChannels)
155 : processSampleInternal<1>(input, channel);
156 return processSampleInternal<0>(input, channel);
171 if (noiseShaping_ && channel >= 0 && channel < kMaxChannels)
174 for (
int i = 0; i < numSamples; ++i)
175 data[i] = processSampleInternal<5>(data[i], channel);
177 for (
int i = 0; i < numSamples; ++i)
178 data[i] = processSampleInternal<1>(data[i], channel);
182 for (
int i = 0; i < numSamples; ++i)
183 data[i] = processSampleInternal<0>(data[i], channel);
191 static constexpr int kMaxChannels = 16;
192 static constexpr int kMaxOrder = 5;
195 static constexpr T kLipshitz5[kMaxOrder] = { T(2.033), T(-2.165), T(1.959), T(-1.590), T(0.6149) };
202 [[nodiscard]]
inline T processSampleInternal(T input,
int channel)
noexcept
208 if (input != input) input = T(0);
209 input = std::clamp(input, T(-2), T(2));
212 const T noise = (nextRandom() + nextRandom()) * quantStep_;
214 T preQuantise = input;
216 if constexpr (Order == 1)
218 preQuantise -= errorState_[
static_cast<size_t>(channel)][0];
220 else if constexpr (Order == kMaxOrder)
222 const auto& e = errorState_[
static_cast<size_t>(channel)];
223 preQuantise -= kLipshitz5[0] * e[0] + kLipshitz5[1] * e[1] + kLipshitz5[2] * e[2]
224 + kLipshitz5[3] * e[3] + kLipshitz5[4] * e[4];
228 const T dithered = preQuantise + noise;
234 const T level = std::clamp(std::nearbyint(dithered * quantScale_), loLevel_, hiLevel_);
235 const T quantised = level * quantStep_;
237 if constexpr (Order > 0)
247 auto& e = errorState_[
static_cast<size_t>(channel)];
248 const T err = std::clamp(quantised - preQuantise, -errorCap_, errorCap_);
249 if constexpr (Order == kMaxOrder)
251 e[4] = e[3]; e[3] = e[2]; e[2] = e[1]; e[1] = e[0];
263 [[nodiscard]]
inline T nextRandom() noexcept
265 rngState_ ^= rngState_ << 13;
266 rngState_ ^= rngState_ >> 17;
267 rngState_ ^= rngState_ << 5;
269 constexpr T scale = T(1) /
static_cast<T
>(std::numeric_limits<uint32_t>::max());
270 return static_cast<T
>(rngState_) * scale - T(0.5);
273 int targetBits_ = 16;
274 T quantStep_ = T(1) / T(32768);
275 T quantScale_ = T(32768);
276 T loLevel_ = T(-32768);
277 T hiLevel_ = T(32767);
278 T errorCap_ = T(2) / T(32768);
279 bool noiseShaping_ =
false;
282 std::array<std::array<T, kMaxOrder>, kMaxChannels> errorState_{};