4
0

tbassrender.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Renders a Transistor Bass state with LYNX's engine the way the REA round-57 EXEC harness drives FL's engine DLL
  2. // (verification, REA_rodada57 §2.11): tbassrender <state.bin> <engine_x64.dll> <out.f32> <length> <rate> [tempo=X] [blk=N]
  3. // [note=start,key,initVol,finalPan,pitchCents,off[,fcut,fres,channel] ...] (off = −1: held). Output: interleaved stereo
  4. // float32.
  5. #include <chrono>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <fstream>
  9. #include <iterator>
  10. #include <sstream>
  11. #include <string>
  12. #include <vector>
  13. #include "core/TransistorBass.h"
  14. int main (int argc, char** argv)
  15. {
  16. if (argc < 6)
  17. {
  18. std::fprintf (stderr, "usage: tbassrender state.bin engine_x64.dll out.f32 length rate [tempo=X] [blk=N] "
  19. "[note=start,key,initVol,finalPan,pitchCents,off[,fcut,fres,channel] ...]\n");
  20. return 2;
  21. }
  22. std::ifstream in (argv[1], std::ios::binary);
  23. const std::vector<std::uint8_t> state ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  24. const int length = std::atoi (argv[4]);
  25. const double rate = std::atof (argv[5]);
  26. lynx::tbass::RefOptions opt;
  27. std::vector<lynx::tbass::RefNote> notes;
  28. for (int i = 6; i < argc; ++i)
  29. {
  30. const std::string a = argv[i];
  31. auto val = [&] { return a.substr (a.find ('=') + 1); };
  32. if (a.rfind ("tempo=", 0) == 0)
  33. opt.tempo = std::atof (val().c_str());
  34. else if (a.rfind ("blk=", 0) == 0)
  35. opt.block = std::atoi (val().c_str());
  36. else if (a.rfind ("note=", 0) == 0)
  37. {
  38. std::stringstream ss (val());
  39. std::string t;
  40. std::vector<double> v;
  41. while (std::getline (ss, t, ','))
  42. v.push_back (std::atof (t.c_str()));
  43. if (v.size() < 6)
  44. continue;
  45. lynx::tbass::RefNote n;
  46. n.start = (int) v[0];
  47. n.key = (int) v[1];
  48. n.initVol = v[2];
  49. n.finalPan = v[3];
  50. n.pitchCents = v[4];
  51. n.release = (int) v[5];
  52. if (v.size() >= 8)
  53. n.fcut = v[6], n.fres = v[7];
  54. if (v.size() >= 9)
  55. n.channel = (int) v[8];
  56. notes.push_back (n);
  57. }
  58. }
  59. if (! lynx::transistorBassStateUsable (state))
  60. {
  61. std::fprintf (stderr, "state not usable\n");
  62. return 1;
  63. }
  64. const auto t0 = std::chrono::steady_clock::now();
  65. const auto out = lynx::tbass::renderReference (state, argv[2], notes, rate, length, opt);
  66. const auto ms = std::chrono::duration<double, std::milli> (std::chrono::steady_clock::now() - t0).count();
  67. if (out.empty())
  68. {
  69. std::fprintf (stderr, "render failed (engine DLL not usable?)\n");
  70. return 1;
  71. }
  72. std::ofstream o (argv[3], std::ios::binary);
  73. o.write (reinterpret_cast<const char*> (out.data()), (std::streamsize) (out.size() * sizeof (float)));
  74. std::printf ("rendered %d samples in %.1f ms\n", length, ms);
  75. return 0;
  76. }