keplerrender.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Renders a Kepler state with LYNX's engine the way the REA round-46 EXEC harness drives FL's engine DLL (verification,
  2. // REA_rodada46 §6): keplerrender <state.bin> <engine_x64.dll> <out.f32> <length> <rate> [tempo=X] [blk=N]
  3. // [note=start,key,initVol,finalPan,pitchCents,off ...] (off = −1: held). Output: interleaved stereo float32.
  4. #include <chrono>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <sstream>
  10. #include <string>
  11. #include <vector>
  12. #include "core/Kepler.h"
  13. int main (int argc, char** argv)
  14. {
  15. if (argc < 6)
  16. {
  17. std::fprintf (stderr, "usage: keplerrender state.bin engine_x64.dll out.f32 length rate [tempo=X] [blk=N] "
  18. "[note=start,key,initVol,finalPan,pitchCents,off ...]\n");
  19. return 2;
  20. }
  21. std::ifstream in (argv[1], std::ios::binary);
  22. const std::vector<std::uint8_t> state ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  23. const int length = std::atoi (argv[4]);
  24. const double rate = std::atof (argv[5]);
  25. lynx::kepler::RefOptions opt;
  26. std::vector<lynx::kepler::RefNote> notes;
  27. for (int i = 6; i < argc; ++i)
  28. {
  29. const std::string a = argv[i];
  30. auto val = [&] { return a.substr (a.find ('=') + 1); };
  31. if (a.rfind ("tempo=", 0) == 0)
  32. opt.tempo = std::atof (val().c_str());
  33. else if (a.rfind ("blk=", 0) == 0)
  34. opt.block = std::atoi (val().c_str());
  35. else if (a.rfind ("note=", 0) == 0)
  36. {
  37. std::stringstream ss (val());
  38. std::string t;
  39. std::vector<double> v;
  40. while (std::getline (ss, t, ','))
  41. v.push_back (std::atof (t.c_str()));
  42. if (v.size() < 6)
  43. continue;
  44. lynx::kepler::RefNote n;
  45. n.start = (int) v[0];
  46. n.key = (int) v[1];
  47. n.initVol = v[2];
  48. n.finalPan = v[3];
  49. n.pitchCents = v[4];
  50. n.release = (int) v[5];
  51. notes.push_back (n);
  52. }
  53. }
  54. if (! lynx::keplerStateUsable (state))
  55. {
  56. std::fprintf (stderr, "state not usable\n");
  57. return 1;
  58. }
  59. const auto t0 = std::chrono::steady_clock::now();
  60. const auto out = lynx::kepler::renderReference (state, argv[2], notes, rate, length, opt);
  61. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - t0).count();
  62. if (out.empty())
  63. {
  64. std::fprintf (stderr, "render failed (engine DLL not usable?)\n");
  65. return 1;
  66. }
  67. std::ofstream o (argv[3], std::ios::binary);
  68. o.write (reinterpret_cast<const char*> (out.data()), (std::streamsize) (out.size() * sizeof (float)));
  69. std::printf ("rendered %d samples in %.1f ms\n", length, ms);
  70. return 0;
  71. }