4
0

lynx_test_heavy_vst3.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. // A heavy VST3 instrument for measuring what Freeze unloads (round 190ay). The test synth of lynx_test_synth_vst3.cpp
  2. // (same build line, output "LYNX Test Heavy.vst3") made expensive on purpose, with nothing to click and no licence:
  3. // - memory: each instance allocates and touches LYNX_TEST_HEAVY_MB megabytes (default 256), like a sample library;
  4. // - CPU: LYNX_TEST_HEAVY_WORK extra sine evaluations per frame (default 400), folded into the sound at -140 dB;
  5. // - latency: it reports LYNX_TEST_HEAVY_LATENCY frames (default 512) and its output comes that much late;
  6. // - state: a detune in cents. A new instance starts at 7 cents x its number in the process, so two instances only
  7. // sound the same when the host gave the second one the first one's state (getState / setState);
  8. // - with LYNX_TEST_HEAVY_LOG = a file path it appends "create <n>" / "destroy <n>" lines: which instances are alive.
  9. #define INIT_CLASS_IID
  10. #include "pluginterfaces/base/funknown.h"
  11. #include "pluginterfaces/base/ipluginbase.h"
  12. #include "pluginterfaces/base/ibstream.h"
  13. #include "pluginterfaces/vst/ivstaudioprocessor.h"
  14. #include "pluginterfaces/vst/ivstcomponent.h"
  15. #include "pluginterfaces/vst/ivsteditcontroller.h"
  16. #include "pluginterfaces/vst/ivstevents.h"
  17. #include "pluginterfaces/vst/ivstprocesscontext.h"
  18. #include "pluginterfaces/gui/iplugview.h"
  19. #include <algorithm>
  20. #include <atomic>
  21. #include <cmath>
  22. #include <cstdio>
  23. #include <cstdlib>
  24. #include <cstring>
  25. using namespace Steinberg;
  26. using namespace Steinberg::Vst;
  27. namespace
  28. {
  29. const FUID synthUid (0x4C594E58, 0x48565931, 0x90A70001, 0x00000001); // "LYNXTSY169c1VST3"
  30. void copyString (String128 to, const char* from)
  31. {
  32. int i = 0;
  33. for (; from[i] != 0 && i < 127; ++i)
  34. to[i] = (char16) from[i];
  35. to[i] = 0;
  36. }
  37. class TestHeavy final : public IComponent, public IAudioProcessor, public IEditController
  38. {
  39. public:
  40. TestHeavy()
  41. {
  42. auto number = [] (const char* name, long fallback) {
  43. const char* v = std::getenv (name);
  44. return v != nullptr && *v != 0 ? std::atol (v) : fallback;
  45. };
  46. megabytes = (size_t) std::max (0L, number ("LYNX_TEST_HEAVY_MB", 256));
  47. work = (int) std::max (0L, number ("LYNX_TEST_HEAVY_WORK", 400));
  48. latency = (int) std::min (8192L, std::max (0L, number ("LYNX_TEST_HEAVY_LATENCY", 512)));
  49. library = (unsigned char*) std::malloc (megabytes * 1048576 + 1);
  50. if (library != nullptr)
  51. for (size_t i = 0; i < megabytes * 1048576; i += 4096)
  52. library[i] = (unsigned char) (i >> 12); // touched: really in memory
  53. cents = 7.0f * (float) id;
  54. note ("create");
  55. }
  56. virtual ~TestHeavy()
  57. {
  58. note ("destroy");
  59. std::free (library);
  60. if (log != nullptr)
  61. std::fclose (log);
  62. }
  63. void note (const char* what) const
  64. {
  65. if (const char* path = std::getenv ("LYNX_TEST_HEAVY_LOG"); path != nullptr && *path != 0)
  66. if (auto* f = std::fopen (path, "a"))
  67. {
  68. std::fprintf (f, "%s %d\n", what, id);
  69. std::fclose (f);
  70. }
  71. }
  72. // FUnknown
  73. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  74. {
  75. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginBase::iid)
  76. || FUnknownPrivate::iidEqual (iid, IComponent::iid))
  77. {
  78. addRef();
  79. *obj = static_cast<IComponent*> (this);
  80. return kResultOk;
  81. }
  82. if (FUnknownPrivate::iidEqual (iid, IAudioProcessor::iid))
  83. {
  84. addRef();
  85. *obj = static_cast<IAudioProcessor*> (this);
  86. return kResultOk;
  87. }
  88. if (FUnknownPrivate::iidEqual (iid, IEditController::iid))
  89. {
  90. addRef();
  91. *obj = static_cast<IEditController*> (this);
  92. return kResultOk;
  93. }
  94. *obj = nullptr;
  95. return kNoInterface;
  96. }
  97. uint32 PLUGIN_API addRef() override { return ++refs; }
  98. uint32 PLUGIN_API release() override
  99. {
  100. const auto r = --refs;
  101. if (r == 0)
  102. delete this;
  103. return r;
  104. }
  105. // IPluginBase (shared by the component and the controller)
  106. tresult PLUGIN_API initialize (FUnknown*) override
  107. {
  108. if (log == nullptr)
  109. if (const char* path = std::getenv ("LYNX_TEST_HEAVY_PROCESS_LOG"); path != nullptr && *path != 0)
  110. {
  111. char name[1024];
  112. std::snprintf (name, sizeof (name), "%s.%d", path, id); // one file per instance (threads)
  113. log = std::fopen (name, "a");
  114. }
  115. return kResultOk;
  116. }
  117. tresult PLUGIN_API terminate() override { return kResultOk; }
  118. // IComponent
  119. tresult PLUGIN_API getControllerClassId (TUID) override { return kNotImplemented; } // single component
  120. tresult PLUGIN_API setIoMode (IoMode) override { return kResultOk; }
  121. int32 PLUGIN_API getBusCount (MediaType type, BusDirection dir) override
  122. {
  123. if (type == kAudio && dir == kOutput)
  124. return 1;
  125. if (type == kEvent && dir == kInput)
  126. return 1;
  127. return 0;
  128. }
  129. tresult PLUGIN_API getBusInfo (MediaType type, BusDirection dir, int32 index, BusInfo& bus) override
  130. {
  131. if (index != 0)
  132. return kInvalidArgument;
  133. std::memset (&bus, 0, sizeof (bus));
  134. bus.mediaType = type;
  135. bus.direction = dir;
  136. bus.busType = kMain;
  137. bus.flags = BusInfo::kDefaultActive;
  138. if (type == kAudio && dir == kOutput)
  139. {
  140. bus.channelCount = 2;
  141. copyString (bus.name, "Out");
  142. return kResultOk;
  143. }
  144. if (type == kEvent && dir == kInput)
  145. {
  146. bus.channelCount = 16;
  147. copyString (bus.name, "MIDI In");
  148. return kResultOk;
  149. }
  150. return kInvalidArgument;
  151. }
  152. tresult PLUGIN_API getRoutingInfo (RoutingInfo&, RoutingInfo&) override { return kNotImplemented; }
  153. tresult PLUGIN_API activateBus (MediaType, BusDirection, int32, TBool) override { return kResultOk; }
  154. tresult PLUGIN_API setActive (TBool state) override
  155. {
  156. if (! state)
  157. {
  158. voiceCount = 0;
  159. std::memset (line, 0, sizeof (line));
  160. linePos = 0;
  161. }
  162. return kResultOk;
  163. }
  164. tresult PLUGIN_API setState (IBStream* s) override
  165. {
  166. char tag[4] {};
  167. float value = 0.0f;
  168. int32 got = 0;
  169. if (s != nullptr && s->read (tag, 4, &got) == kResultOk && got == 4 && std::memcmp (tag, "LTH1", 4) == 0
  170. && s->read (&value, 4, &got) == kResultOk && got == 4)
  171. cents = value;
  172. return kResultOk;
  173. }
  174. tresult PLUGIN_API getState (IBStream* s) override
  175. {
  176. if (s != nullptr)
  177. {
  178. int32 written = 0;
  179. const char tag[4] = { 'L', 'T', 'H', '1' };
  180. s->write ((void*) tag, 4, &written);
  181. s->write ((void*) &cents, 4, &written);
  182. }
  183. return kResultOk;
  184. }
  185. // IAudioProcessor
  186. tresult PLUGIN_API setBusArrangements (SpeakerArrangement*, int32 numIns, SpeakerArrangement* outs, int32 numOuts) override
  187. {
  188. return numIns == 0 && numOuts == 1 && outs[0] == SpeakerArr::kStereo ? kResultTrue : kResultFalse;
  189. }
  190. tresult PLUGIN_API getBusArrangement (BusDirection dir, int32 index, SpeakerArrangement& arr) override
  191. {
  192. if (dir == kOutput && index == 0)
  193. {
  194. arr = SpeakerArr::kStereo;
  195. return kResultOk;
  196. }
  197. return kInvalidArgument;
  198. }
  199. tresult PLUGIN_API canProcessSampleSize (int32 size) override { return size == kSample32 ? kResultTrue : kResultFalse; }
  200. uint32 PLUGIN_API getLatencySamples() override { return (uint32) latency; }
  201. tresult PLUGIN_API setupProcessing (ProcessSetup& setup) override
  202. {
  203. rate = setup.sampleRate > 0.0 ? setup.sampleRate : 44100.0;
  204. offline = setup.processMode == kOffline;
  205. return kResultOk;
  206. }
  207. tresult PLUGIN_API setProcessing (TBool) override { return kResultOk; }
  208. uint32 PLUGIN_API getTailSamples() override { return 0; }
  209. tresult PLUGIN_API process (ProcessData& data) override
  210. {
  211. const auto n = data.numSamples;
  212. if (log != nullptr)
  213. {
  214. const auto* c = data.processContext;
  215. const auto playing = c != nullptr && (c->state & ProcessContext::kPlaying) != 0;
  216. const auto ppq = c != nullptr && (c->state & ProcessContext::kProjectTimeMusicValid) != 0 ? c->projectTimeMusic : -1.0;
  217. std::fprintf (log, "P%d %.9f %d %d %s %d\n", id, ppq, (int) n, playing ? 1 : 0, offline ? "off" : "rt", voiceCount);
  218. }
  219. float* left = nullptr;
  220. float* right = nullptr;
  221. if (data.numOutputs > 0 && data.outputs[0].numChannels >= 2 && n > 0)
  222. {
  223. left = data.outputs[0].channelBuffers32[0];
  224. right = data.outputs[0].channelBuffers32[1];
  225. std::memset (left, 0, sizeof (float) * (size_t) n);
  226. std::memset (right, 0, sizeof (float) * (size_t) n);
  227. }
  228. auto* events = data.inputEvents;
  229. const auto eventCount = events != nullptr ? events->getEventCount() : 0;
  230. int32 next = 0;
  231. Event e {};
  232. auto take = [&] (int32 i) { return events != nullptr && events->getEvent (i, e) == kResultOk; };
  233. bool have = next < eventCount && take (next);
  234. for (int32 s = 0; s < n || (have && n == 0); ++s)
  235. {
  236. while (have && e.sampleOffset <= s)
  237. {
  238. apply (e);
  239. ++next;
  240. have = next < eventCount && take (next);
  241. }
  242. if (n == 0)
  243. break;
  244. float v = 0.0f;
  245. for (int i = 0; i < voiceCount; ++i)
  246. {
  247. auto& voice = voices[i];
  248. v += 0.1f * (float) std::sin (voice.phase);
  249. voice.phase += voice.step;
  250. if (voice.phase > 6.283185307179586)
  251. voice.phase -= 6.283185307179586;
  252. }
  253. // the cost: `work` sines per frame, 140 dB down
  254. double burn = 0.0;
  255. for (int w = 0; w < work; ++w)
  256. burn += std::sin (burnPhase + 0.001 * (double) w);
  257. burnPhase += 0.01;
  258. if (burnPhase > 6.283185307179586)
  259. burnPhase -= 6.283185307179586;
  260. v += (float) (1.0e-7 * burn / (double) (work > 0 ? work : 1));
  261. if (library != nullptr && megabytes > 0)
  262. v += 1.0e-9f * (float) library[(size_t) (burnPhase * 1000.0) * 4096 % (megabytes * 1048576)];
  263. // the latency: out comes what went in `latency` frames ago
  264. if (latency > 0)
  265. {
  266. const auto delayed = line[linePos];
  267. line[linePos] = v;
  268. linePos = (linePos + 1) % latency;
  269. v = delayed;
  270. }
  271. if (left != nullptr)
  272. {
  273. left[s] = v;
  274. right[s] = v;
  275. }
  276. }
  277. while (have) // (events past the block's end)
  278. {
  279. apply (e);
  280. ++next;
  281. have = next < eventCount && take (next);
  282. }
  283. if (log != nullptr && eventCount > 0)
  284. std::fflush (log);
  285. return kResultOk;
  286. }
  287. // IEditController (no parameters, no editor)
  288. tresult PLUGIN_API setComponentState (IBStream*) override { return kResultOk; }
  289. int32 PLUGIN_API getParameterCount() override { return 0; }
  290. tresult PLUGIN_API getParameterInfo (int32, ParameterInfo&) override { return kInvalidArgument; }
  291. tresult PLUGIN_API getParamStringByValue (ParamID, ParamValue, String128) override { return kInvalidArgument; }
  292. tresult PLUGIN_API getParamValueByString (ParamID, TChar*, ParamValue&) override { return kInvalidArgument; }
  293. ParamValue PLUGIN_API normalizedParamToPlain (ParamID, ParamValue v) override { return v; }
  294. ParamValue PLUGIN_API plainParamToNormalized (ParamID, ParamValue v) override { return v; }
  295. ParamValue PLUGIN_API getParamNormalized (ParamID) override { return 0.0; }
  296. tresult PLUGIN_API setParamNormalized (ParamID, ParamValue) override { return kResultOk; }
  297. tresult PLUGIN_API setComponentHandler (IComponentHandler*) override { return kResultOk; }
  298. IPlugView* PLUGIN_API createView (FIDString) override { return nullptr; }
  299. private:
  300. struct Voice
  301. {
  302. int key = 0;
  303. double phase = 0.0, step = 0.0;
  304. };
  305. void apply (const Event& e)
  306. {
  307. if (e.type == Event::kNoteOnEvent)
  308. {
  309. if (e.noteOn.velocity <= 0.0f)
  310. {
  311. noteOff (e.noteOn.pitch, e.sampleOffset);
  312. return;
  313. }
  314. if (log != nullptr)
  315. std::fprintf (log, "E%d on %d %d\n", id, (int) e.noteOn.pitch, (int) e.sampleOffset);
  316. if (voiceCount < maxVoices)
  317. {
  318. auto& v = voices[voiceCount++];
  319. v.key = e.noteOn.pitch;
  320. v.phase = 0.0;
  321. v.step = 6.283185307179586 * 440.0 * std::pow (2.0, (e.noteOn.pitch - 69 + cents / 100.0) / 12.0) / rate;
  322. }
  323. }
  324. else if (e.type == Event::kNoteOffEvent)
  325. noteOff (e.noteOff.pitch, e.sampleOffset);
  326. }
  327. void noteOff (int key, int offset)
  328. {
  329. if (log != nullptr)
  330. std::fprintf (log, "E%d off %d %d\n", id, key, offset);
  331. for (int i = 0; i < voiceCount; ++i)
  332. if (voices[i].key == key)
  333. {
  334. for (int j = i + 1; j < voiceCount; ++j)
  335. voices[j - 1] = voices[j];
  336. --voiceCount;
  337. return;
  338. }
  339. }
  340. size_t megabytes = 0;
  341. unsigned char* library = nullptr;
  342. int work = 0, latency = 0, linePos = 0;
  343. float line[8192] {};
  344. double burnPhase = 0.0;
  345. float cents = 0.0f;
  346. static constexpr int maxVoices = 64;
  347. Voice voices[maxVoices];
  348. int voiceCount = 0;
  349. double rate = 44100.0;
  350. bool offline = false;
  351. std::FILE* log = nullptr;
  352. std::atomic<uint32> refs { 1 };
  353. static inline std::atomic<int> made { 0 };
  354. const int id = ++made; // (each instance's lines carry its number)
  355. };
  356. class Factory final : public IPluginFactory2
  357. {
  358. public:
  359. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  360. {
  361. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginFactory::iid)
  362. || FUnknownPrivate::iidEqual (iid, IPluginFactory2::iid))
  363. {
  364. *obj = static_cast<IPluginFactory2*> (this);
  365. return kResultOk;
  366. }
  367. *obj = nullptr;
  368. return kNoInterface;
  369. }
  370. uint32 PLUGIN_API addRef() override { return 1; }
  371. uint32 PLUGIN_API release() override { return 1; }
  372. tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override
  373. {
  374. std::memset (info, 0, sizeof (*info));
  375. std::strcpy (info->vendor, "LYNX");
  376. std::strcpy (info->url, "https://github.com/Ka1-S/LYNX-SHARE");
  377. info->flags = PFactoryInfo::kUnicode;
  378. return kResultOk;
  379. }
  380. int32 PLUGIN_API countClasses() override { return 1; }
  381. tresult PLUGIN_API getClassInfo (int32 index, PClassInfo* info) override
  382. {
  383. if (index != 0)
  384. return kInvalidArgument;
  385. std::memset (info, 0, sizeof (*info));
  386. synthUid.toTUID (info->cid);
  387. info->cardinality = PClassInfo::kManyInstances;
  388. std::strcpy (info->category, kVstAudioEffectClass);
  389. std::strcpy (info->name, "LYNX Test Heavy");
  390. return kResultOk;
  391. }
  392. tresult PLUGIN_API getClassInfo2 (int32 index, PClassInfo2* info) override
  393. {
  394. if (index != 0)
  395. return kInvalidArgument;
  396. std::memset (info, 0, sizeof (*info));
  397. synthUid.toTUID (info->cid);
  398. info->cardinality = PClassInfo::kManyInstances;
  399. std::strcpy (info->category, kVstAudioEffectClass);
  400. std::strcpy (info->name, "LYNX Test Heavy");
  401. std::strcpy (info->subCategories, "Instrument|Synth");
  402. std::strcpy (info->vendor, "LYNX");
  403. std::strcpy (info->version, "1.0.0");
  404. std::strcpy (info->sdkVersion, "VST 3.7");
  405. return kResultOk;
  406. }
  407. tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override
  408. {
  409. if (! FUnknownPrivate::iidEqual (cid, synthUid.toTUID()))
  410. {
  411. *obj = nullptr;
  412. return kNoInterface;
  413. }
  414. auto* synth = new TestHeavy();
  415. const auto result = synth->queryInterface (iid, obj);
  416. synth->release();
  417. return result;
  418. }
  419. };
  420. Factory factory;
  421. } // namespace
  422. extern "C"
  423. {
  424. __declspec (dllexport) IPluginFactory* PLUGIN_API GetPluginFactory() { return static_cast<IPluginFactory2*> (&factory); }
  425. __declspec (dllexport) bool InitDll() { return true; }
  426. __declspec (dllexport) bool ExitDll() { return true; }
  427. }