FlResampler.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "core/FlResampler.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #if defined(__SSE__) || defined(_M_X64) || defined(__x86_64__) // x86 only: rcpps cannot be reproduced through NEON; ARM64 takes the plain path
  5. #include "core/Simd.h"
  6. #define LYNX_FLRS_SSE 1
  7. #endif
  8. #if defined(__clang__)
  9. #pragma clang fp contract(off)
  10. #elif defined(__GNUC__)
  11. #pragma GCC optimize ("fp-contract=off")
  12. #endif
  13. namespace lynx::flrs
  14. {
  15. namespace
  16. {
  17. constexpr double pi = 3.141592653589793;
  18. // FL's index → half-width table (index 0 is unused)
  19. constexpr int halfWidths[10] = { 1, 1, 3, 8, 12, 16, 32, 64, 128, 256 };
  20. // The 6-point kernel: y(t) = Σ c_k·t^k, t = frac − ½, each c_k a fixed combination of the outer/middle/inner pair
  21. // sums (even powers) or differences (odd powers): k = 5 … 0, weights for pairs (−2, 3), (−1, 2), (0, 1).
  22. constexpr float hermite[6][3] = {
  23. { -1.0f / 24.0f, 5.0f / 24.0f, -5.0f / 12.0f }, // t^5, differences
  24. { 1.0f / 48.0f, -1.0f / 16.0f, 1.0f / 24.0f }, // t^4, sums
  25. { 5.0f / 48.0f, -11.0f / 16.0f, 37.0f / 24.0f }, // t^3, differences
  26. { -5.0f / 96.0f, 13.0f / 32.0f, -17.0f / 48.0f }, // t^2, sums
  27. { -3.0f / 128.0f, 61.0f / 384.0f, -87.0f / 64.0f }, // t^1, differences
  28. { 3.0f / 256.0f, -25.0f / 256.0f, 75.0f / 128.0f }, // t^0, sums
  29. };
  30. #if defined(__x86_64__) || defined(__i386__)
  31. using ext = long double; // the x87 format with GCC/Clang on x86
  32. #else
  33. using ext = double;
  34. #endif
  35. float linear (const float* t, int n, double frac) noexcept
  36. {
  37. const ext a = t[n - 1], b = t[n];
  38. return (float) ((b - a) * (ext) frac + a);
  39. }
  40. float sixPoint (const float* t, double frac) noexcept
  41. {
  42. // taps 0…5 = frames i−2 … i+3
  43. const ext x = (ext) frac - (ext) 0.5f;
  44. const float outerSum = (float) ((ext) t[5] + (ext) t[0]), outerDiff = (float) ((ext) t[0] - (ext) t[5]);
  45. const ext midSum = (ext) t[4] + (ext) t[1], midDiff = (ext) t[1] - (ext) t[4];
  46. const ext inSum = (ext) t[3] + (ext) t[2], inDiff = (ext) t[2] - (ext) t[3];
  47. auto odd = [&] (int k) { return (ext) outerDiff * (ext) hermite[k][0] + midDiff * (ext) hermite[k][1] + inDiff * (ext) hermite[k][2]; };
  48. auto even = [&] (int k) { return (ext) outerSum * (ext) hermite[k][0] + midSum * (ext) hermite[k][1] + inSum * (ext) hermite[k][2]; };
  49. ext acc = odd (0) * x;
  50. acc = (acc + even (1)) * x;
  51. acc = (acc + odd (2)) * x;
  52. acc = (acc + even (3)) * x;
  53. acc = (acc + odd (4)) * x;
  54. return (float) (acc + even (5));
  55. }
  56. // Windowed sinc over 2N taps: weight(d) = sin(w·d)/(π·d) · (0.36 + c + 0.64·c²)/2 with c = cos(π·d/N), d = frac − m
  57. // for tap m (frame i + m, m = −N+1 … N). The sines and cosines start from four library sines per output frame and
  58. // advance by the Chebyshev recurrence s(d−2) = 2·cos·s(d−1) − s(d) in single precision; 1/(π·d) is the SSE estimate
  59. // refined by one Newton step; the four lanes hold the taps m = 1, 0, 2, −1 and step outwards two frames per half.
  60. void sinc (const Kernel& k, const float* tl, const float* tr, double fracIn, float& outL, float& outR) noexcept
  61. {
  62. const int n = k.halfWidth;
  63. const double x = fracIn < 1e-25 ? 1e-25 : fracIn;
  64. const ext ex = (ext) x, na = -(ext) k.step, w = (ext) k.cutoff;
  65. const ext e0 = ex * na + (ext) 1.5707963267948966;
  66. const ext e2 = ex * w;
  67. const double s0 = std::sin ((double) e0), s1 = std::sin ((double) (e0 + na));
  68. const double s2 = std::sin ((double) e2), s3 = std::sin ((double) (e2 + w));
  69. float seed[8];
  70. {
  71. const ext a = s0, b = s1, c2 = (ext) k.twoCosStep;
  72. seed[7] = (float) a;
  73. seed[3] = (float) b;
  74. const ext t = a * c2 - b;
  75. seed[6] = (float) t;
  76. seed[2] = (float) (t * c2 - a);
  77. }
  78. {
  79. const ext a = s2, b = s3, c2 = (ext) k.twoCosCutoff;
  80. seed[5] = (float) a;
  81. seed[1] = (float) b;
  82. const ext t = a * c2 - b;
  83. seed[4] = (float) t;
  84. seed[0] = (float) (t * c2 - a);
  85. }
  86. #if defined(__x86_64__) || defined(__i386__)
  87. const ext piX = 3.14159265358979323846264338327950288L;
  88. #else
  89. const ext piX = pi;
  90. #endif
  91. const ext X = ex * piX;
  92. float denominators[4] = { (float) (X - piX), (float) X, (float) ((X - piX) - piX), (float) (X + piX) };
  93. const float twoPi = 6.2831855f;
  94. const float cw = (float) k.twoCosCutoff, cn = (float) k.twoCosStep;
  95. const int centre = n - 1; // tap index of frame i
  96. #if LYNX_FLRS_SSE
  97. __m128 den = _mm_loadu_ps (denominators), next = _mm_loadu_ps (seed), cur = _mm_loadu_ps (seed + 4);
  98. const __m128 twoCos = _mm_setr_ps (cw, cw, cn, cn);
  99. const __m128 c0 = _mm_setr_ps (0.36f, 0.36f, 0.0f, 0.0f), c2 = _mm_setr_ps (0.64f, 0.64f, 1.0f, 1.0f);
  100. const __m128 stepDen = _mm_setr_ps (-twoPi, twoPi, -twoPi, twoPi);
  101. __m128 acc = _mm_setzero_ps(), hi = _mm_setzero_ps();
  102. for (int j = 0; j < n / 2; ++j)
  103. {
  104. const int fwd = centre + 1 + 2 * j, back = centre - 1 - 2 * j;
  105. __m128 r = _mm_rcp_ps (den);
  106. const __m128 rr = _mm_mul_ps (_mm_mul_ps (den, r), r);
  107. __m128 v = cur;
  108. cur = next;
  109. next = _mm_sub_ps (_mm_mul_ps (next, twoCos), v);
  110. r = _mm_sub_ps (_mm_add_ps (r, r), rr);
  111. hi = _mm_movehl_ps (hi, v);
  112. __m128 win = _mm_add_ps (_mm_add_ps (_mm_mul_ps (_mm_mul_ps (c2, hi), hi), hi), c0);
  113. v = _mm_mul_ps (v, r);
  114. den = _mm_add_ps (den, stepDen);
  115. r = _mm_movehl_ps (r, r);
  116. v = _mm_mul_ps (v, win);
  117. v = _mm_shuffle_ps (v, v, 0x50);
  118. acc = _mm_add_ps (acc, _mm_mul_ps (v, _mm_setr_ps (tl[fwd], tr[fwd], tl[back + 1], tr[back + 1])));
  119. hi = _mm_movehl_ps (hi, cur);
  120. v = cur;
  121. win = _mm_add_ps (_mm_add_ps (_mm_mul_ps (_mm_mul_ps (c2, hi), hi), hi), c0);
  122. cur = next;
  123. next = _mm_sub_ps (_mm_mul_ps (next, twoCos), v);
  124. v = _mm_mul_ps (_mm_mul_ps (v, r), win);
  125. v = _mm_shuffle_ps (v, v, 0x50);
  126. acc = _mm_add_ps (acc, _mm_mul_ps (v, _mm_setr_ps (tl[fwd + 1], tr[fwd + 1], tl[back], tr[back])));
  127. }
  128. const __m128 sum = _mm_mul_ps (_mm_add_ps (_mm_movehl_ps (acc, acc), acc), _mm_set1_ps (0.5f));
  129. float o[4];
  130. _mm_storeu_ps (o, sum);
  131. outL = o[0];
  132. outR = o[1];
  133. #else
  134. // portable path: same recurrences, exact reciprocal
  135. float d[4] = { denominators[0], denominators[1], denominators[2], denominators[3] };
  136. float nx[4] = { seed[0], seed[1], seed[2], seed[3] }, cu[4] = { seed[4], seed[5], seed[6], seed[7] };
  137. const float tc[4] = { cw, cw, cn, cn }, step4[4] = { -twoPi, twoPi, -twoPi, twoPi };
  138. float acc[4] = {};
  139. for (int j = 0; j < n / 2; ++j)
  140. {
  141. const int fwd = centre + 1 + 2 * j, back = centre - 1 - 2 * j;
  142. float r[4], v[4], nn[4];
  143. for (int q = 0; q < 4; ++q)
  144. {
  145. r[q] = 1.0f / d[q];
  146. v[q] = cu[q];
  147. nn[q] = nx[q] * tc[q] - v[q];
  148. d[q] += step4[q];
  149. }
  150. const float wA = (0.64f * v[2] * v[2] + v[2]) + 0.36f, wB = (0.64f * v[3] * v[3] + v[3]) + 0.36f;
  151. const float a0 = v[0] * r[0] * wA, a1 = v[1] * r[1] * wB;
  152. acc[0] += a0 * tl[fwd];
  153. acc[1] += a0 * tr[fwd];
  154. acc[2] += a1 * tl[back + 1];
  155. acc[3] += a1 * tr[back + 1];
  156. float v2[4];
  157. for (int q = 0; q < 4; ++q)
  158. v2[q] = nx[q];
  159. for (int q = 0; q < 4; ++q)
  160. {
  161. cu[q] = nn[q];
  162. nx[q] = nn[q] * tc[q] - v2[q];
  163. }
  164. const float wC = (0.64f * v2[2] * v2[2] + v2[2]) + 0.36f, wD = (0.64f * v2[3] * v2[3] + v2[3]) + 0.36f;
  165. const float b0 = v2[0] * r[2] * wC, b1 = v2[1] * r[3] * wD;
  166. acc[0] += b0 * tl[fwd + 1];
  167. acc[1] += b0 * tr[fwd + 1];
  168. acc[2] += b1 * tl[back];
  169. acc[3] += b1 * tr[back];
  170. }
  171. outL = (acc[2] + acc[0]) * 0.5f;
  172. outR = (acc[3] + acc[1]) * 0.5f;
  173. #endif
  174. }
  175. } // namespace
  176. int halfWidthFor (int quality) noexcept
  177. {
  178. return halfWidths[std::clamp (quality, 1, 9)];
  179. }
  180. void Kernel::setQuality (int quality) noexcept
  181. {
  182. baseHalfWidth = halfWidth = halfWidthFor (quality);
  183. if (halfWidth > 6)
  184. {
  185. step = pi / (double) halfWidth;
  186. twoCosStep = std::cos (step) * 2.0;
  187. }
  188. speed = -1.0;
  189. }
  190. void Kernel::setSpeed (double s) noexcept
  191. {
  192. if (s == speed)
  193. return;
  194. speed = s;
  195. if (halfWidth <= 6)
  196. return;
  197. const auto magnitude = std::fabs (s);
  198. if (magnitude > 1.0)
  199. {
  200. cutoff = pi / magnitude;
  201. twoCosCutoff = std::cos (cutoff) * 2.0;
  202. }
  203. else
  204. {
  205. cutoff = pi;
  206. twoCosCutoff = -2.0;
  207. }
  208. auto width = (std::int64_t) ((std::llrint (magnitude) + 3) & ~(std::int64_t) 3);
  209. width = std::min<std::int64_t> (std::max<std::int64_t> (width, baseHalfWidth), 512);
  210. if ((int) width != halfWidth)
  211. {
  212. halfWidth = (int) width;
  213. step = pi / (double) halfWidth;
  214. twoCosStep = std::cos (step) * 2.0;
  215. }
  216. }
  217. void interpolate (const Kernel& k, const float* tl, const float* tr, double frac, float& outL, float& outR) noexcept
  218. {
  219. if (k.halfWidth < 3)
  220. {
  221. outL = linear (tl, k.halfWidth, frac);
  222. outR = linear (tr, k.halfWidth, frac);
  223. }
  224. else if (k.halfWidth == 3)
  225. {
  226. outL = sixPoint (tl, frac);
  227. outR = sixPoint (tr, frac);
  228. }
  229. else
  230. sinc (k, tl, tr, frac, outL, outR);
  231. }
  232. double speedFor (float cents, double ratio) noexcept
  233. {
  234. if (cents == 0.0f && ratio == 1.0)
  235. return 1.0;
  236. static const double perCent = (std::log (2.0) / 12.0) / 100.0;
  237. return std::exp ((double) cents * perCent) * ratio;
  238. }
  239. } // namespace lynx::flrs