Sampler190dhTests.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Round 190dh: FL's Sampler page (Channel settings › Sampler) — the laws its controls apply (core/SamplerPage.h), the
  2. // loop points of the Playback START / END knobs in the precompute, FL's loop record 0xFB and Content flags 0x8F in
  3. // .flp import / export. FL facts in /mnt/e/lynx-work/sampler190dh/FL-FACTS.md.
  4. #include "core/SamplerPage.h"
  5. namespace sampler190dh
  6. {
  7. inline std::shared_ptr<lynx::SampleData> tone (int frames)
  8. {
  9. auto d = std::make_shared<lynx::SampleData>();
  10. d->sampleRate = 44100.0;
  11. for (int i = 0; i < frames; ++i)
  12. d->left.push_back (0.2f + 0.6f * std::sin (0.021f * (float) i) * (1.0f - (float) i / (float) frames));
  13. return d;
  14. }
  15. inline flp::Project oneSampler (const std::function<void (FlpBuilder&)>& events)
  16. {
  17. FlpBuilder b;
  18. b.u8 (0x40);
  19. b.u16 (0);
  20. b.u8 (0x15);
  21. b.u8 (0);
  22. events (b);
  23. return flp::parseProject (b.bytes);
  24. }
  25. // FL's 0xFB loop record, version 2, as FL writes it (FL's own files: LeriQ - SHINE.flp, Remix a song.flp)
  26. inline std::vector<std::uint8_t> loopRecord (std::int64_t a, std::int64_t b, bool useLoops)
  27. {
  28. std::vector<std::uint8_t> r;
  29. auto put = [&r] (const auto v) {
  30. const auto* p = reinterpret_cast<const std::uint8_t*> (&v);
  31. r.insert (r.end(), p, p + sizeof (v));
  32. };
  33. put ((std::int32_t) 2);
  34. put (a);
  35. put (b);
  36. put (-1.0);
  37. put ((std::int32_t) -1);
  38. put ((std::uint8_t) 0);
  39. put (std::numeric_limits<std::int32_t>::max());
  40. put (std::numeric_limits<std::int32_t>::max());
  41. put ((std::uint8_t) (useLoops ? 1 : 0));
  42. put ((std::uint8_t) 0);
  43. return r;
  44. }
  45. } // namespace sampler190dh
  46. // Ping pong loop (FL's PingPongBox, sample +0x14c): the loop's inside frames follow its end, reversed; it works on the
  47. // loop points of the Playback knobs as on the file's own loop, and only with "Use loop points"
  48. TEST (sampler190dh_ping_pong_loop)
  49. {
  50. namespace pc = lynx::precompute;
  51. const auto in = sampler190dh::tone (1200);
  52. lynx::SamplerSettings s;
  53. // no loop in the file: the knobs' loop 300…500 (inclusive)
  54. s.loopStart = 300, s.loopEnd = 500;
  55. CHECK (pc::needed (s));
  56. auto out = pc::apply (*in, s);
  57. CHECK (out != nullptr && out->loopStart == 300 && out->loopEnd == 501 && (int) out->frames() == 1200);
  58. s.precompute.pingPong = true;
  59. out = pc::apply (*in, s);
  60. CHECK (out != nullptr && (int) out->frames() == 1200 + 199); // frames 499…301 appended after 500
  61. CHECK (out->left[501] == in->left[499] && out->left[699] == in->left[301] && out->left[700] == in->left[501]);
  62. CHECK (out->loopStart == 300 && out->loopEnd == 500 + 199 + 1);
  63. // "Use loop points" off: no loop, so nothing to bounce
  64. s.useLoopPoints = false;
  65. out = pc::apply (*in, s);
  66. CHECK (out != nullptr && (int) out->frames() == 1200 && out->loopStart < 0);
  67. // the knobs' loop replaces the file's
  68. auto looped = sampler190dh::tone (1200);
  69. looped->loopStart = 100, looped->loopEnd = 201;
  70. s = {};
  71. s.precompute.pingPong = true;
  72. out = pc::apply (*looped, s);
  73. CHECK (out != nullptr && out->loopStart == 100 && out->loopEnd == 200 + 99 + 1);
  74. s.loopStart = 600, s.loopEnd = 700;
  75. out = pc::apply (*looped, s);
  76. CHECK (out != nullptr && out->loopStart == 600 && out->loopEnd == 700 + 99 + 1 && out->left[701] == looped->left[699]);
  77. // a sampler with no loop override and no effect is not precomputed (old projects play as before)
  78. CHECK (! pc::needed (lynx::SamplerSettings {}));
  79. }
  80. // FL's loop knobs (FUN_01007150 / FUN_010074b0): START clamps to the sample and drags END after it; END never goes
  81. // before START; Shift moves the whole loop
  82. TEST (sampler190dh_loop_knobs)
  83. {
  84. using namespace lynx::samplerpage;
  85. lynx::SamplerSettings s;
  86. CHECK ((shownLoop (s, -1, -1) == std::pair<std::int64_t, std::int64_t> (-1, -1)));
  87. CHECK ((shownLoop (s, 100, 201) == std::pair<std::int64_t, std::int64_t> (100, 200))); // (the file's end is exclusive)
  88. setLoopStart (s, 300, 1000, -1, -1);
  89. CHECK (s.loopStart == 300 && s.loopEnd == 301);
  90. setLoopEnd (s, 800, 1000, -1, -1);
  91. CHECK (s.loopStart == 300 && s.loopEnd == 800);
  92. setLoopEnd (s, 100, 1000, -1, -1);
  93. CHECK (s.loopEnd == 300);
  94. setLoopEnd (s, 800, 1000, -1, -1);
  95. setLoopStart (s, 900, 1000, -1, -1);
  96. CHECK (s.loopStart == 900 && s.loopEnd == 901);
  97. setLoopStart (s, 5000, 1000, -1, -1);
  98. CHECK (s.loopStart == 999 && s.loopEnd == 999);
  99. // Shift: the loop moves, its length kept, inside the sample
  100. s.loopStart = 200, s.loopEnd = 400;
  101. setLoopStart (s, 700, 1000, -1, -1, true);
  102. CHECK (s.loopStart == 700 && s.loopEnd == 900);
  103. setLoopEnd (s, 1500, 1000, -1, -1, true);
  104. CHECK (s.loopStart == 799 && s.loopEnd == 999);
  105. // from the file's loop: the first turn copies it, then edits it
  106. lynx::SamplerSettings f;
  107. setLoopEnd (f, 500, 1000, 100, 201);
  108. CHECK (f.loopStart == 100 && f.loopEnd == 500);
  109. }
  110. // Declicking mode (FL's DeclickModeSelect, .flp 0xD7 byte 0x53): FL's six items in FL's order, each to its cut release
  111. TEST (sampler190dh_declicking_modes)
  112. {
  113. using namespace lynx::samplerpage;
  114. const std::vector<std::string> fl = { "Out only (no bleeding)", "Transient (no bleeding)", "Transient (bleeding)",
  115. "Generic (bleeding)", "Smooth (bleeding)", "Crossfade (bleeding)" };
  116. CHECK (std::size (declickModeNames) == fl.size());
  117. for (std::size_t i = 0; i < fl.size(); ++i)
  118. CHECK (fl[i] == declickModeNames[i]);
  119. // each mode's cut release, measured on the sampler voice (a raised cosine; mode 1 a follower, mode 5 curved)
  120. auto data = std::make_shared<lynx::SampleData>();
  121. data->sampleRate = 44100.0;
  122. data->left.assign (44100, 0.5f);
  123. for (int mode = 0; mode < 6; ++mode)
  124. {
  125. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  126. sampler->prepare (44100.0);
  127. lynx::ChannelDef def;
  128. def.instrument = lynx::InstrumentKind::sampler;
  129. def.sampler.declickMode = mode;
  130. sampler->configure (def, 120.0);
  131. sampler->noteOn (60, 1.0f);
  132. std::vector<float> l (12000), r (12000);
  133. sampler->render (l.data(), r.data(), 1000, 1.0f, 1.0f);
  134. sampler->cut();
  135. std::fill (l.begin(), l.end(), 0.0f);
  136. sampler->render (l.data(), r.data(), 12000, 1.0f, 1.0f);
  137. int frames = 0; // (one past the last sounding frame)
  138. for (int i = 0; i < 12000; ++i)
  139. if (std::fabs (l[(std::size_t) i]) > 1.0e-6f)
  140. frames = i + 1;
  141. const auto expected = (int) std::lround (declickReleaseMs[mode] * 44.1);
  142. // (mode 1 subtracts a follower: the voice sinks below 1e-6 before its 10 ms end, ~383 frames; mode 5's curve
  143. // reaches it a few frames early)
  144. if (mode == 1)
  145. CHECK (frames > expected / 2 && frames <= expected + 3);
  146. else
  147. CHECK (std::abs (frames - expected) <= 6);
  148. }
  149. // the .flp byte goes to the page's mode and back
  150. const auto p = sampler190dh::oneSampler ([] (FlpBuilder& b) {
  151. std::vector<std::uint8_t> d7 (0xA8, 0);
  152. d7[0x53] = 4;
  153. b.data (0xD7, d7);
  154. });
  155. const auto song = lynx::importFlp (p);
  156. CHECK (! song.channels.empty() && song.channels[0].sampler.declickMode == 4);
  157. }
  158. // FL's precompute order (FUN_00ff0db0, REA round 167h §2): crop, ping pong, reverse, fades, fade stereo, remove DC,
  159. // polarity, normalize, crossfade, swap stereo, trim — checked where the order changes the result
  160. TEST (sampler190dh_precomputed_effects_order)
  161. {
  162. namespace pc = lynx::precompute;
  163. const auto n = 2000;
  164. auto in = std::make_shared<lynx::SampleData>();
  165. in->sampleRate = 44100.0;
  166. for (int i = 0; i < n; ++i)
  167. {
  168. in->left.push_back (0.3f + 0.4f * std::sin (0.05f * (float) i));
  169. in->right.push_back (0.1f * std::cos (0.03f * (float) i));
  170. }
  171. lynx::SamplerSettings s;
  172. // normalize comes after the fade out: the faded peak (not the file's) is brought to 1
  173. s.precompute.fadeOut = 512;
  174. s.precompute.normalize = true;
  175. auto out = pc::apply (*in, s);
  176. float peak = 0.0f;
  177. for (int i = 0; i < n; ++i)
  178. peak = std::max ({ peak, std::fabs (out->left[(std::size_t) i]), std::fabs (out->right[(std::size_t) i]) });
  179. CHECK (std::abs (peak - 1.0f) < 1e-6f);
  180. // remove DC comes after the fade in: the faded signal's mean is 0 (not the file's then faded)
  181. s = {};
  182. s.precompute.fadeIn = 256;
  183. s.precompute.removeDc = true;
  184. out = pc::apply (*in, s);
  185. double mean = 0.0;
  186. for (int i = 0; i < n; ++i)
  187. mean += out->left[(std::size_t) i];
  188. CHECK (std::abs (mean / n) < 1e-5);
  189. // swap stereo comes after fade stereo: the left output carries the right channel's fade (cos((N−1−i)·π/2/(N−1)))
  190. s = {};
  191. s.precompute.fadeStereo = true;
  192. s.precompute.swapStereo = true;
  193. out = pc::apply (*in, s);
  194. const auto a = 1.5707963267948966 / (double) (n - 1);
  195. CHECK (std::abs (out->left[300] - (float) (in->right[300] * std::cos ((double) (n - 1 - 300) * a))) < 1e-6f);
  196. // the fades run in playback order on a reversed sample (reverse before the fades, back at the end)
  197. s = {};
  198. s.reverse = true;
  199. s.precompute.fadeOut = 1024;
  200. out = pc::apply (*in, s);
  201. CHECK (out->left[100] == 0.0f && out->left[n - 1] != 0.0f);
  202. // trim comes last: it cuts the silence the fade out made (fade out 1024 silences all after the first quarter)
  203. s = {};
  204. s.precompute.fadeOut = 1024;
  205. s.precompute.trim = 64;
  206. out = pc::apply (*in, s);
  207. CHECK ((int) out->frames() < n / 4 + 2);
  208. // ping pong comes before the crossfade: the crossfade blends the bounced loop's end into its start
  209. auto looped = std::make_shared<lynx::SampleData> (*in);
  210. looped->loopStart = 400, looped->loopEnd = 801;
  211. s = {};
  212. s.precompute.pingPong = true;
  213. s.precompute.crossfade = 1024;
  214. out = pc::apply (*looped, s);
  215. const auto bounced = 800 + 399; // the loop's end after the bounce
  216. const auto c = (std::int64_t) ((bounced - 400 + 1) * 1024 / 2048);
  217. CHECK (out->loopStart == 400 + c && out->loopEnd == bounced + 1);
  218. // CROSSFADE above 0 switches "Use loop points" on (FL's FXAttackWheelChange)
  219. lynx::SamplerSettings x;
  220. x.useLoopPoints = false;
  221. lynx::samplerpage::turnCrossfade (x, 0);
  222. CHECK (! x.useLoopPoints);
  223. lynx::samplerpage::turnCrossfade (x, 300);
  224. CHECK (x.useLoopPoints && x.precompute.crossfade == 300);
  225. }
  226. // Time stretching: TIME × 2^(MUL/10000) is the length; PITCH is FL's stretch pitch, and in Resample mode turning it
  227. // switches the mode to Auto; the Mode list keeps Resample and LYNX's engines
  228. TEST (sampler190dh_time_stretching_values)
  229. {
  230. using namespace lynx::samplerpage;
  231. lynx::SamplerSettings s;
  232. CHECK (stretchTime (s) == 0.0f);
  233. setStretchTime (s, 4.0f);
  234. CHECK (s.stretchBeats == 4.0f && stretchTime (s) == 4.0f);
  235. setStretchMul (s, 10000, 0.0); // ×2
  236. CHECK (std::abs (s.stretchBeats - 8.0f) < 1e-5f && std::abs (stretchTime (s) - 4.0f) < 1e-5f && s.stretchMul == 10000);
  237. setStretchMul (s, -20000, 0.0); // ×0.25
  238. CHECK (std::abs (s.stretchBeats - 1.0f) < 1e-5f);
  239. setStretchTime (s, 8.0f);
  240. CHECK (std::abs (s.stretchBeats - 2.0f) < 1e-5f);
  241. setStretchMul (s, 99999, 0.0);
  242. CHECK (s.stretchMul == 20000);
  243. // MUL without a TIME: the sample's own length (at the project tempo) times the factor
  244. lynx::SamplerSettings m;
  245. setStretchMul (m, 10000, 3.0);
  246. CHECK (std::abs (m.stretchBeats - 6.0f) < 1e-5f);
  247. setStretchTime (m, 0.0f);
  248. CHECK (m.stretchBeats == 0.0f);
  249. // PITCH: FL's stretch pitch inside the channel's pitch
  250. lynx::SamplerSettings p;
  251. p.pitch = 2.0f; // (the header's PITCH)
  252. turnStretchPitch (p, 300);
  253. CHECK (p.flStretchPitch == 300 && std::abs (p.pitch - 5.0f) < 1e-5f);
  254. CHECK (p.keepPitch && p.flStretchMode == 5 && p.stretchMethod == lynx::StretchMethod::general); // (Resample → Auto)
  255. turnStretchPitch (p, -1500);
  256. CHECK (p.flStretchPitch == -1200 && std::abs (p.pitch - (-10.0f)) < 1e-4f);
  257. lynx::SamplerSettings z;
  258. turnStretchPitch (z, 0); // (back at 0 in Resample: the mode stays)
  259. CHECK (! z.keepPitch);
  260. z.keepPitch = true;
  261. z.stretchMethod = lynx::StretchMethod::mono;
  262. turnStretchPitch (z, 100);
  263. CHECK (z.stretchMethod == lynx::StretchMethod::mono && z.flStretchMode == 0); // (a stretch mode is kept)
  264. // the Mode list
  265. CHECK (std::string (modeItems[modeIndex (lynx::SamplerSettings {})].name) == "Resample");
  266. lynx::SamplerSettings k;
  267. for (int i = 0; i < modeItemCount; ++i)
  268. {
  269. selectMode (k, i);
  270. if (! modeItems[i].header)
  271. CHECK (modeIndex (k) == i);
  272. }
  273. selectMode (k, 1);
  274. CHECK (! k.keepPitch);
  275. // the .flp's TIME and MUL come back as the page shows them, and go out the same way
  276. const auto proj = sampler190dh::oneSampler ([] (FlpBuilder& b) {
  277. std::vector<std::uint8_t> d7 (0xA8, 0);
  278. const std::int32_t pitch = 0, mul = 5000, mode = -1;
  279. const std::uint32_t time = 768; // 4 beats
  280. std::memcpy (d7.data() + 0x60, &time, 4);
  281. std::memcpy (d7.data() + 0x64, &pitch, 4);
  282. std::memcpy (d7.data() + 0x68, &mul, 4);
  283. std::memcpy (d7.data() + 0x6C, &mode, 4);
  284. b.data (0xD7, d7);
  285. b.utf16 (0xC4, u"C:\\samples\\loop.wav");
  286. });
  287. const auto song = lynx::importFlp (proj);
  288. CHECK (! song.channels.empty());
  289. if (! song.channels.empty())
  290. {
  291. const auto& t = song.channels[0].sampler;
  292. CHECK (t.stretchMul == 5000 && std::abs (stretchTime (t) - 4.0f) < 1e-4f && std::abs (t.stretchBeats - 4.0f * (float) std::sqrt (2.0)) < 1e-4f);
  293. }
  294. }
  295. // Content (FL's 0x8F bits) and the loop record 0xFB: imported, saved back by the export
  296. TEST (sampler190dh_content_flags_and_loop_record)
  297. {
  298. using namespace lynx::samplerpage;
  299. const auto p = sampler190dh::oneSampler ([] (FlpBuilder& b) {
  300. b.data (0xFB, sampler190dh::loopRecord (1200, 3400, true));
  301. b.u8 (0x8F);
  302. b.u32 (0x100 | 8 | 4 | 1); // keep on disk, use loop points, slice markers, resample (no load regions)
  303. b.utf16 (0xC4, u"C:\\samples\\loop.wav"); // (a sampler without a sample imports as LYNX's synth)
  304. });
  305. CHECK (p.channels.size() == 1);
  306. if (p.channels.empty())
  307. return;
  308. CHECK (p.channels[0].loopStart == 1200 && p.channels[0].loopEnd == 3400);
  309. auto song = lynx::importFlp (p);
  310. CHECK (! song.channels.empty());
  311. if (song.channels.empty())
  312. return;
  313. const auto& s = song.channels[0].sampler;
  314. CHECK (s.keepOnDisk && s.resampleOnLoad && ! s.loadRegions && s.loadSliceMarkers && s.useLoopPoints);
  315. CHECK (s.loopStart == 1200 && s.loopEnd == 3400 && contentFlags (s) == (0x100 | 8 | 4 | 1));
  316. // FL's own "unset" record (FL's demos): no loop points
  317. const auto unset = sampler190dh::oneSampler ([] (FlpBuilder& b) { b.data (0xFB, sampler190dh::loopRecord (-1, std::numeric_limits<std::int64_t>::max(), true)); });
  318. CHECK (! unset.channels.empty() && unset.channels[0].loopStart == -1);
  319. // round trip through the export (the sample file is not on disk here, so the import guessed a stand-in: make it the
  320. // sampler it is in FL)
  321. song.channels[0].instrument = lynx::InstrumentKind::sampler;
  322. song.channels[0].samplePath = "/samples/loop.wav";
  323. lynx::flpexport::Report report;
  324. const auto exported = lynx::flpexport::toProject (song, {}, report);
  325. bool found = false;
  326. for (const auto& c : exported.channels)
  327. if (c.loopStart == 1200 && c.loopEnd == 3400 && c.sampleFlags == (0x100 | 8 | 4 | 1))
  328. found = true;
  329. CHECK (found);
  330. // a LYNX sampler with the defaults exports what LYNX wrote before (load regions + use loop points), no loop record
  331. lynx::SamplerSettings d;
  332. CHECK (contentFlags (d) == (2 | 8));
  333. d.useLoopPoints = false;
  334. CHECK (contentFlags (d) == 2);
  335. setContentFlags (d, 0x10B);
  336. CHECK (d.keepOnDisk && d.resampleOnLoad && d.loadRegions && d.useLoopPoints && ! d.loadSliceMarkers);
  337. // FL greys Keep on disk while a loop plays
  338. lynx::SamplerSettings k;
  339. CHECK (keepOnDiskAvailable (k, false) && ! keepOnDiskAvailable (k, true));
  340. k.useLoopPoints = false;
  341. CHECK (keepOnDiskAvailable (k, true));
  342. }
  343. // SMP START / LENGTH: FL's end is relative, so moving the start keeps the length
  344. TEST (sampler190dh_smp_start_and_length)
  345. {
  346. using namespace lynx::samplerpage;
  347. lynx::SamplerSettings s;
  348. CHECK (lengthOf (s) == 1.0f);
  349. setLength (s, 0.5f);
  350. CHECK (s.regionStart == 0.0f && std::abs (s.regionEnd - 0.5f) < 1e-6f);
  351. setSmpStart (s, 0.5f);
  352. CHECK (std::abs (s.regionStart - 0.5f) < 1e-6f && std::abs (s.regionEnd - 0.75f) < 1e-6f && std::abs (lengthOf (s) - 0.5f) < 1e-6f);
  353. setSmpStart (s, 1.0f);
  354. CHECK (s.regionEnd == 1.0f && lengthOf (s) == 1.0f);
  355. }