bankrender.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Renders every preset of LYNX's own banks (Toxic Biohazard and PoiZone, round 168n; Harmless, Fruity DX10 and SimSynth,
  2. // round 168o) to WAV files and prints their
  3. // level and brightness, to check the banks by ear and by numbers: bankrender <out dir> [rate]
  4. // peak / RMS (dBFS) over the phrase, the spectral centroid (Hz) of the held part, and the tail level after the release.
  5. #include <cmath>
  6. #include <complex>
  7. #include <cstdio>
  8. #include <cstdlib>
  9. #include <fstream>
  10. #include <string>
  11. #include <vector>
  12. #include "core/Dx10.h"
  13. #include "core/Harmless.h"
  14. #include "core/LynxBanks.h"
  15. #include "core/SimSynth.h"
  16. #include "core/PoiZone.h"
  17. #include "core/Toxic.h"
  18. namespace
  19. {
  20. void writeWav (const std::string& path, const std::vector<float>& interleaved, int rate)
  21. {
  22. std::ofstream f (path, std::ios::binary);
  23. auto u32 = [&f] (std::uint32_t v) { f.write ((const char*) &v, 4); };
  24. auto u16 = [&f] (std::uint16_t v) { f.write ((const char*) &v, 2); };
  25. const auto bytes = (std::uint32_t) (interleaved.size() * 2);
  26. f.write ("RIFF", 4), u32 (36 + bytes), f.write ("WAVEfmt ", 8), u32 (16), u16 (1), u16 (2), u32 ((std::uint32_t) rate),
  27. u32 ((std::uint32_t) rate * 4), u16 (4), u16 (16), f.write ("data", 4), u32 (bytes);
  28. for (const auto v : interleaved)
  29. {
  30. const auto s = (std::int16_t) std::lround (std::max (-1.0f, std::min (1.0f, v)) * 32767.0f);
  31. f.write ((const char*) &s, 2);
  32. }
  33. }
  34. double centroid (const std::vector<float>& interleaved, int from, int rate)
  35. {
  36. const int n = 16384;
  37. if ((int) interleaved.size() / 2 < from + n)
  38. return 0.0;
  39. std::vector<std::complex<double>> x ((std::size_t) n);
  40. for (int i = 0; i < n; ++i)
  41. x[(std::size_t) i] = 0.5 * (interleaved[2 * (std::size_t) (from + i)] + interleaved[2 * (std::size_t) (from + i) + 1])
  42. * (0.5 - 0.5 * std::cos (6.283185307179586 * i / n));
  43. for (int i = 1, j = 0; i < n; ++i) // radix-2 FFT
  44. {
  45. int bit = n >> 1;
  46. for (; j & bit; bit >>= 1)
  47. j ^= bit;
  48. j ^= bit;
  49. if (i < j)
  50. std::swap (x[(std::size_t) i], x[(std::size_t) j]);
  51. }
  52. for (int len = 2; len <= n; len <<= 1)
  53. {
  54. const auto w = std::polar (1.0, -6.283185307179586 / len);
  55. for (int i = 0; i < n; i += len)
  56. {
  57. std::complex<double> wk (1.0);
  58. for (int k = 0; k < len / 2; ++k, wk *= w)
  59. {
  60. const auto u = x[(std::size_t) (i + k)], v = x[(std::size_t) (i + k + len / 2)] * wk;
  61. x[(std::size_t) (i + k)] = u + v;
  62. x[(std::size_t) (i + k + len / 2)] = u - v;
  63. }
  64. }
  65. }
  66. double num = 0.0, den = 0.0;
  67. for (int k = 1; k < n / 2; ++k)
  68. {
  69. const auto m = std::abs (x[(std::size_t) k]);
  70. num += m * k * rate / (double) n;
  71. den += m;
  72. }
  73. return den > 0.0 ? num / den : 0.0;
  74. }
  75. // an engine without a reference renderer: its Instrument, the notes held for `held` samples, interleaved stereo
  76. std::vector<float> renderInstrument (lynx::InstrumentKind kind, const lynx::ChannelDef& c, const std::vector<int>& keys, int rate, int held, int length)
  77. {
  78. auto inst = kind == lynx::InstrumentKind::harmless ? lynx::makeHarmlessInstrument()
  79. : kind == lynx::InstrumentKind::dx10 ? lynx::makeDx10Instrument()
  80. : lynx::makeSimSynthInstrument();
  81. inst->configure (c, 120.0);
  82. inst->prepare ((double) rate);
  83. inst->settleNew();
  84. for (const auto k : keys)
  85. inst->noteOn (k, 0.78f);
  86. std::vector<float> L ((std::size_t) length, 0.0f), R ((std::size_t) length, 0.0f), out;
  87. for (int pos = 0; pos < length; pos += 256)
  88. {
  89. const auto n = std::min (256, length - pos);
  90. if (pos <= held && pos + n > held)
  91. for (const auto k : keys)
  92. inst->noteOff (k);
  93. inst->render (L.data() + pos, R.data() + pos, n, 1.0f, 1.0f);
  94. }
  95. out.reserve (2 * (std::size_t) length);
  96. for (int i = 0; i < length; ++i)
  97. out.push_back (L[(std::size_t) i]), out.push_back (R[(std::size_t) i]);
  98. return out;
  99. }
  100. const char* engineName (lynx::InstrumentKind kind)
  101. {
  102. switch (kind)
  103. {
  104. case lynx::InstrumentKind::toxic: return "Toxic";
  105. case lynx::InstrumentKind::poizone: return "PoiZone";
  106. case lynx::InstrumentKind::harmless: return "Harmless";
  107. case lynx::InstrumentKind::dx10: return "DX10";
  108. default: return "SimSynth";
  109. }
  110. }
  111. double db (double v) { return v > 1e-12 ? 20.0 * std::log10 (v) : -240.0; }
  112. } // namespace
  113. int main (int argc, char** argv)
  114. {
  115. if (argc < 2)
  116. {
  117. std::fprintf (stderr, "usage: bankrender <out dir> [rate]\n");
  118. return 2;
  119. }
  120. const std::string dir = argv[1];
  121. const int rate = argc > 2 ? std::atoi (argv[2]) : 48000;
  122. const int held = 2 * rate, length = 3 * rate;
  123. std::printf ("%-8s %-18s %-6s %8s %8s %9s %9s\n", "engine", "preset", "cat", "peak dB", "rms dB", "centroid", "tail dB");
  124. for (const auto kind : { lynx::InstrumentKind::toxic, lynx::InstrumentKind::poizone, lynx::InstrumentKind::harmless, lynx::InstrumentKind::dx10,
  125. lynx::InstrumentKind::simsynth })
  126. {
  127. const auto& bank = lynx::banks::bankOf (kind);
  128. for (int i = 0; i < (int) bank.size(); ++i)
  129. {
  130. lynx::ChannelDef c;
  131. c.instrument = kind;
  132. const auto loaded = lynx::banks::withPreset (c, i);
  133. if (! loaded)
  134. continue;
  135. std::vector<float> out;
  136. const bool mono = bank[(std::size_t) i].category == "Bass" || bank[(std::size_t) i].category == "Lead";
  137. const int low = bank[(std::size_t) i].category == "Bass" ? 36 : 60; // a bass at C2, a lead at C4
  138. if (kind == lynx::InstrumentKind::toxic)
  139. {
  140. std::vector<lynx::toxic::RefNote> notes;
  141. for (const auto key : { 48, 52, 55 })
  142. if (! mono || key == 48)
  143. notes.push_back ({ 0, mono ? low : key, 1.0, 1.0, 0.0, 0.0, held });
  144. out = lynx::toxic::renderReference (loaded->pluginState, notes, rate, length, "");
  145. }
  146. else if (kind != lynx::InstrumentKind::poizone)
  147. {
  148. std::vector<int> keys;
  149. for (const auto key : { 48, 52, 55 })
  150. if (! mono || key == 48)
  151. keys.push_back (mono ? 36 : key);
  152. out = renderInstrument (kind, *loaded, keys, rate, held, length);
  153. }
  154. else
  155. {
  156. std::vector<lynx::poizone::RefNote> notes;
  157. for (const auto key : { 48, 52, 55 })
  158. if (! mono || key == 48)
  159. notes.push_back ({ 0, mono ? low : key, 1.0, 1.0, 0.0, 0.0, held });
  160. out = lynx::poizone::renderReference (loaded->pluginState, notes, rate, length);
  161. }
  162. float peak = 0.0f;
  163. double sum = 0.0, tail = 0.0;
  164. for (std::size_t s = 0; s < out.size(); ++s)
  165. {
  166. peak = std::max (peak, std::fabs (out[s]));
  167. sum += (double) out[s] * out[s];
  168. if (s >= 2 * (std::size_t) (held + rate / 2))
  169. tail = std::max (tail, (double) std::fabs (out[s]));
  170. }
  171. const auto name = std::string (engineName (kind));
  172. std::printf ("%-8s %-18s %-6s %8.1f %8.1f %9.0f %9.1f\n", name.c_str(), bank[(std::size_t) i].name.c_str(),
  173. bank[(std::size_t) i].category.c_str(), db (peak), db (std::sqrt (sum / std::max<std::size_t> (1, out.size()))),
  174. centroid (out, rate / 2, rate), db (tail));
  175. writeWav (dir + "/" + name + " - " + bank[(std::size_t) i].category + " - " + bank[(std::size_t) i].name + ".wav", out, rate);
  176. }
  177. }
  178. return 0;
  179. }