Reload190aoTests.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Round 190ao (TOP-20 #1, CROSS-DAW F50: Bitwig's "Reload plug-in"): a hosted plugin that crashed is started again
  2. // without reopening the project. The engine keeps a plugin's running instance from one rack build to the next;
  3. // forgetHostedPlugin makes the next build create that one again, and only that one, with the song untouched.
  4. namespace reload190ao
  5. {
  6. struct FakeSynth final : lynx::Instrument
  7. {
  8. static inline int made = 0;
  9. std::string stateAtCreation;
  10. explicit FakeSynth (std::string state) : stateAtCreation (std::move (state)) { ++made; }
  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, float) noexcept override {}
  16. };
  17. struct FakeFx final : lynx::Effect
  18. {
  19. static inline int made = 0;
  20. std::string stateAtCreation;
  21. explicit FakeFx (std::string state) : stateAtCreation (std::move (state)) { ++made; }
  22. void prepare (double) noexcept override {}
  23. void reset() noexcept override {}
  24. void process (float*, float*, int, const lynx::EffectParams&, double) noexcept override {}
  25. };
  26. lynx::RackState::Factories factories()
  27. {
  28. lynx::RackState::Factories f;
  29. f.pluginInstruments = [] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Instrument> { return std::make_shared<FakeSynth> (ref.state); };
  30. f.pluginEffects = [] (const lynx::PluginRef& ref, double) -> std::shared_ptr<lynx::Effect> { return std::make_shared<FakeFx> (ref.state); };
  31. return f;
  32. }
  33. lynx::Song song()
  34. {
  35. lynx::Song s;
  36. s.mixer = lynx::Song::defaultMixer (3);
  37. for (const char* name : { "Synth A", "Synth B" })
  38. {
  39. lynx::ChannelDef c;
  40. c.name = name;
  41. c.instrument = lynx::InstrumentKind::plugin;
  42. c.plugin.id = name;
  43. c.plugin.name = name;
  44. c.plugin.state = "c2F2ZWQ="; // what the last save captured
  45. c.mixerTrack = 1;
  46. s.channels.push_back (c);
  47. }
  48. for (const char* name : { "Fx A", "Fx B" })
  49. {
  50. auto slot = lynx::makeEffectSlot (lynx::EffectKind::plugin);
  51. slot.plugin.id = name;
  52. slot.plugin.name = name;
  53. slot.plugin.state = "c2F2ZWQ=";
  54. s.mixer[1].effects.push_back (slot);
  55. }
  56. s.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::delay));
  57. s.patterns.emplace_back();
  58. s.uniqueInstances();
  59. return s;
  60. }
  61. // what the engine keeps between builds (AudioEngine::lastPublishedShape)
  62. lynx::RackState shapeOf (const lynx::RackState& built)
  63. {
  64. lynx::RackState shape;
  65. shape.song.channels = built.song.channels;
  66. shape.song.mixer = built.song.mixer;
  67. shape.song.bpm = built.song.bpm;
  68. shape.instruments = built.instruments;
  69. shape.effects = built.effects;
  70. shape.sampleRate = built.sampleRate;
  71. shape.serial = built.serial;
  72. return shape;
  73. }
  74. } // namespace reload190ao
  75. TEST (reload190ao_a_plain_rebuild_keeps_every_running_plugin)
  76. {
  77. using namespace reload190ao;
  78. const auto s = song();
  79. const auto f = factories();
  80. FakeSynth::made = FakeFx::made = 0;
  81. const auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  82. CHECK (FakeSynth::made == 2 && FakeFx::made == 2);
  83. auto shape = shapeOf (*a);
  84. const auto b = lynx::RackState::build (s, &shape, 48000.0, f);
  85. CHECK (FakeSynth::made == 2 && FakeFx::made == 2); // nothing reloads by itself
  86. CHECK (b->instruments[0] == a->instruments[0] && b->instruments[1] == a->instruments[1]);
  87. CHECK (b->effects[1][0] == a->effects[1][0] && b->effects[1][1] == a->effects[1][1]);
  88. }
  89. TEST (reload190ao_reloading_a_channel_plugin_makes_only_that_one_again_from_the_song_state)
  90. {
  91. using namespace reload190ao;
  92. auto s = song();
  93. const auto f = factories();
  94. FakeSynth::made = FakeFx::made = 0;
  95. const auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  96. auto shape = shapeOf (*a);
  97. // the state captured since the instance was made (a save, a backup) is what the new instance starts from
  98. s.channels[1].plugin.state = "Y2FwdHVyZWQ=";
  99. shape.song.channels = s.channels;
  100. const auto before = s.channels;
  101. CHECK (lynx::forgetHostedPlugin (shape, 1));
  102. CHECK (s.channels == before); // the song is not edited: no undo step, nothing to save
  103. const auto b = lynx::RackState::build (s, &shape, 48000.0, f);
  104. CHECK (FakeSynth::made == 3 && FakeFx::made == 2);
  105. CHECK (b->instruments[0] == a->instruments[0]);
  106. CHECK (b->instruments[1] != a->instruments[1]);
  107. CHECK (static_cast<FakeSynth*> (b->instruments[1].get())->stateAtCreation == "Y2FwdHVyZWQ=");
  108. CHECK (b->song.channels[1].plugin.id == "Synth B"); // (the built state carries the real plugin)
  109. CHECK (b->effects[1][0] == a->effects[1][0] && b->effects[1][1] == a->effects[1][1]);
  110. // asked once, done once: the next build keeps the new instance (a plugin that crashes again is not reloaded
  111. // unless the user asks again)
  112. auto next = shapeOf (*b);
  113. const auto c = lynx::RackState::build (s, &next, 48000.0, f);
  114. CHECK (FakeSynth::made == 3);
  115. CHECK (c->instruments[1] == b->instruments[1]);
  116. }
  117. TEST (reload190ao_reloading_a_mixer_slot_plugin_keeps_the_others)
  118. {
  119. using namespace reload190ao;
  120. const auto s = song();
  121. const auto f = factories();
  122. FakeSynth::made = FakeFx::made = 0;
  123. const auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  124. auto shape = shapeOf (*a);
  125. CHECK (lynx::forgetHostedPlugin (shape, 1, 1));
  126. const auto b = lynx::RackState::build (s, &shape, 48000.0, f);
  127. CHECK (FakeSynth::made == 2 && FakeFx::made == 3);
  128. CHECK (b->effects[1][0] == a->effects[1][0]);
  129. CHECK (b->effects[1][1] != a->effects[1][1]);
  130. CHECK (b->effects[2][0] == a->effects[2][0]);
  131. CHECK (b->instruments[0] == a->instruments[0] && b->instruments[1] == a->instruments[1]);
  132. }
  133. TEST (reload190ao_undo_after_a_reload_keeps_the_new_instance)
  134. {
  135. using namespace reload190ao;
  136. auto s = song();
  137. const auto f = factories();
  138. const auto earlier = s; // an undo step taken before the crash
  139. s.channels[0].volume = 0.5f; // an edit
  140. FakeSynth::made = FakeFx::made = 0;
  141. const auto a = lynx::RackState::build (s, nullptr, 48000.0, f);
  142. auto shape = shapeOf (*a);
  143. CHECK (lynx::forgetHostedPlugin (shape, 0));
  144. const auto b = lynx::RackState::build (s, &shape, 48000.0, f);
  145. CHECK (FakeSynth::made == 3);
  146. // Undo puts the earlier song back: same plugin, same processing options, so the reloaded instance stays
  147. auto after = shapeOf (*b);
  148. const auto c = lynx::RackState::build (earlier, &after, 48000.0, f);
  149. CHECK (FakeSynth::made == 3);
  150. CHECK (c->instruments[0] == b->instruments[0]);
  151. }
  152. TEST (reload190ao_only_hosted_plugins_can_be_forgotten)
  153. {
  154. using namespace reload190ao;
  155. const auto s = song();
  156. const auto a = lynx::RackState::build (s, nullptr, 48000.0, factories());
  157. auto shape = shapeOf (*a);
  158. CHECK (! lynx::forgetHostedPlugin (shape, -1));
  159. CHECK (! lynx::forgetHostedPlugin (shape, 2));
  160. CHECK (! lynx::forgetHostedPlugin (shape, 2, 0)); // a built-in effect
  161. CHECK (! lynx::forgetHostedPlugin (shape, 1, 5));
  162. CHECK (! lynx::forgetHostedPlugin (shape, 9, 0));
  163. }