ctlaudit.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // ctlaudit (round 170i3): which controls of the FL-native generators' LYNX editors change the sound — through a new
  2. // instrument (state), the running one (live: the editor's path) and automation (setParameter). See tools/CtlAudit.h.
  3. // Usage: ctlaudit --fl <FL Studio's Data folder> [--only <engine name>] [--all] (--all: list every control, not only
  4. // the ones that don't move)
  5. #include <cstdio>
  6. #include <filesystem>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <string>
  10. #include "CtlAudit.h"
  11. #include "WavReader.h"
  12. #include "core/FlpImport.h"
  13. #include "core/NativeEngines.h"
  14. namespace fs = std::filesystem;
  15. namespace
  16. {
  17. std::vector<std::uint8_t> readFile (const fs::path& p)
  18. {
  19. std::ifstream f (p, std::ios::binary);
  20. return { std::istreambuf_iterator<char> (f), {} };
  21. }
  22. std::string local (std::string p)
  23. {
  24. std::replace (p.begin(), p.end(), '\\', '/');
  25. return lynx::existingPathIgnoringCase (p);
  26. }
  27. lynx::natives::FlAccess flAccess (const std::string& data)
  28. {
  29. lynx::natives::FlAccess fl;
  30. fl.roots.data = fs::path (data).string();
  31. fl.roots.factoryData = fs::path (data).parent_path().string();
  32. fl.fileExists = [] (const std::string& p) { return fs::is_regular_file (local (p)); };
  33. fl.readFile = [] (const std::string& p) { return readFile (local (p)); };
  34. fl.listPresets = [] (const std::string& folder) {
  35. std::vector<std::string> out;
  36. std::error_code ec;
  37. for (const auto& e : fs::recursive_directory_iterator (local (folder), ec))
  38. if (e.is_regular_file())
  39. out.push_back (e.path().string());
  40. std::sort (out.begin(), out.end());
  41. return out;
  42. };
  43. fl.saveEmbedded = [] (const std::vector<std::uint8_t>& wav, const std::string& name) {
  44. const auto path = (fs::temp_directory_path() / ("lynx-ctlaudit-" + std::to_string (wav.size()) + "-" + std::to_string (name.size()) + ".wav")).string();
  45. std::ofstream (path, std::ios::binary).write ((const char*) wav.data(), (std::streamsize) wav.size());
  46. return path;
  47. };
  48. return fl;
  49. }
  50. const char* mark (double db, double noise) { return lynx::ctlaudit::moved (db, noise) ? " ok " : " DEAD"; }
  51. } // namespace
  52. int main (int argc, char** argv)
  53. {
  54. std::string flData, only;
  55. bool all = false;
  56. std::vector<std::pair<int, float>> sets; // --set index=raw (a context before the audit)
  57. std::vector<int> pick; // --controls i,j,… (only these)
  58. for (int i = 1; i < argc; ++i)
  59. {
  60. const std::string a = argv[i];
  61. if (a == "--fl" && i + 1 < argc)
  62. flData = argv[++i];
  63. else if (a == "--only" && i + 1 < argc)
  64. only = argv[++i];
  65. else if (a == "--all")
  66. all = true;
  67. else if (a == "--set" && i + 1 < argc)
  68. {
  69. const std::string v = argv[++i];
  70. const auto eq = v.find ('=');
  71. if (eq != std::string::npos)
  72. sets.push_back ({ std::atoi (v.substr (0, eq).c_str()), (float) std::atof (v.substr (eq + 1).c_str()) });
  73. }
  74. else if (a == "--controls" && i + 1 < argc)
  75. {
  76. std::string v = argv[++i];
  77. for (std::size_t at = 0; at < v.size();)
  78. {
  79. const auto comma = v.find (',', at);
  80. pick.push_back (std::atoi (v.substr (at, comma == std::string::npos ? std::string::npos : comma - at).c_str()));
  81. at = comma == std::string::npos ? v.size() : comma + 1;
  82. }
  83. }
  84. }
  85. const auto fl = flAccess (flData);
  86. lynx::ctlaudit::flAccess = &fl;
  87. const lynx::ctlaudit::Samples samples = [] (const std::string& p) { return lynx::tools::readWav (local (p)); };
  88. for (const auto& e : lynx::natives::instrumentEngines())
  89. {
  90. if (! only.empty() && only != e.name)
  91. continue;
  92. auto c = lynx::natives::makeChannel (e.kind, fl);
  93. // a new Granulizer has no sample (silent, as in FL): give it one of FL's loops
  94. if (c && e.kind == lynx::InstrumentKind::granulizer && c->samplePath.empty())
  95. c->samplePath = (fs::path (flData) / "Patches" / "Packs" / "Loops" / "DL Pendulus.wav").string();
  96. if (! c)
  97. {
  98. std::printf ("== %s: no channel\n", e.name);
  99. continue;
  100. }
  101. auto controls = lynx::ctlaudit::editorControls (*c);
  102. for (const auto& [index, raw] : sets)
  103. for (const auto& k : controls)
  104. if (k.index == index)
  105. k.set (*c, raw);
  106. if (! pick.empty())
  107. controls.erase (std::remove_if (controls.begin(), controls.end(),
  108. [&pick] (const lynx::ctlaudit::Control& k) { return std::find (pick.begin(), pick.end(), k.index) == pick.end(); }),
  109. controls.end());
  110. if (controls.empty())
  111. {
  112. std::printf ("== %s: no editor controls\n", e.name);
  113. continue;
  114. }
  115. const auto p = lynx::ctlaudit::patternFor (e.kind);
  116. const auto rep = lynx::ctlaudit::audit (*c, controls, p, samples);
  117. int dead = 0;
  118. for (const auto& r : rep.results)
  119. dead += ! lynx::ctlaudit::moved (r.state, rep.noise) || ! lynx::ctlaudit::moved (r.live, rep.noise)
  120. || (r.automatable && ! lynx::ctlaudit::moved (r.automation, rep.noise));
  121. int rebuilt = 0;
  122. for (const auto& r : rep.results)
  123. rebuilt += r.rebuilt;
  124. std::printf ("== %s: %zu controls, peak %.3f, noise %.0f dB, %d with a dead path, %d live edits rebuild the instrument\n", e.name,
  125. rep.results.size(), rep.basePeak, rep.noise, dead, rebuilt);
  126. for (const auto& r : rep.results)
  127. {
  128. const bool bad = ! lynx::ctlaudit::moved (r.state, rep.noise) || ! lynx::ctlaudit::moved (r.live, rep.noise)
  129. || (r.automatable && ! lynx::ctlaudit::moved (r.automation, rep.noise));
  130. if (bad || all)
  131. std::printf (" %4d %-28s %9.3f -> %9.3f state%s %6.0f live%s %6.0f auto%s %6.0f\n", r.index, r.name.c_str(), r.from, r.to,
  132. mark (r.state, rep.noise), r.state, mark (r.live, rep.noise), r.live,
  133. r.automatable ? mark (r.automation, rep.noise) : " - ", r.automation);
  134. if (r.rebuilt && (bad || all))
  135. std::printf (" (the live edit rebuilt the instrument)\n");
  136. }
  137. // the controls whose flip moved nothing: in random settings of the others (a section switched on)
  138. std::vector<int> silent;
  139. for (std::size_t i = 0; i < controls.size(); ++i)
  140. for (const auto& r : rep.results)
  141. if (r.index == controls[i].index && r.name == controls[i].name && ! lynx::ctlaudit::moved (r.state, rep.noise))
  142. silent.push_back ((int) i);
  143. if (! silent.empty())
  144. {
  145. const auto left = lynx::ctlaudit::stillDead (*c, controls, silent, p, samples);
  146. std::printf (" context pass: %zu of %zu silent controls move with other settings; never moved:", silent.size() - left.size(), silent.size());
  147. for (const auto i : left)
  148. std::printf (" %d %s;", controls[(std::size_t) i].index, controls[(std::size_t) i].name.c_str());
  149. std::printf ("\n");
  150. }
  151. std::fflush (stdout);
  152. }
  153. }