PlugLevel190adTests.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Round 190ad: hosted plugins of an FL project played 3 dB under FL (Surge XT VST3, FL 2026 renders, REA_rodada190ad.md).
  2. // Fruity Wrapper scales the plugin's output by the host's pan kernel of (channel pan, 2 × the channel volume curve):
  3. // √2 × the curve at the centre, 2 × on the side panned to; LYNX's own channel law is 1 × at the centre. Projects
  4. // imported from .flp follow FL (Song::flPluginLevels); LYNX's own projects keep their law.
  5. namespace plug190ad
  6. {
  7. // a fake hosted plugin: keeps the gains the rack hands it
  8. struct GainSpy final : lynx::Instrument
  9. {
  10. float gl = -1.0f, gr = -1.0f;
  11. void prepare (double) noexcept override {}
  12. void noteOn (int, float) noexcept override {}
  13. void noteOff (int) noexcept override {}
  14. void allNotesOff() noexcept override {}
  15. void render (float*, float*, int, float l, float r) noexcept override { gl = l; gr = r; }
  16. };
  17. // the gains the rack hands a hosted plugin channel, under FL's levels or LYNX's own law
  18. std::pair<float, float> gains (bool flLevels, float volume, float pan, bool triangular = false)
  19. {
  20. using namespace lynx;
  21. Song song;
  22. song.mixer = Song::defaultMixer (2);
  23. song.flPluginLevels = flLevels;
  24. song.panTriangular = triangular;
  25. ChannelDef c;
  26. c.instrument = InstrumentKind::plugin;
  27. c.plugin.id = "spy";
  28. c.volume = volume;
  29. c.pan = pan;
  30. song.channels.push_back (c);
  31. auto& p = song.patterns.emplace_back();
  32. p.notes = { { 0, 960, 0, 60 } };
  33. auto spy = std::make_shared<GainSpy>();
  34. RackState::Factories f;
  35. f.pluginInstruments = [spy] (const PluginRef&, double) { return std::shared_ptr<Instrument> (spy); };
  36. const auto state = RackState::build (song, nullptr, 44100.0, f);
  37. if (state == nullptr)
  38. return { -1.0f, -1.0f };
  39. RackRenderer r;
  40. r.prepare (44100.0, 256);
  41. std::vector<float> l (256), rr (256);
  42. r.process (state.get(), true, 0.0, 256, l.data(), rr.data());
  43. return { spy->gl, spy->gr };
  44. }
  45. bool close (float a, float b, float tolerance = 2.0e-6f) { return std::abs (a - b) <= tolerance; }
  46. float db (float ratio) { return 20.0f * std::log10 (ratio); }
  47. } // namespace plug190ad
  48. TEST (plug190ad_lynx_projects_keep_their_plugin_level)
  49. {
  50. using namespace lynx;
  51. // LYNX's own law: the curve at the centre, √2 × on the side panned to — unchanged for every project saved before
  52. for (const auto v : { 0.25f, 0.5f, 0.78f, 1.0f })
  53. {
  54. const auto [l, r] = plug190ad::gains (false, v, 0.0f);
  55. const auto angle = 0.25f * 3.14159265f;
  56. CHECK (plug190ad::close (l, channelGain (v) * std::cos (angle) * 1.41421356f, 1.0e-7f)); // (the old panGains)
  57. CHECK (plug190ad::close (r, channelGain (v) * std::sin (angle) * 1.41421356f, 1.0e-7f));
  58. }
  59. const auto [l, r] = plug190ad::gains (false, 0.78f, -1.0f);
  60. CHECK (plug190ad::close (l, channelGain (0.78f) * 1.41421356f));
  61. CHECK (std::abs (r) < 1.0e-6f);
  62. // the triangular law never reached a plugin channel under LYNX's own law
  63. CHECK (plug190ad::gains (false, 0.78f, -0.5f, true) == plug190ad::gains (false, 0.78f, -0.5f, false));
  64. }
  65. TEST (plug190ad_fl_projects_play_hosted_plugins_at_the_wrapper_level)
  66. {
  67. using namespace lynx;
  68. const float sqrt2 = 1.41421356f;
  69. // centre: √2 × the curve, +3.01 dB over LYNX's own law at every volume (FL's renders: +3.0 dB at 25 / 50 / 78 / 100 %)
  70. for (const auto v : { 0.25f, 0.5f, 0.78f, 1.0f })
  71. {
  72. const auto [l, r] = plug190ad::gains (true, v, 0.0f);
  73. CHECK (plug190ad::close (l, channelGain (v) * sqrt2));
  74. CHECK (plug190ad::close (r, channelGain (v) * sqrt2));
  75. const auto own = plug190ad::gains (false, v, 0.0f);
  76. CHECK (std::abs (plug190ad::db (l / own.first) - 3.0103f) < 0.001f);
  77. }
  78. // FL's default channel (78 %) leaves a plugin at −2.2 dB, where LYNX's own law leaves it at −5.2 dB
  79. CHECK (std::abs (plug190ad::db (plug190ad::gains (true, 0.78f, 0.0f).first) - -2.20f) < 0.01f);
  80. CHECK (std::abs (plug190ad::db (plug190ad::gains (false, 0.78f, 0.0f).first) - -5.21f) < 0.01f);
  81. // pan: FL's kernel of (pan, 2 × the curve): the side panned to reaches 2 ×, the other 0
  82. const auto g = channelGain (0.78f);
  83. {
  84. const auto [l, r] = plug190ad::gains (true, 0.78f, -1.0f);
  85. CHECK (plug190ad::close (l, 2.0f * g));
  86. CHECK (std::abs (r) < 1.0e-6f);
  87. }
  88. {
  89. const auto [l, r] = plug190ad::gains (true, 0.78f, 1.0f);
  90. CHECK (plug190ad::close (r, 2.0f * g));
  91. CHECK (std::abs (l) < 1.0e-6f);
  92. }
  93. {
  94. const auto [l, r] = plug190ad::gains (true, 0.78f, -0.5f);
  95. CHECK (plug190ad::close (l, 2.0f * g * std::cos (3.14159265f / 8.0f)));
  96. CHECK (plug190ad::close (r, 2.0f * g * std::sin (3.14159265f / 8.0f)));
  97. }
  98. // the wrapper's pan is an integer −64…64 (FL's renders: R / L = 1.0249 at 1.5 / 64, 1.0503 at 2.5 / 64, 1 at 0.4 / 64)
  99. {
  100. CHECK (plug190ad::gains (true, 0.78f, 1.5f / 64.0f) == plug190ad::gains (true, 0.78f, 1.0f / 64.0f));
  101. CHECK (plug190ad::gains (true, 0.78f, 2.5f / 64.0f) == plug190ad::gains (true, 0.78f, 2.0f / 64.0f));
  102. CHECK (plug190ad::gains (true, 0.78f, 0.4f / 64.0f) == plug190ad::gains (true, 0.78f, 0.0f));
  103. CHECK (plug190ad::gains (true, 0.78f, -1.5f / 64.0f) == plug190ad::gains (true, 0.78f, -1.0f / 64.0f));
  104. const auto [l, r] = plug190ad::gains (true, 0.78f, 1.5f / 64.0f);
  105. CHECK (std::abs (r / l - 1.0249f) < 0.0002f);
  106. const auto [l2, r2] = plug190ad::gains (true, 0.78f, 2.5f / 64.0f);
  107. CHECK (std::abs (r2 / l2 - 1.0503f) < 0.0002f);
  108. const auto [l3, r3] = plug190ad::gains (true, 0.78f, -0.5f);
  109. CHECK (std::abs (r3 / l3 - 0.4142f) < 0.0002f); // (FL: 0.4142 at half left)
  110. }
  111. // the project's triangular law: the louder side keeps the centre level, the other falls linearly
  112. // (FL's render at half left: L at the centre's level, R / L = 0.5000)
  113. {
  114. const auto [l, r] = plug190ad::gains (true, 0.78f, -0.5f, true);
  115. CHECK (plug190ad::close (l, g * sqrt2));
  116. CHECK (plug190ad::close (r, g * sqrt2 * 0.5f));
  117. const auto centre = plug190ad::gains (true, 0.78f, 0.0f, true);
  118. CHECK (plug190ad::close (centre.first, g * sqrt2) && plug190ad::close (centre.second, g * sqrt2));
  119. }
  120. }
  121. TEST (plug190ad_only_hosted_plugins_follow_the_wrapper_level)
  122. {
  123. using namespace lynx;
  124. // the same song with FL's levels on and off renders the same when it has no hosted plugin (LYNX's synth here)
  125. std::vector<float> out[2];
  126. for (int fl = 0; fl < 2; ++fl)
  127. {
  128. Song song;
  129. song.mixer = Song::defaultMixer (2);
  130. song.flPluginLevels = fl == 1;
  131. ChannelDef c;
  132. c.instrument = InstrumentKind::synth;
  133. c.pan = -0.3f;
  134. song.channels.push_back (c);
  135. auto& p = song.patterns.emplace_back();
  136. p.notes = { { 0, 960, 0, 57 } };
  137. const auto state = RackState::build (song, nullptr, 44100.0, RackState::Factories {});
  138. CHECK (state != nullptr);
  139. if (state == nullptr)
  140. return;
  141. RackRenderer r;
  142. r.prepare (44100.0, 256);
  143. std::vector<float> l (256), rr (256);
  144. const auto tps = ticksPerSample (song.bpm, 44100.0);
  145. for (int b = 0; b < 16; ++b)
  146. {
  147. r.process (state.get(), true, b * 256 * tps, 256, l.data(), rr.data());
  148. out[fl].insert (out[fl].end(), l.begin(), l.end());
  149. out[fl].insert (out[fl].end(), rr.begin(), rr.end());
  150. }
  151. }
  152. CHECK (out[0] == out[1]);
  153. CHECK (std::any_of (out[0].begin(), out[0].end(), [] (float x) { return x != 0.0f; }));
  154. }
  155. TEST (plug190ad_new_projects_take_fl_plugin_levels)
  156. {
  157. using namespace lynx;
  158. // File › New and every built-in template: FL's levels (none of them holds a hosted plugin)
  159. CHECK (makeNewProject().flPluginLevels);
  160. for (int i = 0; i < (int) templateNames().size(); ++i)
  161. {
  162. const auto song = makeNewProject (i);
  163. CHECK (song.flPluginLevels);
  164. CHECK (std::none_of (song.channels.begin(), song.channels.end(), [] (const ChannelDef& c) { return c.instrument == InstrumentKind::plugin; }));
  165. }
  166. // what a project saved before reads as (no key): LYNX's own law
  167. CHECK (! Song::makeDefault().flPluginLevels);
  168. CHECK (! Song {}.flPluginLevels);
  169. // a user's template: without a hosted plugin it takes FL's levels; with one made under LYNX's law it keeps them
  170. auto plain = Song::makeDefault();
  171. CHECK (adoptFlPluginLevels (plain) && plain.flPluginLevels);
  172. auto withPlugin = Song::makeDefault();
  173. ChannelDef c;
  174. c.instrument = InstrumentKind::plugin;
  175. c.plugin.id = "x";
  176. withPlugin.channels.push_back (c);
  177. CHECK (! adoptFlPluginLevels (withPlugin) && ! withPlugin.flPluginLevels);
  178. auto missingPlugin = Song::makeDefault(); // (its plugin is not installed here: still a hosted plugin's channel)
  179. missingPlugin.channels.emplace_back().plugin.foreign.format = ForeignPlugin::Format::vst3;
  180. CHECK (! adoptFlPluginLevels (missingPlugin));
  181. withPlugin.flPluginLevels = true; // (a template saved from now on: already FL's)
  182. CHECK (adoptFlPluginLevels (withPlugin));
  183. }
  184. TEST (plug190ad_wrapper_velocity_is_round_128)
  185. {
  186. using lynx::hostpitch::flWrapperVelocity;
  187. // an .flp note's velocity byte b is b / 128 in LYNX: FL's wrapper sends b itself (127 at most)
  188. for (int b = 1; b <= 128; ++b)
  189. CHECK (flWrapperVelocity ((float) b / 128.0f) == std::min (b, 127));
  190. CHECK (flWrapperVelocity (100.0f / 128.0f) == 100); // (LYNX's own law, round(127 v), sends 99)
  191. CHECK ((int) std::lround (100.0f / 128.0f * 127.0f) == 99);
  192. CHECK (flWrapperVelocity (0.5f / 128.0f) == 1); // Round(0.5) = 0 in FL; a note on stays a note on
  193. CHECK (flWrapperVelocity (2.5f / 128.0f) == 2); // ties to even, as FL's Round
  194. CHECK (flWrapperVelocity (3.5f / 128.0f) == 4);
  195. CHECK (flWrapperVelocity (0.0f) == 1);
  196. CHECK (flWrapperVelocity (2.0f) == 127);
  197. }
  198. TEST (plug190ad_flp_import_and_export_keep_the_plugin_level)
  199. {
  200. using namespace lynx;
  201. const auto written = [] (const Song& s) {
  202. flpexport::Options options;
  203. flpexport::Report report;
  204. return flp::parseProject (flpexport::exportFlp (s, options, report));
  205. };
  206. const auto back = [] (const flp::Project& p) {
  207. FlpImportOptions o;
  208. o.fileExists = [] (const std::string&) { return true; };
  209. return importFlp (p, o);
  210. };
  211. auto s = Song::makeDefault();
  212. CHECK (! s.flPluginLevels); // LYNX's own projects: their own law
  213. s.channels.resize (2);
  214. auto& c = s.channels[0];
  215. c.instrument = InstrumentKind::plugin;
  216. c.plugin.name = "Keys";
  217. c.plugin.foreign.format = ForeignPlugin::Format::vst2;
  218. c.plugin.foreign.vst2Id = 0x4B455953;
  219. c.plugin.foreign.name = "Keys";
  220. c.volume = 0.78f;
  221. s.channels[1].instrument = InstrumentKind::synth;
  222. s.channels[1].volume = 0.6f;
  223. // a project with FL's levels: the knob goes out and comes back as it is
  224. s.flPluginLevels = true;
  225. {
  226. const auto again = back (written (s));
  227. CHECK (again.flPluginLevels);
  228. CHECK (again.channels.size() >= 2);
  229. if (again.channels.size() >= 2)
  230. {
  231. // (the import keeps the wrapper's plugin as `foreign`; the app makes the channel a hosted plugin when it
  232. // finds that plugin installed)
  233. CHECK (again.channels[0].plugin.foreign.format == ForeignPlugin::Format::vst2 && again.channels[0].plugin.foreign.vst2Id == 0x4B455953u);
  234. CHECK (std::abs (again.channels[0].volume - 0.78f) < 1.0e-4f);
  235. CHECK (std::abs (again.channels[1].volume - 0.6f) < 1.0e-4f);
  236. }
  237. }
  238. // LYNX's own law: FL plays the plugin √2 louder at the same knob, so the knob written is the one that sounds
  239. // the same in FL (and in LYNX when the .flp comes back); other channels keep theirs
  240. s.flPluginLevels = false;
  241. {
  242. const auto again = back (written (s));
  243. CHECK (again.flPluginLevels);
  244. CHECK (again.channels.size() >= 2);
  245. if (again.channels.size() >= 2)
  246. {
  247. CHECK (again.channels[0].volume < 0.78f);
  248. CHECK (std::abs (channelGain (again.channels[0].volume) * 1.41421356f - channelGain (0.78f)) < 2.0e-4f);
  249. CHECK (std::abs (again.channels[1].volume - 0.6f) < 1.0e-4f);
  250. }
  251. }
  252. }
  253. TEST (plug190ad_a_patcher_channel_is_not_compensated)
  254. {
  255. using namespace lynx;
  256. // FL's levels apply to the rack's own hosted plugin channels only: what a Patcher hosts keeps the Patcher's level
  257. // (the channel is a Patcher, not a wrapper; nothing inside it reads the project's flag), so it is never raised
  258. // twice — the same render with the flag on and off
  259. std::vector<float> out[2];
  260. for (int fl = 0; fl < 2; ++fl)
  261. {
  262. Song song;
  263. song.mixer = Song::defaultMixer (2);
  264. song.flPluginLevels = fl == 1;
  265. ChannelDef c;
  266. c.instrument = InstrumentKind::patcher;
  267. c.pan = 0.25f;
  268. c.patcher.nodes = { patcher162::generatorNode (InstrumentKind::synth) };
  269. c.patcher.wires = { patcher162::midi (patchFromHost, 0, 0, 0), patcher141::audio (0, patchToHost) };
  270. song.channels.push_back (c);
  271. auto& p = song.patterns.emplace_back();
  272. p.notes = { { 0, 960, 0, 57 } };
  273. const auto state = RackState::build (song, nullptr, 44100.0);
  274. CHECK (state != nullptr);
  275. if (state == nullptr)
  276. return;
  277. RackRenderer r;
  278. r.prepare (44100.0, 256);
  279. std::vector<float> l (256), rr (256);
  280. const auto tps = ticksPerSample (song.bpm, 44100.0);
  281. for (int b = 0; b < 16; ++b)
  282. {
  283. r.process (state.get(), true, b * 256 * tps, 256, l.data(), rr.data());
  284. out[fl].insert (out[fl].end(), l.begin(), l.end());
  285. out[fl].insert (out[fl].end(), rr.begin(), rr.end());
  286. }
  287. }
  288. CHECK (out[0] == out[1]);
  289. CHECK (std::any_of (out[0].begin(), out[0].end(), [] (float x) { return x != 0.0f; }));
  290. }