MultiOut188dTests.inc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Round 188d (issue #114): FL's wrapper Processing › Connections — a hosted generator's outputs to mixer inserts.
  2. namespace multiout188d
  3. {
  4. // A two-output "plugin" as engine/PluginInstrument routes it: output 1 = 0.25, output 2 = 0.5 (DC), each to the
  5. // channel's insert + its offset (offset 0: the channel's own audio), silent when inactive, the channel's gains on both
  6. struct TwoOutputs final : lynx::Instrument
  7. {
  8. int configured = 0, track = 0;
  9. bool processInactive = true;
  10. lynx::PluginOutputRoute routes[2];
  11. std::vector<float> aux = std::vector<float> (4096, 0.0f);
  12. int cursor = 0;
  13. void prepare (double) noexcept override {}
  14. void noteOn (int, float) noexcept override {}
  15. void noteOff (int) noexcept override {}
  16. void allNotesOff() noexcept override {}
  17. void configure (const lynx::ChannelDef& c, double) noexcept override
  18. {
  19. ++configured;
  20. track = c.mixerTrack;
  21. processInactive = c.plugin.processInactive;
  22. for (int k = 0; k < 2; ++k)
  23. routes[k] = c.plugin.outputRoute (k);
  24. }
  25. void beginChunk (int n) noexcept override
  26. {
  27. cursor = 0;
  28. std::fill (aux.begin(), aux.begin() + n, 0.0f);
  29. }
  30. void render (float* l, float* r, int n, float gl, float gr) noexcept override
  31. {
  32. const float level[2] = { 0.25f, 0.5f };
  33. for (int k = 0; k < 2; ++k)
  34. if (routes[k].isActive (true) || processInactive)
  35. for (int i = 0; i < n; ++i)
  36. {
  37. if (routes[k].offset == 0)
  38. {
  39. l[i] += level[k] * gl;
  40. r[i] += level[k] * gr;
  41. }
  42. else if (k == 1)
  43. aux[(std::size_t) (cursor + i)] += level[k] * gl;
  44. }
  45. cursor += n;
  46. }
  47. int auxOutputCount() const noexcept override { return (routes[1].isActive (true) || processInactive) && routes[1].offset != 0 ? 1 : 0; }
  48. int auxOutputTrack (int) const noexcept override { return track + routes[1].offset; }
  49. const float* auxOutputData (int, int) const noexcept override { return aux.data(); }
  50. };
  51. // the master's level with the channel's own insert (1) muted or not
  52. float master (const lynx::Song& song, std::shared_ptr<TwoOutputs> fake, const lynx::RackState* previous = nullptr,
  53. std::unique_ptr<const lynx::RackState>* keep = nullptr)
  54. {
  55. lynx::RackState::Factories f;
  56. f.pluginInstruments = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Instrument> { return fake; };
  57. auto state = lynx::RackState::build (song, previous, 48000.0, f);
  58. lynx::RackRenderer r;
  59. r.prepare (48000.0, 256);
  60. std::vector<float> l (256), rr (256);
  61. for (int b = 0; b < 4; ++b)
  62. r.process (state.get(), true, 0.0, 256, l.data(), rr.data(), nullptr);
  63. if (keep != nullptr)
  64. *keep = std::move (state);
  65. return std::abs (l.back()) + std::abs (rr.back());
  66. }
  67. lynx::Song song (std::vector<lynx::PluginOutputRoute> routes, bool muteOwnInsert, bool processInactive = false)
  68. {
  69. auto s = lynx::Song::makeDefault();
  70. s.channels.clear();
  71. lynx::ChannelDef c;
  72. c.instrument = lynx::InstrumentKind::plugin;
  73. c.plugin.id = "VST-Drums-1";
  74. c.mixerTrack = 1;
  75. c.plugin.outputs = std::move (routes);
  76. c.plugin.processInactive = processInactive;
  77. s.channels.push_back (c);
  78. if (s.mixer.size() < 4)
  79. s.mixer.resize (4);
  80. s.mixer[1].muted = muteOwnInsert;
  81. return s;
  82. }
  83. } // namespace multiout188d
  84. TEST (multiout188d_connections_auto_map_and_reset_as_fl)
  85. {
  86. // FL [EXEC, fl188d a33/b23]: Auto map from output 1's offset, one more per output, inactive ones included
  87. std::vector<lynx::PluginOutputRoute> r;
  88. lynx::autoMapOutputs (r, 8);
  89. CHECK (r.size() == 8 && r[0].offset == 0 && r[1].offset == 1 && r[7].offset == 7);
  90. r[2].active = 0;
  91. r[0].offset = 3;
  92. lynx::autoMapOutputs (r, 8);
  93. CHECK (r[0].offset == 3 && r[2].offset == 5 && ! r[2].isActive (true) && r[7].offset == 10);
  94. // Reset: offsets to "---" (0), the LEDs stay; what is left at the defaults is not stored
  95. lynx::resetOutputOffsets (r, 8);
  96. CHECK (r.size() == 3 && r[2].offset == 0 && ! r[2].isActive (true));
  97. r[2].active = -1;
  98. lynx::trimOutputRoutes (r);
  99. CHECK (r.empty());
  100. // the route of an output past the stored ones: the channel's insert, active as the plugin declares it
  101. lynx::PluginRef ref;
  102. CHECK (ref.outputRoute (5).isActive (true) && ! ref.outputRoute (5).isActive (false) && ref.outputRoute (5).offset == 0);
  103. auto other = ref;
  104. other.outputs = { { 0, -1 }, { 2, -1 } };
  105. CHECK (! (ref == other));
  106. CHECK (lynx::outputTargetTrack (4, { 2, 1 }) == 6 && lynx::outputTargetTrack (4, { 2, 0 }) == -1);
  107. CHECK (lynx::outputTargetTrack (4, { 2, -1 }, false) == -1 && lynx::outputTargetTrack (4, { 2, 1 }, false) == 6);
  108. }
  109. TEST (multiout188d_outputs_reach_their_inserts)
  110. {
  111. using namespace multiout188d;
  112. // by default every output plays on the channel's insert: muting it silences them all
  113. auto fake = std::make_shared<TwoOutputs>();
  114. CHECK (master (song ({}, false), fake) > 0.1f);
  115. CHECK (fake->configured > 0); // a new plugin instrument takes its routes when it is built
  116. CHECK (master (song ({}, true), fake) == 0.0f);
  117. // output 2 auto-mapped (offset 1): it plays on insert 2, past the muted insert 1
  118. CHECK (master (song ({ { 0, 1 }, { 1, 1 } }, true), fake) > 0.1f);
  119. // inactive: silent anywhere
  120. CHECK (master (song ({ { 0, 1 }, { 1, 0 } }, true), fake) == 0.0f);
  121. CHECK (master (song ({ { 0, 1 }, { 0, 0 } }, false), fake) > 0.1f); // (output 1 still plays)
  122. // "Process inactive inputs and outputs" on (FL's default) [EXEC fl188d k33]: the inactive output plays on its insert
  123. CHECK (master (song ({ { 0, 1 }, { 1, 0 } }, true, true), fake) > 0.1f);
  124. // the same running instrument takes new routes (no new instance: the reuse path configures it)
  125. std::unique_ptr<const lynx::RackState> first;
  126. auto a = song ({}, true);
  127. master (a, fake, nullptr, &first);
  128. const auto before = fake->configured;
  129. a.channels[0].plugin.outputs = { { 0, 1 }, { 1, 1 } };
  130. CHECK (master (a, fake, first.get()) > 0.1f);
  131. CHECK (fake->configured > before && fake->routes[1].offset == 1);
  132. }
  133. TEST (multiout188d_flp_connections_records)
  134. {
  135. // FL 2026's records [DATA: mo-a.flp, LYNX Test Drums 2 after Auto map, Clap off, Hat at -2; flags without 0x8 =
  136. // "Process inactive inputs and outputs" off]
  137. lynx::ForeignPlugin made;
  138. made.format = lynx::ForeignPlugin::Format::vst2;
  139. made.vst2Id = 0x4C584433;
  140. made.name = "LYNX Test Drums 2";
  141. lynx::PluginRef ref;
  142. ref.outputs = { { 0, -1 }, { 1, -1 }, { 2, 0 }, { -2, -1 }, { 4, -1 } };
  143. ref.processInactive = false;
  144. const auto blob = lynx::flpexport::writeWrapper (made, true, &ref);
  145. lynx::ForeignPlugin back;
  146. CHECK (lynx::parseFlWrapper (blob, back));
  147. CHECK (back.hasRoutes && back.inputRoutes.empty() && back.outputRoutes.size() == 5);
  148. CHECK (back.outputRoutes[2].offset == 2 && back.outputRoutes[2].active == 0 && back.outputRoutes[3].offset == -2 && back.outputRoutes[1].active == 1);
  149. CHECK ((back.wrapperFlags & 0x8u) == 0);
  150. lynx::PluginRef imported;
  151. lynx::applyFlWrapperOptions (back, imported);
  152. CHECK (! imported.processInactive && imported.outputs.size() == 5 && imported.outputs[3].offset == -2 && ! imported.outputs[2].isActive (true));
  153. // the bytes FL wrote: 0x1E {0 inputs, 8 outputs}, 0x20 = 8 x {i32 offset, u32 active, u32 0}
  154. std::vector<std::uint8_t> fl { 12, 0, 0, 0 };
  155. auto record = [&fl] (std::uint32_t id, std::vector<std::uint32_t> words) {
  156. const auto n = (std::uint64_t) words.size() * 4;
  157. for (int k = 0; k < 4; ++k) fl.push_back ((std::uint8_t) (id >> (8 * k)));
  158. for (int k = 0; k < 8; ++k) fl.push_back ((std::uint8_t) (n >> (8 * k)));
  159. for (const auto w : words)
  160. for (int k = 0; k < 4; ++k) fl.push_back ((std::uint8_t) (w >> (8 * k)));
  161. };
  162. record (0x02, {}); // (an empty options record: ignored)
  163. record (0x1E, { 0, 8, 0, 0 });
  164. record (0x20, { 0, 1, 0, 1, 1, 0, 2, 0, 0, 0xFFFFFFFEu, 1, 0, 4, 1, 0, 5, 1, 0, 6, 1, 0, 7, 1, 0 });
  165. record (0x32, { 4, 0, 0, 0 });
  166. record (0x33, { 0x4C584433 });
  167. lynx::ForeignPlugin parsed;
  168. CHECK (lynx::parseFlWrapper (fl, parsed));
  169. CHECK (parsed.outputRoutes.size() == 8 && parsed.outputRoutes[3].offset == -2 && parsed.outputRoutes[2].active == 0 && parsed.outputRoutes[7].offset == 7);
  170. // an effect's inputs (sidechain indexes) travel too [DATA: mo-c.flp]
  171. lynx::PluginRef fx;
  172. fx.inputs = { { 0, 1 }, { 1, 1 } };
  173. fx.outputs = { { 0, 1 }, { 1, 1 } };
  174. lynx::ForeignPlugin fxBack;
  175. CHECK (lynx::parseFlWrapper (lynx::flpexport::writeWrapper (made, false, &fx), fxBack));
  176. CHECK (fxBack.inputRoutes.size() == 2 && fxBack.inputRoutes[1].offset == 1 && fxBack.outputRoutes.size() == 2);
  177. // without a mapping: one input and one output, as before
  178. lynx::ForeignPlugin plain;
  179. CHECK (lynx::parseFlWrapper (lynx::flpexport::writeWrapper (made, true, nullptr), plain) && plain.outputRoutes.size() == 1);
  180. }
  181. TEST (multiout188d_effect_sidechain_index_picks_the_insert_inputs)
  182. {
  183. // REA_rodada22 §1 + FL [EXEC fl188d n06/n13/n19]: an insert's inputs = [itself, the inserts routed to it in
  184. // ascending order]; sidechain index 1 on insert 1 with insert 2 sidechained to it = insert 2
  185. auto song = lynx::Song::makeDefault();
  186. if (song.mixer.size() < 6)
  187. song.mixer.resize (6);
  188. lynx::mixerops::sidechainTo (song, 4, 1);
  189. lynx::mixerops::sidechainTo (song, 2, 1);
  190. const auto inputs = lynx::mixerops::insertInputs (song, 1);
  191. CHECK ((inputs == std::vector<int> { 1, 2, 4 }));
  192. CHECK (lynx::mixerops::sidechainIndexSource (song, 1, 0) == -1 && lynx::mixerops::sidechainIndexSource (song, 1, 2) == 4);
  193. CHECK (lynx::mixerops::sidechainIndexSource (song, 1, 3) == -1);
  194. CHECK (lynx::mixerops::sidechainIndexOf (song, 1, 4) == 2 && lynx::mixerops::sidechainIndexOf (song, 1, 5) == 0);
  195. }