bankaudit.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // bankaudit: renders LYNX preset-bank candidates with the real engines and prints level / length / spectrum figures
  2. // (round 168p: the sanity check of LYNX's own banks — no silence, no clipping, plausible pitch and length).
  3. //
  4. // bankaudit <presets.txt> [wavdir]
  5. //
  6. // presets.txt, one per line ("#" comments):
  7. // <kind>|<category>|<name>|<notes>|<v0,v1,...>
  8. // kind: fruitkick, plucked, drumpad, sakura (the values are the engine's parameters, see core/DrumControls)
  9. // notes: "key:vel:sec;key:vel:sec;..." (start times 0.0, 0.6, 1.2 s apart unless "@t" is given: "60:100:0.5@1.0")
  10. // Rendered at the channel's default volume (FL's 78 %, −5.2 dB). Prints: name, peak dBFS, RMS dBFS (first 1 s), length to −60 dB, spectral centroid, low-band pitch estimate.
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <complex>
  14. #include <cstdio>
  15. #include <cstring>
  16. #include <fstream>
  17. #include <sstream>
  18. #include <string>
  19. #include <vector>
  20. #include "core/BankStates.h"
  21. #include "core/Instruments.h"
  22. namespace
  23. {
  24. std::vector<std::string> split (const std::string& s, char c)
  25. {
  26. std::vector<std::string> out;
  27. std::string cur;
  28. std::istringstream in (s);
  29. while (std::getline (in, cur, c))
  30. out.push_back (cur);
  31. return out;
  32. }
  33. void writeWav (const std::string& path, const std::vector<float>& l, const std::vector<float>& r, int rate)
  34. {
  35. std::ofstream f (path, std::ios::binary);
  36. const auto n = (std::uint32_t) l.size();
  37. auto u32 = [&f] (std::uint32_t v) { f.write ((const char*) &v, 4); };
  38. auto u16 = [&f] (std::uint16_t v) { f.write ((const char*) &v, 2); };
  39. f.write ("RIFF", 4); u32 (36 + n * 4); f.write ("WAVEfmt ", 8); u32 (16); u16 (1); u16 (2); u32 ((std::uint32_t) rate);
  40. u32 ((std::uint32_t) rate * 4); u16 (4); u16 (16); f.write ("data", 4); u32 (n * 4);
  41. for (std::uint32_t i = 0; i < n; ++i)
  42. for (const auto x : { l[i], r[i] })
  43. {
  44. const auto s = (std::int16_t) std::lround (std::clamp (x, -1.0f, 1.0f) * 32767.0f);
  45. f.write ((const char*) &s, 2);
  46. }
  47. }
  48. double centroid (const std::vector<float>& x, int rate)
  49. {
  50. // average magnitude spectrum of 4096-point Hann frames over the first second
  51. const int n = 4096;
  52. std::vector<double> mag (n / 2, 0.0);
  53. for (int start = 0; start + n <= (int) x.size() && start < rate; start += n / 2)
  54. {
  55. std::vector<std::complex<double>> a (n);
  56. for (int i = 0; i < n; ++i)
  57. a[(std::size_t) i] = x[(std::size_t) (start + i)] * (0.5 - 0.5 * std::cos (2 * M_PI * i / (n - 1)));
  58. // iterative radix-2 FFT
  59. for (int i = 1, j = 0; i < n; ++i)
  60. {
  61. int bit = n >> 1;
  62. for (; j & bit; bit >>= 1)
  63. j ^= bit;
  64. j ^= bit;
  65. if (i < j)
  66. std::swap (a[(std::size_t) i], a[(std::size_t) j]);
  67. }
  68. for (int len = 2; len <= n; len <<= 1)
  69. {
  70. const auto w = std::polar (1.0, -2 * M_PI / len);
  71. for (int i = 0; i < n; i += len)
  72. {
  73. std::complex<double> wk = 1;
  74. for (int k = 0; k < len / 2; ++k, wk *= w)
  75. {
  76. const auto u = a[(std::size_t) (i + k)], v = a[(std::size_t) (i + k + len / 2)] * wk;
  77. a[(std::size_t) (i + k)] = u + v;
  78. a[(std::size_t) (i + k + len / 2)] = u - v;
  79. }
  80. }
  81. }
  82. for (int k = 0; k < n / 2; ++k)
  83. mag[(std::size_t) k] += std::abs (a[(std::size_t) k]);
  84. }
  85. double s = 0, w = 0;
  86. for (int k = 1; k < n / 2; ++k)
  87. {
  88. s += mag[(std::size_t) k];
  89. w += mag[(std::size_t) k] * k * (double) rate / n;
  90. }
  91. return s > 0 ? w / s : 0.0;
  92. }
  93. // strongest spectral peak below 2 kHz in the first 0.3 s (the drum's / note's body pitch)
  94. double bodyPitch (const std::vector<float>& x, int rate)
  95. {
  96. const int n = std::min<int> ((int) x.size(), (int) (0.3 * rate));
  97. double best = 0, bestF = 0;
  98. for (double f = 30; f < 2000; f *= 1.01)
  99. {
  100. double re = 0, im = 0;
  101. for (int i = 0; i < n; ++i)
  102. {
  103. const auto ph = 2 * M_PI * f * i / rate;
  104. re += x[(std::size_t) i] * std::cos (ph);
  105. im += x[(std::size_t) i] * std::sin (ph);
  106. }
  107. const auto m = re * re + im * im;
  108. if (m > best)
  109. best = m, bestF = f;
  110. }
  111. return bestF;
  112. }
  113. } // namespace
  114. int main (int argc, char** argv)
  115. {
  116. if (argc < 2)
  117. {
  118. std::fprintf (stderr, "usage: bankaudit presets.txt [wavdir]\n");
  119. return 2;
  120. }
  121. const std::string wavDir = argc > 2 ? argv[2] : "";
  122. std::ifstream in (argv[1]);
  123. std::string line;
  124. const int rate = 48000;
  125. int bad = 0;
  126. while (std::getline (in, line))
  127. {
  128. if (line.empty() || line[0] == '#')
  129. continue;
  130. const auto f = split (line, '|');
  131. if (f.size() < 5)
  132. continue;
  133. std::vector<float> values;
  134. for (const auto& v : split (f[4], ','))
  135. if (! v.empty())
  136. values.push_back (std::stof (v));
  137. auto def = lynx::bankstates::channelFor (f[0], values);
  138. if (! def)
  139. {
  140. std::printf ("%-28s (cannot build)\n", f[2].c_str());
  141. ++bad;
  142. continue;
  143. }
  144. auto inst = lynx::makeInstrument (def->instrument);
  145. inst->configure (*def, 120.0);
  146. inst->prepare (rate);
  147. struct N { int key; float vel; double len, at; };
  148. std::vector<N> notes;
  149. double t = 0;
  150. for (const auto& s : split (f[3], ';'))
  151. {
  152. auto parts = split (s, '@');
  153. const auto kv = split (parts[0], ':');
  154. N n { std::stoi (kv[0]), kv.size() > 1 ? std::stof (kv[1]) / 127.0f : 100.0f / 127.0f, kv.size() > 2 ? std::stod (kv[2]) : 0.4,
  155. parts.size() > 1 ? std::stod (parts[1]) : t };
  156. notes.push_back (n);
  157. t = n.at + 0.6;
  158. }
  159. double end = 0;
  160. for (const auto& n : notes)
  161. end = std::max (end, n.at + n.len);
  162. const int total = (int) ((end + 4.0) * rate);
  163. std::vector<float> l ((std::size_t) total, 0.0f), r ((std::size_t) total, 0.0f);
  164. const int block = 256;
  165. const auto gain = lynx::channelGain (def->volume);
  166. for (int pos = 0; pos < total; pos += block)
  167. {
  168. const double t0 = (double) pos / rate, t1 = (double) (pos + block) / rate;
  169. for (const auto& n : notes)
  170. {
  171. if (n.at >= t0 && n.at < t1)
  172. inst->noteOn (n.key, n.vel);
  173. if (n.at + n.len >= t0 && n.at + n.len < t1)
  174. inst->noteOff (n.key);
  175. }
  176. inst->render (l.data() + pos, r.data() + pos, std::min (block, total - pos), gain, gain); // (the channel's default volume)
  177. }
  178. float peak = 0;
  179. double sum = 0;
  180. int last = 0;
  181. bool finite = true;
  182. for (int i = 0; i < total; ++i)
  183. {
  184. const auto a = std::max (std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i]));
  185. finite = finite && std::isfinite (a);
  186. peak = std::max (peak, a);
  187. if (i < rate)
  188. sum += 0.5 * (l[(std::size_t) i] * l[(std::size_t) i] + r[(std::size_t) i] * r[(std::size_t) i]);
  189. }
  190. for (int i = 0; i < total; ++i)
  191. if (std::max (std::abs (l[(std::size_t) i]), std::abs (r[(std::size_t) i])) > peak * 0.001f)
  192. last = i;
  193. std::vector<float> mono ((std::size_t) total);
  194. for (int i = 0; i < total; ++i)
  195. mono[(std::size_t) i] = 0.5f * (l[(std::size_t) i] + r[(std::size_t) i]);
  196. double side = 0, mid = 0;
  197. for (int i = 0; i < total; ++i)
  198. {
  199. side += std::pow (0.5 * (l[(std::size_t) i] - r[(std::size_t) i]), 2.0);
  200. mid += std::pow (mono[(std::size_t) i], 2.0);
  201. }
  202. const auto peakDb = 20 * std::log10 (std::max (1e-9f, peak));
  203. const auto rmsDb = 10 * std::log10 (std::max (1e-18, sum / rate));
  204. const bool problem = ! finite || peakDb > -0.5 || peakDb < -30.0;
  205. bad += problem ? 1 : 0;
  206. std::printf ("%-12s %-28s peak %6.1f rms %6.1f len %5.2f s centroid %6.0f Hz body %6.0f Hz width %4.2f%s\n", f[1].c_str(),
  207. f[2].c_str(), peakDb, rmsDb, (double) last / rate, centroid (mono, rate), bodyPitch (mono, rate),
  208. mid > 0 ? std::sqrt (side / mid) : 0.0, problem ? " <-- CHECK" : "");
  209. if (! wavDir.empty())
  210. {
  211. auto file = f[0] + "-" + f[2];
  212. for (auto& c : file)
  213. if (! std::isalnum ((unsigned char) c) && c != '-')
  214. c = '_';
  215. l.resize ((std::size_t) std::min (total, last + rate / 10));
  216. r.resize (l.size());
  217. writeWav (wavDir + "/" + file + ".wav", l, r, rate);
  218. }
  219. }
  220. return bad > 0 ? 1 : 0;
  221. }