ogunrender.cpp 3.4 KB

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