Issue114Tests.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Round 190i, issue #114 as reported: a multi-output drum plugin (the reporter's MT-PowerDrumKit: 8 outputs) on a
  2. // channel routed to Master, its outputs numbered 1…8 in the wrapper's Processing › Connections (their second
  3. // screenshot), so that every kit piece gets a mixer insert of its own, which can then be routed and sent on like
  4. // any insert. The Connections code is round 188d's (MultiOut188dTests.inc); this is the issue's own scenario.
  5. namespace issue114
  6. {
  7. constexpr int kitOutputs = 8;
  8. // output k's level (DC): all different, so the insert an output lands on is known from the level alone
  9. constexpr float levelOf (int k) noexcept { return 0.02f * (float) (k + 1); }
  10. // A drum plugin's outputs as engine/PluginInstrument hands them to the rack: offset 0 = with the channel's own
  11. // audio, anything else = an extra output for the insert `offset` tracks after the channel's
  12. struct DrumKit final : lynx::Instrument
  13. {
  14. int track = 0;
  15. lynx::PluginOutputRoute routes[kitOutputs];
  16. std::vector<float> aux[kitOutputs];
  17. int cursor = 0;
  18. DrumKit()
  19. {
  20. for (auto& a : aux)
  21. a.assign (4096, 0.0f);
  22. }
  23. void prepare (double) noexcept override {}
  24. void noteOn (int, float) noexcept override {}
  25. void noteOff (int) noexcept override {}
  26. void allNotesOff() noexcept override {}
  27. void configure (const lynx::ChannelDef& c, double) noexcept override
  28. {
  29. track = c.mixerTrack;
  30. for (int k = 0; k < kitOutputs; ++k)
  31. routes[k] = c.plugin.outputRoute (k);
  32. }
  33. void beginChunk (int n) noexcept override
  34. {
  35. cursor = 0;
  36. for (auto& a : aux)
  37. std::fill (a.begin(), a.begin() + n, 0.0f);
  38. }
  39. void render (float* l, float* r, int n, float gl, float gr) noexcept override
  40. {
  41. for (int k = 0; k < kitOutputs; ++k)
  42. {
  43. if (! routes[k].isActive (true))
  44. continue;
  45. for (int i = 0; i < n; ++i)
  46. if (routes[k].offset == 0)
  47. {
  48. l[i] += levelOf (k) * gl;
  49. r[i] += levelOf (k) * gr;
  50. }
  51. else
  52. aux[k][(std::size_t) (cursor + i)] += levelOf (k) * gl;
  53. }
  54. cursor += n;
  55. }
  56. int auxOutputCount() const noexcept override { return kitOutputs; }
  57. int auxOutputTrack (int k) const noexcept override
  58. {
  59. return routes[k].offset == 0 ? -1 : lynx::outputTargetTrack (track, routes[k]);
  60. }
  61. const float* auxOutputData (int k, int) const noexcept override { return aux[k].data(); }
  62. };
  63. // the Master's level after a few blocks
  64. float master (const lynx::Song& song)
  65. {
  66. auto kit = std::make_shared<DrumKit>();
  67. lynx::RackState::Factories f;
  68. f.pluginInstruments = [&] (const lynx::PluginRef&, double) -> std::shared_ptr<lynx::Instrument> { return kit; };
  69. const auto state = lynx::RackState::build (song, nullptr, 48000.0, f);
  70. lynx::RackRenderer r;
  71. r.prepare (48000.0, 256);
  72. std::vector<float> l (256), rr (256);
  73. for (int b = 0; b < 8; ++b)
  74. r.process (state.get(), true, 0.0, 256, l.data(), rr.data(), nullptr);
  75. return std::abs (l.back()) + std::abs (rr.back());
  76. }
  77. // the reporter's project: the drum plugin on a channel routed to Master ("MT-PowerDrumKit (Master)")
  78. lynx::Song project()
  79. {
  80. auto s = lynx::Song::makeDefault();
  81. s.channels.clear();
  82. lynx::ChannelDef c;
  83. c.instrument = lynx::InstrumentKind::plugin;
  84. c.plugin.id = "VST-MT-PowerDrumKit";
  85. c.mixerTrack = 0;
  86. s.channels.push_back (c);
  87. if (s.mixer.size() < 12)
  88. s.mixer.resize (12);
  89. return s;
  90. }
  91. // only insert `keep` of 1…8 plays (keep = 0: none of them)
  92. lynx::Song onlyInsert (lynx::Song s, int keep)
  93. {
  94. for (int t = 1; t <= kitOutputs; ++t)
  95. s.mixer[(std::size_t) t].muted = t != keep;
  96. return s;
  97. }
  98. } // namespace issue114
  99. TEST (issue114_drum_plugin_outputs_get_their_own_mixer_inserts)
  100. {
  101. using namespace issue114;
  102. // as loaded: every output plays with the channel, on Master; inserts 1…8 carry nothing
  103. auto song = project();
  104. const auto all = master (song);
  105. CHECK (all > 0.1f);
  106. CHECK (std::abs (master (onlyInsert (song, 0)) - all) < 1.0e-4f * all);
  107. // the reporter's Connections list: output 1 on "1", then Auto map outputs → 1, 2, … 8
  108. auto& routes = song.channels[0].plugin.outputs;
  109. routes = { { 1, -1 } };
  110. lynx::autoMapOutputs (routes, kitOutputs);
  111. CHECK (routes.size() == (std::size_t) kitOutputs);
  112. for (int k = 0; k < kitOutputs; ++k)
  113. CHECK (routes[(std::size_t) k].offset == k + 1
  114. && lynx::outputTargetTrack (0, routes[(std::size_t) k]) == k + 1);
  115. // the mix is the same as before; nothing is left on Master itself: with inserts 1…8 muted it is silent
  116. CHECK (std::abs (master (song) - all) < 1.0e-3f * all);
  117. CHECK (master (onlyInsert (song, 0)) == 0.0f);
  118. // each insert carries exactly its own output: alone, insert k+1 plays output k's level and nothing else
  119. const auto first = master (onlyInsert (song, 1));
  120. CHECK (first > 0.0f);
  121. for (int k = 0; k < kitOutputs; ++k)
  122. {
  123. const auto alone = master (onlyInsert (song, k + 1));
  124. CHECK (std::abs (alone / first - levelOf (k) / levelOf (0)) < 0.01f);
  125. }
  126. // "create sends": the snare's insert (2) can now be routed like any insert — only to insert 10, off Master
  127. auto routed = onlyInsert (song, 2);
  128. const auto snare = master (routed);
  129. CHECK (lynx::mixerops::routeTo (routed, { 2 }, 10, true) > 0);
  130. CHECK (! routed.mixer[2].toMaster);
  131. CHECK (std::abs (master (routed) - snare) < 1.0e-3f * snare); // through insert 10 now
  132. routed.mixer[10].muted = true;
  133. CHECK (master (routed) == 0.0f);
  134. // one output switched off in the list (its LED) is silent; the others stay where they are. ("Process inactive
  135. // inputs and outputs" is the wrapper's own switch, PluginInstrument::configure; a route's LED is what it reads.)
  136. auto off = song;
  137. off.channels[0].plugin.outputs[1].active = 0;
  138. CHECK (master (onlyInsert (off, 2)) == 0.0f);
  139. CHECK (std::abs (master (onlyInsert (off, 3)) / first - levelOf (2) / levelOf (0)) < 0.01f);
  140. // Reset: everything back with the channel, on Master
  141. auto reset = song;
  142. lynx::resetOutputOffsets (reset.channels[0].plugin.outputs, kitOutputs);
  143. CHECK (reset.channels[0].plugin.outputs.empty());
  144. CHECK (std::abs (master (onlyInsert (reset, 0)) - all) < 1.0e-3f * all);
  145. // the same list with the channel on insert 3: the numbers count from the channel's insert (FL: 4, 5, … 11)
  146. auto moved = song;
  147. moved.channels[0].mixerTrack = 3;
  148. for (int t = 1; t < 12; ++t)
  149. moved.mixer[(std::size_t) t].muted = t != 4 + 2;
  150. CHECK (std::abs (master (moved) / first - levelOf (2) / levelOf (0)) < 0.01f);
  151. }