FlmImport190feTests.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // --- FL Studio Mobile (.flm) import, round 190fe: drum-kit cut groups as FLM plays them (a hit spares its own pad, a
  2. // held note's end cuts too), lanes on 3x Osc parameters, an effect's Mix with Post Gain or several LYNX slots (FLM's
  3. // blend as a parallel container), and racks whose instruments and effects take turns (one insert per part)
  4. // (docs/analise/FLM_FORMAT.md §6, §7.4, §9). Every project is written by the tests; no file of Image-Line's is committed.
  5. #include "core/RackOps.h"
  6. #include "core/ThreeOsc.h"
  7. namespace flm190fe
  8. {
  9. using namespace flm182c;
  10. using Bytes = flm182c::Bytes;
  11. // FLM's 3x Osc with every parameter at ½ except Enable, the volume envelope's amount (1: FLM's default, full level)
  12. // and the oscillators' shapes (sine)
  13. inline std::vector<float> threeOsc()
  14. {
  15. std::vector<float> p (149, 0.5f);
  16. p[0] = 1.0f;
  17. p[1] = p[2] = p[3] = 0.0f;
  18. p[26] = 0.0f; // (no phase randomness)
  19. p[106] = 1.0f;
  20. for (int k = 146; k <= 148; ++k)
  21. p[(std::size_t) k] = 0.0f;
  22. return p;
  23. }
  24. inline int indexOf (const lynx::Song& s, const std::string& n)
  25. {
  26. for (std::size_t i = 0; i < s.channels.size(); ++i)
  27. if (s.channels[i].name == n)
  28. return (int) i;
  29. return -1;
  30. }
  31. inline float at (const std::vector<float>& y, double seconds) { return y[(std::size_t) (seconds * 44100.0)]; }
  32. } // namespace flm190fe
  33. // FLM's cut groups (0x1807503a0): any trigger of a pad fades out the voices of the *other* pads of its group, never its
  34. // own; on a lane that holds its notes the note's end is a trigger too
  35. TEST (flm190fe_cut_groups_spare_the_pad_that_plays)
  36. {
  37. using namespace lynx;
  38. namespace t = flm190bs;
  39. namespace sp = flm::samplepad;
  40. const std::vector<float> d (sp::defaults, sp::defaults + sp::count);
  41. const auto kit = [&] { return t::rack (flm::kindNormal, {}, { t::sampleOf ({ "A", d, false, 0.0, 1 }), t::sampleOf ({ "B", d, false, 0.0, 1 }),
  42. t::sampleOf ({ "C", d, false, 0.0, 1 }), t::sampleOf ({ "D", d }) }); };
  43. // tempo 120: a beat is 0.5 s. One-shot lane: A at 0 and again at 0.5 beat, B at 1 beat. Held lane: A from 0 for one
  44. // beat, B from 0.5 beat for 2 beats
  45. const auto bytes = t::song ({ t::rack (flm::kindMaster, {}), kit(), kit(), t::channel (flm::kindMaster, "MASTER", 0, {}),
  46. t::channel (flm::kindNormal, "Hits", 1, { t::lane (3, "Hits", { t::clip (0, 4, 4, 0, 1, { { 0, 0.25, 0 }, { 0.5, 0.25, 0 }, { 1, 0.25, 1 } }) }) }),
  47. t::channel (flm::kindNormal, "Held", 2, { t::lane (0x103, "Held", { t::clip (0, 4, 4, 0, 1, { { 0, 1, 0 }, { 0.5, 2, 1 } }) }) }) });
  48. FlpImportReport report;
  49. const auto song = importFlm (bytes, t::anyFile (&report));
  50. CHECK (song.has_value());
  51. if (! song)
  52. return;
  53. std::vector<const ChannelDef*> pads;
  54. for (const auto& c : song->channels)
  55. if (c.instrument == InstrumentKind::sampler)
  56. pads.push_back (&c);
  57. CHECK (pads.size() == 8);
  58. if (pads.size() != 8)
  59. return;
  60. // a group of three: one shared id, sparing the pad that plays; the held lane's pads also cut at a note's end
  61. for (int k = 0; k < 3; ++k)
  62. {
  63. CHECK (pads[(std::size_t) k]->cutGroup > 0 && pads[(std::size_t) k]->cutGroup == pads[(std::size_t) k]->cutBy && pads[(std::size_t) k]->cutOthersOnly
  64. && ! pads[(std::size_t) k]->cutAtNoteEnd && ! channelCutsItself (*pads[(std::size_t) k]));
  65. CHECK (pads[(std::size_t) k + 4]->cutOthersOnly && pads[(std::size_t) k + 4]->cutAtNoteEnd);
  66. }
  67. CHECK (pads[3]->cutGroup == 0 && ! pads[3]->cutOthersOnly && ! pads[7]->cutAtNoteEnd);
  68. CHECK (report.converted["FL Studio Mobile: drum pad cut group"] == 2);
  69. // "Cut itself" from the rack menu turns the FLM rule off
  70. auto own = *pads[0];
  71. setChannelCutsItself (own, 1, true);
  72. CHECK (! own.cutOthersOnly && ! own.cutAtNoteEnd && channelCutsItself (own));
  73. // heard (a 1 s test sample at 0.5 on every pad): A's second hit adds to its first; B's hit cuts both
  74. auto hits = *song;
  75. for (auto& c : hits.channels)
  76. if (&c - hits.channels.data() >= (pads[4] - song->channels.data()))
  77. c.muted = true;
  78. const auto y = t::rendered (hits, 1.0, 0.5f, 44100);
  79. const auto one = flm190fe::at (y, 0.1);
  80. CHECK (one > 0.1f);
  81. CHECK (std::abs (flm190fe::at (y, 0.4) - 2.0f * one) < 1e-3f); // (LYNX's old rule: A cut itself, 1×)
  82. CHECK (std::abs (flm190fe::at (y, 0.6) - one) < 1e-3f);
  83. // the held lane: B's hit at 0.25 s cuts A; A's note ends at 0.5 s and cuts B
  84. auto held = *song;
  85. for (auto& c : held.channels)
  86. if (&c - held.channels.data() < (pads[4] - song->channels.data()) && c.instrument == InstrumentKind::sampler)
  87. c.muted = true;
  88. const auto h = t::rendered (held, 1.0, 0.5f, 44100);
  89. CHECK (std::abs (flm190fe::at (h, 0.4) - one) < 1e-3f);
  90. CHECK (std::abs (flm190fe::at (h, 0.6)) < 1e-4f);
  91. auto noEnd = held;
  92. for (auto& c : noEnd.channels)
  93. c.cutAtNoteEnd = false;
  94. CHECK (std::abs (flm190fe::at (t::rendered (noEnd, 1.0, 0.5f, 44100), 0.6) - one) < 1e-3f); // (without the rule B goes on)
  95. }
  96. // Every 3x Osc parameter a lane may move writes what the import writes for the same value (the laws are one)
  97. TEST (flm190fe_3xosc_lane_laws_equal_the_import)
  98. {
  99. using namespace lynx;
  100. namespace t = flm190de;
  101. const auto stateFor = [] (const std::vector<float>& p) {
  102. const auto bytes = flm190bs::song ({ t::rack (flm::kindMaster, {}), t::rack (flm::kindNormal, { t::moduleWith (39, 2, p) }),
  103. t::channel (flm::kindMaster, "MASTER", 0, {}),
  104. t::channel (flm::kindNormal, "Osc", 1, { t::lane (0, "Osc", { t::clip (0, 4, 4, 0, 1, { { 0, 1, 60 } }) }) }) });
  105. const auto s = importFlm (bytes, flm190bs::anyFile (nullptr));
  106. const auto* c = s ? flm182c::channelNamed (*s, "Osc") : nullptr;
  107. return c != nullptr && c->instrument == InstrumentKind::threeosc ? c->pluginState : std::vector<std::uint8_t> {};
  108. };
  109. const auto base = flm190fe::threeOsc();
  110. const auto s0 = stateFor (base);
  111. CHECK (s0.size() == 460);
  112. if (s0.size() != 460)
  113. return;
  114. std::uint32_t seed = 190;
  115. int compared = 0;
  116. for (int k = 1; k < threeosc::flmParamCount; ++k)
  117. {
  118. if (! threeosc::isFlmTarget (k) || k == 114)
  119. continue;
  120. for (const auto v : { 0.0f, 1.0f, -1.0f })
  121. {
  122. seed = seed * 1664525u + 1013904223u;
  123. const auto x = v >= 0.0f ? v : (float) (seed >> 8) / 16777216.0f;
  124. auto p = base;
  125. p[(std::size_t) k] = x;
  126. const auto expected = stateFor (p);
  127. auto written = s0;
  128. // the import folds the offsets (½ here) into the cutoff and resonance: the target holds the sum
  129. CHECK (threeosc::writeFlmParam (written.data(), k, x, 4, 120.0));
  130. const auto same = written == expected;
  131. CHECK (same);
  132. if (! same)
  133. std::printf (" 3x Osc parameter %d at %g differs\n", k, (double) x);
  134. ++compared;
  135. }
  136. }
  137. CHECK (compared == 3 * 110);
  138. CHECK (! threeosc::writeFlmParam (std::vector<std::uint8_t> (460).data(), 25, 1.0, 4, 120.0)
  139. && ! threeosc::writeFlmParam (std::vector<std::uint8_t> (460).data(), 114, 1.0, 4, 120.0)
  140. && ! threeosc::isFlmTarget (113) && ! threeosc::isFlmTarget (106) && ! threeosc::isFlmTarget (140) && ! threeosc::isFlmTarget (147));
  141. }
  142. // The lanes: FLM's values on the engine's targets (the cutoff / resonance offsets and the volume envelope's amount folded
  143. // in), voice settings listed; the engine plays a lane held at v as the import plays v
  144. TEST (flm190fe_3xosc_lanes_play)
  145. {
  146. using namespace lynx;
  147. namespace t = flm190de;
  148. const auto ramp = [] (std::uint32_t id) { return flm187d::autoClip (0, 4, 4, id, { { 0, 1, 0 }, { 4, 1, 65535 } }); };
  149. auto p = flm190fe::threeOsc();
  150. p[106] = 0.75f; // the volume envelope's amount: half of FLM's gain
  151. const auto bytes = flm190bs::song ({
  152. t::rack (flm::kindMaster, {}), t::rack (flm::kindNormal, { t::moduleWith (39, 2, p) }), t::channel (flm::kindMaster, "MASTER", 0, {}),
  153. t::channel (flm::kindNormal, "Osc", 1, { t::lane (0, "Osc", { t::clip (0, 4, 4, 0, 1, { { 0, 1, 60 } }) }),
  154. t::lane (1, "Osc 1 Coarse Pitch : 3x Osc", { ramp (1) }, 2, 13),
  155. t::lane (1, "Filter Cutoff Offset : 3x Osc", { ramp (2) }, 2, 115),
  156. t::lane (1, "Volume : 3x Osc", { ramp (3) }, 2, 114),
  157. t::lane (1, "Arpeggio Time : 3x Osc", { ramp (4) }, 2, 140) }) });
  158. FlpImportReport report;
  159. const auto s = importFlm (bytes, flm190bs::anyFile (&report));
  160. CHECK (s.has_value());
  161. if (! s)
  162. return;
  163. const auto osc = flm190fe::indexOf (*s, "Osc");
  164. CHECK (osc >= 0 && s->channels[(std::size_t) osc].instrument == InstrumentKind::threeosc && threeOscIsFlm (s->channels[(std::size_t) osc]));
  165. const auto* coarse = t::automationOf (*s, AutomationTargetKind::channelParam, osc, threeosc::flmParamBase + 13);
  166. const auto* cutoff = t::automationOf (*s, AutomationTargetKind::channelParam, osc, threeosc::flmParamBase + 111);
  167. const auto* volume = t::automationOf (*s, AutomationTargetKind::channelParam, osc, threeosc::flmParamBase + 114);
  168. CHECK (coarse != nullptr && cutoff != nullptr && volume != nullptr);
  169. if (coarse != nullptr && coarse->points.size() == 2)
  170. CHECK (coarse->points[0].value == 0.0f && coarse->points[1].value == 1.0f);
  171. if (cutoff != nullptr && cutoff->points.size() == 2) // cutoff ½ + 2·offset − 1
  172. CHECK (cutoff->points[0].value == 0.0f && std::abs (cutoff->points[1].value - 1.0f) < 1e-6f);
  173. if (volume != nullptr && volume->points.size() == 2) // v · clamp(2·0.75 − 1)
  174. CHECK (volume->points[0].value == 0.0f && std::abs (volume->points[1].value - 0.5f) < 1e-6f);
  175. CHECK (report.notReproduced.count ("FL Studio Mobile: automation of 3x Osc parameter 140 (not imported)") == 1);
  176. CHECK (report.notReproduced.count ("FL Studio Mobile: automation of 3x Osc parameter 13 (not imported)") == 0);
  177. // heard: a lane held at a value against the import of that value, without the lane: coarse +12, the cutoff at ¼
  178. // (on the 3x Osc's filter type 3, HP, so it is heard), the Volume at 0.8 (FLM's gain 0.8 with the envelope amount 0.75)
  179. const auto compare = [&] (int k, std::uint16_t raw) {
  180. auto q = flm190fe::threeOsc();
  181. q[106] = 0.75f;
  182. q[110] = k == 111 ? 3.0f / 7.0f : 0.0f; // (a high-pass to hear the cutoff; else the filter open)
  183. q[111] = 1.0f;
  184. auto still = q;
  185. still[(std::size_t) k] = (float) raw / 65535.0f;
  186. const auto make = [&] (const std::vector<float>& params, bool withLane) {
  187. return flm190bs::song ({ t::rack (flm::kindMaster, {}), t::rack (flm::kindNormal, { t::moduleWith (39, 2, params) }),
  188. t::channel (flm::kindMaster, "MASTER", 0, {}),
  189. withLane ? t::channel (flm::kindNormal, "Osc", 1, { t::lane (0, "Osc", { t::clip (0, 4, 4, 0, 1, { { 0, 1, 60 } }) }),
  190. t::lane (1, "Lane", { flm187d::autoClip (0, 4, 4, 1, { { 0, 1, raw }, { 4, 1, raw } }) }, 2, k) })
  191. : t::channel (flm::kindNormal, "Osc", 1, { t::lane (0, "Osc", { t::clip (0, 4, 4, 0, 1, { { 0, 1, 60 } }) }) }) });
  192. };
  193. const auto withLane = importFlm (make (q, true), flm190bs::anyFile (nullptr));
  194. const auto without = importFlm (make (still, false), flm190bs::anyFile (nullptr));
  195. if (! withLane || ! without || withLane->automations.size() != 1)
  196. {
  197. std::printf (" import: %d %d automations %d\n", (int) withLane.has_value(), (int) without.has_value(), withLane ? (int) withLane->automations.size() : -1);
  198. return 1e9;
  199. }
  200. const auto x = flm190bs::rendered (*withLane, 0.4, 0.5f, 44100), y = flm190bs::rendered (*without, 0.4, 0.5f, 44100);
  201. const auto moved = flm190bs::rendered (*importFlm (make (q, false), flm190bs::anyFile (nullptr)), 0.4, 0.5f, 44100);
  202. double diff = 0.0, peak = 0.0, change = 0.0;
  203. for (std::size_t i = 0; i < x.size() && i < y.size() && i < moved.size(); ++i)
  204. {
  205. diff = std::max (diff, (double) std::abs (x[i] - y[i]));
  206. peak = std::max (peak, (double) std::abs (y[i]));
  207. change = std::max (change, (double) std::abs (moved[i] - y[i]));
  208. }
  209. if (! (peak > 0.01 && change > 0.01))
  210. std::printf (" peak %g change %g\n", peak, change);
  211. return peak > 0.01 && change > 0.01 ? diff / peak : 1e9; // (the value must change the sound, else nothing is shown)
  212. };
  213. for (const auto& [k, raw, tolerance] : { std::tuple<int, std::uint16_t, double> { 13, 49151, 1e-6 }, { 111, 16384, 1e-6 }, { 114, 52428, 1e-4 } })
  214. {
  215. const auto d = compare (k, raw);
  216. CHECK (d < tolerance); // (114: 52428 / 65535 is a hair above 0.8, which the import keeps)
  217. if (! (d < tolerance))
  218. std::printf (" lane on 3x Osc parameter %d: relative difference %g\n", k, d);
  219. }
  220. }
  221. // FLM's wrapper around an effect: dry × (1 − Mix) + Post Gain × wet × Mix, heard
  222. TEST (flm190fe_effect_mix_with_post_gain_is_flms_blend)
  223. {
  224. using namespace lynx;
  225. namespace t = flm190bs;
  226. using flm182c::Bytes;
  227. const auto songWith = [] (const std::vector<Bytes>& modules) {
  228. auto r = flm182c::rack (flm::kindNormal, {}, { t::sampleOf ({ "Hit", {} }) });
  229. Bytes body (r.begin() + 8, r.end());
  230. for (const auto& m : modules)
  231. flm182c::put (body, m.data(), m.size());
  232. return t::song ({ t::rack (flm::kindMaster, {}), flm182c::chunk ("RACK", body), t::channel (flm::kindMaster, "MASTER", 0, {}),
  233. t::channel (flm::kindNormal, "Kit", 1, { t::lane (3, "Kit", { t::clip (0, 4, 4, 0, 1, { { 0, 0.25, 0 } }) }) }) });
  234. };
  235. // Equalizer (3) at its defaults (flat), Mix 0.5, Post Gain 0.65 (+6 dB)
  236. const auto eq = flm190de::moduleWith (3, 2, { 1.0f }, { 0.65f, 0.5f, 0.5f });
  237. FlpImportReport report;
  238. const auto s = importFlm (songWith ({ eq }), t::anyFile (&report));
  239. const auto plain = importFlm (songWith ({}), t::anyFile (nullptr));
  240. CHECK (s.has_value() && plain.has_value());
  241. if (! s || ! plain)
  242. return;
  243. const auto& fx = s->mixer[1].effects;
  244. CHECK (fx.size() == 1 && fx[0].kind == EffectKind::parallel);
  245. if (fx.size() == 1 && fx[0].branches.size() == 2)
  246. CHECK (fx[0].branches[0].chain.empty() && std::abs (fx[0].branches[0].gain - 0.5f) < 1e-6f && fx[0].branches[1].chain.size() == 1
  247. && fx[0].branches[1].chain[0].kind == EffectKind::pocketEq && std::abs (fx[0].branches[1].gain - 0.5f * (float) std::pow (10.0, 0.3)) < 1e-5f);
  248. for (const auto& [what, n] : report.partialEffects)
  249. CHECK (what.find ("Mix") == std::string::npos && what.find ("Post Gain") == std::string::npos);
  250. const auto a = t::rendered (*s, 0.5, 0.5f, 44100), b = t::rendered (*plain, 0.5, 0.5f, 44100);
  251. CHECK (std::abs (flm190fe::at (b, 0.2)) > 0.1f);
  252. CHECK (std::abs (flm190fe::at (a, 0.2) / flm190fe::at (b, 0.2) - (0.5f + 0.5f * (float) std::pow (10.0, 0.3))) < 2e-3f);
  253. }
  254. // A rack's modules work on one buffer in order (0x18059d3b0): an effect hears only what is before it, and an
  255. // instrument's Post Gain / Mix scales what is already there. Each part becomes an insert sending into the next
  256. TEST (flm190fe_rack_parts_take_turns)
  257. {
  258. using namespace lynx;
  259. namespace t = flm190de;
  260. const auto bytes = flm190bs::song ({
  261. t::rack (flm::kindMaster, {}),
  262. // instrument, Graphic EQ, instrument
  263. t::rack (flm::kindNormal, { t::moduleWith (39, 2, flm190fe::threeOsc()), t::moduleWith (7, 3, { 1.0f }), t::moduleWith (39, 4, flm190fe::threeOsc()) }),
  264. // a drum pad, then an instrument at Post Gain 0.35 (−6 dB): the pad is lowered with it
  265. flm182c::rack (flm::kindNormal, { t::moduleWith (39, 2, flm190fe::threeOsc(), { 0.35f, 1.0f, 0.5f }) }, { flm190bs::sampleOf ({ "Hit", {} }) }),
  266. flm182c::rack (flm::kindNormal, { t::moduleWith (39, 2, flm190fe::threeOsc()) }, { flm190bs::sampleOf ({ "Hit", {} }) }),
  267. t::channel (flm::kindMaster, "MASTER", 0, {}),
  268. t::channel (flm::kindNormal, "Turns", 1, { t::lane (0, "Turns", { t::clip (0, 4, 4, 0, 1, { { 0, 1, 60 } }) }) }),
  269. t::channel (flm::kindNormal, "Scaled", 2, { t::lane (3, "Scaled", { t::clip (0, 4, 4, 0, 1, { { 0, 0.25, 0 } }) }) }),
  270. t::channel (flm::kindNormal, "Plain", 3, { t::lane (3, "Plain", { t::clip (0, 4, 4, 0, 1, { { 0, 0.25, 0 } }) }) }) });
  271. FlpImportReport report;
  272. const auto s = importFlm (bytes, flm190bs::anyFile (&report));
  273. CHECK (s.has_value());
  274. if (! s)
  275. return;
  276. const auto* layer = flm182c::channelNamed (*s, "Turns");
  277. CHECK (layer != nullptr && layer->instrument == InstrumentKind::layer && layer->layer.children.size() == 2);
  278. if (layer == nullptr || layer->layer.children.size() != 2)
  279. return;
  280. const auto& first = s->channels[(std::size_t) layer->layer.children[0]];
  281. const auto& second = s->channels[(std::size_t) layer->layer.children[1]];
  282. const auto main = layer->mixerTrack;
  283. CHECK (second.mixerTrack == main && first.mixerTrack != main && first.mixerTrack > 0);
  284. if (first.mixerTrack > 0 && first.mixerTrack < (int) s->mixer.size())
  285. {
  286. const auto& part = s->mixer[(std::size_t) first.mixerTrack];
  287. CHECK (part.effects.size() == 1 && part.effects[0].kind == EffectKind::pocketGraphicEq && ! part.toMaster && part.sends.size() == 1
  288. && part.sends[0].target == main && part.sends[0].level == 1.0f && part.name == "Turns (rack part 1)");
  289. CHECK (s->mixer[(std::size_t) main].effects.empty());
  290. }
  291. CHECK (report.converted["FL Studio Mobile: rack whose instruments and effects take turns (one insert per part)"] == 2);
  292. CHECK (report.partialEffects.count ("FL Studio Mobile: an instrument placed after an effect in its rack (the effect processes it too)") == 0
  293. && report.partialEffects.count ("FL Studio Mobile: instrument Post Gain / Mix in a rack with other sounds (the others are not changed)") == 0);
  294. // heard: the pad of "Scaled" at −6 dB against "Plain" (the instruments get no notes)
  295. const auto* scaledPad = flm182c::channelNamed (*s, "Hit");
  296. CHECK (scaledPad != nullptr);
  297. auto one = *s, other = *s;
  298. for (auto& c : one.channels)
  299. c.muted = c.muted || c.mixerTrack != (scaledPad != nullptr ? scaledPad->mixerTrack : -1);
  300. const auto plainAt = [&] {
  301. for (const auto& c : s->channels)
  302. if (c.name == "Hit" && scaledPad != nullptr && &c != scaledPad)
  303. return c.mixerTrack;
  304. return -1;
  305. }();
  306. for (auto& c : other.channels)
  307. c.muted = c.muted || c.mixerTrack != plainAt;
  308. const auto a = flm190bs::rendered (one, 0.3, 0.5f, 44100), b = flm190bs::rendered (other, 0.3, 0.5f, 44100);
  309. CHECK (std::abs (flm190fe::at (b, 0.1)) > 0.1f);
  310. CHECK (std::abs (flm190fe::at (a, 0.1) / flm190fe::at (b, 0.1) - (float) std::pow (10.0, -0.3)) < 1e-3f);
  311. }