FlScratch165Tests.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // --- Fruity Scratcher (core/Scratcher.h) - round 165g, sub-round flscratch165 (REA_rodada165g-flscratch165.md)
  2. #include "core/Scratcher.h"
  3. namespace flscr165
  4. {
  5. // FL's state as the installation's render test holds it (version 0xF4628, 14 floats, the sample's path)
  6. std::vector<std::uint8_t> flState (std::uint32_t version, std::initializer_list<float> values, const std::string& path)
  7. {
  8. std::vector<std::uint8_t> out;
  9. auto put = [&out] (const void* p, std::size_t n) {
  10. const auto* b = static_cast<const std::uint8_t*> (p);
  11. out.insert (out.end(), b, b + n);
  12. };
  13. put (&version, 4);
  14. for (const auto v : values)
  15. put (&v, 4);
  16. const auto n = (std::int32_t) path.size();
  17. put (&n, 4);
  18. put (path.data(), path.size());
  19. return out;
  20. }
  21. // a stereo sine of `cycles` periods over `frames` frames, on the 16-bit grid
  22. std::shared_ptr<lynx::SampleData> sine (int frames, int cycles, float amp = 0.5f)
  23. {
  24. auto s = std::make_shared<lynx::SampleData>();
  25. s->left.resize ((std::size_t) frames);
  26. s->right.resize ((std::size_t) frames);
  27. for (int i = 0; i < frames; ++i)
  28. {
  29. const auto v = std::round (amp * 32767.0 * std::sin (2.0 * 3.141592653589793 * cycles * i / frames)) / 32768.0;
  30. s->left[(std::size_t) i] = s->right[(std::size_t) i] = (float) v;
  31. }
  32. s->sampleRate = 44100.0;
  33. return s;
  34. }
  35. // zero crossings (rising) per second of a block
  36. double risingPerSecond (const std::vector<float>& x, double rate)
  37. {
  38. int n = 0;
  39. for (std::size_t i = 1; i < x.size(); ++i)
  40. n += x[i - 1] < 0.0f && x[i] >= 0.0f;
  41. return n * rate / (double) x.size();
  42. }
  43. lynx::EffectParams params (std::initializer_list<std::pair<int, float>> set)
  44. {
  45. auto p = lynx::makeEffectSlot (lynx::EffectKind::scratcher).params;
  46. for (const auto& [k, v] : set)
  47. p[(std::size_t) k] = v;
  48. return p;
  49. }
  50. // runs the effect for `blocks` blocks of 256 on a silent input; returns the last `keep` samples (left, right)
  51. std::pair<std::vector<float>, std::vector<float>> run (lynx::Effect& e, const lynx::EffectParams& p, int blocks, int keep)
  52. {
  53. std::vector<float> l, r;
  54. for (int b = 0; b < blocks; ++b)
  55. {
  56. std::vector<float> bl (256, 0.0f), br (256, 0.0f);
  57. e.process (bl.data(), br.data(), 256, p, 120.0);
  58. l.insert (l.end(), bl.begin(), bl.end());
  59. r.insert (r.end(), br.begin(), br.end());
  60. }
  61. const auto from = l.size() - (std::size_t) std::min<std::size_t> ((std::size_t) keep, l.size());
  62. return { { l.begin() + (long) from, l.end() }, { r.begin() + (long) from, r.end() } };
  63. }
  64. double rms (const std::vector<float>& x)
  65. {
  66. double s = 0.0;
  67. for (const auto v : x)
  68. s += (double) v * v;
  69. return x.empty() ? 0.0 : std::sqrt (s / (double) x.size());
  70. }
  71. } // namespace flscr165
  72. TEST (flscr165_controls_and_state)
  73. {
  74. using namespace lynx::scratcher;
  75. // FL's control ranges (the editor's DFM) and their steps
  76. CHECK (controlRange (wavePos).hi == 30000 && controlRange (pan).lo == -128 && controlRange (volume).hi == 255);
  77. CHECK (rawOfValue (volume, 0.78f) == 199 && rawOfValue (wavePos, 0.111f) == 3330 && rawOfValue (pan, 0.5f) == 0);
  78. CHECK (valueOfRaw (wavePos, 30000) == 1.0f && valueOfRaw (transport, -1) == 0.0f && valueOfRaw (transport, 1) == 1.0f);
  79. CHECK (transportOf (0.0f) == -1 && transportOf (0.5f) == 0 && transportOf (1.0f) == 1 && rotationsOf (0.5f) == 9);
  80. CHECK (quantize (speed, 0.504f) == valueOfRaw (speed, 50));
  81. const auto d = defaults();
  82. CHECK (rawOfValue (wavePos, d[wavePos]) == 690 && rawOfValue (volume, d[volume]) == 199 && transportOf (d[transport]) == 0);
  83. CHECK (paramText (transport, 1.0f) == "Forwards" && paramText (speed, 0.5f) == "1.00x" && paramText (rotations, 0.5f) == "+0");
  84. // the render test's state: Wave position 0.111, sound on (1), Hold on, transport stopped, Rotations 9
  85. const auto blob = flscr165::flState (0xF4628u, { 0.111f, 1.0f, 0.5f, 0.25f, 0.5f, 0.78f, 1.0f, 0.5f, 0.0f, 0.5f, 0.908f, 0.999f, 0.4f, 9.0f },
  86. "%FLStudioFactoryData%\\Data\\Patches\\Packs\\Loops\\DL Breaker.wav");
  87. const auto st = parse (blob);
  88. CHECK (st.has_value());
  89. if (st)
  90. {
  91. CHECK (st->values[wavePos] == valueOfRaw (wavePos, 3330) && st->values[mute] == 0.0f && st->values[hold] == 1.0f);
  92. CHECK (st->values[volume] == valueOfRaw (volume, 199) && transportOf (st->values[transport]) == 0 && rotationsOf (st->values[rotations]) == 9);
  93. CHECK (st->samplePath == "%FLStudioFactoryData%\\Data\\Patches\\Packs\\Loops\\DL Breaker.wav" && st->timecode == -1);
  94. // LYNX writes FL 2026's version; reading it back gives the same values
  95. const auto again = parse (toFl (st->values, st->samplePath));
  96. CHECK (again && again->values == st->values && again->samplePath == st->samplePath && again->version == 0xF462Au);
  97. }
  98. // FL's sound flag 0 = muted; unknown versions are refused
  99. const auto muted = parse (flscr165::flState (0xF4628u, { 0, 0, 0.5f, 0.25f, 0.5f, 0.78f, 0, 0.5f, 1, 0.5f, 0.908f, 0.999f, 0.4f, 9 }, ""));
  100. CHECK (muted && muted->values[mute] == 1.0f && transportOf (muted->values[transport]) == 1 && muted->samplePath.empty());
  101. CHECK (! parse (flscr165::flState (0xF4627u, {}, "")) && ! parse ({ 1, 2, 3 }));
  102. // the import's slot: kind, parameters
  103. const auto fx = lynx::effectFromFl ("Fruity Scratcher", blob, 140.0);
  104. CHECK (fx && fx->kind == lynx::EffectKind::scratcher && st && fx->params[wavePos] == st->values[wavePos] && fx->params[hold] == 1.0f);
  105. CHECK (lynx::effectForFlPlugin ("Fruity Scratcher") == lynx::EffectKind::scratcher);
  106. }
  107. TEST (flscr165_sample_as_fl_hosts_it)
  108. {
  109. using namespace lynx::scratcher;
  110. // 16-bit values stay; others are rounded as round(x · 32767) / 32768; the extra frame is frame 0's right side
  111. const float l[3] = { 0.5f, -0.25f, 0.1f }, r[3] = { 0.125f, 0.0f, 0.3f };
  112. const auto t = prepareSample (l, r, 3, 44100.0);
  113. CHECK (t && t->n == 3 && t->frames.size() == 8);
  114. if (t)
  115. {
  116. CHECK (t->frames[0] == (float) (std::lround (0.5 * 32767) / 32768.0) && t->frames[1] == (float) (std::lround (0.125 * 32767) / 32768.0));
  117. CHECK (t->frames[6] == t->frames[1] && t->frames[7] == t->frames[1]);
  118. }
  119. const float g[2] = { 16384.0f / 32768.0f, -3.0f / 32768.0f };
  120. const auto grid = prepareSample (g, nullptr, 2, 44100.0);
  121. CHECK (grid && grid->frames[0] == g[0] && grid->frames[1] == g[0] && grid->frames[2] == g[1] && grid->frames[4] == g[0]);
  122. // another rate: resampled to 44.1 kHz
  123. std::vector<float> long48 (48000, 0.0f);
  124. const auto r48 = prepareSample (long48.data(), nullptr, long48.size(), 48000.0);
  125. CHECK (r48 && r48->n == 44100);
  126. }
  127. TEST (flscr165_motor_mode)
  128. {
  129. using namespace lynx::scratcher;
  130. // a 441 Hz sine (100 cycles over 0.1 s · 10): Speed 0.5 = 1×, Speed 1 = 2× (2 · Speed / (n − 1) turns per sample)
  131. const auto smp = flscr165::sine (44100, 441);
  132. for (const auto& [spd, want] : std::vector<std::pair<float, double>> { { 0.5f, 441.0 }, { 1.0f, 882.0 }, { 0.25f, 220.5 } })
  133. {
  134. auto e = make (smp);
  135. e->prepare (44100.0);
  136. const auto p = flscr165::params ({ { transport, 1.0f }, { speed, spd }, { accel, 1.0f }, { volume, 1.0f } });
  137. const auto [l, r] = flscr165::run (*e, p, 600, 44100); // the inertia (≈ 2000 samples at Acceleration 1) has settled
  138. const auto hz = flscr165::risingPerSecond (l, 44100.0);
  139. CHECK (std::abs (hz - want) < 2.0);
  140. if (std::abs (hz - want) >= 2.0)
  141. std::printf (" motor speed %.2f: %.2f Hz (want %.2f)\n", spd, hz, want);
  142. CHECK (flscr165::rms (l) > 0.3 && std::abs (flscr165::rms (l) - flscr165::rms (r)) < 1e-6); // centred
  143. }
  144. // backwards: the position runs down; stopped: silence once the DC blocker has settled
  145. {
  146. auto e = make (smp);
  147. const auto p = flscr165::params ({ { transport, 0.0f }, { accel, 1.0f } });
  148. flscr165::run (*e, p, 40, 16);
  149. const auto a = e->controlOutput (0);
  150. flscr165::run (*e, p, 4, 16);
  151. const auto b = e->controlOutput (0);
  152. CHECK (a != b && (b < a || a < 0.05f));
  153. auto s = make (smp);
  154. const auto [l, r] = flscr165::run (*s, flscr165::params ({}), 400, 4096);
  155. CHECK (flscr165::rms (l) < 1e-6);
  156. }
  157. // the input passes with gain 1; without a sample nothing is added
  158. {
  159. auto e = make (nullptr);
  160. std::vector<float> l (64, 0.25f), r (64, -0.5f);
  161. e->process (l.data(), r.data(), 64, flscr165::params ({ { transport, 1.0f } }), 120.0);
  162. CHECK (l[10] == 0.25f && r[63] == -0.5f);
  163. }
  164. // like FL, the sample takes over at the end of the first block: that block adds nothing
  165. {
  166. auto e = make (smp);
  167. std::vector<float> l (256, 0.0f), r (256, 0.0f);
  168. e->process (l.data(), r.data(), 256, flscr165::params ({ { transport, 1.0f }, { accel, 1.0f } }), 120.0);
  169. CHECK (flscr165::rms (l) == 0.0);
  170. }
  171. }
  172. TEST (flscr165_hold_mute_pan)
  173. {
  174. using namespace lynx::scratcher;
  175. const auto smp = flscr165::sine (44100, 441);
  176. // Hold: a still platter is silent (DC blocker); moving Wave position plays the sample
  177. {
  178. auto e = make (smp);
  179. const auto still = flscr165::params ({ { hold, 1.0f }, { wavePos, 0.3f }, { volume, 1.0f } });
  180. const auto [l, r] = flscr165::run (*e, still, 2000, 4096); // the smoother converges within ~2 s
  181. CHECK (flscr165::rms (l) < 1e-3);
  182. double moving = 0.0;
  183. for (int b = 0; b < 400; ++b)
  184. {
  185. auto p = still;
  186. p[wavePos] = 0.3f + 0.2f * (float) b / 400.0f;
  187. const auto [ml, mr] = flscr165::run (*e, p, 1, 256);
  188. moving = std::max (moving, flscr165::rms (ml));
  189. }
  190. CHECK (moving > 0.05);
  191. }
  192. // Mute: the gain glides to 0; Pan fully right: the left side is silent (linear balance)
  193. {
  194. auto e = make (smp);
  195. const auto [l, r] = flscr165::run (*e, flscr165::params ({ { transport, 1.0f }, { accel, 1.0f }, { mute, 1.0f } }), 300, 4096);
  196. CHECK (flscr165::rms (l) < 1e-6 && flscr165::rms (r) < 1e-6);
  197. auto p = make (smp);
  198. const auto [pl, pr] = flscr165::run (*p, flscr165::params ({ { transport, 1.0f }, { accel, 1.0f }, { pan, 1.0f } }), 300, 4096);
  199. CHECK (flscr165::rms (pl) < 1e-6 && flscr165::rms (pr) > 0.2);
  200. }
  201. }
  202. TEST (flscr165_render_test_import)
  203. {
  204. // FL's own render test (Data/System/Render tests/Fruity Scratcher.flp): insert 1 slot 0, Hold on, the platter moved
  205. // by 259 pattern events on Wave position (raw 0…131 of the wheel's 0…30000), DL Breaker.wav from the factory data
  206. namespace fs = std::filesystem;
  207. if (realFlpDir.empty())
  208. return;
  209. const auto path = fs::path (realFlpDir) / "System" / "Render tests" / "Fruity Scratcher.flp";
  210. std::ifstream in (path, std::ios::binary);
  211. const std::vector<std::uint8_t> file ((std::istreambuf_iterator<char> (in)), {});
  212. if (file.empty())
  213. return;
  214. const auto local = [] (std::string p) {
  215. std::replace (p.begin(), p.end(), '\\', '/');
  216. return lynx::existingPathIgnoringCase (p);
  217. };
  218. lynx::FlpImportReport report;
  219. lynx::FlpImportOptions options;
  220. options.report = &report;
  221. options.roots.data = fs::path (realFlpDir).string();
  222. options.roots.factoryData = fs::path (realFlpDir).parent_path().string();
  223. options.fileExists = [local] (const std::string& p) { return fs::is_regular_file (local (p)); };
  224. const auto song = lynx::importFlp (flp::parseProject (file), options);
  225. CHECK (song.mixer.size() > 1 && ! song.mixer[1].effects.empty());
  226. if (song.mixer.size() < 2 || song.mixer[1].effects.empty())
  227. return;
  228. const auto& fx = song.mixer[1].effects[0];
  229. using namespace lynx::scratcher;
  230. // the state holds Wave position 3330, but the song's start values (0xD8) set it to 0, as FL does when the song starts
  231. CHECK (fx.kind == lynx::EffectKind::scratcher && fx.params[hold] == 1.0f && rawOfValue (wavePos, fx.params[wavePos]) == 0);
  232. CHECK (fx.samplePath.find ("DL Breaker.wav") != std::string::npos);
  233. CHECK (report.converted["Fruity Scratcher"] == 1 && report.droppedEffects.count ("Fruity Scratcher") == 0);
  234. // the platter's events: a lane on parameter 0 in the wheel's scale (the highest, 131, is 131 / 30000)
  235. const lynx::PatternEvents* lane = nullptr;
  236. for (const auto& p : song.patterns)
  237. for (const auto& l : p.events)
  238. if (l.target.kind == lynx::AutomationTargetKind::effectParam && l.target.index == 1 && l.target.slot == 0 && l.target.param == 0)
  239. lane = &l;
  240. CHECK (lane != nullptr);
  241. if (lane != nullptr)
  242. {
  243. float top = 0.0f;
  244. for (const auto& e : lane->events)
  245. top = std::max (top, e.value);
  246. CHECK (lane->events.size() > 200 && std::abs (top - 131.0f / 30000.0f) < 1e-6f);
  247. }
  248. }
  249. TEST (flscr165_editor_takes_sample_drops)
  250. {
  251. // the editor loads a dropped sample (OS drop or LYNX's Browser) instead of letting it fall to the Playlist: the
  252. // same list as SampleLibrary::isSampleFile (core/SampleFiles.h), FL's .wv packs included, any case
  253. using lynx::scratcher::loadsDroppedFile;
  254. for (const char* ok : { "C:\\Samples\\DL Breaker.wav", "/home/u/loop.WAV", "kick.aiff", "pad.flac", "x.ogg", "y.mp3", "C:\\FL\\Packs\\a.wv", "b.aif" })
  255. CHECK (loadsDroppedFile (ok));
  256. for (const char* no : { "song.flp", "beat.mid", "C:\\dir.wav\\readme", "noext", "trailing.", "preset.fst", "" })
  257. CHECK (! loadsDroppedFile (no));
  258. }