plugexporttest.cpp 26 KB

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