Quality160Tests.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // --- Round 160: sound quality of the signal path (time-stretch, docs/analise/REA_rodada160.md) --------------------
  2. namespace q160
  3. {
  4. lynx::SampleData sineOf (double freq, double seconds, double rate = 44100.0, bool stereo = false)
  5. {
  6. lynx::SampleData s;
  7. s.sampleRate = rate;
  8. const auto n = (std::size_t) (seconds * rate);
  9. for (std::size_t i = 0; i < n; ++i)
  10. s.left.push_back ((float) (0.5 * std::sin (2.0 * 3.141592653589793 * freq * (double) i / rate)));
  11. if (stereo)
  12. s.right = s.left;
  13. return s;
  14. }
  15. // one 3-sample click every `gap` seconds, from 0.1 s
  16. lynx::SampleData clicksOf (int count, double gap, double rate = 44100.0)
  17. {
  18. lynx::SampleData s;
  19. s.sampleRate = rate;
  20. s.left.assign ((std::size_t) ((0.1 + gap * count + 0.3) * rate), 0.0f);
  21. for (int k = 0; k < count; ++k)
  22. {
  23. const auto i = (std::size_t) std::llround ((0.1 + k * gap) * rate);
  24. s.left[i] = 0.9f;
  25. s.left[i + 1] = -0.5f;
  26. s.left[i + 2] = 0.2f;
  27. }
  28. s.right = s.left;
  29. return s;
  30. }
  31. // signal-to-(noise + distortion) of a steady sine, dB (least-squares fit of the known frequency per 4096 frames)
  32. double sinadDb (const lynx::SampleData& d, double freq)
  33. {
  34. std::vector<double> ratios;
  35. const auto n = (long) d.frames();
  36. for (long at = (long) (0.2 * d.sampleRate); at + 4096 < n - (long) (0.2 * d.sampleRate); at += 4096)
  37. {
  38. double ss = 0, cc = 0, sc = 0, xs = 0, xc = 0, xx = 0;
  39. for (long i = 0; i < 4096; ++i)
  40. {
  41. const auto w = 2.0 * 3.141592653589793 * freq * (double) i / d.sampleRate;
  42. const auto s = std::sin (w), c = std::cos (w), x = (double) d.leftData()[at + i];
  43. ss += s * s; cc += c * c; sc += s * c; xs += x * s; xc += x * c; xx += x * x;
  44. }
  45. const auto det = ss * cc - sc * sc;
  46. const auto a = (xs * cc - xc * sc) / det, b = (xc * ss - xs * sc) / det;
  47. const auto fit = a * xs + b * xc; // energy explained by the sine
  48. ratios.push_back (fit / std::max (1e-30, xx - fit));
  49. }
  50. std::sort (ratios.begin(), ratios.end());
  51. return ratios.empty() ? 0.0 : 10.0 * std::log10 (ratios[ratios.size() / 2]);
  52. }
  53. } // namespace q160
  54. TEST (q160_stretch_near_unity_is_a_clean_resample)
  55. {
  56. // FL's recorded takes carry a stretch time equal to their own length: ratio 0.99998 (LEMITGO). The old WSOLA
  57. // rebuilt them from overlapping windows anyway (null vs the ideal −1 dB); now they are resampled.
  58. const auto in = q160::sineOf (1000.0, 2.0, 44100.0, true);
  59. const auto ratio = 0.99998;
  60. const auto out = lynx::timeStretch (in, ratio, lynx::StretchMethod::general);
  61. const auto m = (long) std::llround ((double) in.frames() * ratio);
  62. CHECK ((long) out->frames() == m && out->isStereo());
  63. const auto step = (double) in.frames() / (double) m;
  64. double err = 0.0, ref = 0.0;
  65. for (long j = 2000; j < m - 2000; ++j)
  66. {
  67. const auto ideal = 0.5 * std::sin (2.0 * 3.141592653589793 * 1000.0 * (double) j * step / 44100.0);
  68. err += (out->left[(std::size_t) j] - ideal) * (out->left[(std::size_t) j] - ideal);
  69. ref += ideal * ideal;
  70. }
  71. CHECK (10.0 * std::log10 (err / ref) < -80.0);
  72. // exactly 1 = a copy
  73. const auto same = lynx::timeStretch (in, 1.0);
  74. CHECK (same->frames() == in.frames() && same->left[12345] == in.left[12345]);
  75. }
  76. TEST (q160_stretch_keeps_clicks_sharp_and_on_time)
  77. {
  78. // a click train stretched by 1.5: every click lands where the time map puts it, without pre-echo or smear
  79. // (old WSOLA: lost/moved clicks; plain spectral stretch: −27 dB pre-echo)
  80. const auto in = q160::clicksOf (10, 0.25);
  81. for (auto method : { lynx::StretchMethod::general, lynx::StretchMethod::transients })
  82. for (double ratio : { 0.8, 1.5, 2.0 })
  83. {
  84. const auto out = lynx::timeStretch (in, ratio, method);
  85. CHECK (std::abs ((double) out->frames() - (double) in.frames() * ratio) < 1.5);
  86. int onTime = 0;
  87. double worstPre = -200.0;
  88. for (int k = 0; k < 10; ++k)
  89. {
  90. const auto expect = (long) std::llround ((0.1 + k * 0.25) * 44100.0 * ratio);
  91. long at = expect;
  92. float peak = 0.0f;
  93. for (long i = expect - 400; i < expect + 400; ++i)
  94. if (std::abs (out->left[(std::size_t) i]) > peak)
  95. {
  96. peak = std::abs (out->left[(std::size_t) i]);
  97. at = i;
  98. }
  99. onTime += std::abs (at - expect) <= 2 && peak > 0.7f;
  100. double pre = 0.0, core = 0.0;
  101. for (long i = expect - 1323; i < expect - 66; ++i)
  102. pre += (double) out->left[(std::size_t) i] * out->left[(std::size_t) i];
  103. for (long i = expect - 66; i < expect + 66; ++i)
  104. core += (double) out->left[(std::size_t) i] * out->left[(std::size_t) i];
  105. worstPre = std::max (worstPre, 10.0 * std::log10 ((pre + 1e-20) / core));
  106. }
  107. CHECK (onTime == 10);
  108. CHECK (worstPre < -35.0); // −37…−51 dB with the 15 ms pre-roll (−29…−34 dB with 4 ms)
  109. }
  110. }
  111. TEST (q160_stretch_tonal_purity)
  112. {
  113. // a steady 1 kHz tone stretched by 1.25 / 0.8 stays clean (old WSOLA ≈ 148 dB on a pure sine, but the plain
  114. // Signalsmith preset gave 57 dB; LYNX's 120 ms / 20 ms configuration ≈ 100 dB)
  115. const auto in = q160::sineOf (1000.0, 3.0);
  116. for (auto method : { lynx::StretchMethod::general, lynx::StretchMethod::mono })
  117. for (double ratio : { 0.8, 1.25 })
  118. {
  119. const auto out = lynx::timeStretch (in, ratio, method);
  120. CHECK (q160::sinadDb (*out, 1000.0) > 75.0);
  121. }
  122. const auto drums = lynx::timeStretch (in, 1.25, lynx::StretchMethod::transients);
  123. CHECK (q160::sinadDb (*drums, 1000.0) > 45.0);
  124. }
  125. TEST (q160_stretch_short_sounds_and_mapping)
  126. {
  127. // a 40 ms one-shot (shorter than the analysis block) still stretches to the right length and is not silent
  128. auto hit = q160::sineOf (200.0, 0.04);
  129. for (std::size_t i = 0; i < hit.left.size(); ++i)
  130. hit.left[i] *= (float) std::exp (-(double) i / 300.0);
  131. const auto out = lynx::timeStretch (hit, 1.5);
  132. CHECK (std::abs ((double) out->frames() - (double) hit.frames() * 1.5) < 1.5);
  133. float peak = 0.0f;
  134. for (auto v : out->left)
  135. peak = std::max (peak, std::abs (v));
  136. CHECK (peak > 0.25f);
  137. // FL's stretch modes (REA_rodada3 §5.2) → LYNX's methods
  138. CHECK (lynx::stretchMethodForFlMode (-2) == lynx::StretchMethod::general); // Stretch Pro
  139. CHECK (lynx::stretchMethodForFlMode (5) == lynx::StretchMethod::general); // Auto
  140. CHECK (lynx::stretchMethodForFlMode (2) == lynx::StretchMethod::mono); // E3 mono
  141. CHECK (lynx::stretchMethodForFlMode (9) == lynx::StretchMethod::mono); // E2 speech
  142. CHECK (lynx::stretchMethodForFlMode (7) == lynx::StretchMethod::transients); // E2 transient
  143. CHECK (lynx::stretchMethodForFlMode (3) == lynx::StretchMethod::transients); // slice stretch
  144. // attack detection: one attack per click, on the click's sample
  145. const auto clicks = q160::clicksOf (4, 0.3);
  146. const auto attacks = lynx::stretchdetail::detectAttacks (clicks.left.data(), (long) clicks.frames(), 44100.0, 9.0);
  147. CHECK (attacks.size() == 4);
  148. CHECK (attacks.size() == 4 && attacks[0] == 4410 && attacks[3] == (long) std::llround (1.0 * 44100.0));
  149. // a steady tone has none
  150. const auto tone = q160::sineOf (440.0, 1.0);
  151. CHECK (lynx::stretchdetail::detectAttacks (tone.left.data(), (long) tone.frames(), 44100.0, 9.0).size() <= 1);
  152. }
  153. TEST (q160_master_protection_distortion_before_and_after)
  154. {
  155. // a mastered mix peaking at −0.3 dBFS: the old soft clip bent every peak (measured THD), the new one is bit-exact
  156. const auto amp = std::pow (10.0, -0.3 / 20.0);
  157. std::vector<float> a (44100), b;
  158. for (std::size_t i = 0; i < a.size(); ++i)
  159. a[i] = (float) (amp * std::sin (2.0 * 3.141592653589793 * 1000.0 * (double) i / 44100.0));
  160. b = a;
  161. auto legacy = a;
  162. lynx::legacySoftClip (legacy.data(), (int) legacy.size());
  163. lynx::clipOvers (b.data(), (int) b.size());
  164. CHECK (b == a);
  165. double diff = 0.0, ref = 0.0;
  166. for (std::size_t i = 0; i < a.size(); ++i)
  167. {
  168. diff += (legacy[i] - a[i]) * (legacy[i] - a[i]);
  169. ref += (double) a[i] * a[i];
  170. }
  171. CHECK (10.0 * std::log10 (diff / ref) > -60.0); // the old curve changed the signal audibly (≈ −48 dB, REA_rodada160)
  172. }
  173. TEST (q160_dither_survives_the_writer_conversion)
  174. {
  175. // JUCE: int32 = round((2^31 − 1)·x), then the low 16 / 8 bits dropped. Every grid value must come out as itself,
  176. // for both signs and up to full scale (before round 160 positive values above −6 dBFS came out 1 LSB low).
  177. for (int bits : { 16, 24 })
  178. {
  179. lynx::Dither d (bits, false);
  180. const auto scale = (double) (1 << (bits - 1));
  181. const int shift = 32 - bits;
  182. int wrong = 0;
  183. std::vector<float> x (20000);
  184. for (std::size_t i = 0; i < x.size(); ++i)
  185. x[i] = (float) (0.999 * std::sin ((double) i * 0.0123));
  186. auto y = x;
  187. d.process (y.data(), (int) y.size(), 0);
  188. for (std::size_t i = 0; i < y.size(); ++i)
  189. {
  190. const auto k = (long long) std::llround (((double) y[i] - (double) d.writerBias()) * scale); // the grid value
  191. const double samp = y[i];
  192. const auto i32 = samp >= 1.0 ? 2147483647LL : (long long) std::floor (2147483647.0 * samp + 0.5);
  193. const auto written = i32 >> shift; // arithmetic shift, like JUCE's Int16/Int24 converters
  194. wrong += written != k;
  195. }
  196. CHECK (wrong == 0);
  197. }
  198. }
  199. TEST (q160_event_sort_is_stable_and_allocation_free)
  200. {
  201. lynx::EventBuffer b;
  202. // offsets out of order, an "off" after an "on" on the same sample, equal events keep their order (keys)
  203. const int offsets[] = { 30, 10, 10, 20, 10, 0 };
  204. for (int i = 0; i < 6; ++i)
  205. {
  206. lynx::NoteEvent e;
  207. e.sampleOffset = offsets[i];
  208. e.type = i == 4 ? lynx::NoteEvent::Type::off : lynx::NoteEvent::Type::on;
  209. e.key = i;
  210. b.add (e);
  211. }
  212. b.sortByOffset();
  213. CHECK (b.size() == 6);
  214. CHECK (b[0].sampleOffset == 0 && b[1].key == 4 /* off first */ && b[2].key == 1 && b[3].key == 2);
  215. CHECK (b[4].sampleOffset == 20 && b[5].sampleOffset == 30);
  216. }
  217. TEST (q160_sampler_uses_render_quality_when_export_starts_after_prepare)
  218. {
  219. // the export engine prepares its instruments first and switches to offline rendering afterwards: the note must
  220. // still get FL's render interpolation (32-point sinc), i.e. sound like an instrument prepared during the export
  221. auto data = std::make_shared<lynx::SampleData>();
  222. data->sampleRate = 44100.0;
  223. for (int i = 0; i < 20000; ++i)
  224. data->left.push_back ((float) std::sin ((double) i * 0.9) * 0.5f); // bright: the kernels differ
  225. auto render = [&] (bool offlineBeforePrepare) {
  226. lynx::setOfflineRendering (offlineBeforePrepare);
  227. auto inst = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  228. inst->prepare (48000.0);
  229. lynx::ChannelDef c;
  230. inst->configure (c, 120.0);
  231. lynx::setOfflineRendering (true);
  232. inst->noteOn (63, 1.0f);
  233. std::vector<float> l (4000), r (4000);
  234. inst->render (l.data(), r.data(), 4000, 1.0f, 1.0f);
  235. lynx::setOfflineRendering (false);
  236. return l;
  237. };
  238. CHECK (render (false) == render (true));
  239. }
  240. TEST (q160_asio_suggestion)
  241. {
  242. using lynx::prefs::suggestedAsioDriver;
  243. const std::vector<std::string> laptop { "ASIO4ALL v2", "FL Studio ASIO", "SSL ASIO Driver 1" };
  244. CHECK (suggestedAsioDriver ("Windows Audio", laptop) == "SSL ASIO Driver 1");
  245. CHECK (suggestedAsioDriver ("DirectSound", laptop) == "SSL ASIO Driver 1");
  246. CHECK (suggestedAsioDriver ("ASIO", laptop).empty());
  247. CHECK (suggestedAsioDriver ("Windows Audio (Exclusive Mode)", laptop).empty());
  248. CHECK (suggestedAsioDriver ("Windows Audio", { "ASIO4ALL v2", "LYNX ASIO" }) == "LYNX ASIO");
  249. CHECK (suggestedAsioDriver ("Windows Audio", { "ASIO4ALL v2" }) == "ASIO4ALL v2");
  250. CHECK (suggestedAsioDriver ("Windows Audio", {}).empty());
  251. }
  252. TEST (q160_preview_interpolation_44k1_file_on_48k_device)
  253. {
  254. // the Browser / editor preview reads with FL's 24-point sinc now (it was linear: a 10 kHz tone of a 44.1 kHz file on
  255. // a 48 kHz device came out 1.5 dB low with an image at 13.9 kHz only 21 dB down, REA_rodada160 §4)
  256. const double fi = 44100.0, fo = 48000.0, f = 10000.0;
  257. std::vector<float> x ((std::size_t) fi * 2);
  258. for (std::size_t i = 0; i < x.size(); ++i)
  259. x[i] = (float) (0.5 * std::sin (2.0 * 3.141592653589793 * f * (double) i / fi));
  260. lynx::flrs::Kernel k;
  261. k.setQuality (lynx::flrs::liveQuality);
  262. k.setSpeed (fi / fo);
  263. const auto half = (std::int64_t) k.halfWidth;
  264. lynx::SampleData out;
  265. out.sampleRate = fo;
  266. for (long j = 0; j < (long) fo; ++j)
  267. {
  268. const auto pos = (double) j * fi / fo + 100.0;
  269. const auto whole = lynx::flrs::wholeFrames (pos);
  270. float yl = 0.0f, yr = 0.0f;
  271. lynx::flrs::interpolate (k, x.data() + (whole - half + 1), x.data() + (whole - half + 1), pos - (double) whole, yl, yr);
  272. out.left.push_back (yl);
  273. }
  274. const auto sinad = q160::sinadDb (out, f);
  275. CHECK (sinad > 60.0);
  276. // the linear read it replaces, for the record
  277. lynx::SampleData lin;
  278. lin.sampleRate = fo;
  279. for (long j = 0; j < (long) fo; ++j)
  280. {
  281. const auto pos = (double) j * fi / fo + 100.0;
  282. const auto i = (std::size_t) pos;
  283. const auto fr = (float) (pos - (double) i);
  284. lin.left.push_back (x[i] + fr * (x[i + 1] - x[i]));
  285. }
  286. CHECK (q160::sinadDb (lin, f) < 30.0);
  287. std::printf (" [q160] preview 10 kHz 44.1->48 kHz SINAD: sinc %.1f dB, linear %.1f dB\n", sinad, q160::sinadDb (lin, f));
  288. }
  289. namespace q160
  290. {
  291. struct AllocWindow
  292. {
  293. long start = rt95::allocs.load();
  294. AllocWindow() { rt95::countAllocs = true; }
  295. ~AllocWindow() { rt95::countAllocs = false; }
  296. long count() const { return rt95::allocs.load() - start; }
  297. };
  298. } // namespace q160
  299. TEST (q160_audio_thread_allocations_and_cost_of_builtin_units)
  300. {
  301. // Real-time audit (REA_rodada160 §6): every built-in effect and instrument, steady state at 48 kHz / 512 frames —
  302. // heap allocations inside process()/render() (each is a potential dropout) and the CPU cost per block.
  303. using namespace lynx;
  304. constexpr int block = 512;
  305. constexpr double rate = 48000.0;
  306. std::mt19937 rng (160);
  307. std::vector<float> l (block), r (block);
  308. auto noise = [&] {
  309. for (int i = 0; i < block; ++i)
  310. {
  311. l[(std::size_t) i] = 0.3f * std::uniform_real_distribution<float> (-1.0f, 1.0f) (rng);
  312. r[(std::size_t) i] = l[(std::size_t) i];
  313. }
  314. };
  315. struct Row
  316. {
  317. std::string name;
  318. long allocs;
  319. double usPerBlock;
  320. };
  321. std::vector<Row> rows;
  322. for (int k = 0; k <= (int) lastEffectKind; ++k)
  323. {
  324. const auto kind = (EffectKind) k;
  325. if (kind == EffectKind::plugin)
  326. continue;
  327. const auto slot = makeEffectSlot (kind);
  328. auto fx = makeEffect (slot);
  329. if (fx == nullptr)
  330. continue;
  331. fx->prepare (rate);
  332. for (int b = 0; b < 8; ++b)
  333. {
  334. noise();
  335. fx->process (l.data(), r.data(), block, slot.params, 128.0);
  336. }
  337. long allocs = 0;
  338. const auto t0 = std::chrono::steady_clock::now();
  339. {
  340. const q160::AllocWindow w;
  341. for (int b = 0; b < 100; ++b)
  342. {
  343. noise();
  344. fx->process (l.data(), r.data(), block, slot.params, 128.0);
  345. }
  346. allocs = w.count();
  347. }
  348. const auto us = std::chrono::duration<double, std::micro> (std::chrono::steady_clock::now() - t0).count() / 100.0;
  349. rows.push_back ({ std::string ("fx ") + effectInfo (kind).name, allocs, us });
  350. }
  351. for (int k = 0; k <= (int) InstrumentKind::patcher; ++k)
  352. {
  353. const auto kind = (InstrumentKind) k;
  354. if (kind == InstrumentKind::plugin || kind == InstrumentKind::slayer || kind == InstrumentKind::midiOut || kind == InstrumentKind::audioClip)
  355. continue;
  356. auto data = std::make_shared<SampleData>();
  357. data->sampleRate = 44100.0;
  358. for (int i = 0; i < 88200; ++i)
  359. data->left.push_back (0.3f * (float) std::sin ((double) i * 0.05));
  360. auto inst = makeInstrument (kind, data);
  361. if (inst == nullptr)
  362. continue;
  363. inst->prepare (rate);
  364. ChannelDef c;
  365. c.instrument = kind;
  366. inst->configure (c, 120.0);
  367. std::fill (l.begin(), l.end(), 0.0f);
  368. std::fill (r.begin(), r.end(), 0.0f);
  369. inst->render (l.data(), r.data(), block, 1.0f, 1.0f);
  370. long allocs = 0;
  371. const auto t0 = std::chrono::steady_clock::now();
  372. {
  373. const q160::AllocWindow w;
  374. for (int b = 0; b < 100; ++b)
  375. {
  376. if (b % 10 == 0)
  377. {
  378. inst->noteOn (48 + (b / 10) % 12, 0.8f);
  379. inst->noteOn (55 + (b / 10) % 12, 0.8f);
  380. }
  381. if (b % 10 == 7)
  382. {
  383. inst->noteOff (48 + (b / 10) % 12);
  384. inst->noteOff (55 + (b / 10) % 12);
  385. }
  386. std::fill (l.begin(), l.end(), 0.0f);
  387. std::fill (r.begin(), r.end(), 0.0f);
  388. inst->render (l.data(), r.data(), block, 1.0f, 1.0f);
  389. }
  390. allocs = w.count();
  391. }
  392. const auto us = std::chrono::duration<double, std::micro> (std::chrono::steady_clock::now() - t0).count() / 100.0;
  393. rows.push_back ({ "instrument " + std::to_string (k), allocs, us });
  394. }
  395. int allocating = 0;
  396. for (const auto& row : rows)
  397. if (row.allocs > 0)
  398. {
  399. ++allocating;
  400. std::printf (" [q160 rt] ALLOCATES %-28s %ld allocations in 100 blocks\n", row.name.c_str(), row.allocs);
  401. }
  402. std::sort (rows.begin(), rows.end(), [] (const Row& a, const Row& b) { return a.usPerBlock > b.usPerBlock; });
  403. for (std::size_t i = 0; i < std::min<std::size_t> (15, rows.size()); ++i)
  404. std::printf (" [q160 rt] cost %-28s %8.1f us / 512 frames (%.1f %% of the 10.7 ms budget)\n", rows[i].name.c_str(), rows[i].usPerBlock,
  405. rows[i].usPerBlock / 106.67);
  406. std::printf (" [q160 rt] %d of %d units allocate on the audio thread\n", allocating, (int) rows.size());
  407. CHECK (rows.size() > 100);
  408. CHECK (allocating == 0); // round 160: 0 of 133 built-in units
  409. }