BandlabExtras190chTests.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. // Round 190ch (LYNX's own features in the roles BandLab offers; all local): the project's Lyrics / Notes
  2. // (core/Lyrics.h), reference matching for mastering (core/ReferenceMatch.h) and the "Clean voice" chain
  3. // (core/CleanVoice.h).
  4. #include "core/CleanVoice.h"
  5. #include "core/Lyrics.h"
  6. #include "core/ReferenceMatch.h"
  7. namespace bl190ch
  8. {
  9. constexpr double rate = 44100.0;
  10. // deterministic noise, −1 … 1
  11. struct Lcg
  12. {
  13. std::uint32_t state;
  14. explicit Lcg (std::uint32_t seed) : state (seed) {}
  15. float next() noexcept
  16. {
  17. state = state * 1664525u + 1013904223u;
  18. return (float) ((double) (state >> 8) / 8388608.0 - 1.0);
  19. }
  20. };
  21. // pink noise (Paul Kellet's filter of white noise), about −20 dBFS RMS
  22. inline std::vector<float> pink (std::size_t frames, std::uint32_t seed)
  23. {
  24. Lcg white (seed);
  25. std::vector<float> out (frames);
  26. double b0 = 0, b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0, b6 = 0;
  27. for (auto& v : out)
  28. {
  29. const double w = white.next();
  30. b0 = 0.99886 * b0 + w * 0.0555179;
  31. b1 = 0.99332 * b1 + w * 0.0750759;
  32. b2 = 0.96900 * b2 + w * 0.1538520;
  33. b3 = 0.86650 * b3 + w * 0.3104856;
  34. b4 = 0.55000 * b4 + w * 0.5329522;
  35. b5 = -0.7616 * b5 - w * 0.0168980;
  36. v = (float) ((b0 + b1 + b2 + b3 + b4 + b5 + b6 + w * 0.5362) * 0.05);
  37. b6 = w * 0.115926;
  38. }
  39. return out;
  40. }
  41. inline void sine (std::vector<float>& out, std::size_t from, std::size_t to, double hz, double peakDb)
  42. {
  43. const auto a = std::pow (10.0, peakDb / 20.0);
  44. for (auto i = from; i < to && i < out.size(); ++i)
  45. out[i] += (float) (a * std::sin (2.0 * 3.14159265358979323846 * hz * (double) i / rate));
  46. }
  47. // a chain of slots run over a stereo buffer, in blocks
  48. inline void run (const std::vector<lynx::EffectSlot>& slots, std::vector<float>& l, std::vector<float>& r)
  49. {
  50. for (const auto& slot : slots)
  51. {
  52. auto fx = lynx::makeEffect (slot);
  53. fx->prepare (rate);
  54. fx->reset();
  55. for (std::size_t at = 0; at < l.size(); at += 512)
  56. fx->process (l.data() + at, r.data() + at, (int) std::min<std::size_t> (512, l.size() - at), slot.params, 120.0);
  57. }
  58. }
  59. inline double rmsDb (const std::vector<float>& x, std::size_t from, std::size_t to)
  60. {
  61. double sum = 0.0;
  62. for (auto i = from; i < to; ++i)
  63. sum += (double) x[i] * x[i];
  64. return 10.0 * std::log10 (sum / (double) (to - from) + 1.0e-20);
  65. }
  66. } // namespace bl190ch
  67. // --- Lyrics / Notes --------------------------------------------------------------------------------------------------
  68. TEST (bl190ch_lyrics_lines_headings_and_positions)
  69. {
  70. using namespace lynx;
  71. const std::string text = "[Verse 1]\n[5] Walking down the line\n [5.3] second half\nplain words\n[9] [Chorus]\n[12]\n[not closed\n[a] and more";
  72. const auto lines = lyrics::parse (text);
  73. CHECK (lines.size() == 8);
  74. CHECK (lines[0].section && lines[0].title == "Verse 1" && ! lines[0].stamped);
  75. CHECK (lines[1].stamped && lines[1].bar == 5 && lines[1].beat == 1 && lines[1].body == "Walking down the line" && ! lines[1].section);
  76. CHECK (lines[2].stamped && lines[2].bar == 5 && lines[2].beat == 3 && lines[2].body == "second half");
  77. CHECK (! lines[3].stamped && ! lines[3].section && lines[3].body == "plain words");
  78. CHECK (lines[4].stamped && lines[4].bar == 9 && lines[4].section && lines[4].title == "Chorus");
  79. CHECK (lines[5].stamped && lines[5].bar == 12 && lines[5].body.empty() && ! lines[5].section); // a position alone is not a heading
  80. CHECK (! lines[6].section && ! lines[6].stamped);
  81. CHECK (! lines[7].section && ! lines[7].stamped && lines[7].body == "[a] and more"); // a heading is the whole line
  82. // the byte ranges are the lines
  83. for (const auto& line : lines)
  84. CHECK (text.substr (line.begin, line.end - line.begin).find ('\n') == std::string::npos);
  85. CHECK (text.substr (lines[1].bodyBegin, lines[1].end - lines[1].bodyBegin) == "Walking down the line");
  86. CHECK (lyrics::lineAt (lines, 0) == 0 && lyrics::lineAt (lines, lines[2].begin + 3) == 2 && lyrics::lineAt (lines, text.size()) == 7);
  87. // an empty text is one empty line; Windows line ends don't leak into the words
  88. CHECK (lyrics::parse ("").size() == 1);
  89. const auto crlf = lyrics::parse ("[Intro]\r\n[2] hello\r\n");
  90. CHECK (crlf.size() == 3 && crlf[0].section && crlf[0].title == "Intro" && crlf[1].body == "hello");
  91. // not positions: bar 0, letters, a negative number
  92. for (const char* bad : { "[0] x", "[1.0] x", "[1a] x", "[-3] x", "[] x", "[1.2.3] x" })
  93. CHECK (! lyrics::parse (bad)[0].stamped);
  94. CHECK (lyrics::wordCount (text) == 13); // headings and positions don't count
  95. }
  96. TEST (bl190ch_lyrics_positions_follow_the_songs_bars)
  97. {
  98. using namespace lynx;
  99. Song song;
  100. song.beatsPerBar = 4;
  101. const auto bar = (std::int64_t) ppq * 4;
  102. CHECK (lyrics::tickOf (song, 1, 1) == 0);
  103. CHECK (lyrics::tickOf (song, 5, 1) == 4 * bar);
  104. CHECK (lyrics::tickOf (song, 5, 3) == 4 * bar + 2 * ppq);
  105. CHECK (lyrics::tickOf (song, 2, 9) < 2 * bar); // a beat past the bar stays in the bar
  106. CHECK (lyrics::barBeatOf (song, 4 * bar + 2 * ppq + 17) == std::make_pair (5, 3));
  107. CHECK (lyrics::stampText (9, 1) == "[9]" && lyrics::stampText (9, 3) == "[9.3]");
  108. // a time signature change: from bar 3 on the bars are 3/4
  109. Marker waltz;
  110. waltz.tick = 2 * bar;
  111. waltz.numerator = 3;
  112. waltz.denominator = 4;
  113. song.addMarker (waltz);
  114. for (int b = 1; b <= 12; ++b)
  115. {
  116. const auto tick = lyrics::tickOf (song, b, 1);
  117. const auto at = song.barAt (tick);
  118. CHECK (at.number == b && at.tick == tick);
  119. CHECK (lyrics::barBeatOf (song, tick) == std::make_pair (b, 1));
  120. }
  121. CHECK (lyrics::tickOf (song, 4, 1) == 2 * bar + 3 * ppq);
  122. }
  123. TEST (bl190ch_lyrics_marker_at_playhead_and_jumps)
  124. {
  125. using namespace lynx;
  126. Song song;
  127. const auto bar = (std::int64_t) ppq * 4;
  128. song.info.comments = "[Verse]\nFirst line of the verse that is rather long and keeps going on\n\n[Chorus]\nSing it";
  129. // the heading takes the playhead's place (inside bar 3, beat 2) and a marker named after it is put there
  130. CHECK (lyrics::markAtPlayhead (song, 0, 2 * bar + ppq + 100));
  131. CHECK (song.info.comments.rfind ("[3.2] [Verse]\n", 0) == 0);
  132. CHECK (song.markers().size() == 1 && song.markers()[0].tick == 2 * bar + ppq && song.markers()[0].name == "Verse");
  133. // a text line: the marker gets its first words (never longer than 40 bytes)
  134. CHECK (lyrics::markAtPlayhead (song, 1, 4 * bar));
  135. CHECK (song.markers().size() == 2 && song.markers()[1].tick == 4 * bar);
  136. CHECK (song.markers()[1].name.size() <= 40 && song.markers()[1].name.rfind ("First line of the verse", 0) == 0);
  137. // again on the same line and place: the position is replaced, no second marker
  138. CHECK (lyrics::markAtPlayhead (song, 1, 4 * bar));
  139. CHECK (song.markers().size() == 2);
  140. auto lines = lyrics::parse (song.info.comments);
  141. CHECK (lines[1].stamped && lines[1].bar == 5 && lines[1].beat == 1);
  142. CHECK (std::count (song.info.comments.begin(), song.info.comments.end(), '[') == 4); // [3.2] [Verse] [5] [Chorus]
  143. // moving the line elsewhere re-stamps it
  144. CHECK (lyrics::markAtPlayhead (song, 1, 6 * bar));
  145. lines = lyrics::parse (song.info.comments);
  146. CHECK (lines[1].bar == 7 && lines[1].body.rfind ("First line", 0) == 0);
  147. // an empty line gets a name of its own; a line that does not exist changes nothing
  148. CHECK (lyrics::markAtPlayhead (song, 2, 8 * bar));
  149. CHECK (song.markers().back().name == "Line 3");
  150. const auto before = song;
  151. CHECK (! lyrics::markAtPlayhead (song, 99, 0));
  152. CHECK (song.info.comments == before.info.comments && song.markers().size() == before.markers().size());
  153. // jumps: a stamped line goes to its place; a heading without one goes to the marker of the same name
  154. lines = lyrics::parse (song.info.comments);
  155. CHECK (lyrics::jumpTick (song, lines[0]) == std::optional<std::int64_t> (2 * bar + ppq));
  156. CHECK (! lyrics::jumpTick (song, lines[3])); // [Chorus]: no position, no marker of that name yet
  157. Marker chorus;
  158. chorus.tick = 12 * bar;
  159. chorus.name = "CHORUS";
  160. song.addMarker (chorus);
  161. CHECK (lyrics::jumpTick (song, lines[3]) == std::optional<std::int64_t> (12 * bar)); // (the name's case does not matter)
  162. CHECK (! lyrics::jumpTick (song, lines[4]));
  163. // the line that is playing: the last place at or before the playhead, in song order
  164. CHECK (lyrics::currentLine (song, lines, 0) == -1);
  165. CHECK (lyrics::currentLine (song, lines, 2 * bar + ppq) == 0);
  166. CHECK (lyrics::currentLine (song, lines, 7 * bar) == 1);
  167. CHECK (lyrics::currentLine (song, lines, 9 * bar) == 2);
  168. CHECK (lyrics::currentLine (song, lines, 500 * bar) == 3);
  169. // taking a position off
  170. const auto cleared = lyrics::withoutStamp (song.info.comments, 0);
  171. CHECK (cleared.rfind ("[Verse]\n", 0) == 0);
  172. CHECK (lyrics::withoutStamp (cleared, 0) == cleared);
  173. CHECK (lyrics::withStamp ("", 0, 2, 1) == "[2]" && lyrics::withStamp ("a\nb", 1, 3, 2) == "a\n[3.2] b");
  174. }
  175. TEST (bl190ch_lyrics_search_and_text_export)
  176. {
  177. using namespace lynx;
  178. const std::string text = "[Chorus]\nLove is LOVE, love\nCora\xc3\xa7\xc3\xa3o e amor";
  179. const auto hits = lyrics::find (text, "love");
  180. CHECK (hits.size() == 3);
  181. for (const auto& [a, b] : hits)
  182. CHECK (b - a == 4 && lynx::lyrics::detail::sameText (text.substr (a, 4), "LOVE"));
  183. CHECK (lyrics::find (text, "cora\xc3\xa7\xc3\xa3o").size() == 1); // ASCII letters either case, other bytes as written
  184. CHECK (lyrics::find (text, "").empty() && lyrics::find (text, "nothing here").empty() && lyrics::find ("ab", "abc").empty());
  185. CHECK (lyrics::find ("aaaa", "aa").size() == 2); // (not overlapping)
  186. Song song;
  187. song.bpm = 120.0; // one bar = 2 s
  188. song.info.title = "Night Drive";
  189. song.info.author = "Somebody";
  190. song.info.comments = "[Verse]\n[9] First\r\n[9.3] Second\nno position\n[41]";
  191. CHECK (lyrics::exportText (song) == "Night Drive\nSomebody\n\n[Verse]\n[9] First\n[9.3] Second\nno position\n[41]\n");
  192. CHECK (lyrics::exportText (song, lyrics::Stamps::none) == "Night Drive\nSomebody\n\n[Verse]\nFirst\nSecond\nno position\n");
  193. CHECK (lyrics::exportText (song, lyrics::Stamps::times) == "Night Drive\nSomebody\n\n[Verse]\n[0:16] First\n[0:17] Second\nno position\n[1:20]\n");
  194. song.info.title.clear();
  195. song.info.author.clear();
  196. song.info.comments = "one";
  197. CHECK (lyrics::exportText (song) == "one\n");
  198. // the text is the project's Comments: nothing else holds it (one store, the field .lynx and .flp already carry)
  199. Song other = song;
  200. CHECK (other.info == song.info);
  201. }
  202. // --- Reference match ---------------------------------------------------------------------------------------------------
  203. TEST (bl190ch_reference_analysis_matches_ebu_r128_and_known_signals)
  204. {
  205. using namespace lynx;
  206. const auto second = (std::size_t) bl190ch::rate;
  207. // EBU Tech 3341 case 1 and 2: a stereo 1 kHz sine at −23 / −33 dBFS reads −23.0 / −33.0 LUFS (±0.1)
  208. for (const double level : { -23.0, -33.0 })
  209. {
  210. std::vector<float> l (20 * second, 0.0f);
  211. bl190ch::sine (l, 0, l.size(), 1000.0, level);
  212. const auto a = refmatch::analyse (l, l, bl190ch::rate);
  213. CHECK (a.valid && std::abs (a.seconds - 20.0) < 0.01);
  214. CHECK (std::abs (a.lufs - level) < 0.1);
  215. CHECK (std::abs (a.truePeakDb - level) < 0.2);
  216. CHECK (a.correlation > 0.999 && a.widthPercent() < 0.01); // both channels the same: mono
  217. // all of the sine's power (−3.01 dB under its peak) is in the 1 kHz band, and nowhere else
  218. CHECK (std::abs (refmatch::bandHz (17) - 1000.0) < 1.0e-9);
  219. CHECK (std::abs (a.spectrumDb[17] - (level - 3.01)) < 0.1);
  220. CHECK (a.spectrumDb[14] < level - 70.0 && a.spectrumDb[20] < level - 70.0);
  221. }
  222. // case 3 (gating): 10 s at −36, 60 s at −23, 10 s at −36 dBFS still reads −23.0 LUFS (±0.1)
  223. {
  224. std::vector<float> l (80 * second, 0.0f);
  225. bl190ch::sine (l, 0, 10 * second, 1000.0, -36.0);
  226. bl190ch::sine (l, 10 * second, 70 * second, 1000.0, -23.0);
  227. bl190ch::sine (l, 70 * second, 80 * second, 1000.0, -36.0);
  228. CHECK (std::abs (refmatch::analyse (l, l, bl190ch::rate).lufs - -23.0) < 0.1);
  229. }
  230. // stereo width: unrelated noise left and right is half side energy; one channel inverted is all side
  231. {
  232. const auto l = bl190ch::pink (10 * second, 11u), r = bl190ch::pink (10 * second, 977u);
  233. const auto a = refmatch::analyse (l, r, bl190ch::rate);
  234. CHECK (std::abs (a.correlation) < 0.05 && std::abs (a.widthPercent() - 50.0) < 3.0 && std::abs (a.sideDb) < 0.6);
  235. auto inv = l;
  236. for (auto& v : inv)
  237. v = -v;
  238. const auto b = refmatch::analyse (l, inv, bl190ch::rate);
  239. CHECK (b.correlation < -0.999 && b.widthPercent() > 99.9);
  240. // pink noise: every third-octave band holds the same power (±1 dB over 50 Hz … 10 kHz on 10 s)
  241. const auto mono = refmatch::analyse (l, l, bl190ch::rate);
  242. for (int k = 4; k <= 27; ++k)
  243. CHECK (std::abs (mono.spectrumDb[(std::size_t) k] - mono.spectrumDb[17]) < 1.5);
  244. }
  245. // too short, or nothing: not valid, and a suggestion from it is not either
  246. std::vector<float> tiny (1000, 0.1f);
  247. CHECK (! refmatch::analyse (tiny, tiny, bl190ch::rate).valid);
  248. CHECK (! refmatch::suggest ({}, {}, bl190ch::rate).valid);
  249. // 48 kHz and 22.05 kHz files: the bands above the Nyquist frequency are not used
  250. {
  251. std::vector<float> l (4 * 22050, 0.0f);
  252. for (std::size_t i = 0; i < l.size(); ++i)
  253. l[i] = (float) (0.1 * std::sin (2.0 * 3.14159265358979323846 * 1000.0 * (double) i / 22050.0));
  254. const auto a = refmatch::analyse (l, l, 22050.0);
  255. CHECK (a.valid && a.usedBands < refmatch::bands && refmatch::bandHz (a.usedBands - 1) < 11025.0);
  256. CHECK (std::abs (a.spectrumDb[17] - (-20.0 - 3.01)) < 0.1);
  257. }
  258. }
  259. TEST (bl190ch_reference_match_pink_noise_with_a_known_tilt)
  260. {
  261. using namespace lynx;
  262. const auto frames = (std::size_t) (30.0 * bl190ch::rate);
  263. const auto songL = bl190ch::pink (frames, 1u), songR = bl190ch::pink (frames, 2u);
  264. // the reference: the same noise tilted by 0.6 dB per octave around 1 kHz (Facet's Flat Tilt: its gain is the level
  265. // five octaves above the band frequency) and 4 dB quieter
  266. constexpr double tiltDbPerOctave = 0.6, quieterDb = -4.0;
  267. auto tilt = makeEffectSlot (EffectKind::facetEq);
  268. tilt.params[(std::size_t) facet::param (0, 0)] = facet::shapeValue (facet::Shape::flatTilt);
  269. tilt.params[(std::size_t) facet::param (0, 1)] = facet::hzValue (1000.0);
  270. tilt.params[(std::size_t) facet::param (0, 2)] = facet::gainValue (tiltDbPerOctave * 5.0);
  271. tilt.params[(std::size_t) facet::outputParam] = refmatch::detail::valueFor ([] (float v) { return facet::outputDb (v); }, quieterDb);
  272. auto refL = songL, refR = songR;
  273. bl190ch::run ({ tilt }, refL, refR);
  274. const auto song = refmatch::analyse (songL, songR, bl190ch::rate);
  275. const auto reference = refmatch::analyse (refL, refR, bl190ch::rate);
  276. CHECK (song.valid && reference.valid);
  277. // the analyser sees the tilt that was put in (the mean level aside)
  278. const auto s = refmatch::suggest (song, reference, bl190ch::rate);
  279. CHECK (s.valid);
  280. for (int k = 2; k <= 29; ++k) // 31.5 Hz … 16 kHz
  281. {
  282. const auto expected = tiltDbPerOctave * std::log2 (refmatch::bandHz (k) / 1000.0);
  283. CHECK (std::abs (s.differenceDb[(std::size_t) k] - expected) < 0.3);
  284. CHECK (std::abs (s.targetDb[(std::size_t) k] - expected) < 0.3);
  285. }
  286. CHECK (s.fitErrorDb < 0.35);
  287. // the level: what is left after the EQ's own change of loudness (the tilt turns around 1 kHz: next to nothing)
  288. CHECK (std::abs (s.gainDb + s.eqLoudnessDb - (reference.lufs - song.lufs)) < 1.0e-9 && std::abs (s.gainDb - quieterDb) < 0.5);
  289. CHECK (std::abs (refmatch::kWeightingDb (1000.0, 48000.0)) < 0.75 && refmatch::kWeightingDb (10000.0, 48000.0) > 3.5
  290. && refmatch::kWeightingDb (30.0, 48000.0) < -3.0);
  291. CHECK (std::abs (s.targetLufs - reference.lufs) < 1.0e-9);
  292. // one pass: the song through the two Master slots Match adds, measured again
  293. Song project;
  294. project.mixer = Song::defaultMixer (2);
  295. project.mixer[0].effects.clear();
  296. CHECK (refmatch::apply (project, s));
  297. CHECK (project.mixer[0].effects.size() == 2);
  298. auto outL = songL, outR = songR;
  299. bl190ch::run (project.mixer[0].effects, outL, outR);
  300. const auto after = refmatch::analyse (outL, outR, bl190ch::rate);
  301. double worst = 0.0;
  302. for (int k = 0; k < refmatch::bands; ++k)
  303. worst = std::max (worst, std::abs (after.spectrumDb[(std::size_t) k] - reference.spectrumDb[(std::size_t) k]));
  304. if (worst >= 0.5 || std::abs (after.lufs - reference.lufs) >= 0.5)
  305. {
  306. std::printf (" worst band error after one pass: %.3f dB; LUFS song %.2f reference %.2f after %.2f; gain %.2f, EQ loudness %.2f, fit error %.2f\n", worst,
  307. song.lufs, reference.lufs, after.lufs, s.gainDb, s.eqLoudnessDb, s.fitErrorDb);
  308. for (int k = 0; k < refmatch::bands; ++k)
  309. std::printf (" %7.0f Hz after-reference %+.2f target %+.2f eq %+.2f\n", refmatch::bandHz (k),
  310. after.spectrumDb[(std::size_t) k] - reference.spectrumDb[(std::size_t) k], s.targetDb[(std::size_t) k], s.eqDb[(std::size_t) k]);
  311. }
  312. for (int k = 0; k < refmatch::bands; ++k) // every band, 20 Hz … 20 kHz, in absolute level
  313. CHECK (std::abs (after.spectrumDb[(std::size_t) k] - reference.spectrumDb[(std::size_t) k]) < 0.5);
  314. CHECK (std::abs (after.lufs - reference.lufs) < 0.5);
  315. CHECK (after.truePeakDb <= -1.0 + 0.1); // under the limiter's ceiling
  316. }
  317. TEST (bl190ch_reference_match_limits_and_smoothing)
  318. {
  319. using namespace lynx;
  320. // smoothing keeps a straight tilt as it is, up to both ends, and takes a zigzag out
  321. std::array<double, refmatch::bands> line {}, zigzag {};
  322. for (int k = 0; k < refmatch::bands; ++k)
  323. {
  324. line[(std::size_t) k] = 0.37 * k - 4.0;
  325. zigzag[(std::size_t) k] = k % 2 == 0 ? 2.0 : -2.0;
  326. }
  327. const auto smoothLine = refmatch::smoothed (line, refmatch::bands), smoothZig = refmatch::smoothed (zigzag, refmatch::bands);
  328. for (int k = 0; k < refmatch::bands; ++k)
  329. {
  330. CHECK (std::abs (smoothLine[(std::size_t) k] - line[(std::size_t) k]) < 1.0e-6);
  331. if (k >= 2 && k < refmatch::bands - 2)
  332. CHECK (std::abs (smoothZig[(std::size_t) k]) < 0.5);
  333. }
  334. // a reference that is 15 dB brighter above 2 kHz: no band is asked for more than ±6 dB, and Amount scales it
  335. refmatch::Analysis song, bright;
  336. song.valid = bright.valid = true;
  337. song.lufs = -20.0;
  338. bright.lufs = -9.0;
  339. for (int k = 0; k < refmatch::bands; ++k)
  340. {
  341. song.spectrumDb[(std::size_t) k] = -30.0;
  342. bright.spectrumDb[(std::size_t) k] = refmatch::bandHz (k) > 2000.0 ? -15.0 : -30.0;
  343. }
  344. const auto full = refmatch::suggest (song, bright, bl190ch::rate);
  345. double top = -100.0, bottom = 100.0;
  346. for (int k = 0; k < refmatch::bands; ++k)
  347. {
  348. CHECK (std::abs (full.targetDb[(std::size_t) k]) <= refmatch::maxBandDb + 1.0e-9);
  349. top = std::max (top, full.targetDb[(std::size_t) k]);
  350. bottom = std::min (bottom, full.targetDb[(std::size_t) k]);
  351. }
  352. CHECK (top == refmatch::maxBandDb && bottom < -3.0); // (the mean level is taken out first: the lows go down)
  353. CHECK (std::abs (full.gainDb + full.eqLoudnessDb - 11.0) < 1.0e-9 && full.eqLoudnessDb > 0.5); // (brighter is louder: less gain is needed)
  354. bright.lufs = 3.0;
  355. CHECK (refmatch::suggest (song, bright, bl190ch::rate).gainDb == refmatch::maxLoudnessDb); // the level moves 12 dB at most
  356. // a measured result that came out 2 LU short is asked for again as a trim; the level still moves 12 dB at most
  357. bright.lufs = -9.0;
  358. refmatch::Options trim;
  359. trim.trimDb = 2.0;
  360. CHECK (std::abs (refmatch::suggest (song, bright, bl190ch::rate, trim).gainDb - (full.gainDb + 2.0)) < 1.0e-9);
  361. trim.trimDb = 50.0;
  362. CHECK (refmatch::suggest (song, bright, bl190ch::rate, trim).gainDb == refmatch::maxLoudnessDb);
  363. bright.lufs = 3.0;
  364. refmatch::Options half;
  365. half.amount = 0.25;
  366. half.matchLoudness = false;
  367. const auto quarter = refmatch::suggest (song, bright, bl190ch::rate, half);
  368. CHECK (quarter.gainDb == 0.0);
  369. for (int k = 0; k < refmatch::bands; ++k)
  370. CHECK (std::abs (quarter.targetDb[(std::size_t) k]) <= 0.25 * 15.0 + 1.0e-9);
  371. refmatch::Options none;
  372. none.amount = 0.0;
  373. const auto flat = refmatch::suggest (song, bright, bl190ch::rate, none);
  374. for (int k = 0; k < refmatch::bands; ++k)
  375. CHECK (flat.targetDb[(std::size_t) k] == 0.0 && std::abs (flat.eqDb[(std::size_t) k]) < 1.0e-6);
  376. for (int b = 0; b < refmatch::eqBands; ++b)
  377. CHECK (facet::gainDb (flat.eq[(std::size_t) facet::param (b, 2)]) == 0.0);
  378. // the fitted EQ is the layout the header describes, and its output gain is the loudness gain
  379. CHECK (facet::shape (full.eq[(std::size_t) facet::param (0, 0)]) == facet::Shape::lowShelf);
  380. CHECK (facet::shape (full.eq[(std::size_t) facet::param (7, 0)]) == facet::Shape::highShelf);
  381. for (int b = 1; b < 7; ++b)
  382. CHECK (facet::shape (full.eq[(std::size_t) facet::param (b, 0)]) == facet::Shape::bell);
  383. CHECK (std::abs (facet::outputDb (full.eq[(std::size_t) facet::outputParam]) - full.gainDb) < 0.01);
  384. // the same loudness and tone: nothing to do
  385. const auto same = refmatch::suggest (song, song, bl190ch::rate);
  386. CHECK (same.valid && std::abs (same.gainDb) < 1.0e-4 && same.fitErrorDb < 1.0e-6);
  387. }
  388. TEST (bl190ch_reference_match_is_two_editable_master_slots)
  389. {
  390. using namespace lynx;
  391. refmatch::Analysis a, b;
  392. a.valid = b.valid = true;
  393. a.lufs = -18.0;
  394. b.lufs = -14.0;
  395. for (int k = 0; k < refmatch::bands; ++k)
  396. b.spectrumDb[(std::size_t) k] = 0.2 * (k - 15);
  397. const auto s = refmatch::suggest (a, b, bl190ch::rate);
  398. Song song;
  399. song.mixer = Song::defaultMixer (2);
  400. song.mixer[0].effects.clear();
  401. auto first = makeEffectSlot (EffectKind::compressor);
  402. song.mixer[0].effects.push_back (first);
  403. CHECK (! refmatch::present (song));
  404. CHECK (refmatch::apply (song, s));
  405. const auto& fx = song.mixer[0].effects;
  406. CHECK (fx.size() == 3 && fx[0] == first); // what was on the Master stays, in front
  407. CHECK (fx[1].kind == EffectKind::facetEq && fx[2].kind == EffectKind::apexLimiter);
  408. CHECK (fx[1].presetName == refmatch::slotName && fx[2].presetName == refmatch::slotName && fx[1].enabled && fx[2].enabled);
  409. CHECK (fx[1].look == 0 && fx[2].look == 0); // ordinary slots, not a Master look
  410. CHECK (fx[1].params == s.eq);
  411. CHECK (std::abs (apex::ceilingDb (fx[2].params[apex::pCeiling]) - -1.0) < 0.01 && fx[2].params[apex::pTruePeak] == 1.0f
  412. && apex::gainDb (fx[2].params[apex::pGain]) == 0.0);
  413. CHECK (refmatch::present (song));
  414. // "Hear without" switches both, and a second Match replaces them where they are, keeping that state
  415. refmatch::setBypassed (song, true);
  416. CHECK (refmatch::bypassed (song) && ! fx[1].enabled && ! fx[2].enabled && fx[0].enabled);
  417. refmatch::Options o;
  418. o.amount = 0.5;
  419. const auto s2 = refmatch::suggest (a, b, bl190ch::rate, o);
  420. CHECK (refmatch::apply (song, s2));
  421. CHECK (song.mixer[0].effects.size() == 3 && song.mixer[0].effects[1].params == s2.eq && refmatch::bypassed (song));
  422. refmatch::setBypassed (song, false);
  423. CHECK (! refmatch::bypassed (song));
  424. // a slot the user renamed away is theirs: the next Match adds a fresh pair after cleaning up the half that is left
  425. song.mixer[0].effects[2].presetName = "My limiter";
  426. CHECK (refmatch::apply (song, s));
  427. CHECK (song.mixer[0].effects.size() == 4 && refmatch::matchSlots (song) == std::vector<int> ({ 2, 3 }));
  428. CHECK (song.mixer[0].effects[1].presetName == "My limiter");
  429. refmatch::remove (song);
  430. CHECK (song.mixer[0].effects.size() == 2 && ! refmatch::present (song) && song.mixer[0].effects[0] == first);
  431. // no room for two more slots: nothing changes
  432. while ((int) song.mixer[0].effects.size() < maxEffectSlots - 1)
  433. song.mixer[0].effects.push_back (first);
  434. const auto full = song;
  435. CHECK (! refmatch::apply (song, s));
  436. CHECK (song.mixer[0].effects == full.mixer[0].effects);
  437. CHECK (! refmatch::apply (song, refmatch::Suggestion {})); // nothing measured: nothing applied
  438. }
  439. // --- Clean voice -------------------------------------------------------------------------------------------------------
  440. TEST (bl190ch_clean_voice_chain_settings)
  441. {
  442. using namespace lynx;
  443. namespace cv = cleanvoice;
  444. const auto plain = cv::chain();
  445. CHECK (plain.size() == 4);
  446. CHECK (plain[0].kind == EffectKind::facetEq && plain[1].kind == EffectKind::sentryGate && plain[2].kind == EffectKind::hushDeEsser
  447. && plain[3].kind == EffectKind::crestCompressor);
  448. cv::Options withNoise;
  449. withNoise.noiseReduction = true;
  450. const auto noisy = cv::chain (withNoise);
  451. CHECK (noisy.size() == 5 && noisy[0].kind == EffectKind::quellDenoiser);
  452. CHECK (std::abs (noisy[0].params[voicetools::quell::amount] - cv::noiseAmount) < 1.0e-6f);
  453. for (std::size_t i = 0; i < plain.size(); ++i)
  454. CHECK (noisy[i + 1] == plain[i]); // the same four behind it
  455. for (const auto& slot : noisy)
  456. CHECK (slot.presetName == cv::slotName && slot.enabled && slot.mix == 1.0f && cv::isCleanSlot (slot));
  457. // each setting read back through the effect's own law
  458. const auto& eq = plain[0].params;
  459. CHECK (facet::shape (eq[(std::size_t) facet::param (0, 0)]) == facet::Shape::lowCut);
  460. CHECK (std::abs (facet::hz (eq[(std::size_t) facet::param (0, 1)]) - cv::highPassHz) < 0.5);
  461. CHECK (facet::slopeDb (eq[(std::size_t) facet::param (0, 4)]) == cv::highPassSlope);
  462. for (int b = 1; b < facet::bands; ++b)
  463. CHECK (facet::shape (eq[(std::size_t) facet::param (b, 0)]) == facet::Shape::off);
  464. // an octave under the cut: about 12 dB down (Facet's low cut measures 14); the voice's range untouched
  465. CHECK (facet::responseDb (eq, bl190ch::rate, 40.0) < -10.0 && facet::responseDb (eq, bl190ch::rate, 40.0) > -16.0);
  466. CHECK (std::abs (facet::responseDb (eq, bl190ch::rate, 1000.0)) < 0.1 && facet::responseDb (eq, bl190ch::rate, 250.0) > -1.0);
  467. namespace s = dyn::sentry;
  468. const auto& gate = plain[1].params;
  469. CHECK (s::styleOf (gate[s::style]) == s::Style::clean);
  470. CHECK (std::abs (s::thresholdDb (gate[s::threshold]) - cv::expanderThresholdDb) < 0.01);
  471. CHECK (std::abs (s::ratioOf (gate[s::ratio]) - cv::expanderRatio) < 0.01);
  472. CHECK (std::abs (s::rangeDb (gate[s::range]) - cv::expanderRangeDb) < 0.01);
  473. CHECK (std::abs (s::attackMs (gate[s::attack]) - cv::expanderAttackMs) < 0.01 && std::abs (s::holdMs (gate[s::hold]) - cv::expanderHoldMs) < 0.05
  474. && std::abs (s::releaseMs (gate[s::release]) - cv::expanderReleaseMs) < 0.05);
  475. namespace h = mixsuite::hush;
  476. const auto& deEss = plain[2].params;
  477. CHECK (std::abs (h::hpHz (deEss[h::frequency], h::extended (deEss)) - cv::deEssHz) < 5.0);
  478. CHECK (std::abs (h::thresholdDb (deEss[h::threshold]) - cv::deEssThresholdDb) < 0.01 && std::abs (h::rangeDb (deEss[h::range]) - cv::deEssRangeDb) < 0.01);
  479. namespace c = dyn::crest;
  480. const auto& comp = plain[3].params;
  481. CHECK (c::styleOf (comp) == c::Style::clean);
  482. CHECK (std::abs (c::thresholdDb (comp[c::threshold]) - cv::compThresholdDb) < 0.01 && std::abs (c::ratioOf (comp[c::ratio]) - cv::compRatio) < 0.01);
  483. CHECK (std::abs (c::kneeDb (comp[c::knee]) - cv::compKneeDb) < 0.01 && std::abs (c::attackMs (comp[c::attack]) - cv::compAttackMs) < 0.05
  484. && std::abs (c::releaseMs (comp[c::release]) - cv::compReleaseMs) < 0.5);
  485. // gentle: a loud sine is turned down a little, a quiet one not at all
  486. CHECK (std::abs (c::trimDb (comp[c::output]) - cv::compMakeUpDb) < 0.01);
  487. if (! (c::staticGainDb (comp, -40.0) > -0.5 && c::staticGainDb (comp, -6.0) < -3.0 && c::staticGainDb (comp, -6.0) > -12.0))
  488. std::printf (" compressor static gain: %.2f dB at -40, %.2f dB at -6\n", c::staticGainDb (comp, -40.0), c::staticGainDb (comp, -6.0));
  489. CHECK (c::staticGainDb (comp, -40.0) > -0.5);
  490. CHECK (c::staticGainDb (comp, -6.0) < -3.0 && c::staticGainDb (comp, -6.0) > -12.0);
  491. }
  492. TEST (bl190ch_clean_voice_goes_first_on_the_insert_and_comes_off_cleanly)
  493. {
  494. using namespace lynx;
  495. namespace cv = cleanvoice;
  496. Song song;
  497. song.mixer = Song::defaultMixer (3);
  498. auto reverb = makeEffectSlot (EffectKind::reverb), delay = makeEffectSlot (EffectKind::delay);
  499. reverb.row = 2; // (FL's slot places: the user's effects sit in slots 3 and 6)
  500. delay.row = 5;
  501. song.mixer[2].effects = { reverb, delay };
  502. CHECK (! cv::present (song, 2) && cv::freeSlots (song, 2) == maxEffectSlots - 2);
  503. CHECK (cv::apply (song, 2));
  504. {
  505. const auto& fx = song.mixer[2].effects;
  506. CHECK (fx.size() == 6 && cv::slotsOf (song, 2) == std::vector<int> ({ 0, 1, 2, 3 }));
  507. const auto want = cv::chain();
  508. for (std::size_t i = 0; i < 4; ++i)
  509. CHECK (fx[i].kind == want[i].kind && fx[i].params == want[i].params);
  510. CHECK (fx[4].kind == EffectKind::reverb && fx[5].kind == EffectKind::delay); // the user's effects after it, in order
  511. const auto rows = mixerops::effectRows (fx);
  512. for (std::size_t i = 1; i < rows.size(); ++i)
  513. CHECK (rows[i] > rows[i - 1] && rows[i] < maxEffectSlots);
  514. CHECK (cv::present (song, 2) && ! cv::hasNoiseReduction (song, 2));
  515. }
  516. // pressing it again with noise reduction replaces the chain: nothing doubles
  517. cv::Options noise;
  518. noise.noiseReduction = true;
  519. CHECK (cv::apply (song, 2, noise));
  520. CHECK (song.mixer[2].effects.size() == 7 && song.mixer[2].effects[0].kind == EffectKind::quellDenoiser && cv::hasNoiseReduction (song, 2));
  521. CHECK (song.mixer[2].effects[5].kind == EffectKind::reverb && song.mixer[2].effects[6].kind == EffectKind::delay);
  522. // a slot edited by hand is still part of it (it is found by its name, not by its values)
  523. song.mixer[2].effects[1].params[(std::size_t) facet::param (0, 1)] = facet::hzValue (120.0);
  524. CHECK (cv::slotsOf (song, 2).size() == 5);
  525. cv::remove (song, 2);
  526. CHECK (song.mixer[2].effects.size() == 2 && song.mixer[2].effects[0].kind == EffectKind::reverb && song.mixer[2].effects[1].kind == EffectKind::delay);
  527. CHECK (! cv::present (song, 2));
  528. // other inserts were never touched; the Master and a missing insert refuse
  529. CHECK (song.mixer[1].effects.empty());
  530. const auto before = song;
  531. CHECK (! cv::apply (song, 0) && ! cv::apply (song, 99) && ! cv::apply (song, -1));
  532. CHECK (song.mixer[0].effects == before.mixer[0].effects);
  533. // no room: nothing changes
  534. while ((int) song.mixer[1].effects.size() < maxEffectSlots - 3)
  535. song.mixer[1].effects.push_back (reverb);
  536. const auto full = song.mixer[1].effects;
  537. CHECK (! cv::apply (song, 1));
  538. CHECK (song.mixer[1].effects == full);
  539. song.mixer[1].effects.pop_back();
  540. CHECK (cv::apply (song, 1) && (int) song.mixer[1].effects.size() == maxEffectSlots);
  541. CHECK (! cv::apply (song, 1, noise)); // one more slot than there is room for: the chain that is there stays
  542. CHECK ((int) song.mixer[1].effects.size() == maxEffectSlots && cv::slotsOf (song, 1).size() == 4);
  543. }
  544. TEST (bl190ch_clean_voice_cleans_a_noisy_take)
  545. {
  546. using namespace lynx;
  547. const auto second = (std::size_t) bl190ch::rate;
  548. // a "take": 1 s of room noise, 2 s of voice (a 220 Hz tone with harmonics) over the noise, 2 s of noise again; and
  549. // rumble at 35 Hz all the way through
  550. auto make = [second] (std::vector<float>& l, std::vector<float>& r) {
  551. bl190ch::Lcg hiss (5u);
  552. l.assign (5 * second, 0.0f);
  553. for (auto& v : l)
  554. v = hiss.next() * 0.0016f; // about −60 dBFS RMS
  555. bl190ch::sine (l, 0, l.size(), 35.0, -40.0);
  556. for (int harmonic = 1; harmonic <= 6; ++harmonic)
  557. bl190ch::sine (l, second, 3 * second, 220.0 * harmonic, -14.0 - 4.0 * harmonic);
  558. r = l;
  559. };
  560. std::vector<float> dryL, dryR, wetL, wetR, nrL, nrR;
  561. make (dryL, dryR);
  562. wetL = dryL;
  563. wetR = dryR;
  564. bl190ch::run (cleanvoice::chain(), wetL, wetR);
  565. cleanvoice::Options noise;
  566. noise.noiseReduction = true;
  567. nrL = dryL;
  568. nrR = dryR;
  569. bl190ch::run (cleanvoice::chain (noise), nrL, nrR);
  570. for (const auto* out : { &wetL, &wetR, &nrL, &nrR })
  571. for (const auto v : *out)
  572. if (! std::isfinite (v) || std::abs (v) > 2.0f)
  573. {
  574. CHECK (std::isfinite (v) && std::abs (v) <= 2.0f);
  575. break;
  576. }
  577. // the pause after the voice (its last second, the expander closed): rumble and hiss are well down
  578. const auto pauseDry = bl190ch::rmsDb (dryL, 4 * second, 5 * second), pauseWet = bl190ch::rmsDb (wetL, 4 * second, 5 * second);
  579. if (! (pauseWet < pauseDry - 15.0))
  580. std::printf (" pause dry %.1f wet %.1f | voice dry %.1f wet %.1f\n", pauseDry, pauseWet, bl190ch::rmsDb (dryL, second + second / 2, 3 * second - second / 2),
  581. bl190ch::rmsDb (wetL, second + second / 2, 3 * second - second / 2));
  582. CHECK (pauseWet < pauseDry - 15.0);
  583. // the voice itself keeps its level within a few dB (gentle compression, no gating of the words)
  584. const auto voiceDry = bl190ch::rmsDb (dryL, second + second / 2, 3 * second - second / 2);
  585. const auto voiceWet = bl190ch::rmsDb (wetL, second + second / 2, 3 * second - second / 2);
  586. CHECK (std::abs (voiceWet - voiceDry) < 4.0);
  587. // so the voice stands further above the pause than before
  588. CHECK ((voiceWet - pauseWet) > (voiceDry - pauseDry) + 12.0);
  589. // with the noise-reduction slot the pause is at least as quiet, and the voice is still there
  590. const auto pauseNr = bl190ch::rmsDb (nrL, 4 * second, 5 * second), voiceNr = bl190ch::rmsDb (nrL, second + second / 2, 3 * second - second / 2);
  591. if (! (pauseNr <= pauseWet + 0.5 && std::abs (voiceNr - voiceDry) < 5.0))
  592. std::printf (" with noise reduction: pause %.1f voice %.1f (plain chain: pause %.1f voice %.1f, dry voice %.1f)\n", pauseNr, voiceNr, pauseWet, voiceWet, voiceDry);
  593. CHECK (pauseNr <= pauseWet + 0.5);
  594. CHECK (std::abs (voiceNr - voiceDry) < 5.0);
  595. }