dwrender.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Renders a DirectWave state through LYNX's engine (src/core/DirectWave) for comparison with the DLL (REA round 72).
  2. // usage: dwrender <state.bin> <zonedump.bin|-> <out.f32> <fs> <frames> <block> [prog=<i>] [seed=<n>] [on=n,key,vel,fvol,fpan,cents] [off=n,id]
  3. // zonedump: what r72/run/dw.exe dumpzones= writes (program record, then per zone the 0x41a record and its samples)
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <memory>
  10. #include <vector>
  11. #include "core/DirectWave.h"
  12. using namespace lynx;
  13. static std::vector<std::uint8_t> readAll (const char* p)
  14. {
  15. std::ifstream f (p, std::ios::binary);
  16. return std::vector<std::uint8_t> ((std::istreambuf_iterator<char> (f)), std::istreambuf_iterator<char>());
  17. }
  18. int main (int argc, char** argv)
  19. {
  20. if (argc < 7) { std::fprintf (stderr, "usage\n"); return 2; }
  21. const auto st = dw::parseState (readAll (argv[1]));
  22. if (! st) { std::fprintf (stderr, "state not usable\n"); return 1; }
  23. int pi = 0; unsigned seed = 1;
  24. std::vector<dw::RefNote> notes;
  25. for (int i = 7; i < argc; ++i)
  26. {
  27. dw::RefNote n; int a, id;
  28. if (std::sscanf (argv[i], "prog=%d", &a) == 1) pi = a;
  29. else if (std::sscanf (argv[i], "seed=%u", &seed) == 1) {}
  30. else if (std::sscanf (argv[i], "on=%d,%d,%lf,%lf,%lf,%lf", &n.start, &n.key, &n.velocity, &n.finalVol, &n.finalPan, &n.cents) == 6) notes.push_back (n);
  31. else if (std::sscanf (argv[i], "off=%d,%d", &a, &id) == 2 && id >= 0 && id < (int) notes.size()) notes[(std::size_t) id].release = a;
  32. }
  33. const auto& prog = st->programs[(std::size_t) pi];
  34. std::vector<std::shared_ptr<const dw::ZoneSamples>> samples;
  35. if (std::strcmp (argv[2], "synth") == 0) // our own 16-bit samples (r72/py/synth.py, tests.cpp)
  36. {
  37. for (int z = 0; z < (int) prog.zones.size(); ++z)
  38. {
  39. auto s = std::make_shared<dw::ZoneSamples>();
  40. s->bits = 16; s->channels = 1; s->frames = prog.zones[(std::size_t) z].frames;
  41. std::vector<std::int16_t> v ((std::size_t) s->frames + 512, 0);
  42. for (std::int64_t j = 0; j < s->frames; ++j)
  43. {
  44. const auto a = (j * (13 + z * 7)) % 4096 - 2048, b = ((j * j + z * 31) % 997) - 498;
  45. v[(std::size_t) j + 256] = (std::int16_t) std::max<std::int64_t> (-32768, std::min<std::int64_t> (32767, a * 6 + b * 8));
  46. }
  47. s->i16.push_back (std::move (v));
  48. samples.push_back (s);
  49. }
  50. }
  51. else if (std::strcmp (argv[2], "-") != 0)
  52. {
  53. const auto zd = readAll (argv[2]);
  54. std::size_t o = 0xd60;
  55. for (const auto& z : prog.zones)
  56. {
  57. std::int32_t rec[4] {};
  58. if (o + 16 <= zd.size()) std::memcpy (rec, zd.data() + o, 16);
  59. o += 0x41a;
  60. auto s = std::make_shared<dw::ZoneSamples>();
  61. s->bits = rec[3]; // the loaded file's format, not the state's
  62. s->frames = rec[0];
  63. s->channels = rec[1];
  64. const auto n = (std::size_t) rec[0] + 512;
  65. for (int c = 0; c < rec[1]; ++c)
  66. {
  67. if (s->bits == 32)
  68. {
  69. std::vector<float> v (n);
  70. if (o + 4 * n <= zd.size()) std::memcpy (v.data(), zd.data() + o, 4 * n);
  71. o += 4 * n;
  72. s->f32.push_back (std::move (v));
  73. }
  74. else
  75. {
  76. std::vector<std::int16_t> v (n);
  77. if (o + 2 * n <= zd.size()) std::memcpy (v.data(), zd.data() + o, 2 * n);
  78. o += 2 * n;
  79. s->i16.push_back (std::move (v));
  80. }
  81. }
  82. samples.push_back (s);
  83. }
  84. }
  85. dw::EngineOptions opt;
  86. opt.velocityAsInitVol = (st->flags & 2) != 0;
  87. opt.noGainClamp = (st->flags & 1) != 0;
  88. const auto out = dw::renderReference (prog, samples, opt, notes, std::atof (argv[4]), std::atoi (argv[5]), std::atoi (argv[6]), seed);
  89. std::FILE* f = std::fopen (argv[3], "wb");
  90. std::fwrite (out.data(), 4, out.size(), f);
  91. std::fclose (f);
  92. return 0;
  93. }