4
0

fxrender.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // fxrender: renders WAV files through one of LYNX's built-in effects, offline (round tp172b).
  2. //
  3. // The LYNX side of the black-box parity harness (tools/fxmeasure renders the originals): the same stimulus through
  4. // a LYNX effect at matched settings, so scripts/fxparity.py can score the pair like FL's own render tests.
  5. //
  6. // fxrender <effect name | kind index> [options] <in.wav> <out.wav> [<in.wav> <out.wav> ...]
  7. // --rate <hz> sample rate the effect is prepared at (default: the input file's)
  8. // --block <n> block size (default 512)
  9. // --ff <translator> <file> a FabFilter state (raw component or JUCE host state) translated as LYNX loads one
  10. // (core/FabFilterStates.h: pro-c3, pro-mb, pro-g, saturn2, pro-q4, pro-ds, pro-l2)
  11. // --sub <name> <file> the substitute LYNX opens for the plugin named <name> (core/PluginSubstitutes.h), built
  12. // from that plugin's saved component state <file>; the <effect> argument is then
  13. // ignored (any name, e.g. "-") and replaced by the substitute's effect
  14. // --set <i>=<v> parameter i to the normalized value v
  15. // --text <i>=<value> parameter i to the value whose text (formatEffectParam) matches: a number = the
  16. // closest numeric text, otherwise the same text (case-insensitive)
  17. // --settle <s> silence processed before each file (default 0.5 s, after a reset)
  18. // --list print the parameters (index, name, value, text) and exit
  19. // --dump <file> write the parameters ("index value" lines; LYNX --parityshot reads them)
  20. // Every input is processed from a reset; the output is a 32-bit float stereo WAV, not latency-compensated (the
  21. // latency is printed). Inputs: 32-bit float or 16 / 24-bit PCM WAV, mono or stereo.
  22. // Build (with the core, like the other tools): see scripts/build-local.sh, or
  23. // $CXX $FLAGS $CORE $WAVPACK tools/fxrender.cpp -o build-local/fxrender
  24. #include <algorithm>
  25. #include <cctype>
  26. #include <cmath>
  27. #include <cstdint>
  28. #include <cstdio>
  29. #include <cstdlib>
  30. #include <cstring>
  31. #include <fstream>
  32. #include <iterator>
  33. #include <string>
  34. #include <vector>
  35. #include "core/Effects.h"
  36. #include "core/FabFilterStates.h"
  37. #include "core/PluginSubstitutes.h"
  38. using namespace lynx;
  39. namespace
  40. {
  41. struct Audio
  42. {
  43. int rate = 48000;
  44. std::vector<float> l, r;
  45. };
  46. bool readWav (const std::string& path, Audio& a)
  47. {
  48. std::ifstream f (path, std::ios::binary);
  49. std::vector<std::uint8_t> d ((std::istreambuf_iterator<char> (f)), {});
  50. if (d.size() < 12 || std::memcmp (d.data(), "RIFF", 4) != 0)
  51. return false;
  52. int format = 0, channels = 0, bits = 0;
  53. for (std::size_t i = 12; i + 8 <= d.size();)
  54. {
  55. std::uint32_t size;
  56. std::memcpy (&size, d.data() + i + 4, 4);
  57. if (std::memcmp (d.data() + i, "fmt ", 4) == 0)
  58. {
  59. std::uint16_t fmt, ch, bps;
  60. std::uint32_t sr;
  61. std::memcpy (&fmt, d.data() + i + 8, 2);
  62. std::memcpy (&ch, d.data() + i + 10, 2);
  63. std::memcpy (&sr, d.data() + i + 12, 4);
  64. std::memcpy (&bps, d.data() + i + 22, 2);
  65. format = fmt == 0xFFFE ? (bps == 32 ? 3 : 1) : fmt;
  66. channels = ch;
  67. bits = bps;
  68. a.rate = (int) sr;
  69. }
  70. else if (std::memcmp (d.data() + i, "data", 4) == 0 && channels > 0)
  71. {
  72. const auto* p = d.data() + i + 8;
  73. const std::size_t bytes = std::min<std::size_t> (size, d.size() - i - 8);
  74. const int width = bits / 8;
  75. const std::size_t frames = bytes / (std::size_t) (width * channels);
  76. a.l.resize (frames);
  77. a.r.resize (frames);
  78. for (std::size_t n = 0; n < frames; ++n)
  79. for (int c = 0; c < std::min (channels, 2); ++c)
  80. {
  81. const auto* s = p + (n * (std::size_t) channels + (std::size_t) c) * (std::size_t) width;
  82. float v = 0.0f;
  83. if (format == 3 && width == 4)
  84. std::memcpy (&v, s, 4);
  85. else if (width == 2)
  86. v = (float) (std::int16_t) (s[0] | (s[1] << 8)) / 32768.0f;
  87. else if (width == 3)
  88. v = (float) ((std::int32_t) ((std::uint32_t) s[0] << 8 | (std::uint32_t) s[1] << 16 | (std::uint32_t) s[2] << 24) >> 8) / 8388608.0f;
  89. else if (width == 4)
  90. v = (float) ((double) (std::int32_t) (s[0] | (s[1] << 8) | (s[2] << 16) | ((std::uint32_t) s[3] << 24)) / 2147483648.0);
  91. (c == 0 ? a.l : a.r)[n] = v;
  92. }
  93. if (channels == 1)
  94. a.r = a.l;
  95. return true;
  96. }
  97. i += 8 + size + (size & 1);
  98. }
  99. return false;
  100. }
  101. bool writeWav (const std::string& path, const Audio& a)
  102. {
  103. std::ofstream f (path, std::ios::binary);
  104. const std::uint32_t frames = (std::uint32_t) a.l.size(), data = frames * 8;
  105. const auto u32 = [&] (std::uint32_t v) { f.write ((const char*) &v, 4); };
  106. const auto u16 = [&] (std::uint16_t v) { f.write ((const char*) &v, 2); };
  107. f.write ("RIFF", 4); u32 (36 + data); f.write ("WAVEfmt ", 8); u32 (16); u16 (3); u16 (2); u32 ((std::uint32_t) a.rate);
  108. u32 ((std::uint32_t) a.rate * 8); u16 (8); u16 (32); f.write ("data", 4); u32 (data);
  109. for (std::uint32_t n = 0; n < frames; ++n)
  110. {
  111. f.write ((const char*) &a.l[n], 4);
  112. f.write ((const char*) &a.r[n], 4);
  113. }
  114. return (bool) f;
  115. }
  116. std::string lower (std::string s)
  117. {
  118. for (auto& c : s)
  119. c = (char) std::tolower ((unsigned char) c);
  120. return s;
  121. }
  122. bool numericText (const std::string& t, double& v)
  123. {
  124. const char* s = t.c_str();
  125. while (*s == ' ' || *s == '+')
  126. ++s;
  127. char* end = nullptr;
  128. v = std::strtod (s, &end);
  129. if (end == s)
  130. return false;
  131. // "1.2 k" / "1.2 kHz": thousands
  132. while (*end == ' ')
  133. ++end;
  134. if (*end == 'k' || *end == 'K')
  135. v *= 1000.0;
  136. return true;
  137. }
  138. float valueForText (EffectKind kind, int param, const std::string& want, const EffectParams& context)
  139. {
  140. double target = 0.0;
  141. const bool numeric = numericText (want, target);
  142. float best = -1.0f;
  143. double bestErr = 1e300;
  144. constexpr int steps = 20000;
  145. for (int k = 0; k <= steps; ++k)
  146. {
  147. const float v = (float) k / steps;
  148. const auto text = formatEffectParam (kind, param, v, 120.0, context);
  149. double got = 0.0;
  150. if (numeric)
  151. {
  152. if (numericText (text, got))
  153. {
  154. const double err = std::abs (got - target);
  155. if (err < bestErr)
  156. bestErr = err, best = v;
  157. }
  158. }
  159. else if (lower (text) == lower (want))
  160. return v;
  161. }
  162. if (best < 0.0f)
  163. std::fprintf (stderr, "warning: no value of parameter %d reads \"%s\"\n", param, want.c_str());
  164. return std::max (best, 0.0f);
  165. }
  166. } // namespace
  167. int main (int argc, char** argv)
  168. {
  169. if (argc < 2)
  170. {
  171. std::fprintf (stderr, "usage: fxrender <effect> [--rate hz] [--block n] [--ff translator file] [--set i=v] [--text i=value] [--list] in.wav out.wav ...\n");
  172. return 2;
  173. }
  174. const std::string want = argv[1];
  175. int kindIndex = -1;
  176. for (int k = 0; k <= (int) lastEffectKind; ++k)
  177. if (lower (effectInfo ((EffectKind) k).name) == lower (want) || std::to_string (k) == want)
  178. kindIndex = k;
  179. const bool sub = std::any_of (argv + 2, argv + argc, [] (const char* a) { return std::strcmp (a, "--sub") == 0; });
  180. if (kindIndex < 0 && sub)
  181. kindIndex = 0;
  182. if (kindIndex < 0)
  183. {
  184. std::fprintf (stderr, "unknown effect: %s\n", want.c_str());
  185. return 2;
  186. }
  187. auto kind = (EffectKind) kindIndex;
  188. auto slot = makeEffectSlot (kind);
  189. double rate = 0.0, settle = 0.5;
  190. int block = 512;
  191. bool list = false;
  192. std::string dump;
  193. std::vector<std::string> files;
  194. for (int i = 2; i < argc; ++i)
  195. {
  196. const std::string a = argv[i];
  197. if (a == "--rate" && i + 1 < argc)
  198. rate = std::atof (argv[++i]);
  199. else if (a == "--block" && i + 1 < argc)
  200. block = std::max (1, std::atoi (argv[++i]));
  201. else if (a == "--settle" && i + 1 < argc)
  202. settle = std::atof (argv[++i]);
  203. else if (a == "--list")
  204. list = true;
  205. else if (a == "--dump" && i + 1 < argc)
  206. dump = argv[++i];
  207. else if (a == "--ff" && i + 2 < argc)
  208. {
  209. const auto translate = fabfilter::translatorNamed (argv[++i]);
  210. std::ifstream f (argv[++i], std::ios::binary);
  211. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (f)), {});
  212. const auto state = fabfilter::parseState (bytes);
  213. const int n = translate != nullptr && state ? translate (*state, slot) : 0;
  214. std::printf ("translated %d parameters (state %s, %u values)\n", n, state ? "read" : "not read", state ? state->count : 0u);
  215. if (n == 0) // as a substitute slot that cannot read the state: the effect's defaults
  216. std::printf ("warning: state not translated, the effect's defaults are used\n");
  217. }
  218. else if (a == "--sub" && i + 2 < argc)
  219. {
  220. PluginIdentity id;
  221. id.format = ForeignPlugin::Format::vst3;
  222. id.name = argv[++i];
  223. std::ifstream f (argv[++i], std::ios::binary);
  224. id.state.assign (std::istreambuf_iterator<char> (f), {});
  225. const auto* entry = findPluginSubstitute (id, false);
  226. if (entry == nullptr)
  227. {
  228. std::fprintf (stderr, "no substitute for: %s\n", id.name.c_str());
  229. return 2;
  230. }
  231. int n = 0;
  232. slot = makeSubstituteSlot (*entry, &id, &n);
  233. kind = slot.kind;
  234. std::printf ("substitute %s, translated %d parameters (state %zu bytes)\n", effectInfo (kind).name, n, id.state.size());
  235. }
  236. else if ((a == "--set" || a == "--text") && i + 1 < argc)
  237. {
  238. const std::string s = argv[++i];
  239. const auto eq = s.find ('=');
  240. if (eq == std::string::npos)
  241. return 2;
  242. const int p = std::atoi (s.substr (0, eq).c_str());
  243. if (p < 0 || p >= maxEffectParams)
  244. return 2;
  245. slot.params[(std::size_t) p] = a == "--set" ? (float) std::atof (s.substr (eq + 1).c_str())
  246. : valueForText (kind, p, s.substr (eq + 1), slot.params);
  247. }
  248. else
  249. files.push_back (a);
  250. }
  251. const auto& info = effectInfo (kind);
  252. if (list)
  253. {
  254. for (std::size_t p = 0; p < info.params.size(); ++p)
  255. std::printf ("%zu\t%s\t%.6f\t%s\n", p, info.params[p].name, slot.params[p],
  256. formatEffectParam (kind, (int) p, slot.params[p], 120.0, slot.params).c_str());
  257. return 0;
  258. }
  259. if (! dump.empty())
  260. if (FILE* f = std::fopen (dump.c_str(), "w"))
  261. {
  262. for (std::size_t p = 0; p < info.params.size(); ++p)
  263. std::fprintf (f, "%zu %.9g\n", p, slot.params[p]);
  264. std::fclose (f);
  265. }
  266. for (std::size_t p = 0; p < info.params.size(); ++p)
  267. if (std::abs (slot.params[p] - info.params[p].defaultValue) > 1e-6f)
  268. std::printf ("param %zu %s = %.6f (%s)\n", p, info.params[p].name, slot.params[p],
  269. formatEffectParam (kind, (int) p, slot.params[p], 120.0, slot.params).c_str());
  270. for (std::size_t f = 0; f + 1 < files.size(); f += 2)
  271. {
  272. Audio in;
  273. if (! readWav (files[f], in))
  274. {
  275. std::fprintf (stderr, "cannot read %s\n", files[f].c_str());
  276. return 1;
  277. }
  278. const double sr = rate > 0.0 ? rate : (double) in.rate;
  279. auto fx = makeEffect (slot);
  280. fx->prepare (sr);
  281. fx->reset();
  282. std::vector<float> l ((std::size_t) block), r ((std::size_t) block);
  283. for (int left = (int) (settle * sr); left > 0; left -= block)
  284. {
  285. const int n = std::min (block, left);
  286. std::fill (l.begin(), l.end(), 0.0f);
  287. std::fill (r.begin(), r.end(), 0.0f);
  288. fx->process (l.data(), r.data(), n, slot.params, 120.0);
  289. }
  290. Audio out;
  291. out.rate = (int) sr;
  292. out.l = in.l;
  293. out.r = in.r;
  294. for (std::size_t at = 0; at < out.l.size(); at += (std::size_t) block)
  295. {
  296. const int n = (int) std::min<std::size_t> ((std::size_t) block, out.l.size() - at);
  297. fx->process (out.l.data() + at, out.r.data() + at, n, slot.params, 120.0);
  298. }
  299. bool finite = true;
  300. for (std::size_t n = 0; n < out.l.size(); ++n)
  301. finite = finite && std::isfinite (out.l[n]) && std::isfinite (out.r[n]);
  302. writeWav (files[f + 1], out);
  303. std::printf ("processed %zu samples -> %s latency=%d%s\n", out.l.size(), files[f + 1].c_str(), fx->latencySamples(),
  304. finite ? "" : " NON-FINITE");
  305. }
  306. return 0;
  307. }