Visual164Tests.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // --- Visual plugins: Wave Candy and the Visualizer (core/VisualFx.h) - round 164c -----------------------------------
  2. #include "core/VisualFx.h"
  3. namespace visual164
  4. {
  5. std::vector<float> sine (double hz, double rate, int n, float amplitude = 1.0f)
  6. {
  7. std::vector<float> x ((std::size_t) n);
  8. for (int i = 0; i < n; ++i)
  9. x[(std::size_t) i] = amplitude * (float) std::sin (2.0 * 3.14159265358979 * hz * i / rate);
  10. return x;
  11. }
  12. } // namespace visual164
  13. TEST (visual164_tap_ring_and_pass_through)
  14. {
  15. using namespace lynx;
  16. auto fx = visualfx::make();
  17. fx->prepare (48000.0);
  18. auto* tap = visualfx::tapOf (fx.get());
  19. CHECK (tap != nullptr && visualfx::tapOf (makeEffect (EffectKind::eq).get()) == nullptr);
  20. std::vector<float> l (100), r (100);
  21. for (int i = 0; i < 100; ++i)
  22. {
  23. l[(std::size_t) i] = (float) i;
  24. r[(std::size_t) i] = (float) -i;
  25. }
  26. const auto before = l;
  27. auto slot = makeEffectSlot (EffectKind::waveCandy);
  28. fx->process (l.data(), r.data(), 100, slot.params, 120.0);
  29. CHECK (l == before); // the sound is not touched
  30. std::vector<float> ol (150), orr (150);
  31. CHECK (tap->read (ol.data(), orr.data(), 50) == 100);
  32. CHECK (ol[0] == 50.0f && ol[49] == 99.0f && orr[49] == -99.0f);
  33. // more than was written: zeros first
  34. tap->read (ol.data(), orr.data(), 150);
  35. CHECK (ol[0] == 0.0f && ol[49] == 0.0f && ol[50] == 0.0f && ol[51] == 1.0f && ol[149] == 99.0f);
  36. // the parameters the audio thread used
  37. CHECK (tap->param (visualfx::wavecandy::mode) == slot.params[0]);
  38. // the scene lives in the state: an edit keeps the running effect (no rebuild, no click)
  39. CHECK (fx->acceptState ({ 1, 2, 3 }));
  40. // wraps around the ring
  41. std::vector<float> big ((std::size_t) visualfx::tapSize + 10, 0.25f);
  42. fx->process (big.data(), big.data(), (int) big.size(), slot.params, 120.0);
  43. tap->read (ol.data(), orr.data(), 10);
  44. CHECK (ol[9] == 0.25f && tap->written() == 100 + big.size());
  45. }
  46. TEST (visual164_capture_records_only_while_playing)
  47. {
  48. using namespace lynx;
  49. auto fx = visualfx::make();
  50. fx->prepare (44100.0);
  51. visualfx::Capture cap;
  52. cap.reserve (256);
  53. CHECK (visualfx::setCapture (fx.get(), &cap));
  54. auto slot = makeEffectSlot (EffectKind::visualizer);
  55. slot.params[0] = 0.75f;
  56. std::vector<float> l (64, 0.5f), r (64, -0.5f);
  57. fx->process (l.data(), r.data(), 64, slot.params, 120.0); // not playing (an export's warm-up)
  58. CHECK (cap.used == 0);
  59. fx->setPlaying (true);
  60. fx->process (l.data(), r.data(), 64, slot.params, 120.0);
  61. fx->process (l.data(), r.data(), 64, slot.params, 120.0);
  62. CHECK (cap.used == 128 && cap.left[127] == 0.5f && cap.right[0] == -0.5f);
  63. CHECK (cap.paramsUsed == 2 && cap.params[1].at == 64 && cap.params[1].values[0] == 0.75f);
  64. // full: stops (never grows the vectors on the audio thread)
  65. for (int i = 0; i < 4; ++i)
  66. fx->process (l.data(), r.data(), 64, slot.params, 120.0);
  67. CHECK (cap.overflow && cap.used == 256 && cap.left.size() == 256);
  68. CHECK (visualfx::setCapture (fx.get(), nullptr));
  69. CHECK (! visualfx::setCapture (makeEffect (EffectKind::eq).get(), &cap));
  70. }
  71. TEST (visual164_spectrum_and_trigger)
  72. {
  73. using namespace lynx::visualfx::analysis;
  74. const auto x = visual164::sine (1000.0, 48000.0, 4096);
  75. std::vector<float> db;
  76. std::vector<std::complex<float>> work;
  77. spectrumDb (x.data(), 4096, db, work);
  78. CHECK (db.size() == 2049);
  79. const auto peak = (int) (std::max_element (db.begin(), db.end()) - db.begin());
  80. CHECK (std::abs (peak - (int) std::lround (1000.0 * 4096 / 48000.0)) <= 1);
  81. CHECK (db[(std::size_t) peak] > -1.6f && db[(std::size_t) peak] < 0.3f); // a full-scale sine ≈ 0 dB (Hann scalloping < 1.5 dB)
  82. CHECK (db[20] < -60.0f);
  83. // log bands: the loudest band holds 1 kHz
  84. std::array<float, 32> bands {};
  85. logBands (db, 48000.0, 4096, 20.0, 20000.0, bands.data(), 32);
  86. const auto top = (int) (std::max_element (bands.begin(), bands.end()) - bands.begin());
  87. const auto lo = 20.0 * std::pow (1000.0, top / 32.0), hi = 20.0 * std::pow (1000.0, (top + 1) / 32.0);
  88. CHECK (lo <= 1050.0 && hi >= 950.0);
  89. // tilt: +3 dB/oct around 1 kHz lifts the treble bands
  90. std::array<float, 32> tilted {};
  91. logBands (db, 48000.0, 4096, 20.0, 20000.0, tilted.data(), 32, 3.0);
  92. CHECK (tilted[31] > bands[31] + 9.0f && tilted[0] < bands[0] - 9.0f);
  93. // the scope's trigger: a rising zero crossing
  94. const auto y = visual164::sine (100.0, 48000.0, 4800);
  95. const auto t = triggerPoint (y.data(), 4800, 960);
  96. CHECK (t <= 4800 - 960 && t > 4800 - 2 * 960 && y[(std::size_t) t - 1] < 0.0f && y[(std::size_t) t] >= 0.0f);
  97. }
  98. TEST (visual164_meter_modes)
  99. {
  100. using namespace lynx::visualfx::analysis;
  101. const auto x = visual164::sine (997.0, 48000.0, 48000 * 3);
  102. auto run = [&] (MeterMode mode) {
  103. Meter m;
  104. m.prepare (48000.0);
  105. for (std::size_t at = 0; at + 512 <= x.size(); at += 512)
  106. m.process (x.data() + at, x.data() + at, 512, mode, 1.0, 20.0);
  107. return m;
  108. };
  109. CHECK (std::abs (run (MeterMode::peak).levelDb (0)) < 0.05f);
  110. CHECK (std::abs (run (MeterMode::rms).levelDb (0) + 3.01f) < 0.2f);
  111. CHECK (std::abs (run (MeterMode::vu).levelDb (1) + 3.01f) < 0.3f);
  112. CHECK (run (MeterMode::ppm).levelDb (0) > -0.5f);
  113. // BS.1770: a full-scale 997 Hz sine in both channels = 0 LUFS (−3.01 per channel, summed)
  114. const auto lufs = run (MeterMode::lufs);
  115. CHECK (std::abs (lufs.loudness()) < 0.4f);
  116. // silence afterwards: the peak bar falls at the decay speed, the marker holds first
  117. Meter m;
  118. m.prepare (48000.0);
  119. m.process (x.data(), x.data(), 4800, MeterMode::peak, 1.0, 20.0);
  120. std::vector<float> quiet (24000, 0.0f);
  121. m.process (quiet.data(), quiet.data(), 24000, MeterMode::peak, 1.0, 20.0); // 0.5 s
  122. CHECK (std::abs (m.levelDb (0) + 10.0f) < 0.5f && m.peakDb (0) > -0.1f);
  123. m.process (quiet.data(), quiet.data(), 24000, MeterMode::peak, 1.0, 20.0);
  124. m.process (quiet.data(), quiet.data(), 24000, MeterMode::peak, 1.0, 20.0); // hold (1 s) over: it falls too
  125. CHECK (m.peakDb (0) < -5.0f && m.maxDb() > -0.1f);
  126. }
  127. TEST (visual164_wave_candy_params_and_fl_state)
  128. {
  129. using namespace lynx;
  130. namespace wc = visualfx::wavecandy;
  131. CHECK (effectInfo (EffectKind::waveCandy).params.size() == (std::size_t) wc::count);
  132. CHECK (effectInfo (EffectKind::visualizer).params.size() == (std::size_t) visualfx::visualizer::count);
  133. for (int m = 0; m < wc::modeCount; ++m)
  134. CHECK (wc::modeOf (wc::modeValue ((wc::Mode) m)) == (wc::Mode) m);
  135. CHECK (wc::resolve (wc::Mode::automatic, 80, 400) == wc::Mode::peakMeter);
  136. CHECK (wc::resolve (wc::Mode::automatic, 900, 120) == wc::Mode::oscilloscope);
  137. CHECK (wc::resolve (wc::Mode::automatic, 600, 400) == wc::Mode::spectrum);
  138. CHECK (wc::resolve (wc::Mode::vectorscope, 80, 400) == wc::Mode::vectorscope);
  139. CHECK (formatEffectParam (EffectKind::waveCandy, wc::mode, wc::modeValue (wc::Mode::spectrogram), 120.0) == "Spectrogram");
  140. CHECK (formatEffectParam (EffectKind::waveCandy, wc::gain, 0.5f, 120.0) == "+0.0 dB");
  141. CHECK (formatEffectParam (EffectKind::waveCandy, wc::specResolution, 0.5f, 120.0) == "4096 points");
  142. CHECK (formatEffectParam (EffectKind::waveCandy, wc::meterMode, wc::meterModeValue (visualfx::analysis::MeterMode::lufs), 120.0) == "LUFS");
  143. CHECK (std::abs (wc::windowMs (0.0f) - 2.0) < 1e-6 && std::abs (wc::windowMs (1.0f) - 500.0) < 1e-6);
  144. CHECK (wc::fftSize (0.0f) == 1024 && wc::fftSize (1.0f) == 16384);
  145. // FL's state: … [title] [font] [height < 0] [4 bytes] [mode]
  146. auto flState = [] (const std::string& title, int mode) {
  147. std::vector<std::uint8_t> s (40, 0);
  148. s.push_back ((std::uint8_t) title.size());
  149. s.insert (s.end(), title.begin(), title.end());
  150. const std::string font = "Trebuchet MS";
  151. s.push_back ((std::uint8_t) font.size());
  152. s.insert (s.end(), font.begin(), font.end());
  153. for (const auto b : { 0xf1, 0xff, 0xff, 0xff, 0x01, 0x02, 0x6e, 0x02 })
  154. s.push_back ((std::uint8_t) b);
  155. for (int i = 0; i < 4; ++i)
  156. s.push_back ((std::uint8_t) (mode >> (8 * i)));
  157. s.resize (s.size() + 60, 0);
  158. return s;
  159. };
  160. const std::pair<int, wc::Mode> modes[] = { { 0, wc::Mode::oscilloscope }, { 1, wc::Mode::spectrogram }, { 2, wc::Mode::peakMeter },
  161. { 3, wc::Mode::vectorscope } };
  162. for (const auto& [flMode, mode] : modes)
  163. {
  164. auto slot = makeEffectSlot (EffectKind::waveCandy);
  165. CHECK (wc::fromFl (flState ("Wave Candy", flMode), slot));
  166. CHECK (wc::modeOf (slot.params[0]) == mode);
  167. const auto made = effectFromFl ("Wave Candy", flState ("Meter", flMode));
  168. CHECK (made && made->kind == EffectKind::waveCandy && wc::modeOf (made->params[0]) == mode);
  169. }
  170. auto slot = makeEffectSlot (EffectKind::waveCandy);
  171. CHECK (! wc::fromFl (std::vector<std::uint8_t> (200, 0), slot));
  172. CHECK (effectForFlPlugin ("Wave Candy") == EffectKind::waveCandy);
  173. CHECK (effectForFlPlugin ("ZGameEditor Visualizer") == EffectKind::visualizer);
  174. CHECK (flPluginsForEffect (EffectKind::waveCandy) == std::vector<std::string> { "Wave Candy" });
  175. // FL's own presets (the installation's Wave Candy "Function" presets)
  176. if (! realFlpDir.empty())
  177. {
  178. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / "Wave Candy" / "Function";
  179. const std::pair<const char*, wc::Mode> presets[] = { { "Oscilloscope.fst", wc::Mode::oscilloscope }, { "Spectrum.fst", wc::Mode::spectrogram },
  180. { "Peak meter.fst", wc::Mode::peakMeter }, { "Vectorscope.fst", wc::Mode::vectorscope } };
  181. for (const auto& [file, mode] : presets)
  182. {
  183. std::ifstream in (dir / file, std::ios::binary);
  184. if (! in)
  185. continue;
  186. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  187. const auto fx = effectFromFlPreset (bytes);
  188. CHECK (fx && fx->kind == EffectKind::waveCandy && wc::modeOf (fx->params[0]) == mode);
  189. }
  190. }
  191. }
  192. TEST (visual164_scene_text_round_trip)
  193. {
  194. using namespace lynx::visualfx::scene;
  195. for (const auto& preset : presets())
  196. {
  197. const auto back = parse (write (preset));
  198. CHECK (back && *back == preset);
  199. CHECK (! preset.layers.empty() && preset.layers.front().type == LayerType::background);
  200. }
  201. Scene s;
  202. s.name = "Mine\nline 2";
  203. s.width = 1080;
  204. s.height = 1920;
  205. s.fps = 60;
  206. Layer t;
  207. t.type = LayerType::text;
  208. t.text = "Hello\nWorld \\ back";
  209. t.path = "C:\\pics\\logo.png";
  210. t.colourA = 0x80112233;
  211. t.band = Band::high;
  212. t.count = 7;
  213. t.mirror = true;
  214. s.layers.push_back (t);
  215. const auto back = fromState (toState (s));
  216. CHECK (back && *back == s);
  217. CHECK (! parse ("not a scene"));
  218. // out-of-range values are clamped
  219. const auto odd = parse ("LYNX visualizer 1\nsize=99999x3\nfps=500\nlayer=bars\nsize=7\ncount=100000\nopacity=-2\n");
  220. CHECK (odd && odd->width == 1920 && odd->fps == 120 && odd->layers.size() == 1 && odd->layers[0].size == 1.0f
  221. && odd->layers[0].count == 512 && odd->layers[0].opacity == 0.0f);
  222. // a new Visualizer slot carries LYNX's default scene
  223. const auto slot = lynx::makeEffectSlot (lynx::EffectKind::visualizer);
  224. CHECK (fromState (slot.state) && *fromState (slot.state) == defaultScene());
  225. }
  226. TEST (visual164_zgameeditor_state)
  227. {
  228. using namespace lynx::visualfx::scene;
  229. const std::string ini = "FLhd\x01\x02[General]\r\nGlWindowMode=1\r\nLayerCount=4\r\nLayerOrder=0,2,1,3\r\n"
  230. "[AppSet0]\r\nApp=Background\\FourCornerGradient\r\nEnabled=1\r\n"
  231. "[AppSet1]\r\nApp=Text\\TextTrueType\r\nEnabled=1\r\n"
  232. "[AppSet2]\r\nApp=Peak Effects\\Linear\r\nEnabled=1\r\n"
  233. "[AppSet3]\r\nApp=Terrain\\GoopFlow\r\nEnabled=1\r\n"
  234. "[Video export]\r\nVideoW=1280\r\nVideoH=720\r\nVideoRenderFps=60\r\n"
  235. "[UserContent]\r\nText=\"My song\",\"by me\"\r\nImages=\r\n";
  236. std::vector<std::string> unmapped;
  237. const auto s = fromZge ({ ini.begin(), ini.end() }, &unmapped);
  238. // (round 165e: Terrain maps to a GPU terrain layer)
  239. CHECK (s && s->layers.size() == 4);
  240. CHECK (s->layers[0].type == LayerType::background && s->layers[1].type == LayerType::bars && s->layers[2].type == LayerType::text
  241. && s->layers[3].type == LayerType::terrain);
  242. CHECK (s->layers[2].text == "My song\nby me");
  243. CHECK (s->width == 1280 && s->height == 720 && s->fps == 60);
  244. CHECK (unmapped.empty());
  245. // only effects without a counterpart: LYNX's default scene (in the project's video size)
  246. const std::string only = "[General]\nLayerCount=1\n[AppSet0]\nApp=Feedback\\70sKaleido\n[Video export]\nVideoW=640\nVideoH=480\n";
  247. const auto fallback = fromZge ({ only.begin(), only.end() });
  248. // (round 165e: the feedback layer stays over the default scene)
  249. CHECK (fallback && fallback->layers.size() == defaultScene().layers.size() + 1 && fallback->width == 640
  250. && fallback->layers.back().type == LayerType::feedback);
  251. CHECK (! fromZge ({ 1, 2, 3 }));
  252. const auto fx = lynx::effectFromFl ("ZGameEditor Visualizer", { ini.begin(), ini.end() });
  253. CHECK (fx && fx->kind == lynx::EffectKind::visualizer && fromState (fx->state) && fromState (fx->state)->layers.size() == 4);
  254. if (! realFlpDir.empty())
  255. {
  256. // every FL preset of the plugin is read (an INI), and each gives a scene that draws something
  257. const auto dir = std::filesystem::path (realFlpDir) / "Patches" / "Plugin presets" / "Effects" / "ZGameEditor Visualizer";
  258. int read = 0;
  259. std::error_code ec;
  260. for (auto it = std::filesystem::recursive_directory_iterator (dir, ec); ! ec && it != std::filesystem::recursive_directory_iterator(); it.increment (ec))
  261. if (it->path().extension() == ".fst")
  262. {
  263. std::ifstream in (it->path(), std::ios::binary);
  264. const std::vector<std::uint8_t> bytes ((std::istreambuf_iterator<char> (in)), std::istreambuf_iterator<char>());
  265. const auto scene = fromZge (bytes);
  266. CHECK (scene && scene->layers.size() >= 2);
  267. ++read;
  268. }
  269. CHECK (read == 0 || read > 200);
  270. }
  271. }
  272. TEST (visual164_frames_and_beats)
  273. {
  274. using namespace lynx::visualfx;
  275. std::array<float, shownParams> params {};
  276. params[visualizer::sensitivity] = 0.5f;
  277. params[visualizer::beatThreshold] = 0.4f;
  278. params[visualizer::speed] = 0.5f;
  279. const double rate = 48000.0, fps = 30.0;
  280. // 120 BPM: a 60 Hz kick burst every half second over a quiet hi-hat noise; 4 s
  281. const int n = (int) (rate * 4.0);
  282. std::vector<float> x ((std::size_t) n);
  283. std::uint32_t noise = 1;
  284. for (int i = 0; i < n; ++i)
  285. {
  286. const auto inBeat = std::fmod ((double) i / rate, 0.5);
  287. const auto kick = inBeat < 0.12 ? std::sin (2.0 * 3.14159265 * 60.0 * inBeat) * std::exp (-inBeat * 20.0) : 0.0;
  288. noise = noise * 1664525u + 1013904223u;
  289. x[(std::size_t) i] = (float) (0.8 * kick + 0.02 * ((double) (noise >> 8) / 16777216.0 - 0.5));
  290. }
  291. FrameAnalyzer analyzer;
  292. int beats = 0;
  293. float bassMax = 0.0f, highMax = 0.0f;
  294. for (int frame = 0; frame < (int) (4.0 * fps); ++frame)
  295. {
  296. const auto end = std::max (1, (int) (frame / fps * rate));
  297. const auto& f = analyzer.analyze (x.data(), x.data(), end, rate, frame / fps, 1.0 / fps, params);
  298. beats += f.onset ? 1 : 0;
  299. bassMax = std::max (bassMax, f.bass);
  300. highMax = std::max (highMax, f.high);
  301. CHECK (f.beat >= 0.0f && f.beat <= 1.0f && f.level >= 0.0f && f.level <= 1.0f);
  302. }
  303. CHECK (beats >= 6 && beats <= 9); // 8 kicks (the first may start before the average settles)
  304. CHECK (bassMax > highMax + 0.2f);
  305. // silence: nothing moves
  306. FrameAnalyzer quiet;
  307. std::vector<float> zero (4096, 0.0f);
  308. for (int frame = 0; frame < 20; ++frame)
  309. {
  310. const auto& f = quiet.analyze (zero.data(), zero.data(), 4096, rate, frame / fps, 1.0 / fps, params);
  311. CHECK (! f.onset && f.level == 0.0f && f.bass == 0.0f);
  312. }
  313. // a quiet mix (-26 dB, 80 Hz) still draws a wave: the waveform's automatic gain brings it up within ~3 s
  314. {
  315. FrameAnalyzer agc;
  316. std::vector<float> sine (8192);
  317. float peak = 0.0f;
  318. for (int frame = 0; frame < 120; ++frame)
  319. {
  320. for (int i = 0; i < 8192; ++i)
  321. sine[(std::size_t) i] = 0.05f * (float) std::sin (2.0 * 3.14159265 * 80.0 * ((double) frame * 1600 + i) / rate);
  322. const auto& f = agc.analyze (sine.data(), sine.data(), 8192, rate, frame / fps, 1.0 / fps, params);
  323. if (frame == 119)
  324. for (const auto v : f.waveL)
  325. peak = std::max (peak, std::abs (v));
  326. }
  327. CHECK (peak > 0.25f && peak < 0.75f);
  328. }
  329. // particles: deterministic, a burst on a beat
  330. scene::Layer layer;
  331. layer.type = scene::LayerType::particles;
  332. layer.count = 100;
  333. ParticleField a, b;
  334. Frame f;
  335. f.params = params;
  336. f.dt = 1.0 / 30.0;
  337. for (int i = 0; i < 30; ++i)
  338. {
  339. f.onset = i == 10;
  340. f.bass = i >= 10 ? 0.8f : 0.1f;
  341. const auto before = a.particles().size();
  342. a.step (f, layer, 16.0f / 9.0f);
  343. b.step (f, layer, 16.0f / 9.0f);
  344. if (i == 10)
  345. CHECK (a.particles().size() > before + 10);
  346. CHECK (a.particles().size() <= 200);
  347. }
  348. CHECK (a.particles().size() == b.particles().size() && a.particles().back().x == b.particles().back().x);
  349. }
  350. TEST (visual164_catalog_visual_category)
  351. {
  352. using namespace lynx::plugincatalog;
  353. auto fx = [] (const char* name, const char* category) { return Entry { "id", name, "Vendor", "VST3", category, false }; };
  354. CHECK (categoryOf (fx ("Plugin Visualizer Pro", "Fx")) == "Visual");
  355. CHECK (categoryOf (fx ("Mystery", "Fx|Visual")) == "Visual");
  356. CHECK (categoryOf (fx ("SPAN", "Fx|Analyzer")) == "Analyzer");
  357. CHECK (std::find (categories (false).begin(), categories (false).end(), "Visual") != categories (false).end());
  358. }