lynx_test_pads_vst3.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. // A drum-pad VST3 instrument with clickable pads and a MIDI output (round 190ks, GitHub #198): a hit on a pad in the
  2. // plugin's own window plays the pad AND sends the pad's note out on the plugin's event output bus, as drum samplers with
  3. // "MIDI out" do (the notes a host may record while recording). Built only against the VST3 SDK's pluginterfaces, like
  4. // lynx_test_synth_vst3.cpp:
  5. //
  6. // V=<JUCE>/modules/juce_audio_processors/format_types/VST3_SDK
  7. // Windows: zig c++ -shared -O2 -std=c++17 -target x86_64-windows-gnu -I$V tools/testplugin/lynx_test_pads_vst3.cpp (cont.)
  8. // $V/pluginterfaces/base/funknown.cpp -lole32 -luser32 -lgdi32 -o "LYNX Test Pads.vst3"
  9. // Linux: B="LYNX Test Pads.vst3/Contents/x86_64-linux"; mkdir -p "$B"
  10. // c++ -shared -fPIC -O2 -std=c++17 -I$V tools/testplugin/lynx_test_pads_vst3.cpp (cont.)
  11. // $V/pluginterfaces/base/funknown.cpp -o "$B/LYNX Test Pads.so"
  12. //
  13. // Pads: 8 (two rows of four), keys 36..43 (Kick, Snare, Clap, Hat, Tom 1, Tom 2, Ride, Crash). A pad sounds a decaying
  14. // sine at 110 × (k + 1) Hz, from a played note or a click. The window (Windows; 400 × 220) takes the velocity from where
  15. // the pad is hit: top = soft (1), bottom = hard (127), as FL's FPC. Mouse down = note on, mouse up = note off.
  16. // Environment (for headless tests; every variable optional):
  17. // LYNX_TEST_PADS_AUTO = "call:key:velocity:length,..." hits as if clicked: at the Nth process() call that has audio
  18. // (counted per instance from 1), the key's note on goes out (velocity 1..127), and its note off
  19. // `length` calls later. e.g. "40:36:100:4,80:38:64:4".
  20. // LYNX_TEST_PADS_ECHO = 1 every note the host sends in also goes out (a "MIDI thru" plugin): what a host must not
  21. // record twice.
  22. // LYNX_TEST_PADS_LOG = path appends "in on|off key offset" / "out on|off key velocity" lines.
  23. #define INIT_CLASS_IID
  24. #include "pluginterfaces/base/funknown.h"
  25. #include "pluginterfaces/base/ipluginbase.h"
  26. #include "pluginterfaces/base/ibstream.h"
  27. #include "pluginterfaces/vst/ivstaudioprocessor.h"
  28. #include "pluginterfaces/vst/ivstcomponent.h"
  29. #include "pluginterfaces/vst/ivsteditcontroller.h"
  30. #include "pluginterfaces/vst/ivstevents.h"
  31. #include "pluginterfaces/gui/iplugview.h"
  32. #include <algorithm>
  33. #include <atomic>
  34. #include <cmath>
  35. #include <cstdint>
  36. #include <cstdio>
  37. #include <cstdlib>
  38. #include <cstring>
  39. #if defined(_WIN32)
  40. #include <windows.h>
  41. #endif
  42. using namespace Steinberg;
  43. using namespace Steinberg::Vst;
  44. namespace
  45. {
  46. const FUID padsUid (0x4C594E58, 0x50414453, 0x31393063, 0x6B735633); // "LYNXPADS190cksV3"
  47. constexpr int padCount = 8, firstKey = 36;
  48. const char* const padNames[padCount] = { "Kick", "Snare", "Clap", "Hat", "Tom 1", "Tom 2", "Ride", "Crash" };
  49. void copyString (String128 to, const char* from)
  50. {
  51. int i = 0;
  52. for (; from[i] != 0 && i < 127; ++i)
  53. to[i] = (char16) from[i];
  54. to[i] = 0;
  55. }
  56. // The window -> audio thread: a single-producer single-consumer ring of pad hits (key, velocity 0 = off)
  57. struct HitRing
  58. {
  59. static constexpr int size = 256;
  60. std::atomic<int> head { 0 }, tail { 0 };
  61. int keys[size] {};
  62. int velocities[size] {};
  63. bool push (int key, int velocity)
  64. {
  65. const auto h = head.load (std::memory_order_relaxed);
  66. const auto next = (h + 1) % size;
  67. if (next == tail.load (std::memory_order_acquire))
  68. return false;
  69. keys[h] = key;
  70. velocities[h] = velocity;
  71. head.store (next, std::memory_order_release);
  72. return true;
  73. }
  74. bool pop (int& key, int& velocity)
  75. {
  76. const auto t = tail.load (std::memory_order_relaxed);
  77. if (t == head.load (std::memory_order_acquire))
  78. return false;
  79. key = keys[t];
  80. velocity = velocities[t];
  81. tail.store ((t + 1) % size, std::memory_order_release);
  82. return true;
  83. }
  84. };
  85. class TestPads;
  86. class PadsView final : public IPlugView
  87. {
  88. public:
  89. explicit PadsView (TestPads& p) : pads (p) {}
  90. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  91. {
  92. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPlugView::iid))
  93. {
  94. addRef();
  95. *obj = static_cast<IPlugView*> (this);
  96. return kResultOk;
  97. }
  98. *obj = nullptr;
  99. return kNoInterface;
  100. }
  101. uint32 PLUGIN_API addRef() override { return ++refs; }
  102. uint32 PLUGIN_API release() override
  103. {
  104. const auto r = --refs;
  105. if (r == 0)
  106. delete this;
  107. return r;
  108. }
  109. tresult PLUGIN_API isPlatformTypeSupported (FIDString type) override
  110. {
  111. #if defined(_WIN32)
  112. return std::strcmp (type, kPlatformTypeHWND) == 0 ? kResultTrue : kResultFalse;
  113. #else
  114. (void) type;
  115. return kResultFalse;
  116. #endif
  117. }
  118. tresult PLUGIN_API attached (void* parent, FIDString type) override;
  119. tresult PLUGIN_API removed() override;
  120. tresult PLUGIN_API onWheel (float) override { return kResultFalse; }
  121. tresult PLUGIN_API onKeyDown (char16, int16, int16) override { return kResultFalse; }
  122. tresult PLUGIN_API onKeyUp (char16, int16, int16) override { return kResultFalse; }
  123. tresult PLUGIN_API getSize (ViewRect* r) override
  124. {
  125. if (r == nullptr)
  126. return kInvalidArgument;
  127. *r = ViewRect (0, 0, width, height);
  128. return kResultOk;
  129. }
  130. tresult PLUGIN_API onSize (ViewRect*) override { return kResultOk; }
  131. tresult PLUGIN_API onFocus (TBool) override { return kResultOk; }
  132. tresult PLUGIN_API setFrame (IPlugFrame*) override { return kResultOk; }
  133. tresult PLUGIN_API canResize() override { return kResultFalse; }
  134. tresult PLUGIN_API checkSizeConstraint (ViewRect*) override { return kResultOk; }
  135. static constexpr int width = 400, height = 220;
  136. TestPads& pads;
  137. int held = -1; // the pad the mouse holds down
  138. #if defined(_WIN32)
  139. HWND window = nullptr;
  140. #endif
  141. private:
  142. std::atomic<uint32> refs { 1 };
  143. };
  144. class TestPads final : public IComponent, public IAudioProcessor, public IEditController
  145. {
  146. public:
  147. TestPads() = default;
  148. virtual ~TestPads()
  149. {
  150. if (log != nullptr)
  151. std::fclose (log);
  152. }
  153. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  154. {
  155. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginBase::iid)
  156. || FUnknownPrivate::iidEqual (iid, IComponent::iid))
  157. {
  158. addRef();
  159. *obj = static_cast<IComponent*> (this);
  160. return kResultOk;
  161. }
  162. if (FUnknownPrivate::iidEqual (iid, IAudioProcessor::iid))
  163. {
  164. addRef();
  165. *obj = static_cast<IAudioProcessor*> (this);
  166. return kResultOk;
  167. }
  168. if (FUnknownPrivate::iidEqual (iid, IEditController::iid))
  169. {
  170. addRef();
  171. *obj = static_cast<IEditController*> (this);
  172. return kResultOk;
  173. }
  174. *obj = nullptr;
  175. return kNoInterface;
  176. }
  177. uint32 PLUGIN_API addRef() override { return ++refs; }
  178. uint32 PLUGIN_API release() override
  179. {
  180. const auto r = --refs;
  181. if (r == 0)
  182. delete this;
  183. return r;
  184. }
  185. tresult PLUGIN_API initialize (FUnknown*) override
  186. {
  187. if (const char* e = std::getenv ("LYNX_TEST_PADS_ECHO"); e != nullptr && std::atoi (e) != 0)
  188. echo = true;
  189. if (const char* a = std::getenv ("LYNX_TEST_PADS_AUTO"); a != nullptr)
  190. {
  191. autoCount = 0;
  192. for (const char* p = a; *p != 0 && autoCount < maxAuto;)
  193. {
  194. int call = 0, key = 0, velocity = 0, length = 0, used = 0;
  195. if (std::sscanf (p, "%d:%d:%d:%d%n", &call, &key, &velocity, &length, &used) != 4)
  196. break;
  197. autoHits[autoCount++] = { call, key, velocity, std::max (1, length) };
  198. p += used;
  199. while (*p == ',' || *p == ' ')
  200. ++p;
  201. }
  202. }
  203. if (log == nullptr)
  204. if (const char* path = std::getenv ("LYNX_TEST_PADS_LOG"); path != nullptr && *path != 0)
  205. log = std::fopen (path, "a");
  206. return kResultOk;
  207. }
  208. tresult PLUGIN_API terminate() override { return kResultOk; }
  209. tresult PLUGIN_API getControllerClassId (TUID) override { return kNotImplemented; }
  210. tresult PLUGIN_API setIoMode (IoMode) override { return kResultOk; }
  211. int32 PLUGIN_API getBusCount (MediaType type, BusDirection dir) override { return type == kEvent || (type == kAudio && dir == kOutput) ? 1 : 0; }
  212. tresult PLUGIN_API getBusInfo (MediaType type, BusDirection dir, int32 index, BusInfo& bus) override
  213. {
  214. if (index != 0)
  215. return kInvalidArgument;
  216. std::memset (&bus, 0, sizeof (bus));
  217. bus.mediaType = type;
  218. bus.direction = dir;
  219. bus.busType = kMain;
  220. bus.flags = BusInfo::kDefaultActive;
  221. if (type == kAudio && dir == kOutput)
  222. {
  223. bus.channelCount = 2;
  224. copyString (bus.name, "Out");
  225. return kResultOk;
  226. }
  227. if (type == kEvent)
  228. {
  229. bus.channelCount = 16;
  230. copyString (bus.name, dir == kInput ? "MIDI In" : "MIDI Out");
  231. return kResultOk;
  232. }
  233. return kInvalidArgument; // (no audio input)
  234. }
  235. tresult PLUGIN_API getRoutingInfo (RoutingInfo&, RoutingInfo&) override { return kNotImplemented; }
  236. tresult PLUGIN_API activateBus (MediaType, BusDirection, int32, TBool) override { return kResultOk; }
  237. tresult PLUGIN_API setActive (TBool state) override
  238. {
  239. if (! state)
  240. for (auto& v : voices)
  241. v.level = 0.0;
  242. return kResultOk;
  243. }
  244. tresult PLUGIN_API setState (IBStream*) override { return kResultOk; }
  245. tresult PLUGIN_API getState (IBStream* s) override
  246. {
  247. if (s != nullptr)
  248. {
  249. int32 written = 0;
  250. const char tag[4] = { 'L', 'T', 'P', '1' };
  251. s->write ((void*) tag, 4, &written);
  252. }
  253. return kResultOk;
  254. }
  255. tresult PLUGIN_API setBusArrangements (SpeakerArrangement*, int32 numIns, SpeakerArrangement* outs, int32 numOuts) override
  256. {
  257. return numIns == 0 && numOuts == 1 && outs[0] == SpeakerArr::kStereo ? kResultTrue : kResultFalse;
  258. }
  259. tresult PLUGIN_API getBusArrangement (BusDirection dir, int32 index, SpeakerArrangement& arr) override
  260. {
  261. if (dir == kOutput && index == 0)
  262. {
  263. arr = SpeakerArr::kStereo;
  264. return kResultOk;
  265. }
  266. return kInvalidArgument;
  267. }
  268. tresult PLUGIN_API canProcessSampleSize (int32 size) override { return size == kSample32 ? kResultTrue : kResultFalse; }
  269. uint32 PLUGIN_API getLatencySamples() override { return 0; }
  270. tresult PLUGIN_API setupProcessing (ProcessSetup& setup) override
  271. {
  272. rate = setup.sampleRate > 0.0 ? setup.sampleRate : 44100.0;
  273. return kResultOk;
  274. }
  275. tresult PLUGIN_API setProcessing (TBool) override { return kResultOk; }
  276. uint32 PLUGIN_API getTailSamples() override { return kInfiniteTail; }
  277. tresult PLUGIN_API process (ProcessData& data) override
  278. {
  279. const auto n = data.numSamples;
  280. float* left = nullptr;
  281. float* right = nullptr;
  282. if (data.numOutputs > 0 && data.outputs[0].numChannels >= 2 && n > 0)
  283. {
  284. left = data.outputs[0].channelBuffers32[0];
  285. right = data.outputs[0].channelBuffers32[1];
  286. std::memset (left, 0, sizeof (float) * (size_t) n);
  287. std::memset (right, 0, sizeof (float) * (size_t) n);
  288. }
  289. auto* out = data.outputEvents;
  290. // the host's notes: play them (and, as a thru plugin, send them on)
  291. if (auto* in = data.inputEvents)
  292. for (int32 i = 0, count = in->getEventCount(); i < count; ++i)
  293. {
  294. Event e {};
  295. if (in->getEvent (i, e) != kResultOk)
  296. continue;
  297. const auto on = e.type == Event::kNoteOnEvent && e.noteOn.velocity > 0.0f;
  298. const auto off = e.type == Event::kNoteOffEvent || (e.type == Event::kNoteOnEvent && e.noteOn.velocity <= 0.0f);
  299. if (! on && ! off)
  300. continue;
  301. const int key = on ? e.noteOn.pitch : e.type == Event::kNoteOffEvent ? e.noteOff.pitch : e.noteOn.pitch;
  302. if (log != nullptr)
  303. std::fprintf (log, "in %s %d %d\n", on ? "on" : "off", key, (int) e.sampleOffset);
  304. if (on)
  305. strike (key, e.noteOn.velocity);
  306. if (echo && out != nullptr)
  307. send (out, key, on ? e.noteOn.velocity : 0.0f, e.sampleOffset);
  308. }
  309. // the window's hits (and the scripted ones): played and sent out at the block's start
  310. int key = 0, velocity = 0;
  311. while (hits.pop (key, velocity))
  312. hit (out, key, velocity);
  313. if (n > 0)
  314. {
  315. ++calls;
  316. for (int i = 0; i < autoCount; ++i)
  317. {
  318. if (autoHits[i].call == calls)
  319. hit (out, autoHits[i].key, autoHits[i].velocity);
  320. if (autoHits[i].call + autoHits[i].length == calls)
  321. hit (out, autoHits[i].key, 0);
  322. }
  323. }
  324. for (int32 s = 0; s < n; ++s)
  325. {
  326. float v = 0.0f;
  327. for (auto& voice : voices)
  328. if (voice.level > 1.0e-4)
  329. {
  330. v += (float) (voice.level * std::sin (voice.phase));
  331. voice.phase += voice.step;
  332. if (voice.phase > 6.283185307179586)
  333. voice.phase -= 6.283185307179586;
  334. voice.level *= voice.decay;
  335. }
  336. if (left != nullptr)
  337. {
  338. left[s] = v;
  339. right[s] = v;
  340. }
  341. }
  342. if (log != nullptr)
  343. std::fflush (log);
  344. return kResultOk;
  345. }
  346. tresult PLUGIN_API setComponentState (IBStream*) override { return kResultOk; }
  347. int32 PLUGIN_API getParameterCount() override { return 0; }
  348. tresult PLUGIN_API getParameterInfo (int32, ParameterInfo&) override { return kInvalidArgument; }
  349. tresult PLUGIN_API getParamStringByValue (ParamID, ParamValue, String128) override { return kInvalidArgument; }
  350. tresult PLUGIN_API getParamValueByString (ParamID, TChar*, ParamValue&) override { return kInvalidArgument; }
  351. ParamValue PLUGIN_API normalizedParamToPlain (ParamID, ParamValue v) override { return v; }
  352. ParamValue PLUGIN_API plainParamToNormalized (ParamID, ParamValue v) override { return v; }
  353. ParamValue PLUGIN_API getParamNormalized (ParamID) override { return 0.0; }
  354. tresult PLUGIN_API setParamNormalized (ParamID, ParamValue) override { return kResultOk; }
  355. tresult PLUGIN_API setComponentHandler (IComponentHandler*) override { return kResultOk; }
  356. IPlugView* PLUGIN_API createView (FIDString name) override
  357. {
  358. #if defined(_WIN32)
  359. if (name != nullptr && std::strcmp (name, ViewType::kEditor) == 0)
  360. return new PadsView (*this);
  361. #else
  362. (void) name;
  363. #endif
  364. return nullptr;
  365. }
  366. // the window: a pad hit (velocity 1..127) or its release (0)
  367. void clicked (int key, int velocity) { hits.push (key, velocity); }
  368. private:
  369. struct Voice
  370. {
  371. double phase = 0.0, step = 0.0, level = 0.0, decay = 0.9999;
  372. };
  373. void strike (int key, float velocity)
  374. {
  375. const auto pad = ((key - firstKey) % padCount + padCount) % padCount;
  376. auto& v = voices[pad];
  377. v.phase = 0.0;
  378. v.step = 6.283185307179586 * 110.0 * (pad + 1) / rate;
  379. v.level = 0.25 * (double) velocity;
  380. v.decay = std::pow (0.001, 1.0 / (0.25 * rate)); // -60 dB in 250 ms
  381. }
  382. void send (IEventList* out, int key, float velocity, int32 offset)
  383. {
  384. if (out == nullptr)
  385. return;
  386. Event e {};
  387. e.busIndex = 0;
  388. e.sampleOffset = offset;
  389. if (velocity > 0.0f)
  390. {
  391. e.type = Event::kNoteOnEvent;
  392. e.noteOn.channel = 9; // (MIDI channel 10, drums)
  393. e.noteOn.pitch = (int16) key;
  394. e.noteOn.velocity = velocity;
  395. e.noteOn.noteId = -1;
  396. }
  397. else
  398. {
  399. e.type = Event::kNoteOffEvent;
  400. e.noteOff.channel = 9;
  401. e.noteOff.pitch = (int16) key;
  402. e.noteOff.velocity = 0.0f;
  403. e.noteOff.noteId = -1;
  404. }
  405. out->addEvent (e);
  406. if (log != nullptr)
  407. std::fprintf (log, "out %s %d %d\n", velocity > 0.0f ? "on" : "off", key, (int) std::lround (velocity * 127.0f));
  408. }
  409. void hit (IEventList* out, int key, int velocity)
  410. {
  411. const auto v = (float) std::max (0, std::min (127, velocity)) / 127.0f;
  412. if (v > 0.0f)
  413. strike (key, v);
  414. send (out, key, v, 0);
  415. }
  416. struct AutoHit
  417. {
  418. int call = 0, key = 36, velocity = 100, length = 4;
  419. };
  420. static constexpr int maxAuto = 64;
  421. AutoHit autoHits[maxAuto];
  422. int autoCount = 0;
  423. long long calls = 0;
  424. bool echo = false;
  425. HitRing hits;
  426. Voice voices[padCount];
  427. double rate = 44100.0;
  428. std::FILE* log = nullptr;
  429. std::atomic<uint32> refs { 1 };
  430. };
  431. #if defined(_WIN32)
  432. RECT padRect (int pad)
  433. {
  434. const int w = PadsView::width / 4, h = (PadsView::height - 20) / 2;
  435. const int col = pad % 4, row = 1 - pad / 4; // (pad 1 at the bottom left, as FPC)
  436. return { col * w + 4, 20 + row * h + 4, (col + 1) * w - 4, 20 + (row + 1) * h - 4 };
  437. }
  438. LRESULT CALLBACK padsProc (HWND h, UINT m, WPARAM w, LPARAM l)
  439. {
  440. auto* view = (PadsView*) GetWindowLongPtrA (h, GWLP_USERDATA);
  441. switch (m)
  442. {
  443. case WM_PAINT:
  444. {
  445. PAINTSTRUCT ps;
  446. auto dc = BeginPaint (h, &ps);
  447. RECT all;
  448. GetClientRect (h, &all);
  449. auto bg = CreateSolidBrush (RGB (32, 34, 38));
  450. FillRect (dc, &all, bg);
  451. DeleteObject (bg);
  452. SetBkMode (dc, TRANSPARENT);
  453. SetTextColor (dc, RGB (200, 200, 200));
  454. RECT title { 6, 2, PadsView::width, 18 };
  455. DrawTextA (dc, "LYNX Test Pads - click: top soft, bottom hard", -1, &title, DT_LEFT | DT_SINGLELINE);
  456. for (int pad = 0; pad < padCount; ++pad)
  457. {
  458. auto r = padRect (pad);
  459. auto brush = CreateSolidBrush (view != nullptr && view->held == pad ? RGB (229, 56, 59) : RGB (70, 74, 82));
  460. FillRect (dc, &r, brush);
  461. DeleteObject (brush);
  462. char label[32];
  463. std::snprintf (label, sizeof (label), "%s (%d)", padNames[pad], firstKey + pad);
  464. DrawTextA (dc, label, -1, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  465. }
  466. EndPaint (h, &ps);
  467. return 0;
  468. }
  469. case WM_LBUTTONDOWN:
  470. {
  471. const int x = (short) LOWORD (l), y = (short) HIWORD (l);
  472. for (int pad = 0; view != nullptr && pad < padCount; ++pad)
  473. {
  474. const auto r = padRect (pad);
  475. if (x >= r.left && x < r.right && y >= r.top && y < r.bottom)
  476. {
  477. const auto velocity = 1 + (int) std::lround (126.0 * (double) (y - r.top) / (double) std::max (1L, r.bottom - r.top - 1));
  478. view->held = pad;
  479. view->pads.clicked (firstKey + pad, std::min (127, velocity));
  480. SetCapture (h);
  481. InvalidateRect (h, nullptr, FALSE);
  482. break;
  483. }
  484. }
  485. return 0;
  486. }
  487. case WM_LBUTTONUP:
  488. case WM_CAPTURECHANGED:
  489. if (view != nullptr && view->held >= 0)
  490. {
  491. view->pads.clicked (firstKey + view->held, 0);
  492. view->held = -1;
  493. InvalidateRect (h, nullptr, FALSE);
  494. }
  495. if (m == WM_LBUTTONUP)
  496. ReleaseCapture();
  497. return 0;
  498. default:
  499. break;
  500. }
  501. return DefWindowProcA (h, m, w, l);
  502. }
  503. tresult PLUGIN_API PadsView::attached (void* parent, FIDString)
  504. {
  505. static bool registered = false;
  506. if (! registered)
  507. {
  508. WNDCLASSA c {};
  509. c.lpfnWndProc = padsProc;
  510. c.hInstance = GetModuleHandleA (nullptr);
  511. c.hCursor = LoadCursor (nullptr, IDC_ARROW);
  512. c.lpszClassName = "LYNXTestPadsView";
  513. RegisterClassA (&c);
  514. registered = true;
  515. }
  516. window = CreateWindowExA (0, "LYNXTestPadsView", "LYNX Test Pads", WS_CHILD | WS_VISIBLE, 0, 0, width, height, (HWND) parent,
  517. nullptr, GetModuleHandleA (nullptr), nullptr);
  518. if (window == nullptr)
  519. return kResultFalse;
  520. SetWindowLongPtrA (window, GWLP_USERDATA, (LONG_PTR) this);
  521. return kResultOk;
  522. }
  523. tresult PLUGIN_API PadsView::removed()
  524. {
  525. if (window != nullptr)
  526. {
  527. SetWindowLongPtrA (window, GWLP_USERDATA, 0);
  528. DestroyWindow (window);
  529. window = nullptr;
  530. }
  531. return kResultOk;
  532. }
  533. #else
  534. tresult PLUGIN_API PadsView::attached (void*, FIDString) { return kResultFalse; }
  535. tresult PLUGIN_API PadsView::removed() { return kResultOk; }
  536. #endif
  537. class Factory final : public IPluginFactory2
  538. {
  539. public:
  540. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  541. {
  542. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginFactory::iid)
  543. || FUnknownPrivate::iidEqual (iid, IPluginFactory2::iid))
  544. {
  545. *obj = static_cast<IPluginFactory2*> (this);
  546. return kResultOk;
  547. }
  548. *obj = nullptr;
  549. return kNoInterface;
  550. }
  551. uint32 PLUGIN_API addRef() override { return 1; }
  552. uint32 PLUGIN_API release() override { return 1; }
  553. tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override
  554. {
  555. std::memset (info, 0, sizeof (*info));
  556. std::strcpy (info->vendor, "LYNX");
  557. std::strcpy (info->url, "https://github.com/Ka1-S/LYNX-SHARE");
  558. info->flags = PFactoryInfo::kUnicode;
  559. return kResultOk;
  560. }
  561. int32 PLUGIN_API countClasses() override { return 1; }
  562. tresult PLUGIN_API getClassInfo (int32 index, PClassInfo* info) override
  563. {
  564. if (index != 0)
  565. return kInvalidArgument;
  566. std::memset (info, 0, sizeof (*info));
  567. padsUid.toTUID (info->cid);
  568. info->cardinality = PClassInfo::kManyInstances;
  569. std::strcpy (info->category, kVstAudioEffectClass);
  570. std::strcpy (info->name, "LYNX Test Pads");
  571. return kResultOk;
  572. }
  573. tresult PLUGIN_API getClassInfo2 (int32 index, PClassInfo2* info) override
  574. {
  575. if (index != 0)
  576. return kInvalidArgument;
  577. std::memset (info, 0, sizeof (*info));
  578. padsUid.toTUID (info->cid);
  579. info->cardinality = PClassInfo::kManyInstances;
  580. std::strcpy (info->category, kVstAudioEffectClass);
  581. std::strcpy (info->name, "LYNX Test Pads");
  582. std::strcpy (info->subCategories, "Instrument|Drum");
  583. std::strcpy (info->vendor, "LYNX");
  584. std::strcpy (info->version, "1.0.0");
  585. std::strcpy (info->sdkVersion, "VST 3.7");
  586. return kResultOk;
  587. }
  588. tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override
  589. {
  590. if (! FUnknownPrivate::iidEqual (cid, padsUid.toTUID()))
  591. {
  592. *obj = nullptr;
  593. return kNoInterface;
  594. }
  595. auto* pads = new TestPads();
  596. const auto result = pads->queryInterface (iid, obj);
  597. pads->release();
  598. return result;
  599. }
  600. };
  601. Factory factory;
  602. } // namespace
  603. extern "C"
  604. {
  605. #if defined(_WIN32)
  606. __declspec (dllexport) IPluginFactory* PLUGIN_API GetPluginFactory() { return static_cast<IPluginFactory2*> (&factory); }
  607. __declspec (dllexport) bool InitDll() { return true; }
  608. __declspec (dllexport) bool ExitDll() { return true; }
  609. #else
  610. __attribute__ ((visibility ("default"))) IPluginFactory* PLUGIN_API GetPluginFactory() { return static_cast<IPluginFactory2*> (&factory); }
  611. __attribute__ ((visibility ("default"))) bool ModuleEntry (void*) { return true; }
  612. __attribute__ ((visibility ("default"))) bool ModuleExit() { return true; }
  613. #endif
  614. }