gmsrender.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Renders a GMS state with LYNX's engine exactly like our round-39 reference model drives it (verification harness,
  2. // REA_rodada39 §3.1): gmsrender <state.bin> <out.f32> <length> <rate> <wavesDir> [blk=N] [bpm=X] [maxpoly=N]
  3. // [songpos=X] [nofx] [note=start,key,initVol,finalVol,finalPan,pitchCents,release ...]. Writes interleaved stereo float32.
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <fstream>
  7. #include <iterator>
  8. #include <string>
  9. #include <vector>
  10. #include "core/Gms.h"
  11. int main (int argc, char** argv)
  12. {
  13. if (argc < 6)
  14. {
  15. std::fprintf (stderr, "usage: gmsrender state.bin out.f32 length rate wavesDir [blk=N] [bpm=X] [maxpoly=N] [songpos=X] [nofx] [note=s,k,iv,fv,fp,pc,off ...]\n");
  16. return 2;
  17. }
  18. std::ifstream in (argv[1], std::ios::binary);
  19. const std::vector<std::uint8_t> state ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  20. const int length = std::atoi (argv[3]);
  21. const double rate = std::atof (argv[4]);
  22. const std::string waves = argv[5];
  23. int blk = 512, maxPoly = 0;
  24. double bpm = 140.0, songPos = -1.0;
  25. bool fx = true;
  26. std::vector<lynx::gms::RefNote> notes;
  27. for (int i = 6; i < argc; ++i)
  28. {
  29. const std::string a = argv[i];
  30. if (a.rfind ("blk=", 0) == 0)
  31. blk = std::atoi (a.c_str() + 4);
  32. else if (a.rfind ("bpm=", 0) == 0)
  33. bpm = std::atof (a.c_str() + 4);
  34. else if (a.rfind ("maxpoly=", 0) == 0)
  35. maxPoly = std::atoi (a.c_str() + 8);
  36. else if (a.rfind ("songpos=", 0) == 0)
  37. songPos = std::atof (a.c_str() + 8);
  38. else if (a == "nofx")
  39. fx = false;
  40. else if (a.rfind ("note=", 0) == 0)
  41. {
  42. lynx::gms::RefNote n;
  43. if (std::sscanf (a.c_str() + 5, "%d,%d,%lf,%lf,%lf,%lf,%d", &n.start, &n.key, &n.initVol, &n.finalVol, &n.finalPan, &n.pitchCents,
  44. &n.release)
  45. >= 6)
  46. notes.push_back (n);
  47. }
  48. }
  49. const auto out = lynx::gms::renderReference (state, notes, rate, length, waves, blk, bpm, maxPoly, songPos, fx);
  50. if (out.empty())
  51. {
  52. std::fprintf (stderr, "state not usable\n");
  53. return 1;
  54. }
  55. std::FILE* f = std::fopen (argv[2], "wb");
  56. if (f == nullptr)
  57. return 1;
  58. std::fwrite (out.data(), sizeof (float), out.size(), f);
  59. std::fclose (f);
  60. return 0;
  61. }