ogunrender.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Renders an Ogun state with LYNX's engine exactly like our round-37 reference model drives it (verification harness,
  2. // REA_rodada37 §1.21): ogunrender <state.bin> <out.f32> <length> <rate> [q=N] [tempo=X] [blk=N] [counter=N] [maxpoly=N]
  3. // [kills=0|1] [note=start,key,vel,fvol,fpan,pofs,fcut,fres,relvel,off ...] [j0=s0,s1,...]
  4. // Autogun (REA_rodada57): [autogun=1] [sg=<path of the installation's Autogun\Data\SG.dat>]
  5. // Output: interleaved stereo float32. Prints the render time and the fallbacks the state relies on.
  6. #include <chrono>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include <fstream>
  11. #include <iterator>
  12. #include <sstream>
  13. #include <string>
  14. #include <vector>
  15. #include "core/Ogun.h"
  16. int main (int argc, char** argv)
  17. {
  18. if (argc < 5)
  19. {
  20. std::fprintf (stderr, "usage: ogunrender state.bin out.f32 length rate [q=N] [tempo=X] [blk=N] [counter=N] [maxpoly=N] [kills=0|1] "
  21. "[note=start,key,vel,fvol,fpan,pofs,fcut,fres,relvel,off ...] [j0=s0,s1,...]\n");
  22. return 2;
  23. }
  24. std::ifstream in (argv[1], std::ios::binary);
  25. const std::vector<std::uint8_t> state ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  26. const int length = std::atoi (argv[3]);
  27. const double rate = std::atof (argv[4]);
  28. lynx::ogun::RefOptions opt;
  29. std::vector<lynx::ogun::RefNote> notes;
  30. for (int i = 5; i < argc; ++i)
  31. {
  32. const std::string a = argv[i];
  33. auto val = [&] { return a.substr (a.find ('=') + 1); };
  34. if (a.rfind ("q=", 0) == 0)
  35. opt.quality = std::atoi (val().c_str());
  36. else if (a.rfind ("tempo=", 0) == 0)
  37. opt.tempo = std::atof (val().c_str());
  38. else if (a.rfind ("blk=", 0) == 0)
  39. opt.block = std::atoi (val().c_str());
  40. else if (a.rfind ("counter=", 0) == 0)
  41. opt.counter = std::atoi (val().c_str());
  42. else if (a.rfind ("maxpoly=", 0) == 0)
  43. opt.maxPolyphony = std::atoi (val().c_str());
  44. else if (a.rfind ("autogun=", 0) == 0)
  45. opt.autogun = std::atoi (val().c_str()) != 0;
  46. else if (a.rfind ("sg=", 0) == 0)
  47. {
  48. std::ifstream sg (val(), std::ios::binary);
  49. opt.sgState.assign ((std::istreambuf_iterator<char> (sg)), std::istreambuf_iterator<char>());
  50. }
  51. else if (a.rfind ("kills=", 0) == 0)
  52. opt.hostKills = std::atoi (val().c_str()) != 0;
  53. else if (a.rfind ("j0=", 0) == 0)
  54. {
  55. std::stringstream ss (val());
  56. std::string t;
  57. while (std::getline (ss, t, ','))
  58. opt.j0Shift.push_back (std::atoi (t.c_str()));
  59. }
  60. else if (a.rfind ("note=", 0) == 0)
  61. {
  62. std::stringstream ss (val());
  63. std::string t;
  64. std::vector<double> v;
  65. while (std::getline (ss, t, ','))
  66. v.push_back (std::atof (t.c_str()));
  67. v.resize (10, 0.0);
  68. lynx::ogun::RefNote n;
  69. n.start = (int) v[0], n.key = (int) v[1], n.velocity = v[2], n.finalVol = v[3], n.finalPan = v[4];
  70. n.pitchOffset = v[5], n.fcut = v[6], n.fres = v[7], n.releaseVelocity = v[8], n.release = (int) v[9];
  71. notes.push_back (n);
  72. }
  73. }
  74. const auto t0 = std::chrono::steady_clock::now();
  75. const auto out = lynx::ogun::renderReference (state, notes, rate, length, opt);
  76. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - t0).count();
  77. if (out.empty())
  78. {
  79. std::fprintf (stderr, "state not usable\n");
  80. return 1;
  81. }
  82. std::ofstream o (argv[2], std::ios::binary);
  83. o.write ((const char*) out.data(), (std::streamsize) (out.size() * sizeof (float)));
  84. std::printf ("render_ms=%.1f\n", ms);
  85. for (const auto& s : lynx::ogunFallbacks (state, opt.autogun))
  86. std::printf ("fallback: %s\n", s.c_str());
  87. return 0;
  88. }