granulizerrender.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Renders a Fruity Granulizer state with LYNX's engine exactly like the round-67 EXEC harness drives the DLL
  2. // (verification harness, REA_rodada67 §4): granulizerrender <state.bin> <sample.raw> <channels> <format 0=int16 1=float>
  3. // <sample rate> <out.f32> <length> <rate> [blk=N] [seed=N] [tempo=BPM] [beats=N] [slices=auto|<file of starts>]
  4. // [note=start,key,finalVol,finalPan,pitchCents,release ...]
  5. // The state is normalised first; the raw sample is interleaved frames. Writes interleaved stereo float32.
  6. #include <cstdio>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <fstream>
  10. #include <iterator>
  11. #include <string>
  12. #include <vector>
  13. #include "core/FlAutoSlicer.h"
  14. #include "core/Granulizer.h"
  15. int main (int argc, char** argv)
  16. {
  17. if (argc < 9)
  18. {
  19. std::fprintf (stderr, "usage: granulizerrender state.bin sample.raw ch fmt srate out.f32 length rate [blk=N] [seed=N] [note=s,k,fv,fp,pc,off ...]\n");
  20. return 2;
  21. }
  22. std::ifstream in (argv[1], std::ios::binary);
  23. const std::vector<std::uint8_t> raw ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  24. const auto state = lynx::granulizerNormalisedState (raw, {});
  25. std::ifstream sin (argv[2], std::ios::binary);
  26. const std::vector<char> bytes ((std::istreambuf_iterator<char> (sin)), std::istreambuf_iterator<char>());
  27. const int ch = std::atoi (argv[3]), fmt = std::atoi (argv[4]);
  28. lynx::SampleData smp;
  29. smp.sampleRate = std::atof (argv[5]);
  30. const auto bpf = (std::size_t) ch * (fmt != 0 ? 4 : 2);
  31. const auto frames = bytes.size() / bpf;
  32. for (std::size_t i = 0; i < frames; ++i)
  33. for (int c = 0; c < ch; ++c)
  34. {
  35. float v;
  36. if (fmt != 0)
  37. std::memcpy (&v, bytes.data() + i * bpf + 4 * (std::size_t) c, 4);
  38. else
  39. {
  40. std::int16_t s;
  41. std::memcpy (&s, bytes.data() + i * bpf + 2 * (std::size_t) c, 2);
  42. v = (float) s * 3.0517578125e-05f;
  43. }
  44. (c == 0 ? smp.left : smp.right).push_back (v);
  45. }
  46. const int length = std::atoi (argv[7]);
  47. const double rate = std::atof (argv[8]);
  48. lynx::granulizer::RefOptions opt;
  49. std::vector<lynx::granulizer::RefNote> notes;
  50. for (int i = 9; i < argc; ++i)
  51. {
  52. const std::string a = argv[i];
  53. if (a.rfind ("blk=", 0) == 0)
  54. opt.block = std::atoi (a.c_str() + 4);
  55. else if (a.rfind ("seed=", 0) == 0)
  56. opt.seed = (std::uint32_t) std::strtoul (a.c_str() + 5, nullptr, 10);
  57. else if (a.rfind ("tempo=", 0) == 0)
  58. opt.tempo = std::atof (a.c_str() + 6);
  59. else if (a.rfind ("points=", 0) == 0)
  60. opt.points = std::atoi (a.c_str() + 7);
  61. else if (a.rfind ("beats=", 0) == 0)
  62. opt.beats = (float) std::atof (a.c_str() + 6);
  63. else if (a == "slices=auto")
  64. opt.slices = lynx::flslice::autoSlice (smp.left.data(), ch > 1 ? smp.right.data() : nullptr, (std::int64_t) frames, smp.sampleRate);
  65. else if (a.rfind ("slices=", 0) == 0)
  66. {
  67. if (std::FILE* sf = std::fopen (a.c_str() + 7, "r"))
  68. {
  69. long long v;
  70. while (std::fscanf (sf, "%lld", &v) == 1)
  71. opt.slices.push_back (v);
  72. std::fclose (sf);
  73. }
  74. }
  75. else if (a.rfind ("note=", 0) == 0)
  76. {
  77. lynx::granulizer::RefNote n;
  78. if (std::sscanf (a.c_str() + 5, "%d,%d,%lf,%lf,%lf,%d", &n.start, &n.key, &n.finalVol, &n.finalPan, &n.pitchOffset, &n.release) == 6)
  79. notes.push_back (n);
  80. }
  81. }
  82. const auto out = lynx::granulizer::renderReference (state, smp, notes, rate, length, opt);
  83. if (out.empty())
  84. {
  85. std::fprintf (stderr, "state not usable\n");
  86. return 1;
  87. }
  88. std::FILE* f = std::fopen (argv[6], "wb");
  89. if (f == nullptr)
  90. return 1;
  91. std::fwrite (out.data(), sizeof (float), out.size(), f);
  92. std::fclose (f);
  93. return 0;
  94. }