ctlwake.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // ctlwake (round 173d2): every editor control of the FL-native generators, and for the ones the plain chord pattern
  2. // doesn't move, the context that wakes them (tools/CtlWake.h). Usage:
  3. // ctlwake --fl <FL Data> [--only <engine>] [--tsv <out>] [--known <tsv>] [--known-only] [--controls a,b]
  4. #include <cstdio>
  5. #include <filesystem>
  6. #include <fstream>
  7. #include <iterator>
  8. #include <string>
  9. #include <chrono>
  10. #include <map>
  11. #include <set>
  12. #include "CtlWake.h"
  13. #include "WavReader.h"
  14. #include "core/FlpImport.h"
  15. #include "core/NativeEngines.h"
  16. namespace fs = std::filesystem;
  17. namespace
  18. {
  19. std::vector<std::uint8_t> readFile (const fs::path& p)
  20. {
  21. std::ifstream f (p, std::ios::binary);
  22. return { std::istreambuf_iterator<char> (f), {} };
  23. }
  24. std::string local (std::string p)
  25. {
  26. std::replace (p.begin(), p.end(), '\\', '/');
  27. return lynx::existingPathIgnoringCase (p);
  28. }
  29. lynx::natives::FlAccess flAccess (const std::string& data)
  30. {
  31. lynx::natives::FlAccess fl;
  32. fl.roots.data = fs::path (data).string();
  33. fl.roots.factoryData = fs::path (data).parent_path().string();
  34. fl.fileExists = [] (const std::string& p) { return fs::is_regular_file (local (p)); };
  35. fl.readFile = [] (const std::string& p) { return readFile (local (p)); };
  36. fl.listPresets = [] (const std::string& folder) {
  37. std::vector<std::string> out;
  38. std::error_code ec;
  39. for (const auto& e : fs::recursive_directory_iterator (local (folder), ec))
  40. if (e.is_regular_file())
  41. out.push_back (e.path().string());
  42. std::sort (out.begin(), out.end());
  43. return out;
  44. };
  45. fl.saveEmbedded = [] (const std::vector<std::uint8_t>& wav, const std::string& name) {
  46. const auto path = (fs::temp_directory_path() / ("lynx-ctlaudit-" + std::to_string (wav.size()) + "-" + std::to_string (name.size()) + ".wav")).string();
  47. std::ofstream (path, std::ios::binary).write ((const char*) wav.data(), (std::streamsize) wav.size());
  48. return path;
  49. };
  50. return fl;
  51. }
  52. } // namespace
  53. int main (int argc, char** argv)
  54. {
  55. std::string flData, only, tsvPath, knownPath;
  56. bool knownOnly = false, unreadable = false, listHints = false; // --hints: each engine's hints (engine, id, note, pattern) // --unreadable: list the controls the new channel's state can't read or write
  57. double budget = 0.0; // seconds of search per engine (0 = none)
  58. std::vector<std::string> pick;
  59. for (int i = 1; i < argc; ++i)
  60. {
  61. const std::string a = argv[i];
  62. if (a == "--fl" && i + 1 < argc) flData = argv[++i];
  63. else if (a == "--only" && i + 1 < argc) only = argv[++i];
  64. else if (a == "--tsv" && i + 1 < argc) tsvPath = argv[++i];
  65. else if (a == "--known" && i + 1 < argc) knownPath = argv[++i];
  66. else if (a == "--known-only") knownOnly = true;
  67. else if (a == "--controls" && i + 1 < argc) pick = lynx::ctlwake::split (argv[++i], ',');
  68. else if (a == "--budget" && i + 1 < argc) budget = std::atof (argv[++i]);
  69. else if (a == "--unreadable") unreadable = true;
  70. else if (a == "--hints") listHints = true;
  71. }
  72. // engine → control → key, from an earlier --tsv
  73. std::map<std::string, std::map<std::string, std::string>> known;
  74. if (! knownPath.empty())
  75. {
  76. std::ifstream f (knownPath);
  77. std::string line;
  78. std::getline (f, line);
  79. while (std::getline (f, line))
  80. {
  81. const auto c = lynx::ctlwake::split (line, '\t');
  82. if (c.size() >= 6 && ! c[5].empty())
  83. known[c[0]][c[2]] = c[5];
  84. }
  85. }
  86. const auto fl = flAccess (flData);
  87. lynx::ctlaudit::flAccess = &fl;
  88. const lynx::ctlaudit::Samples samples = [] (const std::string& p) { return lynx::tools::readWav (local (p)); };
  89. std::FILE* tsv = tsvPath.empty() ? nullptr : std::fopen (tsvPath.c_str(), "w");
  90. if (tsv != nullptr)
  91. std::fprintf (tsv, "engine\tindex\tcontrol\tplain_db\tstatus\tkey\tbest_db\tnote\n");
  92. int total = 0, dead = 0, dependent = 0;
  93. for (const auto& e : lynx::natives::instrumentEngines())
  94. {
  95. if (! only.empty() && only != e.name)
  96. continue;
  97. auto c = lynx::natives::makeChannel (e.kind, fl);
  98. if (c && e.kind == lynx::InstrumentKind::granulizer && c->samplePath.empty())
  99. c->samplePath = (fs::path (flData) / "Patches" / "Packs" / "Loops" / "DL Pendulus.wav").string();
  100. if (! c)
  101. {
  102. std::printf ("== %s: no channel\n", e.name);
  103. continue;
  104. }
  105. const auto controls = lynx::ctlaudit::editorControls (*c);
  106. if (controls.empty())
  107. {
  108. std::printf ("== %s: no editor controls\n", e.name);
  109. continue;
  110. }
  111. if (listHints)
  112. {
  113. std::set<std::string> ids;
  114. for (const auto& h : lynx::ctlwake::hintsFor (c->instrument, controls))
  115. {
  116. const auto id = lynx::ctlwake::hintId (h.note);
  117. std::printf ("%s\t%s\t%s\t%s%s\n", e.name, id.c_str(), h.note.c_str(), h.pattern.c_str(), ids.insert (id).second ? "" : "\tDUPLICATE");
  118. }
  119. continue;
  120. }
  121. if (unreadable)
  122. {
  123. int bad = 0;
  124. for (const auto& k : controls)
  125. {
  126. auto d = *c;
  127. const auto v = k.get (*c);
  128. const bool w = v && (k.set (d, k.low) || k.set (d, k.high));
  129. if (! v || ! w)
  130. std::printf (" %s | %s | %s\n", e.name, k.name.c_str(), ! v ? "unreadable" : "unwritable"), ++bad;
  131. }
  132. std::printf ("== %s: %zu controls, %d unreadable or unwritable\n", e.name, controls.size(), bad);
  133. continue;
  134. }
  135. std::vector<int> sub;
  136. for (int i = 0; i < (int) controls.size(); ++i)
  137. if (! pick.empty() && std::find (pick.begin(), pick.end(), controls[(std::size_t) i].name) != pick.end())
  138. sub.push_back (i);
  139. if (! pick.empty() && sub.empty())
  140. continue;
  141. const auto t0 = std::chrono::steady_clock::now();
  142. const auto it = known.find (e.name);
  143. const auto verdicts = lynx::ctlwake::wake (*c, controls, samples, it != known.end() ? it->second : std::map<std::string, std::string> {},
  144. ! knownOnly, sub, budget);
  145. int d = 0, dep = 0;
  146. for (const auto& v : verdicts)
  147. d += v.best <= lynx::ctlwake::wakeDb, dep += v.best > lynx::ctlwake::wakeDb && ! v.key.empty();
  148. total += (int) verdicts.size(), dead += d, dependent += dep;
  149. std::printf ("== %s: %zu controls, %d dependent, %d dead (%.0f s)\n", e.name, verdicts.size(), dep, d,
  150. std::chrono::duration<double> (std::chrono::steady_clock::now() - t0).count());
  151. for (const auto& v : verdicts)
  152. {
  153. const auto& k = controls[(std::size_t) v.control];
  154. const char* st = v.best <= lynx::ctlwake::wakeDb ? "DEAD" : v.key.empty() ? "ok" : "dependent-ok";
  155. if (v.best <= lynx::ctlwake::wakeDb || ! v.key.empty())
  156. std::printf (" %4d %-30s %-12s plain %5.0f best %5.0f %s\n", k.index, k.name.c_str(), st, v.plain, v.best, v.note.c_str());
  157. if (tsv != nullptr)
  158. std::fprintf (tsv, "%s\t%d\t%s\t%.1f\t%s\t%s\t%.1f\t%s\n", e.name, k.index, k.name.c_str(), v.plain, st, v.key.c_str(), v.best,
  159. v.note.c_str());
  160. }
  161. std::fflush (stdout);
  162. if (tsv != nullptr)
  163. std::fflush (tsv);
  164. }
  165. std::printf ("\n%d controls: %d dependent, %d dead\n", total, dependent, dead);
  166. if (tsv != nullptr)
  167. std::fclose (tsv);
  168. }