4
0

distructorrender.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Renders an interleaved float32 stereo file through LYNX's Distructor engine (core/DistructorFx), the way FL calls
  2. // the plugin: state load, sample rate, then blocks. Used to compare against the installation's plugin (REA round 71).
  3. // usage: distructorrender <state.bin> <in.f32> <out.f32> [rate] [block] [module|plugin] [impulse folder]
  4. // module: the state is a single Module_x64 state block instead of a whole Distructor state
  5. // impulse folder: the installation's Plugins/Fruity/Effects/Distructor/module/impulses
  6. #include "core/DistructorFx.h"
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include <fstream>
  11. #include <iterator>
  12. #include <vector>
  13. int main (int argc, char** argv)
  14. {
  15. if (argc < 4)
  16. {
  17. std::fprintf (stderr, "usage: distructorrender <state.bin> <in.f32> <out.f32> [rate] [block] [module]\n");
  18. return 2;
  19. }
  20. std::ifstream sf (argv[1], std::ios::binary);
  21. std::vector<std::uint8_t> st ((std::istreambuf_iterator<char> (sf)), {});
  22. std::ifstream inf (argv[2], std::ios::binary);
  23. std::vector<char> raw ((std::istreambuf_iterator<char> (inf)), {});
  24. std::vector<float> x (raw.size() / 4);
  25. std::memcpy (x.data(), raw.data(), x.size() * 4);
  26. const double rate = argc > 4 ? std::atof (argv[4]) : 44100.0;
  27. const int blk = argc > 5 ? std::atoi (argv[5]) : 512;
  28. const bool module = argc > 6 && std::strcmp (argv[6], "module") == 0;
  29. if (argc > 7)
  30. lynx::distructor::setImpulseFolder (argv[7]);
  31. lynx::distructor::State s;
  32. if (module)
  33. {
  34. auto m = lynx::distructor::parseModule (st.data(), st.size());
  35. if (! m)
  36. return 3;
  37. s.modules.push_back (*m);
  38. s.clip = false;
  39. }
  40. else
  41. {
  42. auto p = lynx::distructor::parse (st);
  43. if (! p)
  44. return 3;
  45. s = *p;
  46. }
  47. lynx::distructor::Engine e;
  48. // like the EXEC harness: the state is loaded before the host sets the rate (at 44100)
  49. e.prepare (rate, blk, std::getenv ("DISTRUCTOR_STATE_RATE") ? std::atof (std::getenv ("DISTRUCTOR_STATE_RATE")) : 0.0);
  50. e.set (s);
  51. const auto n = (int) (x.size() / 2);
  52. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  53. for (int i = 0; i < n; ++i)
  54. l[(std::size_t) i] = x[(std::size_t) (2 * i)], r[(std::size_t) i] = x[(std::size_t) (2 * i + 1)];
  55. for (int i = 0; i < n; i += blk)
  56. e.process (l.data() + i, r.data() + i, std::min (blk, n - i));
  57. for (int i = 0; i < n; ++i)
  58. x[(std::size_t) (2 * i)] = l[(std::size_t) i], x[(std::size_t) (2 * i + 1)] = r[(std::size_t) i];
  59. std::ofstream of (argv[3], std::ios::binary);
  60. of.write ((const char*) x.data(), (std::streamsize) (x.size() * 4));
  61. }