45static constexpr std::array<float, 2464> coeffs = {
46 #include "ResampleCoeffs.ii"
51static constexpr int INDEX_INC = 128;
52static constexpr int COEFF_LEN = int(std::size(coeffs));
53static constexpr int COEFF_HALF_LEN = COEFF_LEN - 1;
66 void getCoeffs(
double ratio, std::span<const int16_t, HALF_TAB_LEN>& permute,
float*& table,
unsigned& filterLen);
76 static Table calcTable(
double ratio, std::span<int16_t, HALF_TAB_LEN> permute,
unsigned& filterLen);
85 std::vector<Element> cache;
88ResampleCoeffs::~ResampleCoeffs()
90 assert(cache.empty());
96 return resampleCoeffs;
100 double ratio, std::span<const int16_t, HALF_TAB_LEN>& permute,
float*& table,
unsigned& filterLen)
102 if (
auto it =
ranges::find(cache, ratio, &Element::ratio);
104 permute = std::span<int16_t, HALF_TAB_LEN>{it->permute};
105 table = it->table.data();
106 filterLen = it->filterLen;
114 auto perm = std::span<int16_t, HALF_TAB_LEN>{elem.permute};
115 elem.table = calcTable(ratio, perm, elem.filterLen);
117 table = elem.table.data();
118 filterLen = elem.filterLen;
119 cache.push_back(std::move(elem));
126 if (it->count == 0) {
238static constexpr unsigned N = TAB_LEN;
239static constexpr unsigned N1 = N - 1;
240static constexpr unsigned N2 = N / 2;
242static constexpr unsigned mapIdx(
unsigned x)
245 return (
t < N2) ?
t : N1 -
t;
248static constexpr std::pair<unsigned, unsigned> next(
unsigned x,
unsigned step)
250 return {mapIdx(x + step), mapIdx(N1 - x + step)};
253static void calcPermute(
double ratio, std::span<int16_t, HALF_TAB_LEN> permute)
255 double r2 = ratio * N;
256 double fract = r2 - floor(r2);
257 auto step = narrow_cast<unsigned>(floor(r2));
272 unsigned restart = incr ? 0 : N2 - 1;
273 unsigned curr = restart;
276 for (
auto i :
xrange(N2)) {
277 auto [nxt1, nxt2] = next(i, step);
278 if ((nxt1 == i) || (nxt2 == i)) { curr = i;
break; }
281 for (
unsigned i = N2 - 1; int(i) >= 0; --i) {
282 auto [nxt1, nxt2] = next(i, step);
283 if ((nxt1 == i) || (nxt2 == i)) { curr = i;
break; }
290 assert(permute[curr] == -1);
292 permute[curr] = narrow<int16_t>(cnt++);
294 auto [nxt1, nxt2] = next(curr, step);
295 if (permute[nxt1] == -1) {
298 }
else if (permute[nxt2] == -1) {
304 if (cnt == N2)
break;
307 while (permute[restart] != -1) {
310 assert(restart != N2);
312 assert(restart != 0);
320 std::array<int16_t, N2> testPerm;
322 assert(std::is_permutation(permute.begin(), permute.end(), testPerm.begin()));
328 double fraction = index.fractionAsDouble();
329 int indx = index.toInt();
330 return double(coeffs[indx]) +
331 fraction * (double(coeffs[indx + 1]) - double(coeffs[indx]));
334ResampleCoeffs::Table ResampleCoeffs::calcTable(
335 double ratio, std::span<int16_t, HALF_TAB_LEN> permute,
unsigned& filterLen)
337 calcPermute(ratio, permute);
339 double floatIncr = (ratio > 1.0) ? INDEX_INC / ratio : INDEX_INC;
340 double normFactor = floatIncr / INDEX_INC;
344 int min_idx = -maxFilterIndex.divAsInt(increment);
345 int max_idx = 1 + (maxFilterIndex - (increment -
FilterIndex(floatIncr))).divAsInt(increment);
346 int idx_cnt = max_idx - min_idx + 1;
347 filterLen = (idx_cnt + 3) & ~3;
348 min_idx -= (narrow<int>(filterLen) - idx_cnt) / 2;
349 Table table(HALF_TAB_LEN * filterLen);
352 for (
auto t :
xrange(HALF_TAB_LEN)) {
353 float* tab = &table[permute[
t] * filterLen];
354 double lastPos = (double(
t) + 0.5) / TAB_LEN;
358 int coeffCount = (maxFilterIndex - filterIndex).divAsInt(increment);
359 filterIndex += increment * coeffCount;
360 int bufIndex = -coeffCount;
362 tab[bufIndex - min_idx] =
363 float(getCoeff(filterIndex) * normFactor);
364 filterIndex -= increment;
368 filterIndex = increment - startFilterIndex;
369 coeffCount = (maxFilterIndex - filterIndex).divAsInt(increment);
370 filterIndex += increment * coeffCount;
371 bufIndex = 1 + coeffCount;
373 tab[bufIndex - min_idx] =
374 float(getCoeff(filterIndex) * normFactor);
375 filterIndex -= increment;
382static const std::array<int16_t, HALF_TAB_LEN> dummyPermute = {};
384template<
unsigned CHANNELS>
388 , hostClock(hostClock_)
389 , ratio(float(hostClock.getPeriod().toDouble() / getEmuClock().getPeriod().toDouble()))
390 , permute(dummyPermute)
395 unsigned extra = filterLen + 1 + narrow_cast<int>(ratio) + 1;
398 size_t initialSize = 4000;
399 buffer.resize((initialSize + extra) * CHANNELS);
402template<
unsigned CHANNELS>
410static inline __m128 reverse(__m128 x)
412 return _mm_shuffle_ps(x, x, _MM_SHUFFLE(0, 1, 2, 3));
415template<
bool REVERSE>
416static inline void calcSseMono(
const float* buf_,
const float* tab_,
size_t len,
float* out)
418 assert((len % 4) == 0);
419 assert((uintptr_t(tab_) % 16) == 0);
421 auto x = narrow<ptrdiff_t>((len & ~7) *
sizeof(
float));
422 assert((x % 32) == 0);
423 const char* buf = std::bit_cast<const char*>(buf_) + x;
424 const char* tab = std::bit_cast<const char*>(tab_) + (REVERSE ? -x : x);
427 __m128 a0 = _mm_setzero_ps();
428 __m128 a1 = _mm_setzero_ps();
430 __m128 b0 = _mm_loadu_ps(std::bit_cast<const float*>(buf + x + 0));
431 __m128 b1 = _mm_loadu_ps(std::bit_cast<const float*>(buf + x + 16));
433 if constexpr (REVERSE) {
434 t0 = reverse(_mm_loadu_ps(std::bit_cast<const float*>(tab - x - 16)));
435 t1 = reverse(_mm_loadu_ps(std::bit_cast<const float*>(tab - x - 32)));
437 t0 = _mm_loadu_ps (std::bit_cast<const float*>(tab + x + 0));
438 t1 = _mm_loadu_ps (std::bit_cast<const float*>(tab + x + 16));
440 __m128 m0 = _mm_mul_ps(b0, t0);
441 __m128 m1 = _mm_mul_ps(b1, t1);
442 a0 = _mm_add_ps(a0, m0);
443 a1 = _mm_add_ps(a1, m1);
444 x += 2 *
sizeof(__m128);
447 __m128 b0 = _mm_loadu_ps(std::bit_cast<const float*>(buf));
449 if constexpr (REVERSE) {
450 t0 =
reverse(_mm_loadu_ps(std::bit_cast<const float*>(tab - 16)));
452 t0 = _mm_loadu_ps (std::bit_cast<const float*>(tab));
454 __m128 m0 = _mm_mul_ps(b0, t0);
455 a0 = _mm_add_ps(a0, m0);
458 __m128 a = _mm_add_ps(a0, a1);
461 __m128
t = _mm_add_ps(a, _mm_movehl_ps(a, a));
462 __m128 s = _mm_add_ss(
t, _mm_shuffle_ps(
t,
t, 1));
464 _mm_store_ss(out, s);
467template<
int N>
static inline __m128 shuffle(__m128 x)
469 return _mm_castsi128_ps(_mm_shuffle_epi32(_mm_castps_si128(x), N));
471template<
bool REVERSE>
472static inline void calcSseStereo(
const float* buf_,
const float* tab_,
size_t len,
float* out)
474 assert((len % 4) == 0);
475 assert((uintptr_t(tab_) % 16) == 0);
477 auto x = narrow<ptrdiff_t>(2 * (len & ~7) *
sizeof(
float));
478 const auto* buf = std::bit_cast<const char*>(buf_) + x;
479 const auto* tab = std::bit_cast<const char*>(tab_);
482 __m128 a0 = _mm_setzero_ps();
483 __m128 a1 = _mm_setzero_ps();
484 __m128 a2 = _mm_setzero_ps();
485 __m128 a3 = _mm_setzero_ps();
487 __m128 b0 = _mm_loadu_ps(std::bit_cast<const float*>(buf + x + 0));
488 __m128 b1 = _mm_loadu_ps(std::bit_cast<const float*>(buf + x + 16));
489 __m128 b2 = _mm_loadu_ps(std::bit_cast<const float*>(buf + x + 32));
490 __m128 b3 = _mm_loadu_ps(std::bit_cast<const float*>(buf + x + 48));
492 if constexpr (REVERSE) {
493 ta =
reverse(_mm_loadu_ps(std::bit_cast<const float*>(tab - 16)));
494 tb =
reverse(_mm_loadu_ps(std::bit_cast<const float*>(tab - 32)));
495 tab -= 2 *
sizeof(__m128);
497 ta = _mm_loadu_ps (std::bit_cast<const float*>(tab + 0));
498 tb = _mm_loadu_ps (std::bit_cast<const float*>(tab + 16));
499 tab += 2 *
sizeof(__m128);
501 __m128 t0 = shuffle<0x50>(ta);
502 __m128 t1 = shuffle<0xFA>(ta);
503 __m128 t2 = shuffle<0x50>(tb);
504 __m128 t3 = shuffle<0xFA>(tb);
505 __m128 m0 = _mm_mul_ps(b0, t0);
506 __m128 m1 = _mm_mul_ps(b1, t1);
507 __m128 m2 = _mm_mul_ps(b2, t2);
508 __m128 m3 = _mm_mul_ps(b3, t3);
509 a0 = _mm_add_ps(a0, m0);
510 a1 = _mm_add_ps(a1, m1);
511 a2 = _mm_add_ps(a2, m2);
512 a3 = _mm_add_ps(a3, m3);
513 x += 4 *
sizeof(__m128);
516 __m128 b0 = _mm_loadu_ps(std::bit_cast<const float*>(buf + 0));
517 __m128 b1 = _mm_loadu_ps(std::bit_cast<const float*>(buf + 16));
519 if constexpr (REVERSE) {
520 ta =
reverse(_mm_loadu_ps(std::bit_cast<const float*>(tab - 16)));
522 ta = _mm_loadu_ps (std::bit_cast<const float*>(tab + 0));
524 __m128 t0 = shuffle<0x50>(ta);
525 __m128 t1 = shuffle<0xFA>(ta);
526 __m128 m0 = _mm_mul_ps(b0, t0);
527 __m128 m1 = _mm_mul_ps(b1, t1);
528 a0 = _mm_add_ps(a0, m0);
529 a1 = _mm_add_ps(a1, m1);
532 __m128 a01 = _mm_add_ps(a0, a1);
533 __m128 a23 = _mm_add_ps(a2, a3);
534 __m128 a = _mm_add_ps(a01, a23);
536 __m128 s = _mm_add_ps(a, _mm_movehl_ps(a, a));
537 _mm_store_ss(&out[0], s);
538 _mm_store_ss(&out[1], shuffle<0x55>(s));
543template<
unsigned CHANNELS>
544void ResampleHQ<CHANNELS>::calcOutput(
545 float pos,
float* __restrict output)
547 assert((filterLen & 3) == 0);
549 int bufIdx = int(pos) + bufStart;
550 assert((bufIdx + filterLen) <= bufEnd);
552 const float* buf = &buffer[bufIdx];
554 auto t = size_t(lrintf(pos * TAB_LEN)) % TAB_LEN;
555 if (!(
t & HALF_TAB_LEN)) {
558 const float* tab = &table[
t * filterLen];
561 if constexpr (CHANNELS == 1) {
562 calcSseMono <false>(buf, tab, filterLen, output);
564 calcSseStereo<false>(buf, tab, filterLen, output);
570 for (
auto ch :
xrange(CHANNELS)) {
575 for (
size_t i = 0; i < filterLen; i += 4) {
576 r0 += tab[i + 0] * buf[CHANNELS * (i + 0)];
577 r1 += tab[i + 1] * buf[CHANNELS * (i + 1)];
578 r2 += tab[i + 2] * buf[CHANNELS * (i + 2)];
579 r3 += tab[i + 3] * buf[CHANNELS * (i + 3)];
581 output[ch] = r0 + r1 + r2 + r3;
586 t = permute[TAB_LEN - 1 -
t];
587 const float* tab = &table[(
t + 1) * filterLen];
590 if constexpr (CHANNELS == 1) {
591 calcSseMono <true>(buf, tab, filterLen, output);
593 calcSseStereo<true>(buf, tab, filterLen, output);
599 for (
auto ch :
xrange(CHANNELS)) {
604 for (ptrdiff_t i = 0; i < ptrdiff_t(filterLen); i += 4) {
605 r0 += tab[-i - 1] * buf[CHANNELS * (i + 0)];
606 r1 += tab[-i - 2] * buf[CHANNELS * (i + 1)];
607 r2 += tab[-i - 3] * buf[CHANNELS * (i + 2)];
608 r3 += tab[-i - 4] * buf[CHANNELS * (i + 3)];
610 output[ch] = r0 + r1 + r2 + r3;
616template<
unsigned CHANNELS>
617void ResampleHQ<CHANNELS>::prepareData(
unsigned emuNum)
620 unsigned free = unsigned(buffer.size() / CHANNELS) - bufEnd;
624 unsigned available = bufEnd - bufStart;
625 memmove(&buffer[0], &buffer[bufStart *
size_t(CHANNELS)],
626 available *
size_t(CHANNELS) *
sizeof(
float));
630 free = unsigned(buffer.size() / CHANNELS) - bufEnd;
631 auto missing = narrow_cast<int>(emuNum - free);
632 if (missing > 0) [[unlikely]] {
640 buffer.resize(buffer.size() + missing *
size_t(CHANNELS));
644 auto tmpBuf =
subspan(tmpBufExtra, 0, emuNum * CHANNELS);
645 if (input.generateInput(tmpBufExtra.data(), emuNum)) {
647 subspan(buffer, bufEnd * CHANNELS));
649 nonzeroSamples = bufEnd - bufStart;
655 assert(bufStart <= bufEnd);
656 assert(bufEnd <= (buffer.size() / CHANNELS));
659template<
unsigned CHANNELS>
661 float* __restrict dataOut,
size_t hostNum, EmuTime::param time)
663 auto& emuClk = getEmuClock();
664 unsigned emuNum = emuClk.getTicksTill(time);
669 bool notMuted = nonzeroSamples > 0;
672 EmuTime host1 = hostClock.getFastAdd(1);
673 assert(host1 > emuClk.getTime());
674 auto pos = narrow_cast<float>(emuClk.getTicksTillDouble(host1));
675 assert(pos <= (ratio + 2));
676 for (
auto i :
xrange(hostNum)) {
677 calcOutput(pos, &dataOut[i * CHANNELS]);
683 nonzeroSamples = std::max<int>(0, nonzeroSamples - emuNum);
685 assert(bufStart <= bufEnd);
686 unsigned available = bufEnd - bufStart;
687 unsigned extra = filterLen + 1 + narrow_cast<int>(ratio) + 1;
688 assert(available == extra); (void)available; (void)extra;
Represents a clock with a variable frequency.
static ResampleCoeffs & instance()
ResampleCoeffs(const ResampleCoeffs &)=delete
ResampleCoeffs & operator=(ResampleCoeffs &&)=delete
void getCoeffs(double ratio, std::span< const int16_t, HALF_TAB_LEN > &permute, float *&table, unsigned &filterLen)
ResampleCoeffs & operator=(const ResampleCoeffs &)=delete
ResampleCoeffs(ResampleCoeffs &&)=delete
void releaseCoeffs(double ratio)
ResampleHQ(ResampledSoundDevice &input, const DynamicClock &hostClock)
bool generateOutputImpl(float *dataOut, size_t num, EmuTime::param time) override
This file implemented 3 utility functions:
FixedPoint< 16 > FilterIndex
constexpr void fill(ForwardRange &&range, const T &value)
constexpr void iota(ForwardIt first, ForwardIt last, T value)
auto find(InputRange &&range, const T &value)
constexpr auto copy(InputRange &&range, OutputIter out)
constexpr auto reverse(Range &&range)
constexpr auto subspan(Range &&range, size_t offset, size_t count=std::dynamic_extent)
void move_pop_back(VECTOR &v, typename VECTOR::iterator it)
Erase the pointed to element from the given vector.
auto rfind_unguarded(RANGE &range, const VAL &val, Proj proj={})
Similar to the find(_if)_unguarded functions above, but searches from the back to front.
constexpr auto xrange(T e)
constexpr auto end(const zstring_view &x)