Issue32Tests.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // Round 168s (issue #32, Majed Salih): the problems found while making a whole track (docs/analise/REA_rodada168s.md)
  2. #define I32_REQUIRE(x) \
  3. do { if (! (x)) { CHECK (x); return; } } while (false)
  4. namespace i32
  5. {
  6. // a hosted plugin effect stand-in: counts how many were made, keeps a "live setting" that only the instance knows
  7. struct FakePluginFx final : lynx::Effect
  8. {
  9. static inline int made = 0, destroyed = 0;
  10. float liveSetting = 0.0f;
  11. FakePluginFx() { ++made; }
  12. ~FakePluginFx() override { ++destroyed; }
  13. void prepare (double) noexcept override {}
  14. void reset() noexcept override {}
  15. void process (float*, float*, int, const lynx::EffectParams&, double) noexcept override {}
  16. };
  17. lynx::RackState::Factories fakePluginFactories()
  18. {
  19. lynx::RackState::Factories f;
  20. f.pluginEffects = [] (const lynx::PluginRef&, double) { return std::make_shared<FakePluginFx>(); };
  21. f.samples = [] (const std::string& path) {
  22. auto s = std::make_shared<lynx::SampleData>();
  23. s->sampleRate = 44100.0;
  24. s->left.assign (44100, path.size() % 2 == 0 ? 0.25f : -0.25f);
  25. return std::shared_ptr<const lynx::SampleData> (s);
  26. };
  27. return f;
  28. }
  29. lynx::EffectSlot pluginSlot (const std::string& id)
  30. {
  31. auto slot = lynx::makeEffectSlot (lynx::EffectKind::plugin);
  32. slot.plugin.id = id;
  33. slot.plugin.name = id;
  34. slot.plugin.state = "c3RhdGU="; // what the project saved (the plugin's live settings are newer)
  35. return slot;
  36. }
  37. lynx::ChannelDef samplerChannel (const std::string& path)
  38. {
  39. lynx::ChannelDef c;
  40. c.name = path;
  41. c.instrument = lynx::InstrumentKind::sampler;
  42. c.samplePath = path;
  43. c.mixerTrack = 1;
  44. return c;
  45. }
  46. } // namespace i32
  47. TEST (issue32_moving_a_plugin_in_the_effect_chain_keeps_its_instance)
  48. {
  49. // #5 "When moving non native plugins inside the mixer chain up or down they reset all their parameters": the slot's
  50. // running plugin moves with it (main: both slots were made again from the saved state, every live change lost)
  51. lynx::Song song;
  52. song.mixer = lynx::Song::defaultMixer (3);
  53. song.mixer[1].effects = { i32::pluginSlot ("SynthMaster FX"), i32::pluginSlot ("Vital FX") };
  54. const auto f = i32::fakePluginFactories();
  55. i32::FakePluginFx::made = i32::FakePluginFx::destroyed = 0;
  56. auto a = lynx::RackState::build (song, nullptr, 48000.0, f);
  57. I32_REQUIRE (a->effects.size() > 1 && a->effects[1].size() == 2 && i32::FakePluginFx::made == 2);
  58. auto* first = a->effects[1][0].get();
  59. auto* second = a->effects[1][1].get();
  60. static_cast<i32::FakePluginFx*> (first)->liveSetting = 0.7f; // turned in its editor, not saved yet
  61. song.swapEffectSlots (1, 0, 1); // Move down
  62. song.uniqueInstances();
  63. auto b = lynx::RackState::build (song, a.get(), 48000.0, f);
  64. CHECK (i32::FakePluginFx::made == 2); // nothing made again
  65. CHECK (b->effects[1][0].get() == second && b->effects[1][1].get() == first);
  66. CHECK (static_cast<i32::FakePluginFx*> (b->effects[1][1].get())->liveSetting == 0.7f);
  67. // and to another insert (drag to insert 2): the same instance
  68. auto moved = song.mixer[1].effects[1];
  69. song.mixer[1].effects.erase (song.mixer[1].effects.begin() + 1);
  70. song.mixer[2].effects.push_back (moved);
  71. song.uniqueInstances();
  72. auto c = lynx::RackState::build (song, b.get(), 48000.0, f);
  73. CHECK (i32::FakePluginFx::made == 2);
  74. CHECK (c->effects[2].size() == 1 && c->effects[2][0].get() == first && c->effects[1][0].get() == second);
  75. // issue #36 (Presswerk crashed in its teardown after a slot move): with every older rack freed, no plugin was torn
  76. // down for the moves (main / PR #37 alone: a new instance was made and the old one destroyed)
  77. a.reset();
  78. b.reset();
  79. CHECK (i32::FakePluginFx::destroyed == 0);
  80. // two of the same plugin swapped (PR #37's duplicate case): each keeps its own instance
  81. song.mixer[1].effects.push_back (i32::pluginSlot ("SynthMaster FX"));
  82. song.uniqueInstances();
  83. auto d = lynx::RackState::build (song, c.get(), 48000.0, f);
  84. auto* x = d->effects[1][0].get();
  85. auto* y = d->effects[1][1].get();
  86. song.swapEffectSlots (1, 0, 1);
  87. song.uniqueInstances();
  88. auto e = lynx::RackState::build (song, d.get(), 48000.0, f);
  89. CHECK (e->effects[1][0].get() == y && e->effects[1][1].get() == x && i32::FakePluginFx::destroyed == 0);
  90. }
  91. TEST (issue36_retired_racks_go_to_the_owner_not_freed_in_the_collecting_callback)
  92. {
  93. // issue #36: the racks the audio thread returns are handed to `dispose` (the engine frees them from a message of
  94. // their own) instead of being deleted inside collectGarbage(), which ran in the engine's timer callback
  95. struct Probe
  96. {
  97. int* alive;
  98. explicit Probe (int* a) : alive (a) { ++*alive; }
  99. ~Probe() { --*alive; }
  100. };
  101. int alive = 0;
  102. std::vector<const Probe*> handed;
  103. {
  104. lynx::SnapshotExchange<Probe> x;
  105. x.dispose = [&handed] (const Probe* p) { handed.push_back (p); };
  106. x.publish (std::make_unique<Probe> (&alive));
  107. x.acquire();
  108. x.publish (std::make_unique<Probe> (&alive));
  109. x.acquire(); // the first one is returned
  110. x.collectGarbage();
  111. CHECK (handed.size() == 1 && alive == 2); // not freed by collectGarbage
  112. for (const auto* p : handed)
  113. delete p;
  114. CHECK (alive == 1);
  115. }
  116. CHECK (alive == 0); // the exchange still frees what it holds itself
  117. }
  118. TEST (issue32_a_pasted_copy_of_a_plugin_slot_gets_its_own_instance_once)
  119. {
  120. // a duplicated slot (paste, preset copy) shares the original's tag: it gets its own instance, made once, and later
  121. // edits rebuild nothing
  122. lynx::Song song;
  123. song.mixer = lynx::Song::defaultMixer (2);
  124. song.mixer[1].effects = { i32::pluginSlot ("Vital FX") };
  125. const auto f = i32::fakePluginFactories();
  126. i32::FakePluginFx::made = 0;
  127. song.uniqueInstances();
  128. auto a = lynx::RackState::build (song, nullptr, 48000.0, f);
  129. song.mixer[1].effects.push_back (song.mixer[1].effects[0]);
  130. song.uniqueInstances();
  131. CHECK (song.mixer[1].effects[0].instance.id != song.mixer[1].effects[1].instance.id);
  132. auto b = lynx::RackState::build (song, a.get(), 48000.0, f);
  133. CHECK (i32::FakePluginFx::made == 2 && b->effects[1][0].get() == a->effects[1][0].get());
  134. song.mixer[1].volume = 0.5f;
  135. song.uniqueInstances();
  136. auto c = lynx::RackState::build (song, b.get(), 48000.0, f);
  137. CHECK (i32::FakePluginFx::made == 2 && c->effects[1][1].get() == b->effects[1][1].get());
  138. // the slots still compare equal (the tag is identity, not content)
  139. CHECK (song.mixer[1].effects[0] == song.mixer[1].effects[1]);
  140. }
  141. TEST (issue32_adding_a_channel_while_playing_keeps_every_other_channels_instrument)
  142. {
  143. // #4 "The playback of the whole track is affected (reset sounds and loops) when adding new sounds": a channel
  144. // inserted above others (FL puts it after the selected channel) shifted every later channel, and each was made
  145. // again (new sample, no voices) or took its neighbour's instrument
  146. lynx::Song song;
  147. song.mixer = lynx::Song::defaultMixer (2);
  148. song.channels = { i32::samplerChannel ("kick.wav"), i32::samplerChannel ("loop1.wav"), i32::samplerChannel ("pad.wav") };
  149. song.patterns.emplace_back();
  150. const auto f = i32::fakePluginFactories();
  151. song.uniqueInstances();
  152. auto a = lynx::RackState::build (song, nullptr, 48000.0, f);
  153. I32_REQUIRE (a->instruments.size() == 3);
  154. lynx::insertChannel (song, 1, i32::samplerChannel ("snare.wav"));
  155. song.uniqueInstances();
  156. auto b = lynx::RackState::build (song, a.get(), 48000.0, f);
  157. I32_REQUIRE (b->instruments.size() == 4);
  158. CHECK (b->instruments[0].get() == a->instruments[0].get());
  159. CHECK (b->instruments[2].get() == a->instruments[1].get());
  160. CHECK (b->instruments[3].get() == a->instruments[2].get());
  161. CHECK (b->channelsMoved && b->channelFrom == (std::vector<int> { 0, -1, 1, 2 }));
  162. // removing it again: back where they were
  163. lynx::removeChannels (song, { 1 });
  164. song.uniqueInstances();
  165. auto c = lynx::RackState::build (song, b.get(), 48000.0, f);
  166. I32_REQUIRE (c->instruments.size() == 3);
  167. CHECK (c->instruments[1].get() == a->instruments[1].get() && c->instruments[2].get() == a->instruments[2].get());
  168. // a clone (after the original) is a new channel: the original keeps its instrument
  169. lynx::cloneChannel (song, 1);
  170. song.uniqueInstances();
  171. auto d = lynx::RackState::build (song, c.get(), 48000.0, f);
  172. I32_REQUIRE (d->instruments.size() == 4);
  173. CHECK (d->instruments[1].get() == a->instruments[1].get() && d->instruments[2].get() != a->instruments[1].get());
  174. CHECK (d->instruments[3].get() == a->instruments[2].get());
  175. }
  176. TEST (issue32_a_note_sounding_on_a_shifted_channel_keeps_sounding)
  177. {
  178. // the renderer side of #4: a held loop on channel 1 keeps playing through the edit that puts a channel above it
  179. lynx::Song song;
  180. song.mixer = lynx::Song::defaultMixer (2);
  181. song.channels = { i32::samplerChannel ("loop1.wav") };
  182. song.patterns.emplace_back();
  183. const auto f = i32::fakePluginFactories();
  184. song.uniqueInstances();
  185. auto a = lynx::RackState::build (song, nullptr, 48000.0, f);
  186. lynx::RackRenderer r;
  187. r.prepare (48000.0, 256);
  188. lynx::LiveEventQueue live;
  189. lynx::NoteEvent on;
  190. on.type = lynx::NoteEvent::Type::on;
  191. on.channel = 0;
  192. on.key = 60;
  193. on.velocity = 0.8f;
  194. live.push (on);
  195. std::vector<float> l (256), rr (256);
  196. for (int i = 0; i < 8; ++i)
  197. r.process (a.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  198. CHECK (std::abs (l[128]) > 0.01f);
  199. lynx::insertChannel (song, 0, i32::samplerChannel ("kick.wav"));
  200. song.uniqueInstances();
  201. auto b = lynx::RackState::build (song, a.get(), 48000.0, f);
  202. r.process (b.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  203. r.process (b.get(), false, 0.0, 256, l.data(), rr.data(), &live);
  204. CHECK (std::abs (l[128]) > 0.01f); // still sounding (main: the loop's sampler was made again, silent)
  205. CHECK ((r.soundingKeys (1)[0] >> 60) & 1u); // and it shows on its channel, now number 1
  206. }
  207. TEST (issue32_the_clicked_playlist_comes_to_front_like_fl)
  208. {
  209. // #8 "The sequencer window is not always on top. Some windows just get stuck on top until pressing F6 or F7": in FL
  210. // a click on the Playlist brings it over the Channel Rack and F6 brings the rack back (r168s f02 → f03 → f04); LYNX
  211. // kept the Playlist behind every floating panel. Only the Browser (docked) stays at the back.
  212. using namespace lynx::workspace;
  213. auto l = Layout::makeDefault (1600, 900);
  214. l.show (Panel::mixer);
  215. auto front = [&l] { return l.stacking().back(); };
  216. CHECK (front() == Panel::mixer);
  217. l.bringToFront (Panel::playlist); // the click on the Playlist
  218. CHECK (front() == Panel::playlist);
  219. CHECK (l.toggle (Panel::channelRack) && front() == Panel::channelRack); // F6: visible but covered → to front
  220. l.bringToFront (Panel::browser);
  221. CHECK (l.stacking().front() == Panel::browser && front() == Panel::channelRack);
  222. CHECK (isBackground (Panel::browser) && ! isBackground (Panel::playlist));
  223. }
  224. TEST (issue32_one_wheel_notch_moves_a_fader_one_fl_step)
  225. {
  226. // #2 "faders can react suddenly on the mouse wheel": one notch over an FL fader = 2 % of its travel (r168s c_f07: 0 dB
  227. // → +0.6 dB); LYNX read a Windows notch (JUCE deltaY 0.234) as 1.95 notches and moved 3.9 %
  228. constexpr double windowsNotch = 120.0 * 0.5 / 256.0, linuxNotch = 50.0 / 256.0;
  229. CHECK (lynx::controls::wheelNotches (windowsNotch, windowsNotch) == 1.0);
  230. CHECK (lynx::controls::wheelNotches (-windowsNotch, windowsNotch) == -1.0);
  231. CHECK (lynx::controls::wheelNotches (linuxNotch, linuxNotch) == 1.0);
  232. CHECK (lynx::controls::wheelNotches (3 * windowsNotch, windowsNotch) == 3.0);
  233. CHECK (lynx::controls::wheelNotches (windowsNotch / 4, windowsNotch) == 1.0); // a fine wheel's part-notch: still a step
  234. CHECK (lynx::controls::wheelNotches (0.0, windowsNotch) == 0.0);
  235. const auto after = lynx::controls::wheelValue (lynx::unityFader, lynx::controls::wheelNotches (windowsNotch, windowsNotch), 0.0, 1.0, false);
  236. CHECK (std::abs (after - 0.82) < 1e-6); // (unityFader is a float)
  237. CHECK (std::abs (20.0 * std::log10 (lynx::faderGain ((float) after)) - 0.6) < 0.05); // FL's hint: "0.6dB"
  238. }
  239. TEST (issue32_fit_to_tempo_keeps_a_loop_in_time)
  240. {
  241. // #7 "Fit to project tempo stretch sometimes out of tempo": main assumed 1, 2, 4, 8... beats, so a 3-bar loop came
  242. // out 16 beats long and a loop with a tail was squeezed so its beats came early
  243. using lynx::clipops::fitBeats;
  244. const auto threeBars = 12.0 * 60.0 / 100.0; // 3 bars at 100 BPM in a 128 BPM project
  245. CHECK (std::abs (fitBeats (threeBars, 128.0, 0.0f, 100.0) - 12.0f) < 1e-4f); // main: 16
  246. const auto withTail = 16.0 * 60.0 / 90.0 + 0.3; // 4 bars at 90 BPM + a 0.3 s tail, at 100 BPM
  247. CHECK (std::abs (fitBeats (withTail, 100.0, 0.0f, 90.0) - 16.45f) < 1e-3f); // main: 16 (the loop 2.7 % short)
  248. CHECK (fitBeats (withTail, 100.0, 6.0f, 90.0) == 6.0f); // a WAV's ACID beats win ("embedded")
  249. CHECK (std::abs (fitBeats (16.0 * 60.0 / 90.0, 140.0, 0.0f, 90.0) - 32.0f) < 1e-3f); // the octave nearest 140: 180 BPM
  250. CHECK (fitBeats (2.4, 128.0) == 4.0f); // nothing known: a power of two, as before
  251. const auto presets = lynx::clipops::timePresetBeats (lynx::Meter (4, 4));
  252. CHECK (presets == (std::vector<float> { 1.0f, 2.0f, 4.0f, 8.0f, 12.0f, 16.0f }));
  253. // the whole path: a click loop of 3 bars at 100 BPM with a tail, detected (no ACID chunk)
  254. lynx::SampleData s;
  255. s.sampleRate = 44100.0;
  256. const auto beatFrames = 44100.0 * 60.0 / 100.0;
  257. s.left.assign ((std::size_t) (12 * beatFrames + 0.25 * 44100.0), 0.0f);
  258. for (int b = 0; b < 12; ++b)
  259. for (int i = 0; i < 600; ++i)
  260. s.left[(std::size_t) (b * beatFrames) + (std::size_t) i] = (b % 4 == 0 ? 0.9f : 0.5f) * std::exp (-i / 120.0f) * (i % 20 < 10 ? 1.0f : -1.0f);
  261. const auto got = lynx::tempodetect::fitBeatsOf (&s, 128.0);
  262. const auto want = (float) ((double) s.left.size() / 44100.0 * 100.0 / 60.0); // 12.42 beats: the loop stays 12 beats long
  263. CHECK (std::abs (got - want) < 0.05f);
  264. }
  265. TEST (digiwheel168s_number_boxes_are_dark_displays_red_only_while_touched)
  266. {
  267. // RANGE / TRACK in the channel settings header and the rack's insert box: FL draws its digiwheels as small dark
  268. // displays, not filled buttons; in the LYNX skin the accent (red) shows only while hovered or dragged
  269. using namespace lynx::themes;
  270. for (const auto& t : builtInThemes())
  271. {
  272. const auto p = derivePalette (t.accent, t.background);
  273. const auto at = [&p] (bool hot, bool dimmed)
  274. { return digitDisplayColours (p.bg0, p.border, p.accent, p.accentHover, p.textPrimary, p.textDisabled, hot, dimmed); };
  275. const auto rest = at (false, false);
  276. CHECK (rest.fill == p.bg0 && rest.edge == p.border && rest.text == p.textPrimary); // no red at rest
  277. CHECK (rest.fill != p.accent && rest.edge != p.accent && rest.text != p.accent);
  278. CHECK (contrastRatio (rest.text, rest.fill) >= 7.0);
  279. CHECK (at (false, true).text == p.textDisabled); // "-": no insert
  280. const auto hot = at (true, true);
  281. CHECK (hot.fill == p.bg0 && hot.edge == p.accent && hot.text == p.accentHover); // red while touched, still dark
  282. CHECK (contrastRatio (hot.text, hot.fill) >= 3.0);
  283. }
  284. }