4
0

fxaudit.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // fxaudit (round 173d, issue #77): does every control of LYNX's built-in effects change the sound, and does any
  2. // setting break the output? See tools/FxAudit.h.
  3. // Usage: fxaudit [--fl <FL Studio's Data folder>] [--only <effect name>[,<name>…]] [--control <name>] [--quick]
  4. // [--segment <s>] [--tail <s>] [--contexts <n>] [--budget <n>] [--tsv <file>] [--all] [--from <n>] [--to <n>]
  5. // the regression test's settings (tests/FxAudit173dTests.inc): --quick --segment 0.25 --tail 0.15 --contexts 4 --budget 60
  6. // --all: print every control (not only the dead / dependent ones); --tsv: one line per control and per finding
  7. #include <chrono>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <filesystem>
  11. #include <fstream>
  12. #include <iterator>
  13. #include <string>
  14. #include "FxAudit.h"
  15. #include "core/FlpImport.h"
  16. #include "core/NativeEngines.h"
  17. namespace fs = std::filesystem;
  18. using namespace lynx;
  19. namespace
  20. {
  21. std::string local (std::string p)
  22. {
  23. std::replace (p.begin(), p.end(), '\\', '/');
  24. return existingPathIgnoringCase (p);
  25. }
  26. natives::FlAccess flAccess (const std::string& data)
  27. {
  28. natives::FlAccess fl;
  29. if (data.empty())
  30. return fl;
  31. fl.roots.data = fs::path (data).string();
  32. fl.roots.factoryData = fs::path (data).parent_path().string();
  33. fl.fileExists = [] (const std::string& p) { return fs::is_regular_file (local (p)); };
  34. fl.readFile = [] (const std::string& p) {
  35. std::ifstream f (local (p), std::ios::binary);
  36. return std::vector<std::uint8_t> { std::istreambuf_iterator<char> (f), {} };
  37. };
  38. fl.listPresets = [] (const std::string& folder) {
  39. std::vector<std::string> out;
  40. std::error_code ec;
  41. for (const auto& e : fs::recursive_directory_iterator (local (folder), ec))
  42. if (e.is_regular_file())
  43. out.push_back (e.path().string());
  44. std::sort (out.begin(), out.end());
  45. return out;
  46. };
  47. fl.saveEmbedded = [] (const std::vector<std::uint8_t>&, const std::string&) { return std::string(); };
  48. return fl;
  49. }
  50. std::vector<std::string> split (const std::string& s)
  51. {
  52. std::vector<std::string> out;
  53. std::size_t at = 0;
  54. while (at <= s.size())
  55. {
  56. const auto c = s.find (',', at);
  57. out.push_back (s.substr (at, c == std::string::npos ? std::string::npos : c - at));
  58. if (c == std::string::npos)
  59. break;
  60. at = c + 1;
  61. }
  62. return out;
  63. }
  64. std::string clean (std::string s)
  65. {
  66. for (auto& c : s)
  67. if (c == '\t' || c == '\n' || c == '|')
  68. c = ' ';
  69. return s;
  70. }
  71. } // namespace
  72. int main (int argc, char** argv)
  73. {
  74. std::string flData, tsvPath;
  75. std::vector<std::string> only;
  76. fxaudit::Options opt;
  77. bool all = false;
  78. double segment = 0.6, tail = 0.25;
  79. int from = 0, to = (int) lastEffectKind, shard = 0, shards = 1;
  80. for (int i = 1; i < argc; ++i)
  81. {
  82. const std::string a = argv[i];
  83. if (a == "--fl" && i + 1 < argc)
  84. flData = argv[++i];
  85. else if (a == "--only" && i + 1 < argc)
  86. only = split (argv[++i]);
  87. else if (a == "--control" && i + 1 < argc)
  88. opt.only = split (argv[++i]);
  89. else if (a == "--quick")
  90. opt.quick = true;
  91. else if (a == "--noclicks")
  92. opt.clicks = false;
  93. else if (a == "--all")
  94. all = true;
  95. else if (a == "--segment" && i + 1 < argc)
  96. segment = std::atof (argv[++i]);
  97. else if (a == "--tail" && i + 1 < argc)
  98. tail = std::atof (argv[++i]);
  99. else if (a == "--contexts" && i + 1 < argc)
  100. opt.contexts = std::atoi (argv[++i]);
  101. else if (a == "--budget" && i + 1 < argc)
  102. opt.searchBudget = std::atoi (argv[++i]);
  103. else if (a == "--tsv" && i + 1 < argc)
  104. tsvPath = argv[++i];
  105. else if (a == "--from" && i + 1 < argc)
  106. from = std::atoi (argv[++i]);
  107. else if (a == "--shard" && i + 1 < argc)
  108. {
  109. const std::string v = argv[++i];
  110. shard = std::atoi (v.c_str()), shards = std::atoi (v.substr (v.find ('/') + 1).c_str());
  111. }
  112. else if (a == "--to" && i + 1 < argc)
  113. to = std::atoi (argv[++i]);
  114. }
  115. const auto fl = flAccess (flData);
  116. const auto prog = fxaudit::programme (segment, tail);
  117. std::ofstream tsv;
  118. if (! tsvPath.empty())
  119. {
  120. tsv.open (tsvPath);
  121. tsv << "kind\teffect\tcontrol\tparam\ttype\tlayout\tstatus\tbest_db\tprerequisite\tnew_instance_only\tvalues\tkey\tbest_default_db\n";
  122. }
  123. int totalControls = 0, totalDead = 0, totalDep = 0, kinds = 0;
  124. for (int k = from; k <= to; ++k)
  125. {
  126. if (shards > 1 && k % shards != shard)
  127. continue;
  128. const auto kind = (EffectKind) k;
  129. const std::string name = effectInfo (kind).name;
  130. if (! only.empty() && std::find (only.begin(), only.end(), name) == only.end())
  131. continue;
  132. if (const char* why = fxaudit::skipReason (kind))
  133. {
  134. std::printf ("== %d %s: skipped (%s)\n", k, name.c_str(), why);
  135. if (tsv)
  136. tsv << k << '\t' << name << "\t-\t-\t-\t-\tskipped\t\t" << why << "\t\t\n";
  137. continue;
  138. }
  139. const auto slot = natives::newEffect (kind, fl);
  140. if (! slot)
  141. {
  142. std::printf ("== %d %s: skipped (needs FL Studio's data)\n", k, name.c_str());
  143. if (tsv)
  144. tsv << k << '\t' << name << "\t-\t-\t-\t-\tskipped\t\tneeds FL Studio's data\t\t\n";
  145. continue;
  146. }
  147. const auto t0 = std::chrono::steady_clock::now();
  148. const auto rep = fxaudit::audit (*slot, prog, opt);
  149. const double secs = std::chrono::duration<double> (std::chrono::steady_clock::now() - t0).count();
  150. ++kinds;
  151. totalControls += (int) rep.controls.size();
  152. totalDead += rep.dead();
  153. totalDep += rep.dependent();
  154. std::printf ("== %d %s: %zu controls (of %d), peak %.3f, noise %.0f dB, %d dead, %d dependent, %zu findings (%.1f s)\n", k, name.c_str(),
  155. rep.controls.size(), rep.listed, rep.peak, rep.noise, rep.dead(), rep.dependent(), rep.broken.size(), secs);
  156. for (const auto& c : rep.controls)
  157. {
  158. const bool show = all || c.status != fxaudit::Status::ok;
  159. std::string vals;
  160. for (const auto& ch : c.changes)
  161. {
  162. char b[200];
  163. std::snprintf (b, sizeof b, "%s%s=%.0f/%.1fdB/st%.0f", vals.empty() ? "" : "; ", clean (ch.text).c_str(), fxaudit::moveOf (ch), ch.band, ch.stereo);
  164. vals += b;
  165. }
  166. if (show)
  167. std::printf (" %4d %-30s %-10s %-4s %-12s best %6.0f dB %s%s [%s]\n", c.control.param, c.control.name.c_str(),
  168. c.control.type == fxaudit::Type::continuous ? "knob" : c.control.type == fxaudit::Type::toggle ? "toggle" : "choice",
  169. c.control.inLayout ? "ui" : "hid", fxaudit::statusText (c.status), c.best, c.prerequisite.c_str(),
  170. c.onlyNewInstance ? " (moves only in a new instance)" : "", vals.c_str());
  171. if (tsv)
  172. tsv << k << '\t' << name << '\t' << clean (c.control.name) << '\t' << c.control.param << '\t'
  173. << (c.control.type == fxaudit::Type::continuous ? "knob" : c.control.type == fxaudit::Type::toggle ? "toggle" : "choice") << '\t'
  174. << (c.control.inLayout ? "ui" : "hidden") << '\t' << fxaudit::statusText (c.status) << '\t' << (int) std::lround (c.best) << '\t'
  175. << clean (c.prerequisite) << '\t' << (c.onlyNewInstance ? "yes" : "") << '\t' << vals << '\t' << c.prereqKey << '\t' << (int) std::lround (c.status == fxaudit::Status::dependent && ! c.prereqKey.empty() ? c.bestDefault : c.best) << '\n';
  176. }
  177. for (const auto& f : rep.broken)
  178. {
  179. std::printf (" BROKEN %-6s %s = %s (%.3g)\n", f.what.c_str(), f.control.c_str(), f.text.c_str(), f.value);
  180. if (tsv)
  181. tsv << k << '\t' << name << '\t' << clean (f.control) << "\t\t\t\tBROKEN-" << f.what << '\t' << f.value << '\t' << clean (f.text) << "\t\t\n";
  182. }
  183. std::fflush (stdout);
  184. }
  185. std::printf ("\n%d effects, %d controls: %d dead, %d dependent\n", kinds, totalControls, totalDead, totalDep);
  186. }