4
0

flkeysrender.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // flkeysrender: renders FL Keys notes with LYNX's port (core/FlKeys), for comparison with the installation's plugin
  2. // (REA_rodada69 §7). Reads the wavetable from FL's folder: raw files and 16-bit PCM WAVs (not Ogg-compressed ones).
  3. // Usage: flkeysrender <Wavtables folder> <state.bin> <out.f32> <rate> <length> <block> [law=x87] [dump=<file>]
  4. // events: on3=<start>,<key>,<init vol>,<final vol>,<final pan>,<cents> off=<sample>,<note index>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <fstream>
  8. #include <iterator>
  9. #include <string>
  10. #include <vector>
  11. #include "core/FlKeys.h"
  12. using namespace lynx;
  13. static std::vector<std::uint8_t> readAll (const std::string& path)
  14. {
  15. std::ifstream f (path, std::ios::binary);
  16. return { std::istreambuf_iterator<char> (f), {} };
  17. }
  18. static std::string unixPath (std::string p)
  19. {
  20. for (auto& c : p)
  21. if (c == '\\')
  22. c = '/';
  23. return p;
  24. }
  25. // 16-bit PCM WAV → interleaved samples
  26. static flkeys::FileSamples pcm (const std::vector<std::uint8_t>& w)
  27. {
  28. flkeys::FileSamples f;
  29. std::uint16_t format = 0, channels = 1, bits = 0;
  30. for (std::size_t pos = 12; pos + 8 <= w.size();)
  31. {
  32. std::uint32_t size = 0;
  33. std::memcpy (&size, w.data() + pos + 4, 4);
  34. if (std::memcmp (w.data() + pos, "fmt ", 4) == 0)
  35. {
  36. std::memcpy (&format, w.data() + pos + 8, 2);
  37. std::memcpy (&channels, w.data() + pos + 10, 2);
  38. std::memcpy (&bits, w.data() + pos + 22, 2);
  39. }
  40. else if (std::memcmp (w.data() + pos, "data", 4) == 0 && format == 1 && bits == 16)
  41. {
  42. const auto n = std::min<std::size_t> (size, w.size() - pos - 8) / 2;
  43. f.samples.resize (n);
  44. std::memcpy (f.samples.data(), w.data() + pos + 8, n * 2);
  45. f.channels = channels;
  46. }
  47. pos += 8 + size + (size & 1);
  48. }
  49. return f;
  50. }
  51. int main (int argc, char** argv)
  52. {
  53. if (argc < 7)
  54. {
  55. std::fprintf (stderr, "usage: flkeysrender <Wavtables> <state.bin> <out.f32> <rate> <length> <block> [law=x87] events...\n");
  56. return 1;
  57. }
  58. const std::string folder = argv[1];
  59. const auto state = flkeys::parseState (readAll (argv[2]));
  60. if (! state)
  61. return 2;
  62. const auto wti = readAll (folder + "/" + state->wavetable + ".wti");
  63. std::vector<std::string> names;
  64. const auto table = flkeys::parseWti (std::string (wti.begin(), wti.end()), names);
  65. if (! table)
  66. return 3;
  67. // (Ogg-compressed files cannot be read here: pass wtdump= for those wavetables)
  68. auto t = *table;
  69. std::vector<flkeys::FileSamples> files;
  70. for (std::size_t i = 0; i < names.size(); ++i)
  71. {
  72. const auto bytes = readAll (folder + "/" + unixPath (names[i]));
  73. if (t.raw)
  74. {
  75. flkeys::FileSamples f;
  76. f.samples.resize (bytes.size() / 2);
  77. std::memcpy (f.samples.data(), bytes.data(), f.samples.size() * 2);
  78. files.push_back (std::move (f));
  79. }
  80. else
  81. {
  82. t.files[i] = flkeys::wavFileInfo (bytes);
  83. files.push_back (pcm (bytes));
  84. }
  85. }
  86. auto wt = flkeys::buildWavetable (t, files);
  87. for (int i = 7; i < argc; ++i)
  88. if (std::strncmp (argv[i], "wtdump=", 7) == 0)
  89. {
  90. // the plugin's own loaded wavetable, dumped by fk.exe dumpwt= (r69/keys/run/fk.c)
  91. const auto b = readAll (argv[i] + 7);
  92. std::int32_t h[8];
  93. std::memcpy (h, b.data(), 32);
  94. auto w = std::make_shared<flkeys::Wavetable>();
  95. std::size_t p = 32;
  96. for (int k = 0; k < h[0]; ++k, p += 0x24)
  97. {
  98. flkeys::Keygroup g;
  99. std::memcpy (&g, b.data() + p, 20);
  100. w->keygroups.push_back (g);
  101. }
  102. w->thresholds.resize ((std::size_t) h[1]);
  103. std::memcpy (w->thresholds.data(), b.data() + p, 4 * (std::size_t) h[1]);
  104. p += 4 * (std::size_t) h[1];
  105. w->stereo = h[2] != 0, w->releaseHighNotes = h[5] != 0;
  106. std::memcpy (&w->sampleRate, &h[3], 4);
  107. std::memcpy (&w->velocityFactor, &h[4], 4);
  108. w->data.resize ((std::size_t) h[6] / 2 + 16);
  109. std::memcpy (w->data.data(), b.data() + p, std::min<std::size_t> ((std::size_t) h[6] + 16, b.size() - p));
  110. wt = w;
  111. }
  112. if (wt == nullptr)
  113. return 4;
  114. bool law = false;
  115. std::vector<flkeys::RefNote> notes;
  116. for (int i = 7; i < argc; ++i)
  117. {
  118. flkeys::RefNote n;
  119. float iv, fv, fp, ce;
  120. int a, b;
  121. if (std::strcmp (argv[i], "law=x87") == 0)
  122. law = true;
  123. else if (std::strncmp (argv[i], "dump=", 5) == 0)
  124. {
  125. // the same layout as fk.exe dumpwt= (r69/keys/run/fk.c)
  126. std::FILE* f = std::fopen (argv[i] + 5, "wb");
  127. std::int32_t h[8] = { (std::int32_t) wt->keygroups.size(), (std::int32_t) wt->thresholds.size(), wt->stereo ? 1 : 0, 0, 0,
  128. wt->releaseHighNotes ? 1 : 0, 0, 0 };
  129. std::memcpy (&h[3], &wt->sampleRate, 4);
  130. std::memcpy (&h[4], &wt->velocityFactor, 4);
  131. const auto ch = wt->stereo ? 2 : 1;
  132. const auto frames = wt->data.size() / (std::size_t) ch - 4;
  133. h[6] = (std::int32_t) (frames * (std::size_t) ch * 2);
  134. std::fwrite (h, 4, 8, f);
  135. for (const auto& k : wt->keygroups)
  136. {
  137. std::uint8_t r[0x24] {};
  138. std::memcpy (r, &k, 20);
  139. std::fwrite (r, 1, 0x24, f);
  140. }
  141. std::fwrite (wt->thresholds.data(), 4, wt->thresholds.size(), f);
  142. std::fwrite (wt->data.data(), 2, frames * (std::size_t) ch, f);
  143. std::fclose (f);
  144. }
  145. else if (std::sscanf (argv[i], "on3=%d,%d,%f,%f,%f,%f", &a, &b, &iv, &fv, &fp, &ce) == 6)
  146. {
  147. n.start = a, n.key = b, n.initVol = iv, n.finalVol = fv, n.finalPan = fp, n.cents = ce;
  148. notes.push_back (n);
  149. }
  150. else if (std::sscanf (argv[i], "off=%d,%d", &a, &b) == 2 && b >= 0 && b < (int) notes.size())
  151. notes[(std::size_t) b].release = a;
  152. }
  153. const auto out = flkeys::renderReference (*wt, *state, notes, std::atof (argv[4]), std::atoi (argv[5]), std::atoi (argv[6]), law);
  154. std::FILE* f = std::fopen (argv[3], "wb");
  155. std::fwrite (out.data(), 4, out.size(), f);
  156. std::fclose (f);
  157. return 0;
  158. }