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.

419 lines
15KB

  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. virtual const char* getLabel() const = 0;
  162. /**
  163. Get an extensive comment/description about the plugin.@n
  164. Optional, returns nothing by default.
  165. */
  166. virtual const char* getDescription() const { return ""; }
  167. /**
  168. Get the plugin author/maker.
  169. */
  170. virtual const char* getMaker() const = 0;
  171. /**
  172. Get the plugin homepage.@n
  173. Optional, returns nothing by default.
  174. */
  175. virtual const char* getHomePage() const { return ""; }
  176. /**
  177. Get the plugin license (a single line of text or a URL).@n
  178. For commercial plugins this should return some short copyright information.
  179. */
  180. virtual const char* getLicense() const = 0;
  181. /**
  182. Get the plugin version, in hexadecimal.
  183. @see d_version()
  184. */
  185. virtual uint32_t getVersion() const = 0;
  186. /**
  187. Get the plugin unique Id.@n
  188. This value is used by LADSPA, DSSI, VST2, VST3 and AUv2 plugin formats.@n
  189. @note It is preferred that you set DISTRHO_PLUGIN_UNIQUE_ID macro instead of overriding this call,
  190. as that is required for AUv2 plugins anyhow.
  191. @see d_cconst()
  192. */
  193. #ifdef DISTRHO_PLUGIN_UNIQUE_ID
  194. virtual int64_t getUniqueId() const
  195. {
  196. return d_cconst(STRINGIFY(DISTRHO_PLUGIN_UNIQUE_ID));
  197. }
  198. #else
  199. virtual int64_t getUniqueId() const = 0;
  200. #endif
  201. /* --------------------------------------------------------------------------------------------------------
  202. * Init */
  203. /**
  204. Initialize the audio port @a index.@n
  205. This function will be called once, shortly after the plugin is created.
  206. */
  207. virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
  208. /**
  209. Initialize the parameter @a index.@n
  210. This function will be called once, shortly after the plugin is created.
  211. */
  212. virtual void initParameter(uint32_t index, Parameter& parameter);
  213. /**
  214. Initialize the port group @a groupId.@n
  215. This function will be called once,
  216. shortly after the plugin is created and all audio ports and parameters have been enumerated.
  217. */
  218. virtual void initPortGroup(uint32_t groupId, PortGroup& portGroup);
  219. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  220. /**
  221. Set the name of the program @a index.@n
  222. This function will be called once, shortly after the plugin is created.@n
  223. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  224. */
  225. virtual void initProgramName(uint32_t index, String& programName) = 0;
  226. #endif
  227. #if DISTRHO_PLUGIN_WANT_STATE
  228. /**
  229. Initialize the state @a index.@n
  230. This function will be called once, shortly after the plugin is created.@n
  231. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  232. */
  233. virtual void initState(uint32_t index, State& state);
  234. DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
  235. virtual void initState(uint32_t, String&, String&) {}
  236. DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
  237. virtual bool isStateFile(uint32_t) { return false; }
  238. #endif
  239. /* --------------------------------------------------------------------------------------------------------
  240. * Internal data */
  241. /**
  242. Get the current value of a parameter.@n
  243. The host may call this function from any context, including realtime processing.
  244. */
  245. virtual float getParameterValue(uint32_t index) const;
  246. /**
  247. Change a parameter value.@n
  248. The host may call this function from any context, including realtime processing.@n
  249. When a parameter is marked as automatable, you must ensure no non-realtime operations are performed.
  250. @note This function will only be called for parameter inputs.
  251. */
  252. virtual void setParameterValue(uint32_t index, float value);
  253. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  254. /**
  255. Load a program.@n
  256. The host may call this function from any context, including realtime processing.@n
  257. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  258. */
  259. virtual void loadProgram(uint32_t index);
  260. #endif
  261. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  262. /**
  263. Get the value of an internal state.@n
  264. The host may call this function from any non-realtime context.@n
  265. Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
  266. @note The use of this function breaks compatibility with the DSSI format.
  267. */
  268. virtual String getState(const char* key) const;
  269. #endif
  270. #if DISTRHO_PLUGIN_WANT_STATE
  271. /**
  272. Change an internal state @a key to @a value.@n
  273. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  274. */
  275. virtual void setState(const char* key, const char* value);
  276. #endif
  277. /* --------------------------------------------------------------------------------------------------------
  278. * Audio/MIDI Processing */
  279. /**
  280. Activate this plugin.
  281. */
  282. virtual void activate() {}
  283. /**
  284. Deactivate this plugin.
  285. */
  286. virtual void deactivate() {}
  287. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  288. /**
  289. Run/process function for plugins with MIDI input.
  290. @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
  291. */
  292. virtual void run(const float** inputs, float** outputs, uint32_t frames,
  293. const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  294. #else
  295. /**
  296. Run/process function for plugins without MIDI input.
  297. @note Some parameters might be null if there are no audio inputs or outputs.
  298. */
  299. virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
  300. #endif
  301. /* --------------------------------------------------------------------------------------------------------
  302. * Callbacks (optional) */
  303. /**
  304. Optional callback to inform the plugin about a buffer size change.@n
  305. This function will only be called when the plugin is deactivated.
  306. @note This value is only a hint!@n
  307. Hosts might call run() with a higher or lower number of frames.
  308. @see getBufferSize()
  309. */
  310. virtual void bufferSizeChanged(uint32_t newBufferSize);
  311. /**
  312. Optional callback to inform the plugin about a sample rate change.@n
  313. This function will only be called when the plugin is deactivated.
  314. @see getSampleRate()
  315. */
  316. virtual void sampleRateChanged(double newSampleRate);
  317. /**
  318. Optional callback to inform the plugin about audio port IO changes.@n
  319. This function will only be called when the plugin is deactivated.@n
  320. Only used in AU (AudioUnit) format when DISTRHO_PLUGIN_EXTRA_IO is defined.
  321. @see DISTRHO_PLUGIN_EXTRA_IO
  322. */
  323. virtual void ioChanged(uint16_t numInputs, uint16_t numOutputs);
  324. // -------------------------------------------------------------------------------------------------------
  325. private:
  326. struct PrivateData;
  327. PrivateData* const pData;
  328. friend class PluginExporter;
  329. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  330. };
  331. /** @} */
  332. /* ------------------------------------------------------------------------------------------------------------
  333. * Create plugin, entry point */
  334. /**
  335. @defgroup EntryPoints Entry Points
  336. @{
  337. */
  338. /**
  339. Create an instance of the Plugin class.@n
  340. This is the entry point for DPF plugins.@n
  341. DPF will call this to either create an instance of your plugin for the host
  342. or to fetch some initial information for internal caching.
  343. */
  344. extern Plugin* createPlugin();
  345. /** @} */
  346. // -----------------------------------------------------------------------------------------------------------
  347. END_NAMESPACE_DISTRHO
  348. #endif // DISTRHO_PLUGIN_HPP_INCLUDED