Audio plugin host https://kx.studio/carla
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.

811 lines
23KB

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