4
0

minisynthrender.cpp 2.4 KB

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