lynx_test_param_vst3.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // A VST3 effect with one parameter for timing tests (round 190jg, sample-accurate automation): it ignores its input and
  2. // outputs its "Level" parameter as a constant (DC) on both channels, applying each point of the parameter's
  3. // IParamValueQueue at the point's sample offset. A host that sends a step of the automation at its sample shows the
  4. // step at that sample in the output; one that sends a value per block shows it at the block's start. With the
  5. // environment variable LYNX_TEST_PARAM_LOG = a file path it appends one line per point received ("P <offset> <value>").
  6. // Built only against the VST3 SDK's pluginterfaces (no SDK helper classes), like lynx_test_crash_vst3.cpp:
  7. // V=<JUCE>/modules/juce_audio_processors/format_types/VST3_SDK
  8. // Linux: B="LYNX Test Param.vst3/Contents/x86_64-linux"; mkdir -p "$B"
  9. // c++ -shared -fPIC -O2 -std=c++17 -I$V tools/testplugin/lynx_test_param_vst3.cpp
  10. // $V/pluginterfaces/base/funknown.cpp -o "$B/LYNX Test Param.so" (one line)
  11. // Windows: zig c++ -shared -O2 -std=c++17 -target x86_64-windows-gnu -I$V tools/testplugin/lynx_test_param_vst3.cpp
  12. // $V/pluginterfaces/base/funknown.cpp -lole32 -o "LYNX Test Param.vst3" (one line)
  13. // then: LYNX --automationtimingtest "LYNX Test Param.vst3" report.txt
  14. #define INIT_CLASS_IID
  15. #include "pluginterfaces/base/funknown.h"
  16. #include "pluginterfaces/base/ipluginbase.h"
  17. #include "pluginterfaces/base/ibstream.h"
  18. #include "pluginterfaces/vst/ivstaudioprocessor.h"
  19. #include "pluginterfaces/vst/ivstcomponent.h"
  20. #include "pluginterfaces/vst/ivsteditcontroller.h"
  21. #include "pluginterfaces/vst/ivstparameterchanges.h"
  22. #include "pluginterfaces/gui/iplugview.h"
  23. #include <atomic>
  24. #include <cstdint>
  25. #include <cstdio>
  26. #include <cstdlib>
  27. #include <cstring>
  28. #if defined(_WIN32)
  29. #include <windows.h>
  30. #define LYNX_TEST_EXPORT __declspec (dllexport)
  31. #else
  32. #define LYNX_TEST_EXPORT __attribute__ ((visibility ("default")))
  33. #endif
  34. using namespace Steinberg;
  35. using namespace Steinberg::Vst;
  36. namespace
  37. {
  38. const FUID effectUid (0x4C594E58, 0x50415241, 0x4D313930, 0x4A475633); // "LYNXPARAM190JGV3"
  39. void copyString (String128 to, const char* from)
  40. {
  41. int i = 0;
  42. for (; from[i] != 0 && i < 127; ++i)
  43. to[i] = (char16) from[i];
  44. to[i] = 0;
  45. }
  46. FILE* logFile()
  47. {
  48. static FILE* f = [] () -> FILE* {
  49. #if defined(_WIN32)
  50. char path[1024] {};
  51. return GetEnvironmentVariableA ("LYNX_TEST_PARAM_LOG", path, sizeof (path)) > 0 ? std::fopen (path, "a") : nullptr;
  52. #else
  53. const char* path = std::getenv ("LYNX_TEST_PARAM_LOG");
  54. return path != nullptr ? std::fopen (path, "a") : nullptr;
  55. #endif
  56. }();
  57. return f;
  58. }
  59. class TestParam final : public IComponent, public IAudioProcessor, public IEditController
  60. {
  61. public:
  62. // FUnknown
  63. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  64. {
  65. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginBase::iid)
  66. || FUnknownPrivate::iidEqual (iid, IComponent::iid))
  67. {
  68. addRef();
  69. *obj = static_cast<IComponent*> (this);
  70. return kResultOk;
  71. }
  72. if (FUnknownPrivate::iidEqual (iid, IAudioProcessor::iid))
  73. {
  74. addRef();
  75. *obj = static_cast<IAudioProcessor*> (this);
  76. return kResultOk;
  77. }
  78. if (FUnknownPrivate::iidEqual (iid, IEditController::iid))
  79. {
  80. addRef();
  81. *obj = static_cast<IEditController*> (this);
  82. return kResultOk;
  83. }
  84. *obj = nullptr;
  85. return kNoInterface;
  86. }
  87. uint32 PLUGIN_API addRef() override { return ++refs; }
  88. uint32 PLUGIN_API release() override
  89. {
  90. const auto r = --refs;
  91. if (r == 0)
  92. delete this;
  93. return r;
  94. }
  95. tresult PLUGIN_API initialize (FUnknown*) override { return kResultOk; }
  96. tresult PLUGIN_API terminate() override { return kResultOk; }
  97. tresult PLUGIN_API getControllerClassId (TUID) override { return kNotImplemented; } // single component
  98. tresult PLUGIN_API setIoMode (IoMode) override { return kResultOk; }
  99. int32 PLUGIN_API getBusCount (MediaType type, BusDirection) override { return type == kAudio ? 1 : 0; }
  100. tresult PLUGIN_API getBusInfo (MediaType type, BusDirection dir, int32 index, BusInfo& bus) override
  101. {
  102. if (type != kAudio || index != 0)
  103. return kInvalidArgument;
  104. std::memset (&bus, 0, sizeof (bus));
  105. bus.mediaType = type;
  106. bus.direction = dir;
  107. bus.channelCount = 2;
  108. bus.busType = kMain;
  109. bus.flags = BusInfo::kDefaultActive;
  110. copyString (bus.name, dir == kOutput ? "Output" : "Input");
  111. return kResultOk;
  112. }
  113. tresult PLUGIN_API getRoutingInfo (RoutingInfo&, RoutingInfo&) override { return kNotImplemented; }
  114. tresult PLUGIN_API activateBus (MediaType, BusDirection, int32, TBool) override { return kResultOk; }
  115. tresult PLUGIN_API setActive (TBool) override { return kResultOk; }
  116. tresult PLUGIN_API setState (IBStream* stream) override
  117. {
  118. float v = 0.5f;
  119. int32 read = 0;
  120. if (stream != nullptr && stream->read (&v, 4, &read) == kResultOk && read == 4)
  121. level = v;
  122. return kResultOk;
  123. }
  124. tresult PLUGIN_API getState (IBStream* stream) override
  125. {
  126. int32 written = 0;
  127. float v = (float) level;
  128. return stream != nullptr ? stream->write (&v, 4, &written) : kInvalidArgument;
  129. }
  130. tresult PLUGIN_API setBusArrangements (SpeakerArrangement* ins, int32 numIns, SpeakerArrangement* outs, int32 numOuts) override
  131. {
  132. return numIns == 1 && numOuts == 1 && ins[0] == SpeakerArr::kStereo && outs[0] == SpeakerArr::kStereo ? kResultTrue : kResultFalse;
  133. }
  134. tresult PLUGIN_API getBusArrangement (BusDirection, int32 index, SpeakerArrangement& arr) override
  135. {
  136. if (index != 0)
  137. return kInvalidArgument;
  138. arr = SpeakerArr::kStereo;
  139. return kResultOk;
  140. }
  141. tresult PLUGIN_API canProcessSampleSize (int32 size) override { return size == kSample32 ? kResultTrue : kResultFalse; }
  142. uint32 PLUGIN_API getLatencySamples() override { return 0; }
  143. tresult PLUGIN_API setupProcessing (ProcessSetup&) override { return kResultOk; }
  144. tresult PLUGIN_API setProcessing (TBool) override { return kResultOk; }
  145. uint32 PLUGIN_API getTailSamples() override { return 0; }
  146. tresult PLUGIN_API process (ProcessData& data) override
  147. {
  148. // the points of parameter 0 in this block (at most 64), applied at their offsets
  149. int32 offsets[64];
  150. double values[64];
  151. int32 points = 0;
  152. if (auto* changes = data.inputParameterChanges)
  153. for (int32 i = 0; i < changes->getParameterCount(); ++i)
  154. if (auto* queue = changes->getParameterData (i); queue != nullptr && queue->getParameterId() == 0)
  155. for (int32 k = 0; k < queue->getPointCount() && points < 64; ++k)
  156. if (queue->getPoint (k, offsets[points], values[points]) == kResultOk)
  157. {
  158. if (FILE* f = logFile())
  159. std::fprintf (f, "P %d %.6f n=%d\n", (int) offsets[points], values[points], (int) data.numSamples);
  160. ++points;
  161. }
  162. if (FILE* f = logFile())
  163. std::fflush (f);
  164. const auto n = data.numSamples;
  165. if (n <= 0 || data.numOutputs < 1 || data.outputs == nullptr || data.outputs[0].numChannels < 2)
  166. {
  167. if (points > 0)
  168. level = values[points - 1]; // (a parameter flush)
  169. return kResultOk;
  170. }
  171. auto& out = data.outputs[0];
  172. int32 next = 0;
  173. for (int32 s = 0; s < n; ++s)
  174. {
  175. while (next < points && offsets[next] <= s)
  176. level = values[next++];
  177. for (int c = 0; c < 2; ++c)
  178. if (out.channelBuffers32[c] != nullptr)
  179. out.channelBuffers32[c][s] = (float) level;
  180. }
  181. while (next < points)
  182. level = values[next++];
  183. return kResultOk;
  184. }
  185. // IEditController: one parameter, "Level" 0..1
  186. tresult PLUGIN_API setComponentState (IBStream*) override { return kResultOk; }
  187. int32 PLUGIN_API getParameterCount() override { return 1; }
  188. tresult PLUGIN_API getParameterInfo (int32 index, ParameterInfo& info) override
  189. {
  190. if (index != 0)
  191. return kInvalidArgument;
  192. std::memset (&info, 0, sizeof (info));
  193. info.id = 0;
  194. copyString (info.title, "Level");
  195. copyString (info.shortTitle, "Level");
  196. info.stepCount = 0;
  197. info.defaultNormalizedValue = 0.5;
  198. info.flags = ParameterInfo::kCanAutomate;
  199. return kResultOk;
  200. }
  201. tresult PLUGIN_API getParamStringByValue (ParamID, ParamValue v, String128 s) override
  202. {
  203. char text[32];
  204. std::snprintf (text, sizeof (text), "%.3f", v);
  205. copyString (s, text);
  206. return kResultOk;
  207. }
  208. tresult PLUGIN_API getParamValueByString (ParamID, TChar*, ParamValue&) override { return kInvalidArgument; }
  209. ParamValue PLUGIN_API normalizedParamToPlain (ParamID, ParamValue v) override { return v; }
  210. ParamValue PLUGIN_API plainParamToNormalized (ParamID, ParamValue v) override { return v; }
  211. ParamValue PLUGIN_API getParamNormalized (ParamID id) override { return id == 0 ? controllerValue : 0.0; }
  212. tresult PLUGIN_API setParamNormalized (ParamID id, ParamValue v) override
  213. {
  214. if (id == 0)
  215. controllerValue = v;
  216. return kResultOk;
  217. }
  218. tresult PLUGIN_API setComponentHandler (IComponentHandler*) override { return kResultOk; }
  219. IPlugView* PLUGIN_API createView (FIDString) override { return nullptr; }
  220. private:
  221. std::atomic<uint32> refs { 1 };
  222. double level = 0.5, controllerValue = 0.5;
  223. };
  224. class Factory final : public IPluginFactory2
  225. {
  226. public:
  227. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  228. {
  229. if (FUnknownPrivate::iidEqual (iid, FUnknown::iid) || FUnknownPrivate::iidEqual (iid, IPluginFactory::iid)
  230. || FUnknownPrivate::iidEqual (iid, IPluginFactory2::iid))
  231. {
  232. *obj = static_cast<IPluginFactory2*> (this);
  233. return kResultOk;
  234. }
  235. *obj = nullptr;
  236. return kNoInterface;
  237. }
  238. uint32 PLUGIN_API addRef() override { return 1; }
  239. uint32 PLUGIN_API release() override { return 1; }
  240. tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override
  241. {
  242. std::memset (static_cast<void*> (info), 0, sizeof (*info));
  243. std::strcpy (info->vendor, "LYNX");
  244. std::strcpy (info->url, "https://github.com/Ka1-S/LYNX-SHARE");
  245. info->flags = PFactoryInfo::kUnicode;
  246. return kResultOk;
  247. }
  248. int32 PLUGIN_API countClasses() override { return 1; }
  249. tresult PLUGIN_API getClassInfo (int32 index, PClassInfo* info) override
  250. {
  251. if (index != 0)
  252. return kInvalidArgument;
  253. std::memset (static_cast<void*> (info), 0, sizeof (*info));
  254. effectUid.toTUID (info->cid);
  255. info->cardinality = PClassInfo::kManyInstances;
  256. std::strcpy (info->category, kVstAudioEffectClass);
  257. std::strcpy (info->name, "LYNX Test Param");
  258. return kResultOk;
  259. }
  260. tresult PLUGIN_API getClassInfo2 (int32 index, PClassInfo2* info) override
  261. {
  262. if (index != 0)
  263. return kInvalidArgument;
  264. std::memset (static_cast<void*> (info), 0, sizeof (*info));
  265. effectUid.toTUID (info->cid);
  266. info->cardinality = PClassInfo::kManyInstances;
  267. std::strcpy (info->category, kVstAudioEffectClass);
  268. std::strcpy (info->name, "LYNX Test Param");
  269. std::strcpy (info->subCategories, "Fx");
  270. std::strcpy (info->vendor, "LYNX");
  271. std::strcpy (info->version, "1.0.0");
  272. std::strcpy (info->sdkVersion, "VST 3.7");
  273. return kResultOk;
  274. }
  275. tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override
  276. {
  277. if (! FUnknownPrivate::iidEqual (cid, effectUid.toTUID()))
  278. {
  279. *obj = nullptr;
  280. return kNoInterface;
  281. }
  282. auto* effect = new TestParam();
  283. const auto result = effect->queryInterface (iid, obj);
  284. effect->release();
  285. return result;
  286. }
  287. };
  288. Factory factory;
  289. } // namespace
  290. extern "C"
  291. {
  292. LYNX_TEST_EXPORT IPluginFactory* PLUGIN_API GetPluginFactory() { return static_cast<IPluginFactory2*> (&factory); }
  293. #if defined(_WIN32)
  294. LYNX_TEST_EXPORT bool InitDll() { return true; }
  295. LYNX_TEST_EXPORT bool ExitDll() { return true; }
  296. #else
  297. LYNX_TEST_EXPORT bool ModuleEntry (void*) { return true; }
  298. LYNX_TEST_EXPORT bool ModuleExit() { return true; }
  299. #endif
  300. }