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.

804 lines
22KB

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