lynx_test_param_vst3.cpp 14 KB

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