DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

462 lines
16KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DISTRHO_PLUGIN_HPP_INCLUDED
  17. #define DISTRHO_PLUGIN_HPP_INCLUDED
  18. #include "DistrhoDetails.hpp"
  19. #include "extra/LeakDetector.hpp"
  20. #include "src/DistrhoPluginChecks.h"
  21. START_NAMESPACE_DISTRHO
  22. /* ------------------------------------------------------------------------------------------------------------
  23. * DPF Plugin */
  24. /**
  25. @defgroup MainClasses Main Classes
  26. @{
  27. */
  28. /**
  29. DPF Plugin class from where plugin instances are created.
  30. The public methods (Host state) are called from the plugin to get or set host information.@n
  31. They can be called from a plugin instance at anytime unless stated otherwise.@n
  32. All other methods are to be implemented by the plugin and will be called by the host.
  33. Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
  34. Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
  35. The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
  36. There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
  37. The buffer size and sample rate values will remain constant between activate and deactivate.@n
  38. Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
  39. Some of this class functions are only available according to some macros.
  40. DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
  41. When enabled you need to implement initProgramName() and loadProgram().
  42. DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
  43. When enabled you need to implement initState() and setState().
  44. The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
  45. When enabled it provides midi input events.
  46. */
  47. class Plugin
  48. {
  49. public:
  50. /**
  51. Plugin class constructor.@n
  52. You must set all parameter values to their defaults, matching ParameterRanges::def.
  53. */
  54. Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
  55. /**
  56. Destructor.
  57. */
  58. virtual ~Plugin();
  59. /* --------------------------------------------------------------------------------------------------------
  60. * Host state */
  61. /**
  62. Get the current buffer size that will probably be used during processing, in frames.@n
  63. This value will remain constant between activate and deactivate.
  64. @note This value is only a hint!@n
  65. Hosts might call run() with a higher or lower number of frames.
  66. @see bufferSizeChanged(uint32_t)
  67. */
  68. uint32_t getBufferSize() const noexcept;
  69. /**
  70. Get the current sample rate that will be used during processing.@n
  71. This value will remain constant between activate and deactivate.
  72. @see sampleRateChanged(double)
  73. */
  74. double getSampleRate() const noexcept;
  75. /**
  76. Get the bundle path where the plugin resides.
  77. Can return null if the plugin is not available in a bundle (if it is a single binary).
  78. @see getBinaryFilename
  79. @see getResourcePath
  80. */
  81. const char* getBundlePath() const noexcept;
  82. /**
  83. Check if this plugin instance is a "dummy" one used for plugin meta-data/information export.@n
  84. When true no processing will be done, the plugin is created only to extract information.@n
  85. In DPF, LADSPA/DSSI, VST2 and VST3 formats create one global instance per plugin binary
  86. while LV2 creates one when generating turtle meta-data.
  87. */
  88. bool isDummyInstance() const noexcept;
  89. /**
  90. Check if this plugin instance is a "selftest" one used for automated plugin tests.@n
  91. To enable this mode build with `DPF_RUNTIME_TESTING` macro defined (i.e. set as compiler build flag),
  92. and run the JACK/Standalone executable with "selftest" as its only and single argument.
  93. A few basic DSP and UI tests will run in self-test mode, with once instance having this function returning true.@n
  94. You can use this chance to do a few tests of your own as well.
  95. */
  96. bool isSelfTestInstance() const noexcept;
  97. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  98. /**
  99. Get the current host transport time position.@n
  100. This function should only be called during run().@n
  101. You can call this during other times, but the returned position is not guaranteed to be in sync.
  102. @note TimePosition is not supported in LADSPA and DSSI plugin formats.
  103. */
  104. const TimePosition& getTimePosition() const noexcept;
  105. #endif
  106. #if DISTRHO_PLUGIN_WANT_LATENCY
  107. /**
  108. Change the plugin audio output latency to @a frames.@n
  109. This function should only be called in the constructor, activate() and run().
  110. @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
  111. */
  112. void setLatency(uint32_t frames) noexcept;
  113. #endif
  114. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  115. /**
  116. Write a MIDI output event.@n
  117. This function must only be called during run().@n
  118. Returns false when the host buffer is full, in which case do not call this again until the next run().
  119. */
  120. bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
  121. #endif
  122. #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  123. /**
  124. Check if parameter value change requests will work with the current plugin host.
  125. @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
  126. @see requestParameterValueChange(uint32_t, float)
  127. */
  128. bool canRequestParameterValueChanges() const noexcept;
  129. /**
  130. Request a parameter value change from the host.
  131. If successful, this function will automatically trigger a parameter update on the UI side as well.
  132. This function can fail, for example if the host is busy with the parameter for read-only automation.
  133. Some hosts simply do not have this functionality, which can be verified with canRequestParameterValueChanges().
  134. @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
  135. */
  136. bool requestParameterValueChange(uint32_t index, float value) noexcept;
  137. #endif
  138. #if DISTRHO_PLUGIN_WANT_STATE
  139. /**
  140. Set state value and notify the host about the change.@n
  141. This function will call `setState()` and also trigger an update on the UI side as necessary.@n
  142. It must not be called during run.@n
  143. The state must be host readable.
  144. @note this function does nothing on DSSI plugin format, as DSSI only supports UI->DSP messages.
  145. TODO API under construction
  146. */
  147. bool updateStateValue(const char* key, const char* value) noexcept;
  148. #endif
  149. protected:
  150. /* --------------------------------------------------------------------------------------------------------
  151. * Information */
  152. /**
  153. Get the plugin name.@n
  154. Returns DISTRHO_PLUGIN_NAME by default.
  155. */
  156. virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
  157. /**
  158. Get the plugin label.@n
  159. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  160. */
  161. #ifdef DISTRHO_PLUGIN_LABEL
  162. virtual const char* getLabel() const
  163. {
  164. return DISTRHO_PLUGIN_LABEL;
  165. }
  166. #else
  167. virtual const char* getLabel() const = 0;
  168. #endif
  169. /**
  170. Get an extensive comment/description about the plugin.@n
  171. Optional, returns nothing by default.
  172. */
  173. virtual const char* getDescription() const
  174. {
  175. #ifdef DISTRHO_PLUGIN_DESCRIPTION
  176. return DISTRHO_PLUGIN_DESCRIPTION;
  177. #else
  178. return "";
  179. #endif
  180. }
  181. /**
  182. Get the plugin author/maker.
  183. */
  184. #ifdef DISTRHO_PLUGIN_MAKER
  185. virtual const char* getMaker() const
  186. {
  187. return DISTRHO_PLUGIN_MAKER;
  188. }
  189. #else
  190. virtual const char* getMaker() const = 0;
  191. #endif
  192. /**
  193. Get the plugin homepage.@n
  194. Optional, returns nothing by default.
  195. */
  196. virtual const char* getHomePage() const
  197. {
  198. #ifdef DISTRHO_PLUGIN_HOMEPAGE
  199. return DISTRHO_PLUGIN_HOMEPAGE;
  200. #else
  201. return "";
  202. #endif
  203. }
  204. /**
  205. Get the plugin license (a single line of text or a URL).@n
  206. For commercial plugins this should return some short copyright information.
  207. */
  208. #ifdef DISTRHO_PLUGIN_LICENSE
  209. virtual const char* getLicense() const
  210. {
  211. return DISTRHO_PLUGIN_LICENSE;
  212. }
  213. #else
  214. virtual const char* getLicense() const = 0;
  215. #endif
  216. #if DISTRHO_PLUGIN_WANT_EXTRA_VERSION
  217. /**
  218. Get the plugin version.
  219. @see VersionInfo
  220. */
  221. virtual VersionInfo getVersion() const = 0;
  222. #else
  223. /**
  224. Get the plugin version, in hexadecimal.
  225. @see d_version()
  226. */
  227. virtual uint32_t getVersion() const = 0;
  228. #endif
  229. /**
  230. Get the plugin unique Id.@n
  231. This value is used by LADSPA, DSSI, VST2, VST3 and AUv2 plugin formats.@n
  232. @note It is preferred that you set DISTRHO_PLUGIN_UNIQUE_ID macro instead of overriding this call,
  233. as that is required for AUv2 plugins anyhow.
  234. @see d_cconst()
  235. */
  236. #ifdef DISTRHO_PLUGIN_UNIQUE_ID
  237. virtual int64_t getUniqueId() const
  238. {
  239. return d_cconst(STRINGIFY(DISTRHO_PLUGIN_UNIQUE_ID));
  240. }
  241. #else
  242. virtual int64_t getUniqueId() const = 0;
  243. #endif
  244. /* --------------------------------------------------------------------------------------------------------
  245. * Init */
  246. /**
  247. Initialize the audio port @a index.@n
  248. This function will be called once, shortly after the plugin is created.
  249. */
  250. virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
  251. /**
  252. Initialize the parameter @a index.@n
  253. This function will be called once, shortly after the plugin is created.
  254. */
  255. virtual void initParameter(uint32_t index, Parameter& parameter);
  256. /**
  257. Initialize the port group @a groupId.@n
  258. This function will be called once,
  259. shortly after the plugin is created and all audio ports and parameters have been enumerated.
  260. */
  261. virtual void initPortGroup(uint32_t groupId, PortGroup& portGroup);
  262. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  263. /**
  264. Set the name of the program @a index.@n
  265. This function will be called once, shortly after the plugin is created.@n
  266. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  267. */
  268. virtual void initProgramName(uint32_t index, String& programName) = 0;
  269. #endif
  270. #if DISTRHO_PLUGIN_WANT_STATE
  271. /**
  272. Initialize the state @a index.@n
  273. This function will be called once, shortly after the plugin is created.@n
  274. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  275. */
  276. virtual void initState(uint32_t index, State& state);
  277. DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
  278. virtual void initState(uint32_t, String&, String&) {}
  279. DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
  280. virtual bool isStateFile(uint32_t) { return false; }
  281. #endif
  282. /* --------------------------------------------------------------------------------------------------------
  283. * Internal data */
  284. /**
  285. Get the current value of a parameter.@n
  286. The host may call this function from any context, including realtime processing.
  287. */
  288. virtual float getParameterValue(uint32_t index) const;
  289. /**
  290. Change a parameter value.@n
  291. The host may call this function from any context, including realtime processing.@n
  292. When a parameter is marked as automatable, you must ensure no non-realtime operations are performed.
  293. @note This function will only be called for parameter inputs.
  294. */
  295. virtual void setParameterValue(uint32_t index, float value);
  296. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  297. /**
  298. Load a program.@n
  299. The host may call this function from any context, including realtime processing.@n
  300. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  301. */
  302. virtual void loadProgram(uint32_t index);
  303. #endif
  304. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  305. /**
  306. Get the value of an internal state.@n
  307. The host may call this function from any non-realtime context.@n
  308. Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
  309. @note The use of this function breaks compatibility with the DSSI format.
  310. */
  311. virtual String getState(const char* key) const;
  312. #endif
  313. #if DISTRHO_PLUGIN_WANT_STATE
  314. /**
  315. Change an internal state @a key to @a value.@n
  316. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  317. */
  318. virtual void setState(const char* key, const char* value);
  319. #endif
  320. /* --------------------------------------------------------------------------------------------------------
  321. * Audio/MIDI Processing */
  322. /**
  323. Activate this plugin.
  324. */
  325. virtual void activate() {}
  326. /**
  327. Deactivate this plugin.
  328. */
  329. virtual void deactivate() {}
  330. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  331. /**
  332. Run/process function for plugins with MIDI input.
  333. @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
  334. */
  335. virtual void run(const float** inputs, float** outputs, uint32_t frames,
  336. const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  337. #else
  338. /**
  339. Run/process function for plugins without MIDI input.
  340. @note Some parameters might be null if there are no audio inputs or outputs.
  341. */
  342. virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
  343. #endif
  344. /* --------------------------------------------------------------------------------------------------------
  345. * Callbacks (optional) */
  346. /**
  347. Optional callback to inform the plugin about a buffer size change.@n
  348. This function will only be called when the plugin is deactivated.
  349. @note This value is only a hint!@n
  350. Hosts might call run() with a higher or lower number of frames.
  351. @see getBufferSize()
  352. */
  353. virtual void bufferSizeChanged(uint32_t newBufferSize);
  354. /**
  355. Optional callback to inform the plugin about a sample rate change.@n
  356. This function will only be called when the plugin is deactivated.
  357. @see getSampleRate()
  358. */
  359. virtual void sampleRateChanged(double newSampleRate);
  360. /**
  361. Optional callback to inform the plugin about audio port IO changes.@n
  362. This function will only be called when the plugin is deactivated.@n
  363. Only used in AU (AudioUnit) format when DISTRHO_PLUGIN_EXTRA_IO is defined.
  364. @see DISTRHO_PLUGIN_EXTRA_IO
  365. */
  366. virtual void ioChanged(uint16_t numInputs, uint16_t numOutputs);
  367. // -------------------------------------------------------------------------------------------------------
  368. private:
  369. struct PrivateData;
  370. PrivateData* const pData;
  371. friend class PluginExporter;
  372. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  373. };
  374. /** @} */
  375. /* ------------------------------------------------------------------------------------------------------------
  376. * Create plugin, entry point */
  377. /**
  378. @defgroup EntryPoints Entry Points
  379. @{
  380. */
  381. /**
  382. Create an instance of the Plugin class.@n
  383. This is the entry point for DPF plugins.@n
  384. DPF will call this to either create an instance of your plugin for the host
  385. or to fetch some initial information for internal caching.
  386. */
  387. extern Plugin* createPlugin();
  388. /** @} */
  389. // -----------------------------------------------------------------------------------------------------------
  390. END_NAMESPACE_DISTRHO
  391. #endif // DISTRHO_PLUGIN_HPP_INCLUDED