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.

757 lines
21KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 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 "extra/String.hpp"
  19. #include "extra/LeakDetector.hpp"
  20. #include "src/DistrhoPluginChecks.h"
  21. START_NAMESPACE_DISTRHO
  22. /* ------------------------------------------------------------------------------------------------------------
  23. * Audio Port Hints */
  24. /**
  25. @defgroup AudioPortHints Audio Port Hints
  26. Various audio port hints.
  27. @see AudioPort::hints
  28. @{
  29. */
  30. /**
  31. Audio port can be used as control voltage (LV2 only).
  32. */
  33. static const uint32_t kAudioPortIsCV = 0x1;
  34. /**
  35. Audio port should be used as sidechan (LV2 only).
  36. */
  37. static const uint32_t kAudioPortIsSidechain = 0x2;
  38. /** @} */
  39. /* ------------------------------------------------------------------------------------------------------------
  40. * Parameter Hints */
  41. /**
  42. @defgroup ParameterHints Parameter Hints
  43. Various parameter hints.
  44. @see Parameter::hints
  45. @{
  46. */
  47. /**
  48. Parameter is automable (real-time safe).
  49. @see Plugin::setParameterValue(uint32_t, float)
  50. */
  51. static const uint32_t kParameterIsAutomable = 0x01;
  52. /**
  53. Parameter value is boolean.@n
  54. It's always at either minimum or maximum value.
  55. */
  56. static const uint32_t kParameterIsBoolean = 0x02;
  57. /**
  58. Parameter value is integer.
  59. */
  60. static const uint32_t kParameterIsInteger = 0x04;
  61. /**
  62. Parameter value is logarithmic.
  63. */
  64. static const uint32_t kParameterIsLogarithmic = 0x08;
  65. /**
  66. Parameter is of output type.@n
  67. When unset, parameter is assumed to be of input type.
  68. Parameter inputs are changed by the host and must not be changed by the plugin.@n
  69. The only exception being when changing programs, see Plugin::loadProgram().@n
  70. Outputs are changed by the plugin and never modified by the host.
  71. */
  72. static const uint32_t kParameterIsOutput = 0x10;
  73. /** @} */
  74. /* ------------------------------------------------------------------------------------------------------------
  75. * Base Plugin structs */
  76. /**
  77. @defgroup BasePluginStructs Base Plugin Structs
  78. @{
  79. */
  80. /**
  81. Audio Port.
  82. */
  83. struct AudioPort {
  84. /**
  85. Hints describing this audio port.
  86. @see AudioPortHints
  87. */
  88. uint32_t hints;
  89. /**
  90. The name of this audio port.@n
  91. An audio port name can contain any character, but hosts might have a hard time with non-ascii ones.@n
  92. The name doesn't have to be unique within a plugin instance, but it's recommended.
  93. */
  94. String name;
  95. /**
  96. The symbol of this audio port.@n
  97. An audio port symbol is a short restricted name used as a machine and human readable identifier.@n
  98. The first character must be one of _, a-z or A-Z and subsequent characters can be from _, a-z, A-Z and 0-9.
  99. @note Audio port and parameter symbols MUST be unique within a plugin instance.
  100. */
  101. String symbol;
  102. /**
  103. Default constructor for a regular audio port.
  104. */
  105. AudioPort() noexcept
  106. : hints(0x0),
  107. name(),
  108. symbol() {}
  109. };
  110. /**
  111. Parameter ranges.@n
  112. This is used to set the default, minimum and maximum values of a parameter.
  113. By default a parameter has 0.0 as minimum, 1.0 as maximum and 0.0 as default.@n
  114. When changing this struct values you must ensure maximum > minimum and default is within range.
  115. */
  116. struct ParameterRanges {
  117. /**
  118. Default value.
  119. */
  120. float def;
  121. /**
  122. Minimum value.
  123. */
  124. float min;
  125. /**
  126. Maximum value.
  127. */
  128. float max;
  129. /**
  130. Default constructor, using 0.0 as minimum, 1.0 as maximum and 0.0 as default.
  131. */
  132. ParameterRanges() noexcept
  133. : def(0.0f),
  134. min(0.0f),
  135. max(1.0f) {}
  136. /**
  137. Constructor using custom values.
  138. */
  139. ParameterRanges(float df, float mn, float mx) noexcept
  140. : def(df),
  141. min(mn),
  142. max(mx) {}
  143. /**
  144. Fix the default value within range.
  145. */
  146. void fixDefault() noexcept
  147. {
  148. fixValue(def);
  149. }
  150. /**
  151. Fix a value within range.
  152. */
  153. void fixValue(float& value) const noexcept
  154. {
  155. if (value < min)
  156. value = min;
  157. else if (value > max)
  158. value = max;
  159. }
  160. /**
  161. Get a fixed value within range.
  162. */
  163. const float& getFixedValue(const float& value) const noexcept
  164. {
  165. if (value <= min)
  166. return min;
  167. if (value >= max)
  168. return max;
  169. return value;
  170. }
  171. /**
  172. Get a value normalized to 0.0<->1.0.
  173. */
  174. float getNormalizedValue(const float& value) const noexcept
  175. {
  176. const float normValue((value - min) / (max - min));
  177. if (normValue <= 0.0f)
  178. return 0.0f;
  179. if (normValue >= 1.0f)
  180. return 1.0f;
  181. return normValue;
  182. }
  183. /**
  184. Get a value normalized to 0.0<->1.0, fixed within range.
  185. */
  186. float getFixedAndNormalizedValue(const float& value) const noexcept
  187. {
  188. if (value <= min)
  189. return 0.0f;
  190. if (value >= max)
  191. return 1.0f;
  192. const float normValue((value - min) / (max - min));
  193. if (normValue <= 0.0f)
  194. return 0.0f;
  195. if (normValue >= 1.0f)
  196. return 1.0f;
  197. return normValue;
  198. }
  199. /**
  200. Get a proper value previously normalized to 0.0<->1.0.
  201. */
  202. float getUnnormalizedValue(const float& value) const noexcept
  203. {
  204. if (value <= 0.0f)
  205. return min;
  206. if (value >= 1.0f)
  207. return max;
  208. return value * (max - min) + min;
  209. }
  210. };
  211. /**
  212. Parameter.
  213. */
  214. struct Parameter {
  215. /**
  216. Hints describing this parameter.
  217. @see ParameterHints
  218. */
  219. uint32_t hints;
  220. /**
  221. The name of this parameter.@n
  222. A parameter name can contain any character, but hosts might have a hard time with non-ascii ones.@n
  223. The name doesn't have to be unique within a plugin instance, but it's recommended.
  224. */
  225. String name;
  226. /**
  227. The symbol of this parameter.@n
  228. A parameter symbol is a short restricted name used as a machine and human readable identifier.@n
  229. The first character must be one of _, a-z or A-Z and subsequent characters can be from _, a-z, A-Z and 0-9.
  230. @note Parameter symbols MUST be unique within a plugin instance.
  231. */
  232. String symbol;
  233. /**
  234. The unit of this parameter.@n
  235. This means something like "dB", "kHz" and "ms".@n
  236. Can be left blank if a unit does not apply to this parameter.
  237. */
  238. String unit;
  239. /**
  240. Ranges of this parameter.@n
  241. The ranges describe the default, minimum and maximum values.
  242. */
  243. ParameterRanges ranges;
  244. /**
  245. MIDI CC to use by default on this parameter.@n
  246. A value of 0 or 32 (bank change) is considered invalid.@n
  247. Must also be less or equal to 120.
  248. @note This value is only a hint! Hosts might map it automatically or completely ignore it.
  249. */
  250. uint8_t midiCC;
  251. /**
  252. Default constructor for a null parameter.
  253. */
  254. Parameter() noexcept
  255. : hints(0x0),
  256. name(),
  257. symbol(),
  258. unit(),
  259. ranges(),
  260. midiCC(0) {}
  261. /**
  262. Constructor using custom values.
  263. */
  264. Parameter(uint32_t h, const char* n, const char* s, const char* u, float def, float min, float max) noexcept
  265. : hints(h),
  266. name(n),
  267. symbol(s),
  268. unit(u),
  269. ranges(def, min, max),
  270. midiCC(0) {}
  271. };
  272. /**
  273. MIDI event.
  274. */
  275. struct MidiEvent {
  276. /**
  277. Size of internal data.
  278. */
  279. static const uint32_t kDataSize = 4;
  280. /**
  281. Time offset in frames.
  282. */
  283. uint32_t frame;
  284. /**
  285. Number of bytes used.
  286. */
  287. uint32_t size;
  288. /**
  289. MIDI data.@n
  290. If size > kDataSize, dataExt is used (otherwise null).
  291. */
  292. uint8_t data[kDataSize];
  293. const uint8_t* dataExt;
  294. };
  295. /**
  296. Time position.@n
  297. The @a playing and @a frame values are always valid.@n
  298. BBT values are only valid when @a bbt.valid is true.
  299. This struct is inspired by the JACK Transport API.
  300. */
  301. struct TimePosition {
  302. /**
  303. Wherever the host transport is playing/rolling.
  304. */
  305. bool playing;
  306. /**
  307. Current host transport position in frames.
  308. */
  309. uint64_t frame;
  310. /**
  311. Bar-Beat-Tick time position.
  312. */
  313. struct BarBeatTick {
  314. /**
  315. Wherever the host transport is using BBT.@n
  316. If false you must not read from this struct.
  317. */
  318. bool valid;
  319. /**
  320. Current bar.@n
  321. Should always be > 0.@n
  322. The first bar is bar '1'.
  323. */
  324. int32_t bar;
  325. /**
  326. Current beat within bar.@n
  327. Should always be > 0 and <= @a beatsPerBar.@n
  328. The first beat is beat '1'.
  329. */
  330. int32_t beat;
  331. /**
  332. Current tick within beat.@n
  333. Should always be > 0 and <= @a ticksPerBeat.@n
  334. The first tick is tick '0'.
  335. */
  336. int32_t tick;
  337. /**
  338. Number of ticks that have elapsed between frame 0 and the first beat of the current measure.
  339. */
  340. double barStartTick;
  341. /**
  342. Time signature "numerator".
  343. */
  344. float beatsPerBar;
  345. /**
  346. Time signature "denominator".
  347. */
  348. float beatType;
  349. /**
  350. Number of ticks within a bar.@n
  351. Usually a moderately large integer with many denominators, such as 1920.0.
  352. */
  353. double ticksPerBeat;
  354. /**
  355. Number of beats per minute.
  356. */
  357. double beatsPerMinute;
  358. /**
  359. Default constructor for a null BBT time position.
  360. */
  361. BarBeatTick() noexcept
  362. : valid(false),
  363. bar(0),
  364. beat(0),
  365. tick(0),
  366. barStartTick(0.0),
  367. beatsPerBar(0.0f),
  368. beatType(0.0f),
  369. ticksPerBeat(0.0),
  370. beatsPerMinute(0.0) {}
  371. } bbt;
  372. /**
  373. Default constructor for a time position.
  374. */
  375. TimePosition() noexcept
  376. : playing(false),
  377. frame(0),
  378. bbt() {}
  379. };
  380. /** @} */
  381. /* ------------------------------------------------------------------------------------------------------------
  382. * DPF Plugin */
  383. /**
  384. @defgroup MainClasses Main Classes
  385. @{
  386. */
  387. /**
  388. DPF Plugin class from where plugin instances are created.
  389. The public methods (Host state) are called from the plugin to get or set host information.@n
  390. They can be called from a plugin instance at anytime unless stated otherwise.@n
  391. All other methods are to be implemented by the plugin and will be called by the host.
  392. Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
  393. Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
  394. The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
  395. There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
  396. The buffer size and sample rate values will remain constant between activate and deactivate.@n
  397. Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
  398. Some of this class functions are only available according to some macros.
  399. DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
  400. When enabled you need to implement initProgramName() and loadProgram().
  401. DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
  402. When enabled you need to implement initStateKey() and setState().
  403. The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
  404. When enabled it provides midi input events.
  405. */
  406. class Plugin
  407. {
  408. public:
  409. /**
  410. Plugin class constructor.@n
  411. You must set all parameter values to their defaults, matching ParameterRanges::def.
  412. */
  413. Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
  414. /**
  415. Destructor.
  416. */
  417. virtual ~Plugin();
  418. /* --------------------------------------------------------------------------------------------------------
  419. * Host state */
  420. /**
  421. Get the current buffer size that will probably be used during processing, in frames.@n
  422. This value will remain constant between activate and deactivate.
  423. @note This value is only a hint!@n
  424. Hosts might call run() with a higher or lower number of frames.
  425. @see bufferSizeChanged(uint32_t)
  426. */
  427. uint32_t getBufferSize() const noexcept;
  428. /**
  429. Get the current sample rate that will be used during processing.@n
  430. This value will remain constant between activate and deactivate.
  431. @see sampleRateChanged(double)
  432. */
  433. double getSampleRate() const noexcept;
  434. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  435. /**
  436. Get the current host transport time position.@n
  437. This function should only be called during run().@n
  438. You can call this during other times, but the returned position is not guaranteed to be in sync.
  439. @note TimePosition is not supported in LADSPA and DSSI plugin formats.
  440. */
  441. const TimePosition& getTimePosition() const noexcept;
  442. #endif
  443. #if DISTRHO_PLUGIN_WANT_LATENCY
  444. /**
  445. Change the plugin audio output latency to @a frames.@n
  446. This function should only be called in the constructor, activate() and run().
  447. @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
  448. */
  449. void setLatency(uint32_t frames) noexcept;
  450. #endif
  451. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  452. /**
  453. Write a MIDI output event.@n
  454. This function must only be called during run().@n
  455. Returns false when the host buffer is full, in which case do not call this again until the next run().
  456. @note This function is not implemented yet!@n
  457. It's here so that developers can prepare MIDI plugins in advance.@n
  458. If you plan to use this, please report to DPF authos so it can be implemented.
  459. */
  460. bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
  461. #endif
  462. protected:
  463. /* --------------------------------------------------------------------------------------------------------
  464. * Information */
  465. /**
  466. Get the plugin name.@n
  467. Returns DISTRHO_PLUGIN_NAME by default.
  468. */
  469. virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
  470. /**
  471. Get the plugin label.@n
  472. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  473. */
  474. virtual const char* getLabel() const = 0;
  475. /**
  476. Get an extensive comment/description about the plugin.@n
  477. Optional, returns nothing by default.
  478. */
  479. virtual const char* getDescription() const { return ""; }
  480. /**
  481. Get the plugin author/maker.
  482. */
  483. virtual const char* getMaker() const = 0;
  484. /**
  485. Get the plugin homepage.@n
  486. Optional, returns nothing by default.
  487. */
  488. virtual const char* getHomePage() const { return ""; }
  489. /**
  490. Get the plugin license (a single line of text or a URL).@n
  491. For commercial plugins this should return some short copyright information.
  492. */
  493. virtual const char* getLicense() const = 0;
  494. /**
  495. Get the plugin version, in hexadecimal.
  496. @see d_version()
  497. */
  498. virtual uint32_t getVersion() const = 0;
  499. /**
  500. Get the plugin unique Id.@n
  501. This value is used by LADSPA, DSSI and VST plugin formats.
  502. @see d_cconst()
  503. */
  504. virtual int64_t getUniqueId() const = 0;
  505. /* --------------------------------------------------------------------------------------------------------
  506. * Init */
  507. /**
  508. Initialize the audio port @a index.@n
  509. This function will be called once, shortly after the plugin is created.
  510. */
  511. virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
  512. /**
  513. Initialize the parameter @a index.@n
  514. This function will be called once, shortly after the plugin is created.
  515. */
  516. virtual void initParameter(uint32_t index, Parameter& parameter) = 0;
  517. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  518. /**
  519. Set the name of the program @a index.@n
  520. This function will be called once, shortly after the plugin is created.@n
  521. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  522. */
  523. virtual void initProgramName(uint32_t index, String& programName) = 0;
  524. #endif
  525. #if DISTRHO_PLUGIN_WANT_STATE
  526. /**
  527. Set the state key and default value of @a index.@n
  528. This function will be called once, shortly after the plugin is created.@n
  529. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  530. */
  531. virtual void initState(uint32_t index, String& stateKey, String& defaultStateValue) = 0;
  532. #endif
  533. /* --------------------------------------------------------------------------------------------------------
  534. * Internal data */
  535. /**
  536. Get the current value of a parameter.@n
  537. The host may call this function from any context, including realtime processing.
  538. */
  539. virtual float getParameterValue(uint32_t index) const = 0;
  540. /**
  541. Change a parameter value.@n
  542. The host may call this function from any context, including realtime processing.@n
  543. When a parameter is marked as automable, you must ensure no non-realtime operations are performed.
  544. @note This function will only be called for parameter inputs.
  545. */
  546. virtual void setParameterValue(uint32_t index, float value) = 0;
  547. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  548. /**
  549. Load a program.@n
  550. The host may call this function from any context, including realtime processing.@n
  551. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  552. */
  553. virtual void loadProgram(uint32_t index) = 0;
  554. #endif
  555. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  556. /**
  557. Get the value of an internal state.@n
  558. The host may call this function from any non-realtime context.@n
  559. Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_PROGRAMS or DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
  560. @note The use of this function breaks compatibility with the DSSI format.
  561. */
  562. virtual String getState(const char* key) const = 0;
  563. #endif
  564. #if DISTRHO_PLUGIN_WANT_STATE
  565. /**
  566. Change an internal state @a key to @a value.@n
  567. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  568. */
  569. virtual void setState(const char* key, const char* value) = 0;
  570. #endif
  571. /* --------------------------------------------------------------------------------------------------------
  572. * Audio/MIDI Processing */
  573. /**
  574. Activate this plugin.
  575. */
  576. virtual void activate() {}
  577. /**
  578. Deactivate this plugin.
  579. */
  580. virtual void deactivate() {}
  581. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  582. /**
  583. Run/process function for plugins with MIDI input.
  584. @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
  585. */
  586. virtual void run(const float** inputs, float** outputs, uint32_t frames,
  587. const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  588. #else
  589. /**
  590. Run/process function for plugins without MIDI input.
  591. @note Some parameters might be null if there are no audio inputs or outputs.
  592. */
  593. virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
  594. #endif
  595. /* --------------------------------------------------------------------------------------------------------
  596. * Callbacks (optional) */
  597. /**
  598. Optional callback to inform the plugin about a buffer size change.@n
  599. This function will only be called when the plugin is deactivated.
  600. @note This value is only a hint!@n
  601. Hosts might call run() with a higher or lower number of frames.
  602. @see getBufferSize()
  603. */
  604. virtual void bufferSizeChanged(uint32_t newBufferSize);
  605. /**
  606. Optional callback to inform the plugin about a sample rate change.@n
  607. This function will only be called when the plugin is deactivated.
  608. @see getSampleRate()
  609. */
  610. virtual void sampleRateChanged(double newSampleRate);
  611. // -------------------------------------------------------------------------------------------------------
  612. private:
  613. struct PrivateData;
  614. PrivateData* const pData;
  615. friend class PluginExporter;
  616. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  617. };
  618. /** @} */
  619. /* ------------------------------------------------------------------------------------------------------------
  620. * Create plugin, entry point */
  621. /**
  622. @defgroup EntryPoints Entry Points
  623. @{
  624. */
  625. /**
  626. TODO.
  627. */
  628. extern Plugin* createPlugin();
  629. /** @} */
  630. // -----------------------------------------------------------------------------------------------------------
  631. END_NAMESPACE_DISTRHO
  632. #endif // DISTRHO_PLUGIN_HPP_INCLUDED