harmorrender.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 ...] [p=sample,k,raw ...]. 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 <cctype>
  6. #include <cstdio>
  7. #include <unistd.h>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include <fstream>
  11. #include <iterator>
  12. #include <string>
  13. #include <vector>
  14. #include "core/HarmlessFx.h"
  15. #include "core/Harmor.h"
  16. #include "core/HarmorResynth.h"
  17. // resynthesis samples (round natives190kj): FL's path variables and Windows drives mapped to this machine's mounts
  18. // (HR_FLDATA, default /mnt/c/Program Files/Image-Line/FL Studio 2026/Data); non-WAV files decoded with ffmpeg
  19. static bool loadSample (const std::string& saved, lynx::harmor::SampleData& out)
  20. {
  21. std::string p = saved;
  22. const std::string var = "%FLStudioData%", fac = "%FLStudioFactoryData%";
  23. if (p.compare (0, var.size(), var) == 0)
  24. {
  25. const char* e = std::getenv ("HR_FLDATA");
  26. p = std::string (e != nullptr ? e : "/mnt/c/Program Files/Image-Line/FL Studio 2026/Data") + p.substr (var.size());
  27. }
  28. else if (p.compare (0, fac.size(), fac) == 0)
  29. p = std::string ("/mnt/c/Program Files/Image-Line/FL Studio 2026") + p.substr (fac.size());
  30. else if (p.size() > 2 && p[1] == ':')
  31. p = std::string ("/mnt/") + (char) std::tolower ((unsigned char) p[0]) + p.substr (2);
  32. for (auto& c : p)
  33. if (c == '\\')
  34. c = '/';
  35. const auto dot = p.find_last_of ('.');
  36. const auto ext = dot == std::string::npos ? std::string() : p.substr (dot + 1);
  37. // WAV, FL's Ogg-in-WAV, Ogg Vorbis and WavPack are decoded by the core reader (FL's own Ogg decoding); others by ffmpeg
  38. if (ext == "wav" || ext == "WAV" || ext == "ogg" || ext == "OGG" || ext == "wv" || ext == "WV")
  39. return lynx::harmor::readWavMono (p, out);
  40. const std::string tmp = "/tmp/claude-1000/hr_dec_" + std::to_string ((long long) getpid()) + ".wav";
  41. const std::string cmd = "ffmpeg -v quiet -y -i \"" + p + "\" -c:a pcm_f32le \"" + tmp + "\"";
  42. if (std::system (cmd.c_str()) != 0)
  43. return false;
  44. const bool ok = lynx::harmor::readWavMono (tmp, out);
  45. std::remove (tmp.c_str());
  46. return ok;
  47. }
  48. int main (int argc, char** argv)
  49. {
  50. if (argc < 6)
  51. {
  52. 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");
  53. return 2;
  54. }
  55. std::ifstream in (argv[1], std::ios::binary);
  56. const std::vector<std::uint8_t> state ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  57. const int length = std::atoi (argv[3]);
  58. std::vector<std::uint32_t> mt;
  59. if (std::strcmp (argv[4], "-") != 0)
  60. {
  61. std::ifstream m (argv[4]);
  62. unsigned long long v = 0;
  63. while (m >> v)
  64. mt.push_back ((std::uint32_t) v);
  65. }
  66. const double rate = std::atof (argv[5]);
  67. lynx::harmor::setSampleLoader (loadSample);
  68. int blk = 512, rev = 1;
  69. double bpm = 120.0;
  70. std::vector<lynx::harmor::RefNote> notes;
  71. std::vector<lynx::harmor::RefParam> params; // round 190pr: p=sample,k,raw (FL's parameter k set to raw, as hm.exe's p=)
  72. std::vector<std::uint8_t> comp;
  73. for (int i = 6; i < argc; ++i)
  74. {
  75. const std::string a = argv[i];
  76. if (a.rfind ("rev=", 0) == 0)
  77. rev = std::atoi (a.c_str() + 4);
  78. else if (a.rfind ("blk=", 0) == 0)
  79. blk = std::atoi (a.c_str() + 4);
  80. else if (a.rfind ("comp=", 0) == 0)
  81. {
  82. std::ifstream f (a.substr (5), std::ios::binary);
  83. const std::vector<std::uint8_t> fst ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  84. if (auto st = lynx::hlfx::maximusStateFromFst (fst))
  85. comp = *st;
  86. }
  87. else if (a.rfind ("bpm=", 0) == 0)
  88. bpm = std::atof (a.c_str() + 4);
  89. else if (lynx::harmor::RefParam pr; a.rfind ("p=", 0) == 0 && std::sscanf (a.c_str() + 2, "%d,%d,%d", &pr.at, &pr.k, &pr.raw) == 3)
  90. params.push_back (pr);
  91. else if (a.rfind ("note=", 0) == 0)
  92. {
  93. lynx::harmor::RefNote n;
  94. 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)
  95. notes.push_back (n);
  96. }
  97. }
  98. const auto out = lynx::harmor::renderReference (state, notes, rate, length, blk, mt, bpm, comp, rev, params);
  99. if (out.empty())
  100. {
  101. std::fprintf (stderr, "state not usable\n");
  102. return 1;
  103. }
  104. std::FILE* f = std::fopen (argv[2], "wb");
  105. if (f == nullptr)
  106. return 1;
  107. std::fwrite (out.data(), sizeof (float), out.size(), f);
  108. std::fclose (f);
  109. for (const auto& s : lynx::harmorFallbacks (state))
  110. std::printf ("fallback: %s\n", s.c_str());
  111. return 0;
  112. }