Issues171aTests.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Round 171a: issues #67 (Parametric EQ state after save and reopen), #68 ("Cut itself" in two places), #63 (per-note
  2. // pan), #66 (Peak Controller display) and #62 (detached windows after a restart).
  3. #include "core/EffectPresets.h"
  4. #include "core/FlEffects.h"
  5. #include "core/ParametricEqFl.h"
  6. namespace issues171a
  7. {
  8. std::vector<std::uint8_t> flEffectPreset (const char* folder, const char* file)
  9. {
  10. if (realFlpDir.empty())
  11. return {};
  12. const auto p = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / folder / file;
  13. std::ifstream in (p, std::ios::binary);
  14. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  15. }
  16. // what a project save + reload does to a slot's parameters (engine/ProjectFile.cpp effectToVar / effectFromVar)
  17. lynx::EffectSlot savedAndReloaded (const lynx::EffectSlot& slot)
  18. {
  19. auto loaded = lynx::makeEffectSlot (slot.kind);
  20. const auto count = lynx::savedEffectParamCount (slot);
  21. for (std::size_t i = 0; i < count; ++i)
  22. loaded.params[i] = std::clamp ((float) lynx::savedEffectParamValue (slot.params[i]), 0.0f, 1.0f);
  23. lynx::upgradeLoadedEffectParams (loaded, count);
  24. return loaded;
  25. }
  26. // a Parametric EQ 2 state as FL saves it (version 8, 0x162 bytes): the preset "40Hz + 18kHz cut"'s values
  27. std::vector<std::uint8_t> peq2State()
  28. {
  29. std::vector<std::uint8_t> d (0x162, 0);
  30. auto put = [&d] (std::size_t at, std::int32_t v) { std::memcpy (d.data() + at, &v, 4); };
  31. put (0, 8);
  32. const std::int32_t gain[7] { 322, -184, 0, 0, -520, 50, -950 }, freq[7] { 4523, 7228, 25486, 32768, 64210, 64613, 64901 },
  33. width[7] { 36068, 41796, 25412, 25412, 47712, 7456, 36068 }, type[7] { 3, 0, 0, 0, 0, 0, 1 }, order[7] { -3, 0, 0, 0, 0, 0, -3 };
  34. for (int b = 0; b < 7; ++b)
  35. {
  36. put (4 + 4 * (std::size_t) b, gain[b]);
  37. put (4 + 4 * (std::size_t) (7 + b), freq[b]);
  38. put (4 + 4 * (std::size_t) (14 + b), width[b]);
  39. put (4 + 4 * (std::size_t) (21 + b), type[b]);
  40. put (4 + 4 * (std::size_t) (28 + b), order[b]);
  41. }
  42. d[0x13D] = 66;
  43. put (0x13E, 0x7F);
  44. return d;
  45. }
  46. bool sameSettings (const lynx::peqfl::Settings& a, const lynx::peqfl::Settings& b)
  47. {
  48. return a.law == b.law && a.gain == b.gain && a.freq == b.freq && a.width == b.width && a.type == b.type && a.order == b.order
  49. && a.main == b.main && a.processing == b.processing && a.linearPhase == b.linearPhase && a.mask == b.mask;
  50. }
  51. } // namespace issues171a
  52. // #67: "40Hz + 18kHz cut" (Steep 8 low cut at 32 Hz, Steep 8 high cut) read back as LYNX's own EQ after a reload: the
  53. // FL law marker [36] and the processing / mask / linear fields after the 36 named parameters were not saved.
  54. TEST (issues171a_parametric_eq_fl_law_survives_save_and_reload)
  55. {
  56. using namespace issues171a;
  57. using namespace lynx;
  58. const auto state = peq2State();
  59. const auto settings = peqfl::parsePeq2 (state);
  60. CHECK (settings.has_value());
  61. auto slot = makeEffectSlot (EffectKind::parametricEq);
  62. peqfl::toParams (*settings, slot.params);
  63. CHECK (peq::flLaw (slot.params) && peq::orderText (slot.params, 0) == "Steep 8" && peq::orderText (slot.params, 6) == "Steep 8");
  64. // the saved count reaches past the named parameters to the law, processing, linear and mask fields
  65. CHECK (savedEffectParamCount (slot) > (std::size_t) peqfl::maskParam);
  66. const auto reloaded = savedAndReloaded (slot);
  67. CHECK (reloaded.params == slot.params); // bit for bit
  68. CHECK (peq::flLaw (reloaded.params) && peq::orderText (reloaded.params, 0) == "Steep 8");
  69. CHECK (sameSettings (peqfl::fromParams (reloaded.params), *settings)); // FL's integers, every one
  70. // the same curve before and after
  71. for (const double f : { 20.0, 32.0, 40.0, 100.0, 1000.0, 15000.0, 18000.0, 19000.0 })
  72. CHECK (peq::responseDb (reloaded.params, 48000.0, f) == peq::responseDb (slot.params, 48000.0, f));
  73. // the 4-decimal rounding of old saves lost FL's raw values (a frequency of raw/65536 moved by up to 3 steps)
  74. for (int raw = 0; raw <= 65536; raw += 7)
  75. {
  76. const auto v = (float) ((double) raw / 65536.0);
  77. CHECK ((float) savedEffectParamValue (v) == v);
  78. }
  79. // values that are already 4 decimals stay written as before
  80. CHECK (savedEffectParamValue (0.25f) == 0.25 && savedEffectParamValue (0.5f) == 0.5 && savedEffectParamValue (0.0f) == 0.0);
  81. // a user preset keeps it too (the hidden fields as "#index" lines)
  82. const auto text = presets::toText (EffectKind::parametricEq, "cut", slot.params);
  83. CHECK (text.find ("#36 = 0.5") != std::string::npos);
  84. const auto preset = presets::fromText (EffectKind::parametricEq, text);
  85. CHECK (preset && preset->params == slot.params);
  86. // a LYNX-law EQ saves no hidden fields
  87. CHECK (savedEffectParamCount (makeEffectSlot (EffectKind::parametricEq)) == effectInfo (EffectKind::parametricEq).params.size());
  88. }
  89. // #67 with FL's own preset files (when the FL installation is there): every Parametric EQ 2 / Parametric EQ preset reads
  90. // FL's integers back exactly after a save and reload
  91. TEST (issues171a_parametric_eq_fl_presets_round_trip_exactly)
  92. {
  93. using namespace issues171a;
  94. using namespace lynx;
  95. const auto cut = flEffectPreset ("Fruity Parametric EQ 2", "40Hz + 18kHz cut.fst");
  96. if (cut.empty())
  97. return; // (no FL installation)
  98. const auto slot = effectFromFlPreset (cut);
  99. CHECK (slot && slot->kind == EffectKind::parametricEq && peqfl::lawOf (slot->params) == peqfl::Law::peq2);
  100. if (! slot)
  101. return;
  102. CHECK (peq::orderText (slot->params, 0) == "Steep 8" && peq::orderText (slot->params, 6) == "Steep 8");
  103. // FL's preset itself keeps gains on bands it does not use (band 2: −1.84 dB, band 5: −5.2 dB…): the sliders show them
  104. const auto s = peqfl::fromParams (slot->params);
  105. CHECK (s.type[1] == 0 && s.gain[1] == -184 && s.gain[4] == -520 && s.gain[6] == -950 && s.order[0] == -3 && s.order[6] == -3);
  106. CHECK (s.freq[0] == 4523 && s.width[0] == 36068);
  107. CHECK (savedAndReloaded (*slot).params == slot->params);
  108. for (const char* folder : { "Fruity Parametric EQ 2", "Fruity Parametric EQ" })
  109. {
  110. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / folder;
  111. if (! std::filesystem::is_directory (dir))
  112. continue;
  113. for (const auto& e : std::filesystem::directory_iterator (dir))
  114. {
  115. if (e.path().extension() != ".fst")
  116. continue;
  117. std::ifstream in (e.path(), std::ios::binary);
  118. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  119. const auto fx = effectFromFlPreset (bytes);
  120. if (! fx || fx->kind != EffectKind::parametricEq)
  121. continue;
  122. const auto back = savedAndReloaded (*fx);
  123. CHECK (back.params == fx->params);
  124. CHECK (sameSettings (peqfl::fromParams (back.params), peqfl::fromParams (fx->params)));
  125. }
  126. }
  127. }
  128. // #67, the audit: every built-in effect, every parameter (named and hidden) at a value that is not its default, saved and
  129. // reloaded (project) and as a user preset: the same values back
  130. TEST (issues171a_every_effect_param_survives_save_and_reload)
  131. {
  132. using namespace issues171a;
  133. using namespace lynx;
  134. int presetMismatches = 0;
  135. for (int k = 0; k <= (int) lastEffectKind; ++k)
  136. {
  137. const auto kind = (EffectKind) k;
  138. if (kind == EffectKind::plugin)
  139. continue;
  140. auto slot = makeEffectSlot (kind);
  141. for (std::size_t i = 0; i < slot.params.size(); ++i)
  142. {
  143. // an odd value (not 4 decimals), away from the default
  144. auto v = (float) ((double) ((i * 7919 + (std::size_t) k * 104729) % 65521) / 65521.0);
  145. if (v == slot.params[i])
  146. v = 1.0f - v;
  147. slot.params[i] = v;
  148. }
  149. const auto back = savedAndReloaded (slot);
  150. if (back.params != slot.params)
  151. std::printf (" project round trip differs: %s\n", effectInfo (kind).name);
  152. CHECK (back.params == slot.params);
  153. // user presets: by name (hidden ones by index); Fruity Send's "Send to" is written over its insert span
  154. const auto text = presets::toText (kind, "audit", slot.params);
  155. const auto preset = presets::fromText (kind, text);
  156. CHECK (preset.has_value());
  157. if (! preset)
  158. continue;
  159. for (std::size_t i = 0; i < slot.params.size(); ++i)
  160. if (preset->params[i] != slot.params[i] && ! (kind == EffectKind::fruitySend && i == 3))
  161. {
  162. std::printf (" preset round trip differs: %s param %d (%s)\n", effectInfo (kind).name, (int) i,
  163. i < effectInfo (kind).params.size() ? effectInfo (kind).params[i].name : "hidden");
  164. ++presetMismatches;
  165. }
  166. }
  167. CHECK (presetMismatches == 0);
  168. }
  169. #include "core/RackOps.h"
  170. // #68: the Channel Rack menu's "Cut itself" and the sample panel's checkbox are one state (FL: one "Cut self", the
  171. // channel's Cut and By groups the same)
  172. TEST (issues171a_cut_itself_is_one_state)
  173. {
  174. using namespace lynx;
  175. ChannelDef c;
  176. CHECK (! channelCutsItself (c));
  177. setChannelCutsItself (c, 3, true); // the rack menu / the Misc page
  178. CHECK (channelCutsItself (c) && c.cutGroup == 1003 && c.cutBy == 1003 && c.sampler.cut); // the sample panel shows it
  179. setChannelCutsItself (c, 3, false);
  180. CHECK (! channelCutsItself (c) && c.cutGroup == 0 && c.cutBy == 0 && ! c.sampler.cut);
  181. // a project saved by the sample panel before 0.1.24 (only the sampler's flag): the rack menu shows it ticked
  182. c.sampler.cut = true;
  183. CHECK (channelCutsItself (c));
  184. // other cut groups are not "cut itself"
  185. c.sampler.cut = false;
  186. c.cutGroup = 2, c.cutBy = 5;
  187. CHECK (! channelCutsItself (c));
  188. }
  189. // #63: per-note pan (the Channel Rack's graph editor, the piano roll) on the built-in drums (the default project's Kick,
  190. // Clap, Hat and Snare): it was ignored, the drums played every note in the middle
  191. TEST (issues171a_note_pan_reaches_the_drums)
  192. {
  193. using namespace lynx;
  194. for (const auto kind : { InstrumentKind::snare, InstrumentKind::kick, InstrumentKind::hat, InstrumentKind::clap })
  195. {
  196. auto render = [kind] (float pan, float cents, std::vector<float>& l, std::vector<float>& r) {
  197. auto inst = makeInstrument (kind, nullptr);
  198. inst->prepare (48000.0);
  199. inst->setNextNoteExpression (pan, cents);
  200. inst->noteOn (60, 1.0f);
  201. l.assign (2400, 0.0f);
  202. r.assign (2400, 0.0f);
  203. inst->render (l.data(), r.data(), 2400, 1.0f, 1.0f);
  204. };
  205. auto energy = [] (const std::vector<float>& x) { double e = 0.0; for (auto v : x) e += (double) v * v; return e; };
  206. std::vector<float> l, r;
  207. render (0.0f, 0.0f, l, r);
  208. const auto centre = energy (l);
  209. CHECK (centre > 1e-3 && l == r);
  210. render (-1.0f, 0.0f, l, r); // fully left
  211. CHECK (energy (l) > centre && energy (r) < 1e-9);
  212. render (1.0f, 0.0f, l, r); // fully right
  213. CHECK (energy (r) > centre && energy (l) < 1e-9);
  214. render (0.5f, 0.0f, l, r);
  215. CHECK (energy (r) > energy (l) && energy (l) > 1e-6);
  216. // the next note without an expression is centred again
  217. auto inst = makeInstrument (kind, nullptr);
  218. inst->prepare (48000.0);
  219. inst->setNextNoteExpression (-1.0f, 0.0f);
  220. inst->noteOn (60, 1.0f);
  221. inst->noteOn (60, 1.0f);
  222. std::vector<float> l2 (64), r2 (64);
  223. inst->render (l2.data(), r2.data(), 64, 1.0f, 1.0f);
  224. CHECK (energy (r2) > 0.0);
  225. }
  226. // fine pitch moves the kick's tone
  227. std::vector<float> a, b, l, r;
  228. auto kick = [] (float cents, std::vector<float>& out) {
  229. auto inst = makeInstrument (InstrumentKind::kick, nullptr);
  230. inst->prepare (48000.0);
  231. inst->setNextNoteExpression (0.0f, cents);
  232. inst->noteOn (60, 1.0f);
  233. std::vector<float> r (2400);
  234. out.assign (2400, 0.0f);
  235. inst->render (out.data(), r.data(), 2400, 1.0f, 1.0f);
  236. };
  237. kick (0.0f, a);
  238. kick (1200.0f, b);
  239. CHECK (a != b);
  240. // the whole way: the default project's Snare with a note panned hard left, through the rack
  241. auto song = Song::makeDefault();
  242. std::size_t snare = 0;
  243. for (std::size_t c = 0; c < song.channels.size(); ++c)
  244. if (song.channels[c].instrument == InstrumentKind::snare)
  245. snare = c;
  246. CHECK (song.channels[snare].instrument == InstrumentKind::snare);
  247. for (auto& p : song.patterns)
  248. p.notes.clear();
  249. Note n { 0, 96, (int) snare, 60 };
  250. n.pan = -1.0f;
  251. song.patterns[0].notes = { n };
  252. song.mode = PlayMode::pattern;
  253. auto state = RackState::build (song, nullptr, 48000.0);
  254. RackRenderer renderer;
  255. renderer.prepare (48000.0, 512);
  256. std::vector<float> bl (512), br (512);
  257. double el = 0.0, er = 0.0;
  258. for (int b2 = 0; b2 < 8; ++b2)
  259. {
  260. renderer.process (state.get(), true, (double) b2 * 512.0 * ticksPerSample (song.bpm, 48000.0), 512, bl.data(), br.data());
  261. for (int i = 0; i < 512; ++i)
  262. el += (double) bl[(std::size_t) i] * bl[(std::size_t) i], er += (double) br[(std::size_t) i] * br[(std::size_t) i];
  263. }
  264. std::printf (" snare panned left through the rack: L %.4g R %.4g\n", el, er);
  265. CHECK (el > 1e-3 && er < el * 1e-3);
  266. }
  267. // #66: the Peak Controller's display (Peak + LFO, Peak, LFO) scrolled flat: the audio thread never published its outputs
  268. // for the editor, and on a silent insert it was smart-disabled (its LFO stopped)
  269. TEST (issues171a_peak_controller_publishes_its_outputs)
  270. {
  271. using namespace lynx;
  272. auto song = Song::makeDefault();
  273. song.channels.clear();
  274. song.playlist.clear();
  275. for (auto& p : song.patterns)
  276. p.notes.clear();
  277. song.mixer = Song::defaultMixer (4);
  278. auto fx = makeEffectSlot (EffectKind::peakController);
  279. fx.params[4] = 0.3f; // LFO base
  280. fx.params[5] = 1.0f; // LFO amount: the most
  281. fx.params[7] = 0.0f; // the fastest speed
  282. song.mixer[2].effects.push_back (fx);
  283. auto state = RackState::build (song, nullptr, 48000.0);
  284. RackRenderer renderer;
  285. renderer.prepare (48000.0, 256);
  286. std::vector<float> l (256), r (256), loud (256, 0.8f);
  287. const auto key = ((std::size_t) 2 * maxEffectSlots + 0) * ProcessTimes::meterOutputs;
  288. auto& meters = renderer.processTimes().effectMeters;
  289. // silence for 2 s (past smart disable's hold): the LFO line still moves
  290. float lo = 1.0f, hi = 0.0f;
  291. for (int b = 0; b < 400; ++b)
  292. {
  293. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data());
  294. if (b > 200)
  295. {
  296. const auto v = meters[key + 2].load();
  297. lo = std::min (lo, v), hi = std::max (hi, v);
  298. }
  299. }
  300. std::printf (" peak controller LFO in silence: %.3f … %.3f\n", lo, hi);
  301. CHECK (hi - lo > 0.1f);
  302. // a loud input: the peak line rises
  303. const auto quiet = meters[key + 1].load();
  304. for (int b = 0; b < 8; ++b)
  305. {
  306. renderer.setMetronomeInput (loud.data(), loud.data(), 2);
  307. renderer.process (state.get(), false, 0.0, 256, l.data(), r.data());
  308. }
  309. CHECK (meters[key + 1].load() > quiet + 0.1f);
  310. }
  311. #include "core/Workspace.h"
  312. // #62: a detached Mixer is detached again after a restart, at its place and size (the layout file round trip), also from a
  313. // file written while the C locale used a decimal comma (a plugin's GTK switches it; the file then did not load at all)
  314. TEST (issues171a_detached_windows_survive_a_restart)
  315. {
  316. using namespace lynx::workspace;
  317. auto layout = Layout::makeDefault (1600, 900);
  318. for (const auto p : { Panel::mixer, Panel::channelRack, Panel::pianoRoll, Panel::playlist, Panel::browser })
  319. {
  320. layout.show (p);
  321. layout[p].detached = true;
  322. layout[p].windowBounds = { 100 + 10 * (int) p, 120, 900 + (int) p, 500 };
  323. }
  324. const auto text = layout.serialise();
  325. const auto back = Layout::parse (text, 1600, 900);
  326. CHECK (back.has_value());
  327. if (! back)
  328. return;
  329. for (const auto p : { Panel::mixer, Panel::channelRack, Panel::pianoRoll, Panel::playlist, Panel::browser })
  330. CHECK ((*back)[p].detached && (*back)[p].visible && (*back)[p].windowBounds.x == 100 + 10 * (int) p && (*back)[p].windowBounds.w == 900 + (int) p
  331. && (*back)[p].windowBounds.h == 500);
  332. // the same file with decimal commas
  333. std::string comma = text;
  334. for (std::size_t at = comma.find ("0."); at != std::string::npos; at = comma.find ("0.", at + 2))
  335. comma[at + 1] = ',';
  336. CHECK (comma != text);
  337. const auto fromComma = Layout::parse (comma, 1600, 900);
  338. CHECK (fromComma.has_value() && (*fromComma)[Panel::mixer].detached && (*fromComma)[Panel::mixer].windowBounds.x == 100 + 10 * (int) Panel::mixer);
  339. }