PitcherDsp.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #pragma once
  2. // FL Studio's Pitcher DSP, rebuilt from the behaviour decoded in docs/analise/REA_rodada30.md §2 (clean-room; the
  3. // reference model r30/py/pitcher_model.py matches the installation's engine bit-exactly in 16 of 21 cases, the rest
  4. // limited by the CRT's log2f rounding). Header-only so the numerical harness can drive it directly.
  5. //
  6. // Detector (§2.2): a 0.6 tracker high-pass, 16 one-pole low-passes in double, an integer-lag AMDF over frames of
  7. // N = 4·⌊fs/f_min/4⌋ samples, first local minimum below 0.3, valid f_min…1000 Hz. Correction (§2.3): nearest
  8. // allowed note of a 128-note mask (midpoint rule), key bypass, Correction Speed pole, Pitch Reference.
  9. // Shifters: plain (§2.4: 4 Hermite read heads on a 2N delay line, period splices with a 128-sample cross-fade,
  10. // 9-sample latency) or formant (§2.5: TD-PSOLA with 2-period Hann grains resampled by the Gender factor; a
  11. // 256-Hann overlap-add with 257-sample latency while unvoiced or unshifted). Harmonize and MIDI (§2.6/2.7) take
  12. // held notes through noteOn()/noteOff().
  13. //
  14. // Arithmetic follows the model: float32 everywhere, double where the model uses it (filter stages, log2, pow,
  15. // cos). Memory is reserved by reserve(); nothing allocates while processing.
  16. #include <algorithm>
  17. #include <array>
  18. #include <cmath>
  19. #include <cstdint>
  20. #include <vector>
  21. // The model rounds every float32 operation: no fused multiply-adds here (on FMA targets they move the output by
  22. // ~1e-7 and, over time, the instant of a splice)
  23. #if defined(__clang__)
  24. #pragma clang fp contract(off)
  25. #elif defined(__GNUC__)
  26. #pragma GCC push_options
  27. #pragma GCC optimize("fp-contract=off")
  28. #endif
  29. namespace lynx::pitcher
  30. {
  31. // FL's raw parameter values (Pitcher parameter indices in brackets)
  32. struct Params
  33. {
  34. float speed = 1.0f; // [0] Correction Speed
  35. float stereo = 1.0f; // [1] harmony voice pan multiplier
  36. float gender = 0.5f; // [2] formant path: grain resampling 2^(2v − 1)
  37. float fine = 0.5f; // [3] 2v − 1 semitones
  38. bool bypassAll = false; // [4]
  39. std::array<bool, 12> keyBypass {}; // [5…16]
  40. std::array<float, 4> voicePan { 0.5f, 0.5f, 0.5f, 0.5f }; // [17…20]
  41. bool formant = false; // [22]
  42. float aRef = 0.5f; // [23] A = 55·2^((2v − 1)/12)
  43. float pitchRef = 0.0f; // [24] int(2v): 0 left, 1 independent, 2 right
  44. bool midi = false, octaves = false, harmonize = false; // [25] [26] [27]
  45. bool mix = false, vel = false; // [30] Replace/Mix, [31] velocity weight
  46. int range = 2; // [32] f_min 220/170/110/80/55/25 Hz
  47. std::array<bool, 12> scale { true, true, true, true, true, true, true, true, true, true, true, true }; // note mask C…B
  48. };
  49. class Pitcher
  50. {
  51. public:
  52. static constexpr double rangeHz[6] { 220.0, 170.0, 110.0, 80.0, 55.0, 25.0 };
  53. static int windowFor (double fs, int range) noexcept
  54. {
  55. return 4 * ((int) (fs / rangeHz[range < 0 ? 0 : (range > 5 ? 5 : range)]) >> 2);
  56. }
  57. // Reserves the buffers for every range up to `maxFs` (call off the audio thread).
  58. void reserve (double maxFs)
  59. {
  60. const auto n = (std::size_t) windowFor (maxFs, 5) + 4;
  61. const auto b = (std::size_t) (maxFs * 0.1) + 4;
  62. for (int c = 0; c < 2; ++c)
  63. {
  64. hist[c].assign (n, 0.0f);
  65. amdf[c].assign (n, 0.0f);
  66. delay[c].assign (2 * n, 0.0f);
  67. inRing[c].assign (b + 2, 0.0f);
  68. lpRing[c].assign (b + 2, 0.0f);
  69. outRing[c].assign (b, 0.0f);
  70. }
  71. grain.assign (2 * n + 4, 0.0f);
  72. capacityN = (int) n - 4;
  73. capacityB = (int) b - 4;
  74. for (int i = 0; i < 256; ++i)
  75. hann256[(std::size_t) i] = (float) ((1.0 - std::cos ((double) ((float) i * 0.00390625f) * 6.283185307179586)) * 0.5);
  76. }
  77. void prepare (double sampleRate) noexcept
  78. {
  79. fs = sampleRate;
  80. initialised = false;
  81. }
  82. void reset() noexcept { initialised = false; }
  83. // MIDI input (Harmonize / MIDI mode): velocity 0…127, 0 = note off
  84. void noteOn (int key, int velocity) noexcept
  85. {
  86. if (key >= 0 && key < 128)
  87. held[(std::size_t) key] = (float) velocity * 0.007874015718698502f;
  88. }
  89. void noteOff (int key) noexcept { noteOn (key, 0); }
  90. void allNotesOff() noexcept { held.fill (0.0f); }
  91. // Latency of the current path in samples (plain 9; formant pass-through 257)
  92. int latency() const noexcept { return formantOn ? 257 : 9; }
  93. void process (float* left, float* right, int n, const Params& p) noexcept
  94. {
  95. configure (p);
  96. if (N <= 0)
  97. return;
  98. harmony (p);
  99. const auto L = this->L;
  100. for (int t = 0; t < n; ++t)
  101. {
  102. const float in[2] { left[t], right[t] };
  103. float shifts[2][4] {}, gains[2][4] {};
  104. bool mixDry = false;
  105. if (! p.harmonize)
  106. {
  107. shifts[0][0] = fine + (ref == 2 ? sm[1] : sm[0]);
  108. shifts[1][0] = fine + ((ref == 1 || ref == 2) ? sm[1] : sm[0]);
  109. gains[0][0] = gains[1][0] = 1.0f;
  110. }
  111. else
  112. {
  113. for (int c = 0; c < 2; ++c)
  114. for (int v = 0; v < 4; ++v)
  115. {
  116. shifts[c][v] = hShift[c][v];
  117. gains[c][v] = hGain[c][v];
  118. }
  119. mixDry = hMix;
  120. }
  121. float o[2] { 0.0f, 0.0f };
  122. if (formantOn)
  123. {
  124. if (ref == 0)
  125. std::copy (timer[0], timer[0] + 4, timer[1]);
  126. else if (ref == 2)
  127. std::copy (timer[1], timer[1] + 4, timer[0]);
  128. o[0] = renderFormant (0, shifts, gains, in[0]);
  129. o[1] = renderFormant (1, shifts, gains, in[1]);
  130. if (++W2 == B)
  131. W2 = 0;
  132. }
  133. // the reference channel's read heads are copied (only the heads, as FL does)
  134. if (ref == 0)
  135. std::copy (R[0], R[0] + 4, R[1]);
  136. else if (ref == 2)
  137. std::copy (R[1], R[1] + 4, R[0]);
  138. if (! formantOn)
  139. {
  140. for (int c = 0; c < 2; ++c)
  141. {
  142. const auto P = period[c];
  143. auto* dl = delay[c].data();
  144. for (int v = 0; v < 4; ++v)
  145. {
  146. auto rho = 1.0f;
  147. if (P != 0.0f)
  148. {
  149. auto s = shifts[c][v];
  150. s = s > 12.0f ? 12.0f : (s < -12.0f ? -12.0f : s);
  151. rho = pow2Cached (rhoCache[c][v], s * 0.083333336f);
  152. }
  153. auto r = R[c][v];
  154. auto y = interp (dl, L, r);
  155. if (rho < 1.0f)
  156. {
  157. auto d = r - (float) W;
  158. if (d < 0.0f)
  159. d = d + (float) L;
  160. if (d <= 128.0f)
  161. {
  162. O[c][v] = r;
  163. r = r + P;
  164. K[c][v] = 128;
  165. }
  166. }
  167. else if (rho > 1.0f)
  168. {
  169. auto d = r - (float) W;
  170. if (d < 0.0f)
  171. d = d + (float) L;
  172. if (d >= (float) (L - 128))
  173. {
  174. O[c][v] = r;
  175. r = r - P;
  176. K[c][v] = 128;
  177. if (r < 0.0f)
  178. r = (float) L + r;
  179. }
  180. }
  181. const auto k = K[c][v];
  182. if (k > 0)
  183. {
  184. const auto yo = interp (dl, L, O[c][v]);
  185. K[c][v] = k - 1;
  186. O[c][v] = O[c][v] + rho;
  187. const auto w = (float) k * 0.0078125f;
  188. y = (1.0f - w) * y + w * yo;
  189. }
  190. while (r >= (float) L)
  191. r = r - (float) L;
  192. while (r < 0.0f)
  193. r = r + (float) L;
  194. R[c][v] = r + rho;
  195. const auto g = (gains[c][v] - G[c][v]) * 0.007f + G[c][v];
  196. G[c][v] = g;
  197. o[c] = o[c] + g * y;
  198. }
  199. dl[W] = in[c];
  200. }
  201. if (++W == L)
  202. W = 0;
  203. }
  204. if (mixDry)
  205. {
  206. o[0] = o[0] + in[0];
  207. o[1] = o[1] + in[1];
  208. }
  209. left[t] = o[0];
  210. right[t] = o[1];
  211. // detector
  212. for (int c = 0; c < 2; ++c)
  213. {
  214. const auto xi = in[c];
  215. const auto d = (xi - dc[c]) * 0.6f + dc[c];
  216. dc[c] = d;
  217. if (1e-9f > d && d > -1e-9f)
  218. dc[c] = 0.0f;
  219. auto v = (double) (xi - d);
  220. auto& st = lp[c];
  221. for (int k = 0; k < 16; ++k)
  222. {
  223. const auto s = (v - st[(std::size_t) k]) * 0.125 + st[(std::size_t) k];
  224. st[(std::size_t) k] = (1e-9 > s && s > -1e-9) ? 0.0 : s;
  225. v = (double) (float) s;
  226. }
  227. const auto y = (float) v;
  228. lpRing[c][(std::size_t) W2] = y;
  229. // AMDF: slot k (lag N − k) pairs y with the k-th oldest history value
  230. auto* A = amdf[c].data();
  231. const auto* H = hist[c].data();
  232. const auto h0 = histPos;
  233. // two contiguous runs of the ring (no wrap test inside): each A[k] still gets the same single
  234. // addition, so the result is unchanged, and the loops vectorise
  235. const auto first = N - h0;
  236. for (int k = 0; k < first; ++k)
  237. A[k] += std::abs (H[h0 + k] - y);
  238. for (int k = first; k < N; ++k)
  239. A[k] += std::abs (H[k - first] - y);
  240. hist[c][(std::size_t) h0] = y;
  241. sm[c] = freq[c] == 0.0f ? 0.0f : (target[c] - sm[c]) * speed + sm[c];
  242. }
  243. if (++histPos == N)
  244. histPos = 0;
  245. if (++frameCount == N)
  246. {
  247. detect();
  248. frameCount = 0;
  249. }
  250. }
  251. }
  252. private:
  253. void configure (const Params& p) noexcept
  254. {
  255. const auto range = p.range < 0 ? 0 : (p.range > 5 ? 5 : p.range);
  256. auto n = windowFor (fs, range);
  257. if (n > capacityN)
  258. n = capacityN;
  259. if (! initialised || n != N || range != rangeIndex)
  260. {
  261. rangeIndex = range;
  262. N = n;
  263. L = 2 * n;
  264. B = (int) (fs * 0.1);
  265. if (B > capacityB)
  266. B = capacityB;
  267. fmin = rangeHz[range];
  268. restartDetector();
  269. formantOn = p.formant;
  270. restartShifter();
  271. initialised = true;
  272. }
  273. else if (p.formant != formantOn)
  274. {
  275. formantOn = p.formant;
  276. restartShifter();
  277. }
  278. const auto u = 1.0f - p.speed;
  279. speed = (double) u >= 0.01 ? (float) (8.82 / (fs * (double) (u * u * u * 3.0f + 0.001f))) : 1.0f;
  280. fine = p.fine + p.fine - 1.0f;
  281. A = (float) (std::pow (2.0, (2.0 * (double) p.aRef - 1.0) / 12.0) * 55.0);
  282. ref = (int) ((double) p.pitchRef + (double) p.pitchRef);
  283. if (p.gender != lastGenderParam)
  284. {
  285. lastGenderParam = p.gender;
  286. genderStep = (float) std::pow (2.0, (double) (2.0f * p.gender - 1.0f));
  287. }
  288. midi = p.midi;
  289. bypassAll = p.bypassAll;
  290. keyBypass = p.keyBypass;
  291. buildMask (p);
  292. }
  293. void restartDetector() noexcept
  294. {
  295. for (int c = 0; c < 2; ++c)
  296. {
  297. std::fill (hist[c].begin(), hist[c].end(), 0.0f);
  298. std::fill (amdf[c].begin(), amdf[c].end(), 0.0f);
  299. lp[c].fill (0.0);
  300. dc[c] = 0.0f;
  301. freq[c] = period[c] = cls[c] = target[c] = sm[c] = 0.0f;
  302. cls[c] = -1.0f;
  303. octave[c] = -1;
  304. }
  305. histPos = frameCount = 0;
  306. }
  307. // FL's restart (Formant toggled, sample rate set): buffers cleared, heads at L − 10, timers and gains 0
  308. void restartShifter() noexcept
  309. {
  310. for (int c = 0; c < 2; ++c)
  311. {
  312. std::fill (delay[c].begin(), delay[c].end(), 0.0f);
  313. std::fill (inRing[c].begin(), inRing[c].end(), 0.0f);
  314. std::fill (lpRing[c].begin(), lpRing[c].end(), 0.0f);
  315. std::fill (outRing[c].begin(), outRing[c].end(), 0.0f);
  316. for (int v = 0; v < 4; ++v)
  317. {
  318. R[c][v] = (float) L - 10.0f;
  319. O[c][v] = 0.0f;
  320. K[c][v] = 0;
  321. G[c][v] = 0.0f;
  322. timer[c][v] = 0;
  323. }
  324. }
  325. W = W2 = 0;
  326. }
  327. void buildMask (const Params& p) noexcept
  328. {
  329. mask.fill (false);
  330. if (! p.midi)
  331. {
  332. for (int c = 0; c < 12; ++c)
  333. if (p.scale[(std::size_t) c])
  334. for (int k = c; k < 128; k += 12)
  335. mask[(std::size_t) k] = true;
  336. }
  337. else if (! p.octaves)
  338. {
  339. for (int k = 0; k < 128; ++k)
  340. if (held[(std::size_t) k] != 0.0f)
  341. for (int j = k % 12; j < 128; j += 12)
  342. mask[(std::size_t) j] = true;
  343. }
  344. else
  345. {
  346. for (int k = 13; k < 128; ++k)
  347. if (held[(std::size_t) k] != 0.0f)
  348. mask[(std::size_t) (k - 12)] = true;
  349. }
  350. anyAllowed = false;
  351. for (auto m : mask)
  352. anyAllowed = anyAllowed || m;
  353. }
  354. // frame end: normalised AMDF, first local minimum below 0.3 from the short lags
  355. void detect() noexcept
  356. {
  357. for (int c = 0; c < 2; ++c)
  358. {
  359. auto* a = amdf[c].data();
  360. auto mx = 0.0f;
  361. for (int i = 0; i < N; ++i)
  362. mx = a[i] > mx ? a[i] : mx;
  363. if (mx == 0.0f)
  364. {
  365. period[c] = freq[c] = 0.0f;
  366. continue;
  367. }
  368. const auto inv = 1.0f / mx;
  369. for (int i = 0; i < N; ++i)
  370. a[i] = inv * a[i];
  371. auto found = -1;
  372. for (int i = N - 2; i > 0; --i)
  373. if (a[i] < a[i - 1] && a[i] < a[i + 1] && a[i] < 0.3f)
  374. {
  375. found = i;
  376. break;
  377. }
  378. auto f = 0.0f;
  379. if (found < 0)
  380. period[c] = 0.0f;
  381. else
  382. {
  383. period[c] = (float) N - (float) found;
  384. f = (float) (fs / (double) period[c]);
  385. }
  386. freq[c] = f;
  387. if (f < (float) fmin || 1000.0f < f)
  388. period[c] = freq[c] = 0.0f;
  389. }
  390. for (int c = 0; c < 2; ++c)
  391. std::fill (amdf[c].begin(), amdf[c].begin() + N, 0.0f);
  392. targets();
  393. }
  394. void targets() noexcept
  395. {
  396. for (int c = 0; c < 2; ++c)
  397. {
  398. if (freq[c] == 0.0f || ! anyAllowed)
  399. {
  400. cls[c] = -1.0f;
  401. octave[c] = -1;
  402. target[c] = sm[c] = 0.0f;
  403. continue;
  404. }
  405. const auto lg = (float) std::log2 ((double) (A / freq[c]));
  406. const auto nn = 9.0f - lg * 12.0f;
  407. const auto o = (int) (nn * 0.083333336f);
  408. octave[c] = o;
  409. auto idx = (int) std::abs (nn + 0.5f);
  410. idx = idx > 127 ? 127 : idx;
  411. const auto cl = nn - (float) o * 12.0f;
  412. cls[c] = cl;
  413. auto lo = idx, hi = idx;
  414. if (! mask[(std::size_t) idx])
  415. {
  416. while (lo >= 1)
  417. if (mask[(std::size_t) --lo])
  418. break;
  419. while (hi <= 0x7e)
  420. if (mask[(std::size_t) ++hi])
  421. break;
  422. }
  423. const auto chosen = nn < (float) (hi - lo) * 0.5f + (float) lo ? lo : hi;
  424. target[c] = (float) chosen - nn;
  425. if (! midi)
  426. {
  427. auto k = (int) (cl + 0.5f);
  428. if (k >= 12)
  429. k = 0;
  430. if (k < 0)
  431. k += 12; // (the model's index wraps; only reachable below C1 with the 25 Hz range)
  432. if (keyBypass[(std::size_t) k] || bypassAll)
  433. target[c] = 0.0f;
  434. }
  435. }
  436. }
  437. // Harmonize: voices from the first 4 held notes, once per block
  438. void harmony (const Params& p) noexcept
  439. {
  440. if (! p.harmonize)
  441. return;
  442. for (int c = 0; c < 2; ++c)
  443. for (int v = 0; v < 4; ++v)
  444. hGain[c][v] = hShift[c][v] = 0.0f;
  445. hMix = p.mix;
  446. if (! hMix)
  447. hGain[0][0] = hGain[1][0] = 1.0f;
  448. int notes[4] { -1, -1, -1, -1 };
  449. float weights[4] {};
  450. if (p.midi)
  451. {
  452. int count = 0;
  453. for (int k = 0; k < 128 && count < 4; ++k)
  454. {
  455. const auto v = held[(std::size_t) k];
  456. if (v == 0.0f)
  457. continue;
  458. if (! p.octaves)
  459. {
  460. const auto cl = k % 12;
  461. if (cl == notes[0] || cl == notes[1] || cl == notes[2])
  462. continue;
  463. weights[count] = v * v + v * v;
  464. notes[count] = cl;
  465. }
  466. else
  467. notes[count] = k - 12;
  468. ++count;
  469. }
  470. }
  471. for (int c = 0; c < 2; ++c)
  472. {
  473. auto nd = cls[c];
  474. if (p.octaves)
  475. nd = (float) ((double) octave[c] * 12.0 + (double) nd);
  476. for (int j = 0; j < 4; ++j)
  477. if (notes[j] >= 0 && freq[c] != 0.0f)
  478. {
  479. const auto d = (p.voicePan[(std::size_t) j] - 0.5f) * p.stereo;
  480. auto g = c == 0 ? 0.5f - d : d + 0.5f;
  481. g = g + g;
  482. g = g < 0.0f ? 0.0f : (g > 1.0f ? 1.0f : g);
  483. if (p.vel)
  484. g = g * weights[j];
  485. hGain[c][j] = g;
  486. hShift[c][j] = (float) notes[j] - nd;
  487. }
  488. }
  489. }
  490. struct PowCache
  491. {
  492. float arg = 0.0f, value = 1.0f;
  493. };
  494. static float pow2Cached (PowCache& cache, float x) noexcept
  495. {
  496. if (x != cache.arg)
  497. {
  498. cache.arg = x;
  499. cache.value = (float) std::pow (2.0, (double) x);
  500. }
  501. return cache.value;
  502. }
  503. // 4-point cubic Hermite between the 2nd and 3rd points from ⌊pos⌋
  504. static float interp (const float* buf, int L, float pos) noexcept
  505. {
  506. if (pos < 0.0f)
  507. return 0.0f;
  508. auto i0 = (int) pos;
  509. const auto f = pos - (float) i0;
  510. i0 = i0 >= L ? i0 - L : i0;
  511. auto i1 = i0 + 1;
  512. i1 = i1 >= L ? i1 - L : i1;
  513. auto i2 = i1 + 1;
  514. i2 = i2 >= L ? i2 - L : i2;
  515. auto i3 = i2 + 1;
  516. i3 = i3 >= L ? i3 - L : i3;
  517. const auto y0 = buf[i0], y1 = buf[i1], y2 = buf[i2], y3 = buf[i3];
  518. const auto c1 = (y2 - y0) * 0.5f;
  519. const auto c3 = (y3 - y0) * 0.5f + (y1 - y2) * 1.5f;
  520. const auto c2 = ((y0 - y1) + c1) - c3;
  521. return ((c3 * f + c2) * f + c1) * f + y1;
  522. }
  523. // grain overlap-add into the output ring from the current position; step 0 = no resampling
  524. void overlapAdd (int c, int n, float step, float gain) noexcept
  525. {
  526. auto* o = outRing[c].data();
  527. auto pos = W2;
  528. if (step == 0.0f)
  529. {
  530. for (int i = 0; i < n; ++i)
  531. {
  532. const auto q = pos >= B ? pos - B : pos;
  533. o[q] = gain * grain[(std::size_t) i] + o[q];
  534. pos = q + 1;
  535. }
  536. return;
  537. }
  538. auto fp = 0.0f;
  539. while (fp < (float) n)
  540. {
  541. const auto i = (int) fp;
  542. const auto fr = fp - (float) i;
  543. auto v = (1.0f - fr) * grain[(std::size_t) i];
  544. if (i < n - 1)
  545. v = fr * grain[(std::size_t) i + 1] + v;
  546. fp = fp + step;
  547. const auto q = pos >= B ? pos - B : pos;
  548. o[q] = v * gain + o[q];
  549. pos = q + 1;
  550. }
  551. }
  552. float renderFormant (int c, const float (&shifts)[2][4], const float (&gains)[2][4], float x) noexcept
  553. {
  554. const auto* inr = inRing[c].data();
  555. const auto* lpr = lpRing[c].data();
  556. const auto P = period[c];
  557. for (int v = 0; v < 4; ++v)
  558. {
  559. const auto sh = shifts[c][v];
  560. auto f25 = 1.0f;
  561. if (P != 0.0f)
  562. f25 = pow2Cached (spacingCache[c][v], sh * -0.083333336f);
  563. if (--timer[c][v] < 1)
  564. {
  565. if (P == 0.0f || ! (std::abs (sh) > 0.0f))
  566. {
  567. // pass-through grain: 256-sample periodic Hann from 257…2 samples back, every 128 samples
  568. auto st = W2 - 0x101;
  569. if (st < 0)
  570. st += B;
  571. for (int i = 0; i < 256; ++i)
  572. {
  573. grain[(std::size_t) i] = hann256[(std::size_t) i] * inr[st];
  574. if (++st >= B)
  575. st = 0;
  576. }
  577. overlapAdd (c, 256, 0.0f, G[c][v]);
  578. timer[c][v] = 128;
  579. }
  580. else
  581. {
  582. // PSOLA grain: 2 periods centred 70 samples before the pitch mark (largest positive local
  583. // maximum of the filtered signal within 1.1 periods back from half a grain ago)
  584. const auto len = (int) (P + P);
  585. const auto h = len >> 1;
  586. auto k = h + 1, found = -1, j = W2 - k;
  587. auto best = 0.0f;
  588. for (auto count = (int) ((double) P * 1.1); count > 0; --count, ++k, --j)
  589. {
  590. const auto jj = j < 0 ? j + B : j;
  591. const auto s = lpr[jj];
  592. const auto before = lpr[jj == 0 ? B + 1 : jj - 1]; // (B + 1 is never written: 0)
  593. if (best < s && before < s && lpr[jj + 1] < s)
  594. {
  595. best = s;
  596. found = k;
  597. }
  598. }
  599. if (found >= 0)
  600. {
  601. const auto centre = (W2 - h) - found;
  602. auto st = centre - 70;
  603. if (st < 0)
  604. st = B - 70 + centre;
  605. const auto inv = 1.0f / (float) len;
  606. for (int i = 0; i < len; ++i)
  607. {
  608. const auto w = (float) ((1.0 - std::cos ((double) ((float) i * inv) * 6.283185307179586)) * 0.5);
  609. grain[(std::size_t) i] = w * inr[st];
  610. if (++st >= B)
  611. st -= B;
  612. }
  613. overlapAdd (c, len, genderStep, G[c][v]);
  614. }
  615. timer[c][v] = (int) (f25 * P);
  616. }
  617. }
  618. G[c][v] = (gains[c][v] - G[c][v]) * 0.007f + G[c][v];
  619. }
  620. inRing[c][(std::size_t) W2] = x;
  621. auto& slot = outRing[c][(std::size_t) W2];
  622. const auto out = slot;
  623. slot = 0.0f;
  624. return out;
  625. }
  626. double fs = 44100.0, fmin = 110.0;
  627. bool initialised = false, formantOn = false, midi = false, bypassAll = false, anyAllowed = true;
  628. int rangeIndex = 2, N = 0, L = 0, B = 0, capacityN = 0, capacityB = 0;
  629. float speed = 1.0f, fine = 0.0f, A = 55.0f, genderStep = 1.0f, lastGenderParam = -1.0f;
  630. int ref = 0;
  631. std::array<bool, 12> keyBypass {};
  632. std::array<bool, 128> mask {};
  633. std::array<float, 128> held {};
  634. // detector
  635. std::array<float, 2> dc {};
  636. std::array<std::array<double, 16>, 2> lp {};
  637. std::vector<float> hist[2], amdf[2];
  638. int histPos = 0, frameCount = 0;
  639. float freq[2] {}, period[2] {}, cls[2] { -1.0f, -1.0f }, target[2] {}, sm[2] {};
  640. int octave[2] { -1, -1 };
  641. // harmony (per block)
  642. float hGain[2][4] {}, hShift[2][4] {};
  643. bool hMix = false;
  644. // plain shifter
  645. std::vector<float> delay[2];
  646. int W = 0;
  647. float R[2][4] {}, O[2][4] {}, G[2][4] {};
  648. int K[2][4] {};
  649. PowCache rhoCache[2][4] {}, spacingCache[2][4] {};
  650. // formant shifter
  651. std::vector<float> inRing[2], lpRing[2], outRing[2], grain;
  652. int W2 = 0;
  653. int timer[2][4] {};
  654. std::array<float, 256> hann256 {};
  655. };
  656. } // namespace lynx::pitcher
  657. #if defined(__clang__)
  658. #pragma clang fp contract(on)
  659. #elif defined(__GNUC__)
  660. #pragma GCC pop_options
  661. #endif