91 static_assert(std::is_floating_point_v<T>,
"MinBlepTable requires float or double");
95 static constexpr int kTaps = 128;
126 if (!(t >= T(0)) || !(t <
static_cast<T
>(
kTaps)))
128 const auto whole =
static_cast<int>(t);
129 const T pos = (t -
static_cast<T
>(whole)) *
static_cast<T
>(
kPhases);
130 const auto q = std::min(
static_cast<int>(pos),
kPhases - 1);
131 const T w = pos -
static_cast<T
>(q);
132 const float* a = rows_.data() +
static_cast<size_t>(q) *
kTaps;
133 const T ra =
static_cast<T
>(a[whole]);
134 const T rb =
static_cast<T
>(a[
kTaps + whole]);
135 return ra + w * (rb - ra);
148 void addStep(T* ring,
int head, T jump, T frac)
const noexcept
150 addRows(rows_, ring, head, jump, frac);
164 addRows(rows_, ring, head, jump, frac);
165 addEarly(ring, head, frac, [jump](T)
noexcept {
return jump; });
185 addRows(rampRows_, ring, head, slopeJump, frac);
186 const T delay = dcDelay_;
187 addEarly(ring, head, frac, [slopeJump, delay](T t)
noexcept {
return slopeJump * (t - delay); });
201 [[nodiscard]] T
dcDelay() const noexcept {
return dcDelay_; }
205 static constexpr int kCoarse = 64;
207 static constexpr double kBeta = 13.0;
213 [[nodiscard]]
static double refine(
const double* y,
double x)
noexcept
215 const double z = x - 0.5;
216 const double even1 = y[3] + y[2], odd1 = y[3] - y[2];
217 const double even2 = y[4] + y[1], odd2 = y[4] - y[1];
218 const double even3 = y[5] + y[0], odd3 = y[5] - y[0];
219 const double c0 = even1 * 0.42685983409379380 + even2 * 0.07238123511170030
220 + even3 * 0.00075893079450573;
221 const double c1 = odd1 * 0.35831772348893259 + odd2 * 0.20451644554758297
222 + odd3 * 0.00562658797241955;
223 const double c2 = even1 * -0.217009177221292431 + even2 * 0.20051376594086157
224 + even3 * 0.01649541128040211;
225 const double c3 = odd1 * -0.25112715343740988 + odd2 * 0.04223025992200458
226 + odd3 * 0.02488727472995134;
227 const double c4 = even1 * 0.04166946673533273 + even2 * -0.06250420114356986
228 + even3 * 0.02083473440841799;
229 const double c5 = odd1 * 0.08349799235675044 + odd2 * -0.04174912841630993
230 + odd3 * 0.00834987866042734;
231 return ((((c5 * z + c4) * z + c3) * z + c2) * z + c1) * z + c0;
234 void build() noexcept
236 constexpr int n =
kTaps * kCoarse + 1;
237 constexpr size_t fftSize = 131072;
238 constexpr double pi = std::numbers::pi_v<double>;
243 auto besselI0 = [](
double x)
noexcept {
244 double sum = 1.0, term = 1.0;
245 for (
int k = 1; k < 200; ++k)
247 term *= (x / (2.0 * k)) * (x / (2.0 * k));
249 if (term < sum * 1e-18)
break;
253 std::vector<double> x(fftSize, 0.0);
254 const double center = 0.5 * (n - 1);
255 const double norm = besselI0(kBeta);
257 for (
int i = 0; i < n; ++i)
259 const double t = (
static_cast<double>(i) - center) / kCoarse;
260 const double s = (std::abs(t) < 1e-9) ? 1.0 : std::sin(
pi * t) / (
pi * t);
261 const double r = (
static_cast<double>(i) - center) / center;
262 const double w = besselI0(kBeta * std::sqrt(std::max(0.0, 1.0 - r * r))) / norm;
263 x[
static_cast<size_t>(i)] = s * w;
266 for (
int i = 0; i < n; ++i)
267 x[
static_cast<size_t>(i)] /= dc;
272 FFTReal<double> fft(fftSize);
273 std::vector<double> spec(fftSize + 2);
274 std::vector<double> cep(fftSize);
275 fft.forward(x.data(), spec.data());
277 const size_t bins = fftSize / 2 + 1;
278 for (
size_t k = 0; k < bins; ++k)
280 const double re = spec[2 * k];
281 const double im = spec[2 * k + 1];
286 constexpr double floorMag = 1e-7;
287 const double mag = std::sqrt(re * re + im * im + floorMag * floorMag);
288 spec[2 * k] = std::log(mag);
289 spec[2 * k + 1] = 0.0;
291 fft.inverse(spec.data(), cep.data());
296 for (
size_t q = 1; q < fftSize / 2; ++q)
299 cep[fftSize - q] = 0.0;
304 fft.forward(cep.data(), spec.data());
305 for (
size_t k = 0; k < bins; ++k)
307 const double m = std::exp(spec[2 * k]);
308 const double ph = spec[2 * k + 1];
309 spec[2 * k] = m * std::cos(ph);
310 spec[2 * k + 1] = m * std::sin(ph);
312 fft.inverse(spec.data(), x.data());
316 std::vector<double> step(
static_cast<size_t>(n));
318 for (
int i = 0; i < n; ++i)
320 acc += x[
static_cast<size_t>(i)];
321 step[
static_cast<size_t>(i)] = acc;
323 const double settle = step[
static_cast<size_t>(n - 1)];
324 for (
double& s : step) s /= settle;
329 auto stepAt = [&](
long i)
noexcept {
330 if (i < 0)
return 0.0;
331 if (i >= n)
return 1.0;
332 return step[
static_cast<size_t>(i)];
334 constexpr long fineEnd =
static_cast<long>(
kTaps) *
kPhases;
335 constexpr int ratio =
kPhases / kCoarse;
336 std::vector<double> r1(
static_cast<size_t>(fineEnd + 1), 0.0);
337 for (
long fine = 0; fine < fineEnd; ++fine)
339 const long i = fine / ratio;
340 const double frac =
static_cast<double>(fine % ratio) / ratio;
342 for (
int k = 0; k < 6; ++k) y[k] = stepAt(i - 2 + k);
343 r1[
static_cast<size_t>(fine)] = refine(y, frac) - 1.0;
350 std::vector<double> r2(
static_cast<size_t>(fineEnd + 1), 0.0);
351 for (
long fine = 1; fine <= fineEnd; ++fine)
352 r2[
static_cast<size_t>(fine)] = r2[
static_cast<size_t>(fine - 1)]
353 + 0.5 * (r1[
static_cast<size_t>(fine - 1)] + r1[
static_cast<size_t>(fine)]) /
kPhases;
354 const double delay = -r2[
static_cast<size_t>(fineEnd)];
355 dcDelay_ =
static_cast<T
>(
delay);
358 rows_.assign(
static_cast<size_t>(
kPhases + 1) *
kTaps, 0.0f);
359 rampRows_.assign(
static_cast<size_t>(
kPhases + 1) *
kTaps, 0.0f);
360 for (
int q = 0; q <=
kPhases; ++q)
361 for (
int j = 0; j <
kTaps; ++j)
363 const long fine =
static_cast<long>(j) *
kPhases + q;
364 const auto at =
static_cast<size_t>(q) *
kTaps +
static_cast<size_t>(j);
365 if (fine >= fineEnd)
continue;
366 rows_[at] =
static_cast<float>(r1[
static_cast<size_t>(fine)]);
367 rampRows_[at] =
static_cast<float>(r2[
static_cast<size_t>(fine)] + delay);
374 template <
typename Term>
375 void addEarly(T* ring,
int head, T frac, Term term)
const noexcept
377 if (!(frac >= T(0)))
return;
378 for (
int j = 0; j <
kTaps; ++j)
380 const T t =
static_cast<T
>(j) + frac;
381 if (!(t < dcDelay_))
break;
382 ring[(head + j) & (
kTaps - 1)] += term(t);
387 static void addRows(
const std::vector<float>& rows, T* ring,
int head, T scale, T frac)
noexcept
389 if (!(frac >= T(0)) || !(frac <
static_cast<T
>(
kTaps)))
391 const auto whole =
static_cast<int>(frac);
392 const T pos = (frac -
static_cast<T
>(whole)) *
static_cast<T
>(
kPhases);
393 const auto q = std::min(
static_cast<int>(pos),
kPhases - 1);
394 const T w = pos -
static_cast<T
>(q);
395 const float* a = rows.data() +
static_cast<size_t>(q) *
kTaps + whole;
396 const float* b = a +
kTaps;
397 const int n =
kTaps - whole;
399 const int first = std::min(n,
kTaps - (head & (
kTaps - 1)));
400 T* r = ring + (head & (
kTaps - 1));
401 for (
int j = 0; j < first; ++j)
403 const T ra =
static_cast<T
>(a[j]);
404 r[j] += scale * (ra + w * (
static_cast<T
>(b[j]) - ra));
406 for (
int j = first; j < n; ++j)
408 const T ra =
static_cast<T
>(a[j]);
409 ring[j - first] += scale * (ra + w * (
static_cast<T
>(b[j]) - ra));
413 std::vector<float> rows_;
414 std::vector<float> rampRows_;