plugexporttest.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. // plugexporttest <plugin.vst3 | plugin.clap | plugin.lv2 (Linux, rounds 190ov and 190pk)> <report.txt> [editor picture prefix]
  2. // Loads LYNX's exported plugins the way a host does (round 180b, docs/PLUGIN_EXPORT.md) and checks, with no window on screen:
  3. // the scan, the sound against the same engine run directly in this process (null test), automation, the state (a new
  4. // instance sounds the same; the state opens as a .lynx project), the editor, memory per instance and CPU.
  5. // Exit 0 = every check passed.
  6. #include <atomic>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <juce_audio_processors/juce_audio_processors.h>
  11. #include <juce_gui_basics/juce_gui_basics.h>
  12. #include <clap/clap.h>
  13. #include "core/Effects.h"
  14. #include "core/LynxBanks.h"
  15. #include "core/ModelParams.h"
  16. #include "core/RackRenderer.h"
  17. #include "engine/ClapFormat.h"
  18. #include "engine/ProjectFile.h"
  19. #include "plugin/ExportProcessor.h"
  20. #if JUCE_WINDOWS
  21. #include <windows.h>
  22. #include <psapi.h>
  23. #endif
  24. using namespace lynx;
  25. using lynx::plugexport::Entry;
  26. namespace
  27. {
  28. constexpr double rate = 48000.0;
  29. constexpr int block = 512, blocks = 200;
  30. juce::String report;
  31. int failures = 0;
  32. void check (bool ok, const juce::String& what)
  33. {
  34. report << (ok ? "PASS " : "FAIL ") << what << "\n";
  35. failures += ok ? 0 : 1;
  36. }
  37. double memoryMb()
  38. {
  39. #if JUCE_WINDOWS
  40. PROCESS_MEMORY_COUNTERS c {};
  41. GetProcessMemoryInfo (GetCurrentProcess(), &c, sizeof (c));
  42. return (double) c.WorkingSetSize / 1048576.0;
  43. #else
  44. const auto fields = juce::StringArray::fromTokens (juce::File ("/proc/self/statm").loadFileAsString(), " ", "");
  45. return fields[1].getDoubleValue() * 4096.0 / 1048576.0;
  46. #endif
  47. }
  48. struct Audio
  49. {
  50. std::vector<float> l, r;
  51. double peak() const
  52. {
  53. double p = 0.0;
  54. for (auto v : l) p = std::max (p, (double) std::abs (v));
  55. for (auto v : r) p = std::max (p, (double) std::abs (v));
  56. return p;
  57. }
  58. // the first sample where the two differ by more than `tolerance` (-1 = none)
  59. long firstDifference (const Audio& o, double tolerance = 1.0e-5) const
  60. {
  61. for (std::size_t i = 0; i < std::min (l.size(), o.l.size()); ++i)
  62. if (std::abs (l[i] - o.l[i]) > tolerance || std::abs (r[i] - o.r[i]) > tolerance)
  63. return (long) i;
  64. return -1;
  65. }
  66. double difference (const Audio& o) const
  67. {
  68. double p = l.size() == o.l.size() ? 0.0 : 1e9;
  69. for (std::size_t i = 0; i < std::min (l.size(), o.l.size()); ++i)
  70. p = std::max ({ p, (double) std::abs (l[i] - o.l[i]), (double) std::abs (r[i] - o.r[i]) });
  71. return p;
  72. }
  73. };
  74. // test input: a decaying noise / sine mix (deterministic)
  75. void fillInput (float* l, float* r, int n, int start)
  76. {
  77. Noise noise;
  78. noise.state = 0x1234567u + (std::uint32_t) start;
  79. for (int i = 0; i < n; ++i)
  80. {
  81. const auto t = (double) (start + i) / rate;
  82. const auto v = 0.2f * (float) std::sin (2.0 * juce::MathConstants<double>::pi * 440.0 * t) + 0.1f * noise.next();
  83. l[i] = v;
  84. r[i] = 0.7f * v;
  85. }
  86. }
  87. juce::MidiBuffer notesFor (int b)
  88. {
  89. juce::MidiBuffer m;
  90. if (b == 2) m.addEvent (juce::MidiMessage::noteOn (1, 60, 0.8f), 100);
  91. if (b == 2) m.addEvent (juce::MidiMessage::noteOn (1, 67, 0.6f), 300);
  92. if (b == 100) m.addEvent (juce::MidiMessage::noteOff (1, 60), 50);
  93. if (b == 100) m.addEvent (juce::MidiMessage::noteOff (1, 67), 50);
  94. return m;
  95. }
  96. Audio render (juce::AudioProcessor& p, bool instrument, double* seconds = nullptr)
  97. {
  98. Audio out;
  99. p.setPlayConfigDetails (instrument ? 0 : 2, 2, rate, block);
  100. p.prepareToPlay (rate, block);
  101. juce::AudioBuffer<float> buffer (2, block);
  102. const auto t0 = juce::Time::getMillisecondCounterHiRes();
  103. for (int b = 0; b < blocks; ++b)
  104. {
  105. buffer.clear();
  106. if (! instrument)
  107. fillInput (buffer.getWritePointer (0), buffer.getWritePointer (1), block, b * block);
  108. auto midi = instrument ? notesFor (b) : juce::MidiBuffer();
  109. p.processBlock (buffer, midi);
  110. out.l.insert (out.l.end(), buffer.getReadPointer (0), buffer.getReadPointer (0) + block);
  111. out.r.insert (out.r.end(), buffer.getReadPointer (1), buffer.getReadPointer (1) + block);
  112. }
  113. if (seconds != nullptr)
  114. *seconds = (juce::Time::getMillisecondCounterHiRes() - t0) / 1000.0;
  115. p.releaseResources();
  116. return out;
  117. }
  118. // Round 188b: an effect with its side-chain bus switched on (stereo), the side fed with `side` × a square wave; the main
  119. // input as in render(). Empty: the host couldn't switch the bus on.
  120. Audio renderWithSide (juce::AudioProcessor& p, float side)
  121. {
  122. Audio out;
  123. auto layout = p.getBusesLayout();
  124. if (layout.inputBuses.size() < 2)
  125. return out;
  126. layout.inputBuses.getReference (1) = juce::AudioChannelSet::stereo();
  127. if (! p.setBusesLayout (layout))
  128. return out;
  129. p.setRateAndBufferSizeDetails (rate, block);
  130. p.prepareToPlay (rate, block);
  131. const auto channels = juce::jmax (p.getTotalNumInputChannels(), p.getTotalNumOutputChannels());
  132. juce::AudioBuffer<float> buffer (channels, block);
  133. for (int b = 0; b < blocks; ++b)
  134. {
  135. buffer.clear();
  136. fillInput (buffer.getWritePointer (0), buffer.getWritePointer (1), block, b * block);
  137. for (int c = 2; c < juce::jmin (4, channels); ++c)
  138. for (int i = 0; i < block; ++i)
  139. buffer.setSample (c, i, side * (i % 64 < 32 ? 1.0f : -1.0f));
  140. juce::MidiBuffer midi;
  141. p.processBlock (buffer, midi);
  142. out.l.insert (out.l.end(), buffer.getReadPointer (0), buffer.getReadPointer (0) + block);
  143. out.r.insert (out.r.end(), buffer.getReadPointer (1), buffer.getReadPointer (1) + block);
  144. }
  145. p.releaseResources();
  146. // (back to the main buses only, as render() expects)
  147. layout.inputBuses.getReference (1) = juce::AudioChannelSet::disabled();
  148. p.setBusesLayout (layout);
  149. return out;
  150. }
  151. // The same engine with no plugin between: the effect itself / a rack with the channel
  152. Audio directEffect (EffectKind kind, const EffectParams& params)
  153. {
  154. const juce::ScopedNoDenormals noDenormals; // (as the plugin's processBlock: denormals flushed)
  155. Audio out;
  156. auto effect = makeEffect (kind);
  157. effect->prepare (rate);
  158. std::vector<float> l ((std::size_t) block), r ((std::size_t) block);
  159. for (int b = 0; b < blocks; ++b)
  160. {
  161. fillInput (l.data(), r.data(), block, b * block);
  162. effect->setSongPosition (0.0);
  163. effect->setPlaying (false);
  164. effect->process (l.data(), r.data(), block, params, 120.0);
  165. out.l.insert (out.l.end(), l.begin(), l.end());
  166. out.r.insert (out.r.end(), r.begin(), r.end());
  167. }
  168. return out;
  169. }
  170. Audio directInstrument (const Entry& entry)
  171. {
  172. Audio out;
  173. auto song = Song::makeDefault();
  174. song.channels.clear();
  175. song.mixer.resize (2);
  176. song.mixer[0].effects.clear();
  177. song.mixer[1].effects.clear();
  178. // the plugin's own starting channel (round 190pq): Tetra and Braid have no preset bank, and dereferencing the empty
  179. // banks::defaultChannel copied a ChannelDef from garbage (CI plugins run 44975814572: std::length_error in the CLAP)
  180. auto channel = plugexport::instrumentChannel (entry);
  181. channel.mixerTrack = 0;
  182. song.channels.push_back (std::move (channel));
  183. auto state = RackState::build (song, nullptr, rate);
  184. auto renderer = std::make_unique<RackRenderer>();
  185. renderer->prepare (rate, block);
  186. LiveEventQueue queue;
  187. std::vector<float> l ((std::size_t) block), r ((std::size_t) block);
  188. for (int b = 0; b < blocks; ++b)
  189. {
  190. auto done = 0;
  191. const auto upTo = [&] (int end) {
  192. if (end > done)
  193. renderer->process (state.get(), false, 0.0, end - done, l.data() + done, r.data() + done, &queue);
  194. done = std::max (done, end);
  195. };
  196. for (const auto m : notesFor (b))
  197. {
  198. upTo (m.samplePosition);
  199. const auto message = m.getMessage();
  200. NoteEvent e;
  201. e.channel = 0;
  202. e.key = message.getNoteNumber();
  203. e.type = message.isNoteOn() ? NoteEvent::Type::on : NoteEvent::Type::off;
  204. e.velocity = message.isNoteOn() ? message.getFloatVelocity() : 0.0f;
  205. queue.push (e);
  206. }
  207. upTo (block);
  208. out.l.insert (out.l.end(), l.begin(), l.end());
  209. out.r.insert (out.r.end(), r.begin(), r.end());
  210. }
  211. return out;
  212. }
  213. void writeEditorPicture (const Entry& entry, const juce::String& prefix)
  214. {
  215. std::unique_ptr<juce::AudioProcessor> p (plugexport::makeProcessor (entry));
  216. {
  217. juce::MemoryBlock state; // (in-process: a VST3 host sees it inside JUCE's own chunk)
  218. p->getStateInformation (state);
  219. juce::String parseError;
  220. check (ProjectFile::fromJson (juce::String::createStringFromData (state.getData(), (int) state.getSize()), parseError).has_value(),
  221. "the state opens as a .lynx project " + parseError);
  222. }
  223. const auto before = memoryMb();
  224. std::unique_ptr<juce::AudioProcessorEditor> editor (p->createEditorIfNeeded());
  225. check (editor != nullptr && editor->getWidth() > 100 && editor->getHeight() > 50,
  226. juce::String ("in-process editor of ") + entry.name + " " + juce::String (editor != nullptr ? editor->getWidth() : 0) + "x" + juce::String (editor != nullptr ? editor->getHeight() : 0));
  227. report << " editor open adds " << juce::String (memoryMb() - before, 1) << " MB\n";
  228. if (editor == nullptr)
  229. return;
  230. juce::Image image (juce::Image::ARGB, editor->getWidth(), editor->getHeight(), true);
  231. {
  232. juce::Graphics g (image);
  233. editor->paintEntireComponent (g, false);
  234. }
  235. const juce::File file (prefix + juce::String (entry.name).removeCharacters (" ") + ".png");
  236. file.deleteFile();
  237. juce::PNGImageFormat png;
  238. if (auto stream = file.createOutputStream())
  239. png.writeImageToStream (image, *stream);
  240. p->editorBeingDeleted (editor.get());
  241. editor.reset();
  242. }
  243. } // namespace
  244. #if JUCE_MAC
  245. #include <CoreFoundation/CoreFoundation.h>
  246. #else
  247. namespace juce::detail { bool dispatchNextMessageOnSystemQueue (bool returnIfNoPendingMessages); } // (juce_events' native code)
  248. #endif
  249. namespace
  250. {
  251. // Lets the message loop (and a CLAP plugin's timer) run for a while
  252. void pump (int ms, const std::function<void()>& tick = {})
  253. {
  254. const auto end = juce::Time::getMillisecondCounter() + (juce::uint32) ms;
  255. while (juce::Time::getMillisecondCounter() < end)
  256. {
  257. #if JUCE_MAC
  258. CFRunLoopRunInMode (kCFRunLoopDefaultMode, 0.005, false); // (JUCE's messages come through the main run loop)
  259. #else
  260. while (juce::detail::dispatchNextMessageOnSystemQueue (true)) {}
  261. juce::Thread::sleep (5);
  262. #endif
  263. if (tick)
  264. tick();
  265. }
  266. }
  267. // Counts what a host would take as an edit: parameter changes coming from the plugin
  268. struct EditCounter final : juce::AudioProcessorParameter::Listener
  269. {
  270. std::atomic<int> changes { 0 };
  271. void parameterValueChanged (int, float) override { ++changes; }
  272. void parameterGestureChanged (int, bool) override { ++changes; }
  273. };
  274. // A loaded state (a project being opened) is not an edit: the host must get no parameter changes from it, at once or
  275. // later from the editor's sync (round 180b: FL Studio asked to save a project it had only opened)
  276. void stateLoadIsSilent (juce::AudioPluginFormat& format, const juce::PluginDescription& description, const juce::MemoryBlock& state)
  277. {
  278. juce::String error;
  279. auto third = format.createInstanceFromDescription (description, rate, block, error);
  280. if (third == nullptr)
  281. return;
  282. third->prepareToPlay (rate, block);
  283. EditCounter counter;
  284. for (auto* p : third->getParameters())
  285. p->addListener (&counter);
  286. third->setStateInformation (state.getData(), (int) state.getSize());
  287. // (JUCE's VST3 host re-reads every value inside setStateInformation and tells its own listeners: that is the host
  288. // updating its view, not an edit from the plugin, whose wrapper sends none while a state loads; what comes later is)
  289. report << " host refreshed " << counter.changes.exchange (0) << " values inside the state load\n";
  290. pump (300);
  291. const auto afterLoad = counter.changes.load();
  292. std::unique_ptr<juce::AudioProcessorEditor> editor (third->createEditorIfNeeded());
  293. pump (600);
  294. const auto withEditor = counter.changes.load() - afterLoad;
  295. editor.reset();
  296. for (auto* p : third->getParameters())
  297. p->removeListener (&counter);
  298. check (afterLoad == 0 && withEditor == 0, "a loaded state sends the host no edits: " + juce::String (afterLoad)
  299. + " at load, " + juce::String (withEditor) + " with the editor open");
  300. }
  301. // The same for the CLAP, with a host of our own that sees what LYNX's CLAP host ignores: parameter events the plugin
  302. // sends out and host_state.mark_dirty ("project modified")
  303. struct RawClapHost
  304. {
  305. static inline std::atomic<int> dirty { 0 }, flushes { 0 };
  306. static inline clap_id timer = CLAP_INVALID_ID;
  307. clap_host_t host {};
  308. clap_host_params_t params { [] (const clap_host_t*, clap_param_rescan_flags) {}, [] (const clap_host_t*, clap_id, clap_param_clear_flags) {},
  309. [] (const clap_host_t*) { ++flushes; } };
  310. clap_host_state_t state { [] (const clap_host_t*) { ++dirty; } };
  311. clap_host_latency_t latency { [] (const clap_host_t*) {} };
  312. clap_host_timer_support_t timers { [] (const clap_host_t*, uint32_t, clap_id* id) { *id = timer = 1; return true; },
  313. [] (const clap_host_t*, clap_id) { timer = CLAP_INVALID_ID; return true; } };
  314. RawClapHost()
  315. {
  316. host.clap_version = CLAP_VERSION;
  317. host.host_data = this;
  318. host.name = "plugexporttest";
  319. host.vendor = "LYNX";
  320. host.url = "";
  321. host.version = "1";
  322. host.get_extension = [] (const clap_host_t* h, const char* id) -> const void* {
  323. auto* self = static_cast<RawClapHost*> (h->host_data);
  324. if (std::strcmp (id, CLAP_EXT_PARAMS) == 0) return &self->params;
  325. if (std::strcmp (id, CLAP_EXT_STATE) == 0) return &self->state;
  326. if (std::strcmp (id, CLAP_EXT_LATENCY) == 0) return &self->latency;
  327. if (std::strcmp (id, CLAP_EXT_TIMER_SUPPORT) == 0) return &self->timers;
  328. return nullptr;
  329. };
  330. host.request_restart = [] (const clap_host_t*) {};
  331. host.request_process = [] (const clap_host_t*) {};
  332. host.request_callback = [] (const clap_host_t*) {};
  333. }
  334. };
  335. void clapStateLoadIsSilent (const juce::File& file, const Entry& entry)
  336. {
  337. std::fprintf (stderr, "plugexporttest: CLAP state in a plain host: %s\n", entry.name); // (the report is written at the end; this says where a crash was)
  338. std::fflush (stderr);
  339. // a state with moved parameters, from the same engine in this process
  340. std::unique_ptr<juce::AudioProcessor> source (plugexport::makeProcessor (entry));
  341. for (auto* p : source->getParameters())
  342. p->setValueNotifyingHost (std::fmod (p->getDefaultValue() + 0.37f, 1.0f));
  343. juce::MemoryBlock saved;
  344. source->getStateInformation (saved);
  345. juce::DynamicLibrary library;
  346. auto binary = file; // (macOS: the executable inside the .clap bundle; clap_entry.init still gets the bundle)
  347. if (file.isDirectory())
  348. if (const auto inside = file.getChildFile ("Contents/MacOS").getChildFile (file.getFileNameWithoutExtension()); inside.existsAsFile())
  349. binary = inside;
  350. if (! library.open (binary.getFullPathName()))
  351. return check (false, "CLAP opens in a plain host");
  352. const auto* clapEntry = static_cast<const clap_plugin_entry_t*> (library.getFunction ("clap_entry"));
  353. if (clapEntry == nullptr || ! clapEntry->init (file.getFullPathName().toRawUTF8()))
  354. return check (false, "CLAP entry in a plain host");
  355. {
  356. RawClapHost host;
  357. const auto* factory = static_cast<const clap_plugin_factory_t*> (clapEntry->get_factory (CLAP_PLUGIN_FACTORY_ID));
  358. const auto* plugin = factory != nullptr ? factory->create_plugin (factory, &host.host, entry.clapId) : nullptr;
  359. if (plugin == nullptr || ! plugin->init (plugin))
  360. check (false, "CLAP instance in a plain host");
  361. else
  362. {
  363. const auto* state = static_cast<const clap_plugin_state_t*> (plugin->get_extension (plugin, CLAP_EXT_STATE));
  364. const auto* params = static_cast<const clap_plugin_params_t*> (plugin->get_extension (plugin, CLAP_EXT_PARAMS));
  365. const auto* gui = static_cast<const clap_plugin_gui_t*> (plugin->get_extension (plugin, CLAP_EXT_GUI));
  366. const auto* timer = static_cast<const clap_plugin_timer_support_t*> (plugin->get_extension (plugin, CLAP_EXT_TIMER_SUPPORT));
  367. int sentOut = 0;
  368. clap_input_events_t none { nullptr, [] (const clap_input_events_t*) -> uint32_t { return 0; },
  369. [] (const clap_input_events_t*, uint32_t) -> const clap_event_header_t* { return nullptr; } };
  370. clap_output_events_t out { &sentOut, [] (const clap_output_events_t* o, const clap_event_header_t*) { ++*static_cast<int*> (o->ctx); return true; } };
  371. auto tick = [&] {
  372. if (timer != nullptr && RawClapHost::timer != CLAP_INVALID_ID)
  373. timer->on_timer (plugin, RawClapHost::timer);
  374. if (params != nullptr)
  375. params->flush (plugin, &none, &out);
  376. };
  377. struct Reader { const juce::MemoryBlock* data; std::size_t at = 0; } reader { &saved };
  378. clap_istream_t in { &reader, [] (const clap_istream_t* s, void* buffer, uint64_t size) -> int64_t {
  379. auto* r = static_cast<Reader*> (s->ctx);
  380. const auto n = std::min<std::size_t> ((std::size_t) size, r->data->getSize() - r->at);
  381. std::memcpy (buffer, static_cast<const char*> (r->data->getData()) + r->at, n);
  382. r->at += n;
  383. return (int64_t) n;
  384. } };
  385. RawClapHost::dirty = 0;
  386. const auto loaded = state != nullptr && state->load (plugin, &in);
  387. check (loaded, "CLAP state loads in a plain host");
  388. pump (300, tick);
  389. const auto atLoad = RawClapHost::dirty.load() + sentOut;
  390. int withEditor = 0;
  391. #if JUCE_WINDOWS
  392. const char* api = CLAP_WINDOW_API_WIN32;
  393. #else
  394. const char* api = CLAP_WINDOW_API_X11;
  395. #endif
  396. if (gui != nullptr && gui->create (plugin, api, false))
  397. {
  398. const auto before = RawClapHost::dirty.load() + sentOut;
  399. pump (600, tick);
  400. withEditor = RawClapHost::dirty.load() + sentOut - before;
  401. gui->destroy (plugin);
  402. }
  403. check (atLoad == 0 && withEditor == 0, "a loaded CLAP state marks nothing dirty and sends no parameter events: "
  404. + juce::String (atLoad) + " at load, " + juce::String (withEditor) + " with the editor open");
  405. plugin->destroy (plugin);
  406. }
  407. }
  408. clapEntry->deinit();
  409. }
  410. const Entry* entryNamed (const juce::String& name)
  411. {
  412. for (const auto& e : plugexport::catalog)
  413. if (name == e.name)
  414. return &e;
  415. return nullptr;
  416. }
  417. void testPlugin (juce::AudioPluginFormat& format, const juce::PluginDescription& description, const juce::String& picturePrefix)
  418. {
  419. std::fprintf (stderr, "plugexporttest: %s (%s)\n", description.name.toRawUTF8(), format.getName().toRawUTF8());
  420. std::fflush (stderr);
  421. const auto* entry = entryNamed (description.name);
  422. check (entry != nullptr, "known name: " + description.name);
  423. if (entry == nullptr)
  424. return;
  425. report << "--- " << description.name << " (" << format.getName() << ", " << (description.isInstrument ? "instrument" : "effect") << ")\n";
  426. check (description.isInstrument == entry->instrument, "scan says instrument or effect correctly");
  427. juce::String error;
  428. const auto base = memoryMb();
  429. auto make = [&] { return format.createInstanceFromDescription (description, rate, block, error); };
  430. auto instance = make();
  431. check (instance != nullptr, "instance " + error);
  432. if (instance == nullptr)
  433. return;
  434. report << " first instance: +" << juce::String (memoryMb() - base, 1) << " MB\n";
  435. // the effect engine's parameter i in the host's list. JUCE's LV2 host lists the plugin's lv2:Parameter properties in
  436. // an order of its own and adds the lv2:enabled port as "Bypass": there it is the one whose id ends in ":p<i>" (the
  437. // plugin's parameter id, round 190pk); the VST3 and CLAP hosts keep the plugin's order
  438. const auto lv2 = format.getName() == "LV2";
  439. auto engineParameter = [&] (int i) -> juce::AudioProcessorParameter*
  440. {
  441. const auto& all = instance->getParameters();
  442. if (lv2)
  443. {
  444. for (auto* p : all)
  445. if (auto* hosted = dynamic_cast<juce::HostedAudioProcessorParameter*> (p))
  446. if (hosted->getParameterID().endsWith (":p" + juce::String (i)))
  447. return p;
  448. return nullptr;
  449. }
  450. return juce::isPositiveAndBelow (i, all.size()) ? all[i] : nullptr;
  451. };
  452. check (instance->getParameters().size() > 0, "parameters: " + juce::String (instance->getParameters().size()));
  453. double seconds = 0.0;
  454. const auto sound = render (*instance, entry->instrument, &seconds);
  455. report << " " << blocks << " blocks of " << block << ": " << juce::String (100.0 * seconds / (blocks * block / rate), 2) << " % of real time\n";
  456. check (sound.peak() > 1.0e-3, "makes sound, peak " + juce::String (sound.peak(), 4));
  457. auto direct = entry->instrument ? directInstrument (*entry) : directEffect (entry->effect, makeEffectSlot (entry->effect).params);
  458. const auto nulled = sound.difference (direct);
  459. check (nulled < 1.0e-5, "null test at the defaults: peak difference " + juce::String (nulled, 8));
  460. // round 188b: the side-chain bus. Switched on and silent, nothing changes (at the defaults no effect listens to it)
  461. if (! entry->instrument)
  462. {
  463. const auto withSide = renderWithSide (*instance, 0.0f);
  464. if (withSide.l.empty())
  465. report << " side-chain bus: this host can't switch it on\n";
  466. else
  467. check (withSide.difference (sound) < 1.0e-5, "side-chain bus on and silent: peak difference " + juce::String (withSide.difference (sound), 8));
  468. if (entry->effect == EffectKind::crestCompressor && ! withSide.l.empty())
  469. {
  470. // its external side chain on: a loud side signal compresses the main input
  471. const auto& names = effectInfo (entry->effect).params;
  472. for (int i = 0; i < (int) names.size(); ++i)
  473. if (auto* p = engineParameter (i); p != nullptr && juce::String::fromUTF8 (names[(std::size_t) i].name) == "External SC")
  474. p->setValue (1.0f);
  475. const auto quiet = renderWithSide (*instance, 0.0f), loud = renderWithSide (*instance, 0.9f);
  476. const auto energy = [] (const Audio& a) { double e = 0.0; for (auto v : a.l) e += (double) v * v; return e; };
  477. check (energy (loud) < 0.7 * energy (quiet), "external side chain through the bus compresses: " + juce::String (energy (loud) / juce::jmax (1.0e-12, energy (quiet)), 3));
  478. for (int i = 0; i < (int) names.size(); ++i)
  479. if (auto* p = engineParameter (i); p != nullptr && juce::String::fromUTF8 (names[(std::size_t) i].name) == "External SC")
  480. p->setValue (0.0f);
  481. }
  482. }
  483. auto& parameters = instance->getParameters();
  484. if (! entry->instrument)
  485. {
  486. // every named parameter moved through the host, against the same values on the engine run directly. Facet EQ's
  487. // defaults are flat (its output is its input), so this is the null test that says something.
  488. auto params = makeEffectSlot (entry->effect).params;
  489. const auto named = (int) effectInfo (entry->effect).params.size();
  490. int found = 0;
  491. for (int i = 0; i < named; ++i)
  492. if (auto* p = engineParameter (i))
  493. {
  494. const auto v = 0.5f + 0.45f * (float) std::sin ((double) i * 0.7);
  495. p->setValue (v);
  496. params[(std::size_t) i] = p->getValue();
  497. ++found;
  498. }
  499. check (found == juce::jmin (named, parameters.size()), "the host lists the engine's parameters: " + juce::String (found) + " of " + juce::String (named));
  500. const auto wet = render (*instance, false);
  501. const auto d = wet.difference (direct = directEffect (entry->effect, params));
  502. check (d < 1.0e-5, "null test with " + juce::String (named) + " parameters moved by the host: peak difference " + juce::String (d, 8)
  503. + (d < 1.0e-5 ? juce::String() : ", first at sample " + juce::String (wet.firstDifference (direct))));
  504. check (wet.difference (sound) > 1.0e-3, "those settings change the sound, difference " + juce::String (wet.difference (sound), 5));
  505. }
  506. else
  507. {
  508. // the engine's first continuous parameter after its first one (VST3 / CLAP: the host's parameter 1, as before). JUCE's
  509. // LV2 host lists them in an order of its own: there it is found by its id (round 190pq: its parameter 1 was Fourfold's
  510. // stepped "A On" switch, which a move of 0.3 leaves as it was)
  511. juce::AudioProcessorParameter* moved = nullptr;
  512. if (const auto* table = modelparams::tableOf (entry->synth); table != nullptr && table->params != nullptr)
  513. for (std::size_t k = 1; moved == nullptr && k < table->params->size(); ++k)
  514. if (const auto& e = (*table->params)[k]; ! (e.stepped && e.high > e.low && e.high - e.low <= 4096.0f)) // (the plugin's rule for a discrete parameter)
  515. moved = lv2 ? engineParameter (e.index) : engineParameter ((int) k);
  516. check (moved != nullptr, "the host lists a continuous parameter of the engine");
  517. if (moved != nullptr)
  518. {
  519. const auto was = moved->getValue();
  520. moved->setValue (was < 0.5f ? was + 0.3f : was - 0.3f);
  521. report << " moved '" << moved->getName (64) << "' " << was << " -> " << moved->getValue() << "\n";
  522. const auto changed = render (*instance, true);
  523. check (changed.difference (sound) > 1.0e-3, "the moved parameter changes the sound, difference " + juce::String (changed.difference (sound), 5));
  524. }
  525. }
  526. // state: a new instance of what we just heard sounds the same
  527. juce::MemoryBlock state;
  528. instance->getStateInformation (state);
  529. report << " state: " << (int) state.getSize() << " bytes\n";
  530. auto second = make();
  531. check (second != nullptr, "second instance");
  532. if (second != nullptr)
  533. {
  534. second->setStateInformation (state.getData(), (int) state.getSize());
  535. const auto first = render (*instance, entry->instrument), restored = render (*second, entry->instrument);
  536. const auto d = restored.difference (first);
  537. check (d < 1.0e-3, "state restores the sound: peak difference " + juce::String (d, 8)
  538. + (d < 1.0e-3 ? juce::String() : ", first at sample " + juce::String (restored.firstDifference (first, 1.0e-3))));
  539. }
  540. stateLoadIsSilent (format, description, state);
  541. // round 190pq: every parameter moved (as a host's randomizer does), the state loaded into a new instance saves back
  542. // byte for byte (clap-validator's state-reproducibility-binary failed on Fourfold and Wavecoil: values written back
  543. // through their range moved by a float step)
  544. if (auto third = make())
  545. {
  546. for (int i = 0; i < parameters.size(); ++i)
  547. parameters[i]->setValue (0.5f + 0.49f * (float) std::sin ((double) i * 1.37 + 0.3));
  548. juce::MemoryBlock saved, again;
  549. instance->getStateInformation (saved);
  550. third->setStateInformation (saved.getData(), (int) saved.getSize());
  551. third->getStateInformation (again);
  552. auto at = -1;
  553. for (size_t i = 0; at < 0 && i < juce::jmin (again.getSize(), saved.getSize()); ++i)
  554. if (again[i] != saved[i])
  555. at = (int) i;
  556. if (at < 0 && again.getSize() != saved.getSize())
  557. at = (int) juce::jmin (again.getSize(), saved.getSize());
  558. check (at < 0, "a loaded state saves back byte for byte with " + juce::String (parameters.size()) + " parameters moved"
  559. + (at < 0 ? juce::String() : ": " + juce::String ((int) again.getSize()) + " vs " + juce::String ((int) saved.getSize())
  560. + " bytes, first difference at byte " + juce::String (at)));
  561. }
  562. else
  563. check (false, "third instance");
  564. // hosted editor object (never put on the screen)
  565. if (auto* editor = instance->createEditorIfNeeded())
  566. {
  567. check (editor->getWidth() > 100, "hosted editor created, " + juce::String (editor->getWidth()) + "x" + juce::String (editor->getHeight()));
  568. delete editor;
  569. }
  570. else
  571. check (false, "hosted editor created");
  572. const auto oneMore = memoryMb();
  573. std::vector<std::unique_ptr<juce::AudioPluginInstance>> many;
  574. for (int i = 0; i < 8; ++i)
  575. many.push_back (make());
  576. report << " 8 more instances: +" << juce::String ((memoryMb() - oneMore) / 8.0, 1) << " MB each\n";
  577. many.clear();
  578. instance.reset();
  579. if (picturePrefix.isNotEmpty())
  580. writeEditorPicture (*entry, picturePrefix);
  581. }
  582. } // namespace
  583. int main (int argc, char** argv)
  584. {
  585. if (argc < 3)
  586. return 2;
  587. juce::ScopedJuceInitialiser_GUI gui;
  588. const juce::File plugin (juce::String::fromUTF8 (argv[1]));
  589. const juce::String picturePrefix = argc > 3 ? juce::String::fromUTF8 (argv[3]) : juce::String();
  590. juce::VST3PluginFormat vst3;
  591. ClapPluginFormat clap;
  592. juce::AudioPluginFormat* chosen = &vst3;
  593. if (plugin.hasFileExtension ("clap"))
  594. chosen = &clap;
  595. #if JUCE_PLUGINHOST_LV2 && JUCE_LINUX
  596. juce::LV2PluginFormat lv2; // (round 190ov: a bundle folder; JUCE's LV2 host loads it with lilv)
  597. if (plugin.hasFileExtension ("lv2"))
  598. chosen = &lv2;
  599. #endif
  600. juce::AudioPluginFormat& format = *chosen;
  601. juce::OwnedArray<juce::PluginDescription> found;
  602. format.findAllTypesForFile (found, plugin.getFullPathName());
  603. report << "plugin " << plugin.getFileName() << ": " << found.size() << " found\n";
  604. check (found.size() > 0, "scan finds plugins");
  605. for (auto* d : found)
  606. testPlugin (format, *d, picturePrefix);
  607. if (plugin.hasFileExtension ("clap"))
  608. for (auto* d : found)
  609. if (const auto* entry = entryNamed (d->name))
  610. clapStateLoadIsSilent (plugin, *entry);
  611. report << "result=" << (failures == 0 ? "OK" : "FAIL (" + juce::String (failures) + ")") << "\n";
  612. juce::File (juce::String::fromUTF8 (argv[2])).replaceWithText (report);
  613. std::fputs (report.toRawUTF8(), stdout);
  614. return failures == 0 ? 0 : 1;
  615. }