harmorrender.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Renders a Harmor state with LYNX's engine exactly like our round-34 reference model drives it (verification
  2. // harness, REA_rodada34 §1.25): harmorrender <state.bin> <out.f32> <length> <mt.txt|-> <rate> [blk=N] [bpm=X]
  3. // [comp=<Harmor System\Compression\n.fst>] [note=start,key,velocity,finalVol,finalPan,release ...]. Writes interleaved stereo float32; mt.txt holds the
  4. // DLL's Mersenne-Twister state (index, then 624 words) or "-" for LYNX's fixed seed.
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <cstring>
  8. #include <fstream>
  9. #include <iterator>
  10. #include <string>
  11. #include <vector>
  12. #include "core/HarmlessFx.h"
  13. #include "core/Harmor.h"
  14. int main (int argc, char** argv)
  15. {
  16. if (argc < 6)
  17. {
  18. std::fprintf (stderr, "usage: harmorrender state.bin out.f32 length mt.txt|- rate [blk=N] [bpm=X] [note=s,k,v,fv,fp,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[3]);
  24. std::vector<std::uint32_t> mt;
  25. if (std::strcmp (argv[4], "-") != 0)
  26. {
  27. std::ifstream m (argv[4]);
  28. unsigned long long v = 0;
  29. while (m >> v)
  30. mt.push_back ((std::uint32_t) v);
  31. }
  32. const double rate = std::atof (argv[5]);
  33. int blk = 512;
  34. double bpm = 120.0;
  35. std::vector<lynx::harmor::RefNote> notes;
  36. std::vector<std::uint8_t> comp;
  37. for (int i = 6; i < argc; ++i)
  38. {
  39. const std::string a = argv[i];
  40. if (a.rfind ("blk=", 0) == 0)
  41. blk = std::atoi (a.c_str() + 4);
  42. else if (a.rfind ("comp=", 0) == 0)
  43. {
  44. std::ifstream f (a.substr (5), std::ios::binary);
  45. const std::vector<std::uint8_t> fst ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  46. if (auto st = lynx::hlfx::maximusStateFromFst (fst))
  47. comp = *st;
  48. }
  49. else if (a.rfind ("bpm=", 0) == 0)
  50. bpm = std::atof (a.c_str() + 4);
  51. else if (a.rfind ("note=", 0) == 0)
  52. {
  53. lynx::harmor::RefNote n;
  54. if (std::sscanf (a.c_str() + 5, "%d,%d,%lf,%lf,%lf,%d", &n.start, &n.key, &n.velocity, &n.finalVol, &n.finalPan, &n.release) >= 5)
  55. notes.push_back (n);
  56. }
  57. }
  58. const auto out = lynx::harmor::renderReference (state, notes, rate, length, blk, mt, bpm, comp);
  59. if (out.empty())
  60. {
  61. std::fprintf (stderr, "state not usable\n");
  62. return 1;
  63. }
  64. std::FILE* f = std::fopen (argv[2], "wb");
  65. if (f == nullptr)
  66. return 1;
  67. std::fwrite (out.data(), sizeof (float), out.size(), f);
  68. std::fclose (f);
  69. for (const auto& s : lynx::harmorFallbacks (state))
  70. std::printf ("fallback: %s\n", s.c_str());
  71. return 0;
  72. }