Jsfx190pjTests.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Round 190pj (issue #241, docs/design/JSFX.md stages 1–2): JSFX scripts' identity, file policy helpers and saved state
  2. // (core/Jsfx.h), the bundled pack embedded in the executable, and the ysfx runtime itself against upstream ysfx's
  3. // reference render of a bundled script (src/engine/JsfxReference.h, tools/jsfx_ref.cpp). The format end to end (slots,
  4. // MIDI, PDC, the file policy at run time) is in the app's --selftest (engine/JsfxFormat.cpp, jsfx::selfTest).
  5. #include <ysfx.h>
  6. #include "core/Inflate.h"
  7. #include "core/Jsfx.h"
  8. #include "engine/JsfxPackData.h"
  9. #include "engine/JsfxReference.h"
  10. TEST (jsfx190pj_paths_and_ids)
  11. {
  12. using namespace lynx::jsfx;
  13. CHECK (normalise ("C:\\Users\\me\\JSFX\\..\\JSFX\\a.jsfx") == "C:/Users/me/JSFX/a.jsfx");
  14. CHECK (normalise ("/home/me//JSFX/./Utility/") == "/home/me/JSFX/Utility");
  15. CHECK (normalise ("//server/share/x") == "//server/share/x");
  16. CHECK (isInside ("/home/me/JSFX/Utility/v.jsfx", "/home/me/JSFX", false));
  17. CHECK (! isInside ("/home/me/JSFXother/v.jsfx", "/home/me/JSFX", false));
  18. CHECK (! isInside ("/home/me/JSFX/../secret.txt", "/home/me/JSFX", false)); // (the ".." is resolved first)
  19. CHECK (isInside ("c:/users/ME/jsfx/a.jsfx", "C:\\Users\\me\\JSFX", true));
  20. CHECK (! isInside ("c:/users/ME/jsfx/a.jsfx", "C:\\Users\\me\\JSFX", false));
  21. const std::vector<std::string> roots { "/home/me/Documents/LYNX/JSFX", "/home/me/.config/LYNX/JSFX pack", "/home/me/.config/REAPER/Effects",
  22. "/home/me/.config/REAPER/Effects/Saike" };
  23. auto at = locate ("/home/me/.config/LYNX/JSFX pack/chokehold/Clipper/soft_clipper.jsfx", roots, false);
  24. CHECK (at.root == 1 && at.relative == "chokehold/Clipper/soft_clipper.jsfx");
  25. at = locate ("/home/me/.config/REAPER/Effects/Saike/Tight_Compressor.jsfx", roots, false); // (the deepest root)
  26. CHECK (at.root == 3 && at.relative == "Tight_Compressor.jsfx");
  27. CHECK (locate ("/tmp/x.jsfx", roots, false).root == -1);
  28. // the id is the relative path's: the same on every computer, case-folded, and another one for the generator listing
  29. CHECK (uniqueIdOf ("Utility/volume.jsfx", false) == uniqueIdOf ("utility\\VOLUME.jsfx", false));
  30. CHECK (uniqueIdOf ("Utility/volume.jsfx", false) != uniqueIdOf ("Utility/volume.jsfx", true));
  31. CHECK (uniqueIdOf ("Utility/volume.jsfx", false) != uniqueIdOf ("Utility/volume2.jsfx", false));
  32. CHECK (sourceHash ("abc") != sourceHash ("abd") && sourceHash ("") == 14695981039346656037ull);
  33. }
  34. TEST (jsfx190pj_headers_and_roles)
  35. {
  36. using namespace lynx::jsfx;
  37. CHECK (looksLikeJsfx ("desc:Volume\nslider1:v=0<-12,12>Gain\n"));
  38. CHECK (looksLikeJsfx ("// This effect Copyright (C) 2004 and later Cockos Incorporated\n// License: LGPL\n\ndesc: 3-Band EQ\n"));
  39. CHECK (looksLikeJsfx ("\xef\xbb\xbf" "desc:BOM first\n"));
  40. CHECK (looksLikeJsfx ("version: 1.0\nauthor: me\ndesc:Late desc\n"));
  41. CHECK (! looksLikeJsfx ("hello world\ndesc:not a header\n"));
  42. CHECK (! looksLikeJsfx ("@init\ndesc:\n"));
  43. CHECK (! looksLikeJsfx (""));
  44. CHECK (listAsGenerator (0, {}, "x.jsfx"));
  45. CHECK (listAsGenerator (2, { "utility", "synth" }, "x.jsfx"));
  46. CHECK (listAsGenerator (2, { "MIDI" }, "x.jsfx"));
  47. CHECK (listAsGenerator (2, {}, "midi/midi_transpose"));
  48. CHECK (! listAsGenerator (2, { "processing", "gain" }, "Utility/volume.jsfx"));
  49. CHECK (listAsEffect (2, 2) && listAsEffect (0, 0) && ! listAsEffect (0, 2));
  50. }
  51. TEST (jsfx190pj_saved_state)
  52. {
  53. using namespace lynx::jsfx;
  54. SavedState s;
  55. s.relative = "chokehold/Clipper/soft_clipper.jsfx";
  56. s.source = 0x0123456789abcdefull;
  57. s.sliders = { { 0, -3.5 }, { 1, 12.0 }, { 255, 1.0e-300 } };
  58. s.serialized = std::string ("\x00\x01\x02\xff", 4);
  59. const auto bytes = encodeState (s);
  60. SavedState back;
  61. CHECK (decodeState (bytes.data(), bytes.size(), back));
  62. CHECK (back.relative == s.relative && back.source == s.source && back.sliders == s.sliders && back.serialized == s.serialized);
  63. // not ours / cut short: refused, the target left as it was
  64. SavedState untouched;
  65. untouched.relative = "keep";
  66. CHECK (! decodeState ("VC2!xxxxxxxx", 12, untouched) && untouched.relative == "keep");
  67. for (std::size_t cut = 0; cut < bytes.size(); ++cut)
  68. CHECK (! decodeState (bytes.data(), cut, untouched));
  69. CHECK (untouched.relative == "keep");
  70. // a slider count that cannot fit is refused before anything is reserved
  71. auto big = bytes;
  72. const auto countAt = 4 + 4 + 4 + s.relative.size() + 8;
  73. big[countAt + 3] = (char) 0x7f;
  74. CHECK (! decodeState (big.data(), big.size(), untouched));
  75. }
  76. TEST (jsfx190pj_bundled_pack)
  77. {
  78. // every embedded file inflates to the bytes of resources/jsfx (the pack is MIT only: owner decision 2)
  79. int scripts = 0;
  80. bool same = true, licence = false;
  81. for (const auto& f : lynx::jsfxpack::files)
  82. {
  83. const auto data = lynx::inflateZlib (f.zlib, f.zlibSize);
  84. CHECK (data.has_value() && data->size() == f.size);
  85. const auto disk = i18ntest::readFile (std::string ("resources/jsfx/") + f.path);
  86. same = same && data.has_value() && disk.size() == data->size() && std::equal (disk.begin(), disk.end(), (const char*) data->data());
  87. const std::string path (f.path);
  88. scripts += path.size() > 5 && path.substr (path.size() - 5) == ".jsfx" ? 1 : 0;
  89. licence = licence || path == "chokehold/LICENSE.txt";
  90. // no @gfx scripts before LYNX draws @gfx (stage 3), and no REAPER stock effects
  91. if (data && path.find (".jsfx") != std::string::npos)
  92. {
  93. const std::string text ((const char*) data->data(), data->size());
  94. CHECK (text.find ("\n@gfx") == std::string::npos);
  95. CHECK (text.find ("Cockos Incorporated") == std::string::npos);
  96. }
  97. }
  98. CHECK (same);
  99. CHECK (licence);
  100. CHECK (scripts == 31);
  101. }
  102. // the runtime LYNX links (third_party/ysfx, with LYNX's file filter unset here) renders a bundled script as upstream
  103. // ysfx does: tools/jsfx_ref.cpp's input and slider schedule, every 61st frame against its output
  104. TEST (jsfx190pj_runtime_matches_upstream)
  105. {
  106. ysfx_config_t* config = ysfx_config_new();
  107. ysfx_set_log_reporter (config, [] (intptr_t, ysfx_log_level, const char*) {});
  108. ysfx_t* fx = ysfx_new (config);
  109. ysfx_config_free (config);
  110. const auto ok = ysfx_load_file (fx, "resources/jsfx/chokehold/Clipper/soft_clipper.jsfx", 0) && ysfx_compile (fx, ysfx_compile_no_gfx);
  111. CHECK (ok);
  112. if (! ok)
  113. {
  114. ysfx_free (fx);
  115. return;
  116. }
  117. ysfx_set_sample_rate (fx, 48000.0);
  118. ysfx_set_block_size (fx, 4096);
  119. ysfx_init (fx);
  120. ysfx_time_info_t info {};
  121. info.tempo = 120.0;
  122. info.time_signature[0] = info.time_signature[1] = 4;
  123. std::vector<float> l (512), r (512);
  124. std::uint32_t lcg = 12345u;
  125. long frame = 0;
  126. std::size_t point = 0;
  127. float worst = 0.0f;
  128. for (int b = 0; b < 64; ++b)
  129. {
  130. if (b == 32)
  131. ysfx_slider_set_value (fx, 1, 12.0, true);
  132. for (int i = 0; i < 512; ++i)
  133. {
  134. const double n = (double) (frame + i);
  135. lcg = lcg * 1664525u + 1013904223u;
  136. const double noise = (double) (lcg >> 8) / 16777216.0 * 0.2 - 0.1;
  137. l[(std::size_t) i] = (float) (0.6 * std::sin (2.0 * 3.14159265358979323846 * 220.0 * n / 48000.0) + 0.3 * std::sin (2.0 * 3.14159265358979323846 * 3317.0 * n / 48000.0));
  138. r[(std::size_t) i] = (float) (0.5 * std::sin (2.0 * 3.14159265358979323846 * 440.0 * n / 48000.0 + 0.3) + noise);
  139. }
  140. ysfx_set_time_info (fx, &info);
  141. const float* ins[2] = { l.data(), r.data() };
  142. float* outs[2] = { l.data(), r.data() };
  143. ysfx_process_float (fx, ins, outs, 2, 2, 512);
  144. for (int i = 0; i < 512; ++i, ++frame)
  145. if (frame % 61 == 0 && point < std::size (lynx::jsfxref::softClipper))
  146. {
  147. worst = std::max ({ worst, std::abs (l[(std::size_t) i] - lynx::jsfxref::softClipper[point][0]),
  148. std::abs (r[(std::size_t) i] - lynx::jsfxref::softClipper[point][1]) });
  149. ++point;
  150. }
  151. }
  152. ysfx_free (fx);
  153. CHECK (point == std::size (lynx::jsfxref::softClipper));
  154. CHECK (worst <= 1.0e-5f);
  155. std::printf (" jsfx190pj: soft_clipper against upstream ysfx, %d points, max |diff| %.3g\n", (int) point, (double) worst);
  156. }