4
0

fruitkickrender.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Renders a Fruit Kick state with LYNX's engine exactly like our round-43 reference model drives it (verification
  2. // harness, REA_rodada43 §1.9): fruitkickrender <state.bin> <out.f32> <length> <rate> [blk=N] [tick=first,period]
  3. // [tempo=X] [render] [note=start,key,finalVol,finalPan,pitchCents,release ...]. Writes interleaved stereo float32;
  4. // `render` = as while LYNX exports (FL's file-render waveshaper routine, REA_rodada55 §2.3).
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <string>
  10. #include <vector>
  11. #include "core/FruitKick.h"
  12. namespace lynx
  13. {
  14. static bool renderFlag = false;
  15. bool offlineRendering() noexcept { return renderFlag; } // this tool links the engine alone
  16. } // namespace lynx
  17. int main (int argc, char** argv)
  18. {
  19. if (argc < 5)
  20. {
  21. std::fprintf (stderr, "usage: fruitkickrender state.bin out.f32 length rate [blk=N] [tick=first,period] [tempo=X] [note=s,k,fv,fp,pc,off ...]\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. int blk = 512;
  29. double tempo = 140.0;
  30. std::vector<int> ticks;
  31. std::vector<lynx::fruitkick::RefNote> notes;
  32. for (int i = 5; i < argc; ++i)
  33. {
  34. const std::string a = argv[i];
  35. int first = 0, period = 0;
  36. if (a == "render")
  37. lynx::renderFlag = true;
  38. else if (a.rfind ("blk=", 0) == 0)
  39. blk = std::atoi (a.c_str() + 4);
  40. else if (a.rfind ("tempo=", 0) == 0)
  41. tempo = std::atof (a.c_str() + 6);
  42. else if (std::sscanf (a.c_str(), "tick=%d,%d", &first, &period) == 2 && period > 0)
  43. for (int t = first; t < length; t += period)
  44. ticks.push_back (t);
  45. else if (a.rfind ("note=", 0) == 0)
  46. {
  47. lynx::fruitkick::RefNote n;
  48. if (std::sscanf (a.c_str() + 5, "%d,%d,%lf,%lf,%lf,%d", &n.start, &n.key, &n.finalVol, &n.finalPan, &n.pitchCents, &n.release) >= 5)
  49. notes.push_back (n);
  50. }
  51. }
  52. const auto out = lynx::fruitkick::renderReference (state, notes, rate, length, blk, ticks, tempo);
  53. if (out.empty())
  54. {
  55. std::fprintf (stderr, "state not usable\n");
  56. return 1;
  57. }
  58. std::FILE* f = std::fopen (argv[2], "wb");
  59. if (f == nullptr)
  60. return 1;
  61. std::fwrite (out.data(), sizeof (float), out.size(), f);
  62. std::fclose (f);
  63. return 0;
  64. }