lynx_test_synth_vst3.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // A minimal VST3 instrument for checking what LYNX sends a hosted synth (round 169c, issue #32). It is built only
  2. // against the VST3 SDK's pluginterfaces (no SDK helper classes) and on purpose has NO IMidiMapping, like many
  3. // third-party synths (KV331 SynthMaster 2): a MIDI controller such as All Notes Off (CC 123) can never reach it.
  4. //
  5. // V=<JUCE>/modules/juce_audio_processors/format_types/VST3_SDK
  6. // zig c++ -shared -O2 -std=c++17 -target x86_64-windows-gnu -I$V tools/testplugin/lynx_test_synth_vst3.cpp \
  7. // $V/pluginterfaces/base/funknown.cpp -lole32 -o "LYNX Test Synth.vst3"
  8. //
  9. // Sound: one sine voice per note on (a second note on of a sounding key adds a second voice, as a polyphonic synth
  10. // does), a note off ends the oldest voice of its key. With the environment variable LYNX_TEST_SYNTH_LOG = a file path
  11. // it appends to <path>.<instance> one line per process call ("P<instance> <ppq> <samples> <playing> <rt|off> <voices>") and per
  12. // event ("E<instance> on|off <key> <sampleOffset>"): what the host sent and when.
  13. #define INIT_CLASS_IID
  14. #include "pluginterfaces/base/funknown.h"
  15. #include "pluginterfaces/base/ipluginbase.h"
  16. #include "pluginterfaces/base/ibstream.h"
  17. #include "pluginterfaces/vst/ivstaudioprocessor.h"
  18. #include "pluginterfaces/vst/ivstcomponent.h"
  19. #include "pluginterfaces/vst/ivsteditcontroller.h"
  20. #include "pluginterfaces/vst/ivstevents.h"
  21. #include "pluginterfaces/vst/ivstprocesscontext.h"
  22. #include "pluginterfaces/gui/iplugview.h"
  23. #include <atomic>
  24. #include <cmath>
  25. #include <cstdio>
  26. #include <cstdlib>
  27. #include <cstring>
  28. using namespace Steinberg;
  29. using namespace Steinberg::Vst;
  30. namespace
  31. {
  32. const FUID synthUid (0x4C594E58, 0x54535931, 0x36396331, 0x56535433); // "LYNXTSY169c1VST3"
  33. void copyString (String128 to, const char* from)
  34. {
  35. int i = 0;
  36. for (; from[i] != 0 && i < 127; ++i)
  37. to[i] = (char16) from[i];
  38. to[i] = 0;
  39. }
  40. class TestSynth final : public IComponent, public IAudioProcessor, public IEditController
  41. {
  42. public:
  43. TestSynth() = default;
  44. virtual ~TestSynth()
  45. {
  46. if (log != nullptr)
  47. std::fclose (log);
  48. }
  49. // FUnknown
  50. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  51. {
  52. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginBase::iid)
  53. || FUnknownPrivate::iidEqual (iid, IComponent::iid))
  54. {
  55. addRef();
  56. *obj = static_cast<IComponent*> (this);
  57. return kResultOk;
  58. }
  59. if (FUnknownPrivate::iidEqual (iid, IAudioProcessor::iid))
  60. {
  61. addRef();
  62. *obj = static_cast<IAudioProcessor*> (this);
  63. return kResultOk;
  64. }
  65. if (FUnknownPrivate::iidEqual (iid, IEditController::iid))
  66. {
  67. addRef();
  68. *obj = static_cast<IEditController*> (this);
  69. return kResultOk;
  70. }
  71. *obj = nullptr;
  72. return kNoInterface;
  73. }
  74. uint32 PLUGIN_API addRef() override { return ++refs; }
  75. uint32 PLUGIN_API release() override
  76. {
  77. const auto r = --refs;
  78. if (r == 0)
  79. delete this;
  80. return r;
  81. }
  82. // IPluginBase (shared by the component and the controller)
  83. tresult PLUGIN_API initialize (FUnknown*) override
  84. {
  85. if (log == nullptr)
  86. if (const char* path = std::getenv ("LYNX_TEST_SYNTH_LOG"); path != nullptr && *path != 0)
  87. {
  88. char name[1024];
  89. std::snprintf (name, sizeof (name), "%s.%d", path, id); // one file per instance (threads)
  90. log = std::fopen (name, "a");
  91. }
  92. return kResultOk;
  93. }
  94. tresult PLUGIN_API terminate() override { return kResultOk; }
  95. // IComponent
  96. tresult PLUGIN_API getControllerClassId (TUID) override { return kNotImplemented; } // single component
  97. tresult PLUGIN_API setIoMode (IoMode) override { return kResultOk; }
  98. int32 PLUGIN_API getBusCount (MediaType type, BusDirection dir) override
  99. {
  100. if (type == kAudio && dir == kOutput)
  101. return 1;
  102. if (type == kEvent && dir == kInput)
  103. return 1;
  104. return 0;
  105. }
  106. tresult PLUGIN_API getBusInfo (MediaType type, BusDirection dir, int32 index, BusInfo& bus) override
  107. {
  108. if (index != 0)
  109. return kInvalidArgument;
  110. std::memset (&bus, 0, sizeof (bus));
  111. bus.mediaType = type;
  112. bus.direction = dir;
  113. bus.busType = kMain;
  114. bus.flags = BusInfo::kDefaultActive;
  115. if (type == kAudio && dir == kOutput)
  116. {
  117. bus.channelCount = 2;
  118. copyString (bus.name, "Out");
  119. return kResultOk;
  120. }
  121. if (type == kEvent && dir == kInput)
  122. {
  123. bus.channelCount = 16;
  124. copyString (bus.name, "MIDI In");
  125. return kResultOk;
  126. }
  127. return kInvalidArgument;
  128. }
  129. tresult PLUGIN_API getRoutingInfo (RoutingInfo&, RoutingInfo&) override { return kNotImplemented; }
  130. tresult PLUGIN_API activateBus (MediaType, BusDirection, int32, TBool) override { return kResultOk; }
  131. tresult PLUGIN_API setActive (TBool state) override
  132. {
  133. if (! state)
  134. voiceCount = 0;
  135. return kResultOk;
  136. }
  137. tresult PLUGIN_API setState (IBStream*) override { return kResultOk; }
  138. tresult PLUGIN_API getState (IBStream* s) override
  139. {
  140. if (s != nullptr)
  141. {
  142. int32 written = 0;
  143. const char tag[4] = { 'L', 'T', 'S', '1' };
  144. s->write ((void*) tag, 4, &written);
  145. }
  146. return kResultOk;
  147. }
  148. // IAudioProcessor
  149. tresult PLUGIN_API setBusArrangements (SpeakerArrangement*, int32 numIns, SpeakerArrangement* outs, int32 numOuts) override
  150. {
  151. return numIns == 0 && numOuts == 1 && outs[0] == SpeakerArr::kStereo ? kResultTrue : kResultFalse;
  152. }
  153. tresult PLUGIN_API getBusArrangement (BusDirection dir, int32 index, SpeakerArrangement& arr) override
  154. {
  155. if (dir == kOutput && index == 0)
  156. {
  157. arr = SpeakerArr::kStereo;
  158. return kResultOk;
  159. }
  160. return kInvalidArgument;
  161. }
  162. tresult PLUGIN_API canProcessSampleSize (int32 size) override { return size == kSample32 ? kResultTrue : kResultFalse; }
  163. uint32 PLUGIN_API getLatencySamples() override { return 0; }
  164. tresult PLUGIN_API setupProcessing (ProcessSetup& setup) override
  165. {
  166. rate = setup.sampleRate > 0.0 ? setup.sampleRate : 44100.0;
  167. offline = setup.processMode == kOffline;
  168. return kResultOk;
  169. }
  170. tresult PLUGIN_API setProcessing (TBool) override { return kResultOk; }
  171. uint32 PLUGIN_API getTailSamples() override { return 0; }
  172. tresult PLUGIN_API process (ProcessData& data) override
  173. {
  174. const auto n = data.numSamples;
  175. if (log != nullptr)
  176. {
  177. const auto* c = data.processContext;
  178. const auto playing = c != nullptr && (c->state & ProcessContext::kPlaying) != 0;
  179. const auto ppq = c != nullptr && (c->state & ProcessContext::kProjectTimeMusicValid) != 0 ? c->projectTimeMusic : -1.0;
  180. std::fprintf (log, "P%d %.9f %d %d %s %d\n", id, ppq, (int) n, playing ? 1 : 0, offline ? "off" : "rt", voiceCount);
  181. }
  182. float* left = nullptr;
  183. float* right = nullptr;
  184. if (data.numOutputs > 0 && data.outputs[0].numChannels >= 2 && n > 0)
  185. {
  186. left = data.outputs[0].channelBuffers32[0];
  187. right = data.outputs[0].channelBuffers32[1];
  188. std::memset (left, 0, sizeof (float) * (size_t) n);
  189. std::memset (right, 0, sizeof (float) * (size_t) n);
  190. }
  191. auto* events = data.inputEvents;
  192. const auto eventCount = events != nullptr ? events->getEventCount() : 0;
  193. int32 next = 0;
  194. Event e {};
  195. auto take = [&] (int32 i) { return events != nullptr && events->getEvent (i, e) == kResultOk; };
  196. bool have = next < eventCount && take (next);
  197. for (int32 s = 0; s < n || (have && n == 0); ++s)
  198. {
  199. while (have && e.sampleOffset <= s)
  200. {
  201. apply (e);
  202. ++next;
  203. have = next < eventCount && take (next);
  204. }
  205. if (n == 0)
  206. break;
  207. float v = 0.0f;
  208. for (int i = 0; i < voiceCount; ++i)
  209. {
  210. auto& voice = voices[i];
  211. v += 0.1f * (float) std::sin (voice.phase);
  212. voice.phase += voice.step;
  213. if (voice.phase > 6.283185307179586)
  214. voice.phase -= 6.283185307179586;
  215. }
  216. if (left != nullptr)
  217. {
  218. left[s] = v;
  219. right[s] = v;
  220. }
  221. }
  222. while (have) // (events past the block's end)
  223. {
  224. apply (e);
  225. ++next;
  226. have = next < eventCount && take (next);
  227. }
  228. if (log != nullptr && eventCount > 0)
  229. std::fflush (log);
  230. return kResultOk;
  231. }
  232. // IEditController (no parameters, no editor)
  233. tresult PLUGIN_API setComponentState (IBStream*) override { return kResultOk; }
  234. int32 PLUGIN_API getParameterCount() override { return 0; }
  235. tresult PLUGIN_API getParameterInfo (int32, ParameterInfo&) override { return kInvalidArgument; }
  236. tresult PLUGIN_API getParamStringByValue (ParamID, ParamValue, String128) override { return kInvalidArgument; }
  237. tresult PLUGIN_API getParamValueByString (ParamID, TChar*, ParamValue&) override { return kInvalidArgument; }
  238. ParamValue PLUGIN_API normalizedParamToPlain (ParamID, ParamValue v) override { return v; }
  239. ParamValue PLUGIN_API plainParamToNormalized (ParamID, ParamValue v) override { return v; }
  240. ParamValue PLUGIN_API getParamNormalized (ParamID) override { return 0.0; }
  241. tresult PLUGIN_API setParamNormalized (ParamID, ParamValue) override { return kResultOk; }
  242. tresult PLUGIN_API setComponentHandler (IComponentHandler*) override { return kResultOk; }
  243. IPlugView* PLUGIN_API createView (FIDString) override { return nullptr; }
  244. private:
  245. struct Voice
  246. {
  247. int key = 0;
  248. double phase = 0.0, step = 0.0;
  249. };
  250. void apply (const Event& e)
  251. {
  252. if (e.type == Event::kNoteOnEvent)
  253. {
  254. if (e.noteOn.velocity <= 0.0f)
  255. {
  256. noteOff (e.noteOn.pitch, e.sampleOffset);
  257. return;
  258. }
  259. if (log != nullptr)
  260. std::fprintf (log, "E%d on %d %d\n", id, (int) e.noteOn.pitch, (int) e.sampleOffset);
  261. if (voiceCount < maxVoices)
  262. {
  263. auto& v = voices[voiceCount++];
  264. v.key = e.noteOn.pitch;
  265. v.phase = 0.0;
  266. v.step = 6.283185307179586 * 440.0 * std::pow (2.0, (e.noteOn.pitch - 69) / 12.0) / rate;
  267. }
  268. }
  269. else if (e.type == Event::kNoteOffEvent)
  270. noteOff (e.noteOff.pitch, e.sampleOffset);
  271. }
  272. void noteOff (int key, int offset)
  273. {
  274. if (log != nullptr)
  275. std::fprintf (log, "E%d off %d %d\n", id, key, offset);
  276. for (int i = 0; i < voiceCount; ++i)
  277. if (voices[i].key == key)
  278. {
  279. for (int j = i + 1; j < voiceCount; ++j)
  280. voices[j - 1] = voices[j];
  281. --voiceCount;
  282. return;
  283. }
  284. }
  285. static constexpr int maxVoices = 64;
  286. Voice voices[maxVoices];
  287. int voiceCount = 0;
  288. double rate = 44100.0;
  289. bool offline = false;
  290. std::FILE* log = nullptr;
  291. std::atomic<uint32> refs { 1 };
  292. static inline std::atomic<int> made { 0 };
  293. const int id = ++made; // (each instance's lines carry its number)
  294. };
  295. class Factory final : public IPluginFactory2
  296. {
  297. public:
  298. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  299. {
  300. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginFactory::iid)
  301. || FUnknownPrivate::iidEqual (iid, IPluginFactory2::iid))
  302. {
  303. *obj = static_cast<IPluginFactory2*> (this);
  304. return kResultOk;
  305. }
  306. *obj = nullptr;
  307. return kNoInterface;
  308. }
  309. uint32 PLUGIN_API addRef() override { return 1; }
  310. uint32 PLUGIN_API release() override { return 1; }
  311. tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override
  312. {
  313. std::memset (info, 0, sizeof (*info));
  314. std::strcpy (info->vendor, "LYNX");
  315. std::strcpy (info->url, "https://github.com/Ka1-S/LYNX-SHARE");
  316. info->flags = PFactoryInfo::kUnicode;
  317. return kResultOk;
  318. }
  319. int32 PLUGIN_API countClasses() override { return 1; }
  320. tresult PLUGIN_API getClassInfo (int32 index, PClassInfo* info) override
  321. {
  322. if (index != 0)
  323. return kInvalidArgument;
  324. std::memset (info, 0, sizeof (*info));
  325. synthUid.toTUID (info->cid);
  326. info->cardinality = PClassInfo::kManyInstances;
  327. std::strcpy (info->category, kVstAudioEffectClass);
  328. std::strcpy (info->name, "LYNX Test Synth");
  329. return kResultOk;
  330. }
  331. tresult PLUGIN_API getClassInfo2 (int32 index, PClassInfo2* info) override
  332. {
  333. if (index != 0)
  334. return kInvalidArgument;
  335. std::memset (info, 0, sizeof (*info));
  336. synthUid.toTUID (info->cid);
  337. info->cardinality = PClassInfo::kManyInstances;
  338. std::strcpy (info->category, kVstAudioEffectClass);
  339. std::strcpy (info->name, "LYNX Test Synth");
  340. std::strcpy (info->subCategories, "Instrument|Synth");
  341. std::strcpy (info->vendor, "LYNX");
  342. std::strcpy (info->version, "1.0.0");
  343. std::strcpy (info->sdkVersion, "VST 3.7");
  344. return kResultOk;
  345. }
  346. tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override
  347. {
  348. if (! FUnknownPrivate::iidEqual (cid, synthUid.toTUID()))
  349. {
  350. *obj = nullptr;
  351. return kNoInterface;
  352. }
  353. auto* synth = new TestSynth();
  354. const auto result = synth->queryInterface (iid, obj);
  355. synth->release();
  356. return result;
  357. }
  358. };
  359. Factory factory;
  360. } // namespace
  361. extern "C"
  362. {
  363. __declspec (dllexport) IPluginFactory* PLUGIN_API GetPluginFactory() { return static_cast<IPluginFactory2*> (&factory); }
  364. __declspec (dllexport) bool InitDll() { return true; }
  365. __declspec (dllexport) bool ExitDll() { return true; }
  366. }