4
0

keplerrender.cpp 3.2 KB

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