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.

1237 lines
36KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2022 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 and JACK standalone only).
  32. */
  33. static const uint32_t kAudioPortIsCV = 0x1;
  34. /**
  35. Audio port should be used as sidechan (LV2 and VST3 only).
  36. This hint should not be used with CV style ports.
  37. @note non-sidechain audio ports must exist in the plugin if this flag is set.
  38. */
  39. static const uint32_t kAudioPortIsSidechain = 0x2;
  40. /**
  41. CV port has bipolar range (-1 to +1, or -5 to +5 if scaled).
  42. This is merely a hint to tell the host what value range to expect.
  43. */
  44. static const uint32_t kCVPortHasBipolarRange = 0x10;
  45. /**
  46. CV port has negative unipolar range (-1 to 0, or -10 to 0 if scaled).
  47. This is merely a hint to tell the host what value range to expect.
  48. */
  49. static const uint32_t kCVPortHasNegativeUnipolarRange = 0x20;
  50. /**
  51. CV port has positive unipolar range (0 to +1, or 0 to +10 if scaled).
  52. This is merely a hint to tell the host what value range to expect.
  53. */
  54. static const uint32_t kCVPortHasPositiveUnipolarRange = 0x40;
  55. /**
  56. CV port has scaled range to match real values (-5 to +5v bipolar, +/-10 to 0v unipolar).
  57. One other range flag is required if this flag is set.
  58. When enabled, this makes the port a mod:CVPort, compatible with the MOD Devices platform.
  59. */
  60. static const uint32_t kCVPortHasScaledRange = 0x80;
  61. /** @} */
  62. /* ------------------------------------------------------------------------------------------------------------
  63. * Parameter Hints */
  64. /**
  65. @defgroup ParameterHints Parameter Hints
  66. Various parameter hints.
  67. @see Parameter::hints
  68. @{
  69. */
  70. /**
  71. Parameter is automatable (real-time safe).
  72. @see Plugin::setParameterValue(uint32_t, float)
  73. */
  74. static const uint32_t kParameterIsAutomatable = 0x01;
  75. /** It was a typo, sorry.. */
  76. DISTRHO_DEPRECATED_BY("kParameterIsAutomatable")
  77. static const uint32_t kParameterIsAutomable = kParameterIsAutomatable;
  78. /**
  79. Parameter value is boolean.@n
  80. It's always at either minimum or maximum value.
  81. */
  82. static const uint32_t kParameterIsBoolean = 0x02;
  83. /**
  84. Parameter value is integer.
  85. */
  86. static const uint32_t kParameterIsInteger = 0x04;
  87. /**
  88. Parameter value is logarithmic.
  89. */
  90. static const uint32_t kParameterIsLogarithmic = 0x08;
  91. /**
  92. Parameter is of output type.@n
  93. When unset, parameter is assumed to be of input type.
  94. Parameter inputs are changed by the host and typically should not be changed by the plugin.@n
  95. One exception is when changing programs, see Plugin::loadProgram().@n
  96. The other exception is with parameter change requests, see Plugin::requestParameterValueChange().@n
  97. Outputs are changed by the plugin and never modified by the host.
  98. If you are targetting VST2, make sure to order your parameters so that all inputs are before any outputs.
  99. */
  100. static const uint32_t kParameterIsOutput = 0x10;
  101. /**
  102. Parameter value is a trigger.@n
  103. This means the value resets back to its default after each process/run call.@n
  104. Cannot be used for output parameters.
  105. @note Only officially supported under LV2. For other formats DPF simulates the behaviour.
  106. */
  107. static const uint32_t kParameterIsTrigger = 0x20 | kParameterIsBoolean;
  108. /** @} */
  109. /* ------------------------------------------------------------------------------------------------------------
  110. * State Hints */
  111. /**
  112. @defgroup StateHints State Hints
  113. Various state hints.
  114. @see State::hints
  115. @{
  116. */
  117. /**
  118. State is visible and readable by hosts that support string-type plugin parameters.
  119. */
  120. static const uint32_t kStateIsHostReadable = 0x01;
  121. /**
  122. State is writable by the host, allowing users to arbitrarily change the state.@n
  123. For obvious reasons a writable state is also readable by the host.
  124. */
  125. static const uint32_t kStateIsHostWritable = 0x02 | kStateIsHostReadable;
  126. /**
  127. State is a filename path instead of a regular string.@n
  128. The readable and writable hints are required for filenames to work, and thus are automatically set.
  129. */
  130. static const uint32_t kStateIsFilenamePath = 0x04 | kStateIsHostWritable;
  131. /**
  132. State is a base64 encoded string.
  133. */
  134. static const uint32_t kStateIsBase64Blob = 0x08;
  135. /**
  136. State is for Plugin/DSP side only, meaning there is never a need to notify the UI when it changes.
  137. */
  138. static const uint32_t kStateIsOnlyForDSP = 0x10;
  139. /**
  140. State is for UI side only.@n
  141. If the DSP and UI are separate and the UI is not available, this property won't be saved.
  142. */
  143. static const uint32_t kStateIsOnlyForUI = 0x20;
  144. /** @} */
  145. /* ------------------------------------------------------------------------------------------------------------
  146. * Base Plugin structs */
  147. /**
  148. @defgroup BasePluginStructs Base Plugin Structs
  149. @{
  150. */
  151. /**
  152. Parameter designation.@n
  153. Allows a parameter to be specially designated for a task, like bypass.
  154. Each designation is unique, there must be only one parameter that uses it.@n
  155. The use of designated parameters is completely optional.
  156. @note Designated parameters have strict ranges.
  157. @see ParameterRanges::adjustForDesignation()
  158. */
  159. enum ParameterDesignation {
  160. /**
  161. Null or unset designation.
  162. */
  163. kParameterDesignationNull = 0,
  164. /**
  165. Bypass designation.@n
  166. When on (> 0.5f), it means the plugin must run in a bypassed state.
  167. */
  168. kParameterDesignationBypass = 1
  169. };
  170. /**
  171. Predefined Port Groups Ids.
  172. This enumeration provides a few commonly used groups for convenient use in plugins.
  173. For preventing conflicts with user code, negative values are used here.
  174. When rolling your own port groups, you MUST start their group ids from 0 and they MUST be sequential.
  175. @see PortGroup
  176. */
  177. enum PredefinedPortGroupsIds {
  178. /**
  179. Null or unset port group.
  180. */
  181. kPortGroupNone = (uint32_t)-1,
  182. /**
  183. A single channel audio group.
  184. */
  185. kPortGroupMono = (uint32_t)-2,
  186. /**
  187. A 2-channel discrete stereo audio group,
  188. where the 1st audio port is the left channel and the 2nd port is the right channel.
  189. */
  190. kPortGroupStereo = (uint32_t)-3
  191. };
  192. /**
  193. Audio Port.
  194. Can be used as CV port by specifying kAudioPortIsCV in hints,@n
  195. but this is only supported in LV2 and JACK standalone formats.
  196. */
  197. struct AudioPort {
  198. /**
  199. Hints describing this audio port.
  200. @see AudioPortHints
  201. */
  202. uint32_t hints;
  203. /**
  204. The name of this audio port.@n
  205. An audio port name can contain any character, but hosts might have a hard time with non-ascii ones.@n
  206. The name doesn't have to be unique within a plugin instance, but it's recommended.
  207. */
  208. String name;
  209. /**
  210. The symbol of this audio port.@n
  211. An audio port symbol is a short restricted name used as a machine and human readable identifier.@n
  212. 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.
  213. @note Audio port and parameter symbols MUST be unique within a plugin instance.
  214. */
  215. String symbol;
  216. /**
  217. The group id that this audio/cv port belongs to.
  218. No group is assigned by default.
  219. You can use a group from PredefinedPortGroups or roll your own.@n
  220. When rolling your own port groups, you MUST start their group ids from 0 and they MUST be sequential.
  221. @see PortGroup, Plugin::initPortGroup
  222. */
  223. uint32_t groupId;
  224. /**
  225. Default constructor for a regular audio port.
  226. */
  227. AudioPort() noexcept
  228. : hints(0x0),
  229. name(),
  230. symbol(),
  231. groupId(kPortGroupNone) {}
  232. };
  233. /**
  234. Parameter ranges.@n
  235. This is used to set the default, minimum and maximum values of a parameter.
  236. By default a parameter has 0.0 as minimum, 1.0 as maximum and 0.0 as default.@n
  237. When changing this struct values you must ensure maximum > minimum and default is within range.
  238. */
  239. struct ParameterRanges {
  240. /**
  241. Default value.
  242. */
  243. float def;
  244. /**
  245. Minimum value.
  246. */
  247. float min;
  248. /**
  249. Maximum value.
  250. */
  251. float max;
  252. /**
  253. Default constructor, using 0.0 as default, 0.0 as minimum, 1.0 as maximum.
  254. */
  255. ParameterRanges() noexcept
  256. : def(0.0f),
  257. min(0.0f),
  258. max(1.0f) {}
  259. /**
  260. Constructor using custom values.
  261. */
  262. ParameterRanges(float df, float mn, float mx) noexcept
  263. : def(df),
  264. min(mn),
  265. max(mx) {}
  266. /**
  267. Fix the default value within range.
  268. */
  269. void fixDefault() noexcept
  270. {
  271. fixValue(def);
  272. }
  273. /**
  274. Fix a value within range.
  275. */
  276. void fixValue(float& value) const noexcept
  277. {
  278. if (value < min)
  279. value = min;
  280. else if (value > max)
  281. value = max;
  282. }
  283. /**
  284. Get a fixed value within range.
  285. */
  286. float getFixedValue(const float& value) const noexcept
  287. {
  288. if (value <= min)
  289. return min;
  290. if (value >= max)
  291. return max;
  292. return value;
  293. }
  294. /**
  295. Get a value normalized to 0.0<->1.0.
  296. */
  297. float getNormalizedValue(const float& value) const noexcept
  298. {
  299. const float normValue((value - min) / (max - min));
  300. if (normValue <= 0.0f)
  301. return 0.0f;
  302. if (normValue >= 1.0f)
  303. return 1.0f;
  304. return normValue;
  305. }
  306. /**
  307. Get a value normalized to 0.0<->1.0, fixed within range.
  308. */
  309. float getFixedAndNormalizedValue(const float& value) const noexcept
  310. {
  311. if (value <= min)
  312. return 0.0f;
  313. if (value >= max)
  314. return 1.0f;
  315. const float normValue((value - min) / (max - min));
  316. if (normValue <= 0.0f)
  317. return 0.0f;
  318. if (normValue >= 1.0f)
  319. return 1.0f;
  320. return normValue;
  321. }
  322. /**
  323. Get a proper value previously normalized to 0.0<->1.0.
  324. */
  325. float getUnnormalizedValue(const float& value) const noexcept
  326. {
  327. if (value <= 0.0f)
  328. return min;
  329. if (value >= 1.0f)
  330. return max;
  331. return value * (max - min) + min;
  332. }
  333. };
  334. /**
  335. Parameter enumeration value.@n
  336. A string representation of a plugin parameter value.@n
  337. Used together can be used to give meaning to parameter values, working as an enumeration.
  338. */
  339. struct ParameterEnumerationValue {
  340. /**
  341. Parameter value.
  342. */
  343. float value;
  344. /**
  345. String representation of this value.
  346. */
  347. String label;
  348. /**
  349. Default constructor, using 0.0 as value and empty label.
  350. */
  351. ParameterEnumerationValue() noexcept
  352. : value(0.0f),
  353. label() {}
  354. /**
  355. Constructor using custom values.
  356. */
  357. ParameterEnumerationValue(float v, const char* l) noexcept
  358. : value(v),
  359. label(l) {}
  360. };
  361. /**
  362. Collection of parameter enumeration values.@n
  363. Handy class to handle the lifetime and count of all enumeration values.
  364. */
  365. struct ParameterEnumerationValues {
  366. /**
  367. Number of elements allocated in @values.
  368. */
  369. uint8_t count;
  370. /**
  371. Wherever the host is to be restricted to only use enumeration values.
  372. @note This mode is only a hint! Not all hosts and plugin formats support this mode.
  373. */
  374. bool restrictedMode;
  375. /**
  376. Array of @ParameterEnumerationValue items.@n
  377. This pointer must be null or have been allocated on the heap with `new ParameterEnumerationValue[count]`.
  378. */
  379. ParameterEnumerationValue* values;
  380. /**
  381. Default constructor, for zero enumeration values.
  382. */
  383. ParameterEnumerationValues() noexcept
  384. : count(0),
  385. restrictedMode(false),
  386. values() {}
  387. /**
  388. Constructor using custom values.@n
  389. The pointer to @values must have been allocated on the heap with `new`.
  390. */
  391. ParameterEnumerationValues(uint32_t c, bool r, ParameterEnumerationValue* v) noexcept
  392. : count(c),
  393. restrictedMode(r),
  394. values(v) {}
  395. ~ParameterEnumerationValues() noexcept
  396. {
  397. count = 0;
  398. restrictedMode = false;
  399. if (values != nullptr)
  400. {
  401. delete[] values;
  402. values = nullptr;
  403. }
  404. }
  405. DISTRHO_DECLARE_NON_COPYABLE(ParameterEnumerationValues)
  406. };
  407. /**
  408. Parameter.
  409. */
  410. struct Parameter {
  411. /**
  412. Hints describing this parameter.
  413. @see ParameterHints
  414. */
  415. uint32_t hints;
  416. /**
  417. The name of this parameter.@n
  418. A parameter name can contain any character, but hosts might have a hard time with non-ascii ones.@n
  419. The name doesn't have to be unique within a plugin instance, but it's recommended.
  420. */
  421. String name;
  422. /**
  423. The short name of this parameter.@n
  424. Used when displaying the parameter name in a very limited space.
  425. @note This value is optional, the full name is used when the short one is missing.
  426. */
  427. String shortName;
  428. /**
  429. The symbol of this parameter.@n
  430. A parameter symbol is a short restricted name used as a machine and human readable identifier.@n
  431. 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.
  432. @note Parameter symbols MUST be unique within a plugin instance.
  433. */
  434. String symbol;
  435. /**
  436. The unit of this parameter.@n
  437. This means something like "dB", "kHz" and "ms".@n
  438. Can be left blank if a unit does not apply to this parameter.
  439. */
  440. String unit;
  441. /**
  442. An extensive description/comment about the parameter.
  443. @note This value is optional and only used for LV2.
  444. */
  445. String description;
  446. /**
  447. Ranges of this parameter.@n
  448. The ranges describe the default, minimum and maximum values.
  449. */
  450. ParameterRanges ranges;
  451. /**
  452. Enumeration values.@n
  453. Can be used to give meaning to parameter values, working as an enumeration.
  454. */
  455. ParameterEnumerationValues enumValues;
  456. /**
  457. Designation for this parameter.
  458. */
  459. ParameterDesignation designation;
  460. /**
  461. MIDI CC to use by default on this parameter.@n
  462. A value of 0 or 32 (bank change) is considered invalid.@n
  463. Must also be less or equal to 120.
  464. @note This value is only a hint! Hosts might map it automatically or completely ignore it.
  465. */
  466. uint8_t midiCC;
  467. /**
  468. The group id that this parameter belongs to.
  469. No group is assigned by default.
  470. You can use a group from PredefinedPortGroups or roll your own.@n
  471. When rolling your own port groups, you MUST start their group ids from 0 and they MUST be sequential.
  472. @see PortGroup, Plugin::initPortGroup
  473. */
  474. uint32_t groupId;
  475. /**
  476. Default constructor for a null parameter.
  477. */
  478. Parameter() noexcept
  479. : hints(0x0),
  480. name(),
  481. shortName(),
  482. symbol(),
  483. unit(),
  484. ranges(),
  485. enumValues(),
  486. designation(kParameterDesignationNull),
  487. midiCC(0),
  488. groupId(kPortGroupNone) {}
  489. /**
  490. Constructor using custom values.
  491. */
  492. Parameter(uint32_t h, const char* n, const char* s, const char* u, float def, float min, float max) noexcept
  493. : hints(h),
  494. name(n),
  495. shortName(),
  496. symbol(s),
  497. unit(u),
  498. ranges(def, min, max),
  499. enumValues(),
  500. designation(kParameterDesignationNull),
  501. midiCC(0),
  502. groupId(kPortGroupNone) {}
  503. /**
  504. Initialize a parameter for a specific designation.
  505. */
  506. void initDesignation(ParameterDesignation d) noexcept
  507. {
  508. designation = d;
  509. switch (d)
  510. {
  511. case kParameterDesignationNull:
  512. break;
  513. case kParameterDesignationBypass:
  514. hints = kParameterIsAutomatable|kParameterIsBoolean|kParameterIsInteger;
  515. name = "Bypass";
  516. shortName = "Bypass";
  517. symbol = "dpf_bypass";
  518. unit = "";
  519. midiCC = 0;
  520. groupId = kPortGroupNone;
  521. ranges.def = 0.0f;
  522. ranges.min = 0.0f;
  523. ranges.max = 1.0f;
  524. break;
  525. }
  526. }
  527. };
  528. /**
  529. Port Group.@n
  530. Allows to group together audio/cv ports or parameters.
  531. Each unique group MUST have an unique symbol and a name.
  532. A group can be applied to both inputs and outputs (at the same time).
  533. The same group cannot be used in audio ports and parameters.
  534. When both audio and parameter groups are used, audio groups MUST be defined first.
  535. That is, group indexes start with audio ports, then parameters.
  536. An audio port group logically combines ports which should be considered part of the same stream.@n
  537. For example, two audio ports in a group may form a stereo stream.
  538. A parameter group provides meta-data to the host to indicate that some parameters belong together.
  539. The use of port groups is completely optional.
  540. @see Plugin::initPortGroup, AudioPort::group, Parameter::group
  541. */
  542. struct PortGroup {
  543. /**
  544. The name of this port group.@n
  545. A port group name can contain any character, but hosts might have a hard time with non-ascii ones.@n
  546. The name doesn't have to be unique within a plugin instance, but it's recommended.
  547. */
  548. String name;
  549. /**
  550. The symbol of this port group.@n
  551. A port group symbol is a short restricted name used as a machine and human readable identifier.@n
  552. 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.
  553. @note Port group symbols MUST be unique within a plugin instance.
  554. */
  555. String symbol;
  556. };
  557. /**
  558. State.
  559. In DPF states refer to key:value string pairs, used to store arbitrary non-parameter data.@n
  560. By default states are completely internal to the plugin and not visible by the host.@n
  561. Flags can be set to allow hosts to see and/or change them.
  562. TODO API under construction
  563. */
  564. struct State {
  565. /**
  566. Hints describing this state.
  567. @note Changing these hints can break compatibility with previously saved data.
  568. @see StateHints
  569. */
  570. uint32_t hints;
  571. /**
  572. The key or "symbol" of this state.@n
  573. A state key is a short restricted name used as a machine and human readable identifier.
  574. @note State keys MUST be unique within a plugin instance.
  575. TODO define rules for allowed characters, must be usable as URI non-encoded parameters
  576. */
  577. String key;
  578. /**
  579. The default value of this state.@n
  580. Can be left empty if considered a valid initial state.
  581. */
  582. String defaultValue;
  583. /**
  584. String representation of this state.
  585. */
  586. String label;
  587. /**
  588. An extensive description/comment about this state.
  589. @note This value is optional and only used for LV2.
  590. */
  591. String description;
  592. };
  593. /**
  594. MIDI event.
  595. */
  596. struct MidiEvent {
  597. /**
  598. Size of internal data.
  599. */
  600. static const uint32_t kDataSize = 4;
  601. /**
  602. Time offset in frames.
  603. */
  604. uint32_t frame;
  605. /**
  606. Number of bytes used.
  607. */
  608. uint32_t size;
  609. /**
  610. MIDI data.@n
  611. If size > kDataSize, dataExt is used (otherwise null).
  612. When dataExt is used, the event holder is responsible for
  613. keeping the pointer valid during the entirety of the run function.
  614. */
  615. uint8_t data[kDataSize];
  616. const uint8_t* dataExt;
  617. };
  618. /**
  619. Time position.@n
  620. The @a playing and @a frame values are always valid.@n
  621. BBT values are only valid when @a bbt.valid is true.
  622. This struct is inspired by the [JACK Transport API](https://jackaudio.org/api/structjack__position__t.html).
  623. */
  624. struct TimePosition {
  625. /**
  626. Wherever the host transport is playing/rolling.
  627. */
  628. bool playing;
  629. /**
  630. Current host transport position in frames.
  631. @note This value is not always monotonic,
  632. with some plugin hosts assigning it based on a source that can accumulate rounding errors.
  633. */
  634. uint64_t frame;
  635. /**
  636. Bar-Beat-Tick time position.
  637. */
  638. struct BarBeatTick {
  639. /**
  640. Wherever the host transport is using BBT.@n
  641. If false you must not read from this struct.
  642. */
  643. bool valid;
  644. /**
  645. Current bar.@n
  646. Should always be > 0.@n
  647. The first bar is bar '1'.
  648. */
  649. int32_t bar;
  650. /**
  651. Current beat within bar.@n
  652. Should always be > 0 and <= @a beatsPerBar.@n
  653. The first beat is beat '1'.
  654. */
  655. int32_t beat;
  656. /**
  657. Current tick within beat.@n
  658. Should always be >= 0 and < @a ticksPerBeat.@n
  659. The first tick is tick '0'.
  660. @note Fraction part of tick is only available on some plugin formats.
  661. */
  662. double tick;
  663. /**
  664. Number of ticks that have elapsed between frame 0 and the first beat of the current measure.
  665. */
  666. double barStartTick;
  667. /**
  668. Time signature "numerator".
  669. */
  670. float beatsPerBar;
  671. /**
  672. Time signature "denominator".
  673. */
  674. float beatType;
  675. /**
  676. Number of ticks within a beat.@n
  677. Usually a moderately large integer with many denominators, such as 1920.0.
  678. */
  679. double ticksPerBeat;
  680. /**
  681. Number of beats per minute.
  682. */
  683. double beatsPerMinute;
  684. /**
  685. Default constructor for a null BBT time position.
  686. */
  687. BarBeatTick() noexcept
  688. : valid(false),
  689. bar(0),
  690. beat(0),
  691. tick(0),
  692. barStartTick(0.0),
  693. beatsPerBar(0.0f),
  694. beatType(0.0f),
  695. ticksPerBeat(0.0),
  696. beatsPerMinute(0.0) {}
  697. /**
  698. Reinitialize this position using the default null initialization.
  699. */
  700. void clear() noexcept
  701. {
  702. valid = false;
  703. bar = 0;
  704. beat = 0;
  705. tick = 0;
  706. barStartTick = 0.0;
  707. beatsPerBar = 0.0f;
  708. beatType = 0.0f;
  709. ticksPerBeat = 0.0;
  710. beatsPerMinute = 0.0;
  711. }
  712. } bbt;
  713. /**
  714. Default constructor for a time position.
  715. */
  716. TimePosition() noexcept
  717. : playing(false),
  718. frame(0),
  719. bbt() {}
  720. /**
  721. Reinitialize this position using the default null initialization.
  722. */
  723. void clear() noexcept
  724. {
  725. playing = false;
  726. frame = 0;
  727. bbt.clear();
  728. }
  729. };
  730. /** @} */
  731. /* ------------------------------------------------------------------------------------------------------------
  732. * DPF Plugin */
  733. /**
  734. @defgroup MainClasses Main Classes
  735. @{
  736. */
  737. /**
  738. DPF Plugin class from where plugin instances are created.
  739. The public methods (Host state) are called from the plugin to get or set host information.@n
  740. They can be called from a plugin instance at anytime unless stated otherwise.@n
  741. All other methods are to be implemented by the plugin and will be called by the host.
  742. Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
  743. Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
  744. The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
  745. There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
  746. The buffer size and sample rate values will remain constant between activate and deactivate.@n
  747. Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
  748. Some of this class functions are only available according to some macros.
  749. DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
  750. When enabled you need to implement initProgramName() and loadProgram().
  751. DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
  752. When enabled you need to implement initStateKey() and setState().
  753. The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
  754. When enabled it provides midi input events.
  755. */
  756. class Plugin
  757. {
  758. public:
  759. /**
  760. Plugin class constructor.@n
  761. You must set all parameter values to their defaults, matching ParameterRanges::def.
  762. */
  763. Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
  764. /**
  765. Destructor.
  766. */
  767. virtual ~Plugin();
  768. /* --------------------------------------------------------------------------------------------------------
  769. * Host state */
  770. /**
  771. Get the current buffer size that will probably be used during processing, in frames.@n
  772. This value will remain constant between activate and deactivate.
  773. @note This value is only a hint!@n
  774. Hosts might call run() with a higher or lower number of frames.
  775. @see bufferSizeChanged(uint32_t)
  776. */
  777. uint32_t getBufferSize() const noexcept;
  778. /**
  779. Get the current sample rate that will be used during processing.@n
  780. This value will remain constant between activate and deactivate.
  781. @see sampleRateChanged(double)
  782. */
  783. double getSampleRate() const noexcept;
  784. /**
  785. Get the bundle path where the plugin resides.
  786. Can return null if the plugin is not available in a bundle (if it is a single binary).
  787. @see getBinaryFilename
  788. @see getResourcePath
  789. */
  790. const char* getBundlePath() const noexcept;
  791. /**
  792. Check if this plugin instance is a "dummy" one used for plugin meta-data/information export.@n
  793. When true no processing will be done, the plugin is created only to extract information.@n
  794. In DPF, LADSPA/DSSI, VST2 and VST3 formats create one global instance per plugin binary
  795. while LV2 creates one when generating turtle meta-data.
  796. */
  797. bool isDummyInstance() const noexcept;
  798. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  799. /**
  800. Get the current host transport time position.@n
  801. This function should only be called during run().@n
  802. You can call this during other times, but the returned position is not guaranteed to be in sync.
  803. @note TimePosition is not supported in LADSPA and DSSI plugin formats.
  804. */
  805. const TimePosition& getTimePosition() const noexcept;
  806. #endif
  807. #if DISTRHO_PLUGIN_WANT_LATENCY
  808. /**
  809. Change the plugin audio output latency to @a frames.@n
  810. This function should only be called in the constructor, activate() and run().
  811. @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
  812. */
  813. void setLatency(uint32_t frames) noexcept;
  814. #endif
  815. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  816. /**
  817. Write a MIDI output event.@n
  818. This function must only be called during run().@n
  819. Returns false when the host buffer is full, in which case do not call this again until the next run().
  820. */
  821. bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
  822. #endif
  823. #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  824. /**
  825. Check if parameter value change requests will work with the current plugin host.
  826. @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
  827. @see requestParameterValueChange(uint32_t, float)
  828. */
  829. bool canRequestParameterValueChanges() const noexcept;
  830. /**
  831. Request a parameter value change from the host.
  832. If successful, this function will automatically trigger a parameter update on the UI side as well.
  833. This function can fail, for example if the host is busy with the parameter for read-only automation.
  834. Some hosts simply do not have this functionality, which can be verified with canRequestParameterValueChanges().
  835. @note This function is only available if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST is enabled.
  836. */
  837. bool requestParameterValueChange(uint32_t index, float value) noexcept;
  838. #endif
  839. #if DISTRHO_PLUGIN_WANT_STATE
  840. /**
  841. Set state value and notify the host about the change.@n
  842. This function will call `setState()` and also trigger an update on the UI side as necessary.@n
  843. It must not be called during run.@n
  844. The state must be host readable.
  845. @note this function does nothing on DSSI plugin format, as DSSI only supports UI->DSP messages.
  846. TODO API under construction
  847. */
  848. bool updateStateValue(const char* key, const char* value) noexcept;
  849. #endif
  850. protected:
  851. /* --------------------------------------------------------------------------------------------------------
  852. * Information */
  853. /**
  854. Get the plugin name.@n
  855. Returns DISTRHO_PLUGIN_NAME by default.
  856. */
  857. virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
  858. /**
  859. Get the plugin label.@n
  860. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  861. */
  862. virtual const char* getLabel() const = 0;
  863. /**
  864. Get an extensive comment/description about the plugin.@n
  865. Optional, returns nothing by default.
  866. */
  867. virtual const char* getDescription() const { return ""; }
  868. /**
  869. Get the plugin author/maker.
  870. */
  871. virtual const char* getMaker() const = 0;
  872. /**
  873. Get the plugin homepage.@n
  874. Optional, returns nothing by default.
  875. */
  876. virtual const char* getHomePage() const { return ""; }
  877. /**
  878. Get the plugin license (a single line of text or a URL).@n
  879. For commercial plugins this should return some short copyright information.
  880. */
  881. virtual const char* getLicense() const = 0;
  882. /**
  883. Get the plugin version, in hexadecimal.
  884. @see d_version()
  885. */
  886. virtual uint32_t getVersion() const = 0;
  887. /**
  888. Get the plugin unique Id.@n
  889. This value is used by LADSPA, DSSI and VST plugin formats.
  890. @see d_cconst()
  891. */
  892. virtual int64_t getUniqueId() const = 0;
  893. /* --------------------------------------------------------------------------------------------------------
  894. * Init */
  895. /**
  896. Initialize the audio port @a index.@n
  897. This function will be called once, shortly after the plugin is created.
  898. */
  899. virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
  900. /**
  901. Initialize the parameter @a index.@n
  902. This function will be called once, shortly after the plugin is created.
  903. */
  904. virtual void initParameter(uint32_t index, Parameter& parameter);
  905. /**
  906. Initialize the port group @a groupId.@n
  907. This function will be called once,
  908. shortly after the plugin is created and all audio ports and parameters have been enumerated.
  909. */
  910. virtual void initPortGroup(uint32_t groupId, PortGroup& portGroup);
  911. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  912. /**
  913. Set the name of the program @a index.@n
  914. This function will be called once, shortly after the plugin is created.@n
  915. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  916. */
  917. virtual void initProgramName(uint32_t index, String& programName) = 0;
  918. #endif
  919. #if DISTRHO_PLUGIN_WANT_STATE
  920. /**
  921. Initialize the state @a index.@n
  922. This function will be called once, shortly after the plugin is created.@n
  923. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  924. */
  925. virtual void initState(uint32_t index, State& state);
  926. DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
  927. virtual void initState(uint32_t, String&, String&) {}
  928. DISTRHO_DEPRECATED_BY("initState(uint32_t,State&)")
  929. virtual bool isStateFile(uint32_t) { return false; }
  930. #endif
  931. /* --------------------------------------------------------------------------------------------------------
  932. * Internal data */
  933. /**
  934. Get the current value of a parameter.@n
  935. The host may call this function from any context, including realtime processing.
  936. */
  937. virtual float getParameterValue(uint32_t index) const;
  938. /**
  939. Change a parameter value.@n
  940. The host may call this function from any context, including realtime processing.@n
  941. When a parameter is marked as automatable, you must ensure no non-realtime operations are performed.
  942. @note This function will only be called for parameter inputs.
  943. */
  944. virtual void setParameterValue(uint32_t index, float value);
  945. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  946. /**
  947. Load a program.@n
  948. The host may call this function from any context, including realtime processing.@n
  949. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  950. */
  951. virtual void loadProgram(uint32_t index);
  952. #endif
  953. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  954. /**
  955. Get the value of an internal state.@n
  956. The host may call this function from any non-realtime context.@n
  957. Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
  958. @note The use of this function breaks compatibility with the DSSI format.
  959. */
  960. virtual String getState(const char* key) const;
  961. #endif
  962. #if DISTRHO_PLUGIN_WANT_STATE
  963. /**
  964. Change an internal state @a key to @a value.@n
  965. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  966. */
  967. virtual void setState(const char* key, const char* value);
  968. #endif
  969. /* --------------------------------------------------------------------------------------------------------
  970. * Audio/MIDI Processing */
  971. /**
  972. Activate this plugin.
  973. */
  974. virtual void activate() {}
  975. /**
  976. Deactivate this plugin.
  977. */
  978. virtual void deactivate() {}
  979. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  980. /**
  981. Run/process function for plugins with MIDI input.
  982. @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
  983. */
  984. virtual void run(const float** inputs, float** outputs, uint32_t frames,
  985. const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  986. #else
  987. /**
  988. Run/process function for plugins without MIDI input.
  989. @note Some parameters might be null if there are no audio inputs or outputs.
  990. */
  991. virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
  992. #endif
  993. /* --------------------------------------------------------------------------------------------------------
  994. * Callbacks (optional) */
  995. /**
  996. Optional callback to inform the plugin about a buffer size change.@n
  997. This function will only be called when the plugin is deactivated.
  998. @note This value is only a hint!@n
  999. Hosts might call run() with a higher or lower number of frames.
  1000. @see getBufferSize()
  1001. */
  1002. virtual void bufferSizeChanged(uint32_t newBufferSize);
  1003. /**
  1004. Optional callback to inform the plugin about a sample rate change.@n
  1005. This function will only be called when the plugin is deactivated.
  1006. @see getSampleRate()
  1007. */
  1008. virtual void sampleRateChanged(double newSampleRate);
  1009. // -------------------------------------------------------------------------------------------------------
  1010. private:
  1011. struct PrivateData;
  1012. PrivateData* const pData;
  1013. friend class PluginExporter;
  1014. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  1015. };
  1016. /** @} */
  1017. /* ------------------------------------------------------------------------------------------------------------
  1018. * Create plugin, entry point */
  1019. /**
  1020. @defgroup EntryPoints Entry Points
  1021. @{
  1022. */
  1023. /**
  1024. Create an instance of the Plugin class.@n
  1025. This is the entry point for DPF plugins.@n
  1026. DPF will call this to either create an instance of your plugin for the host
  1027. or to fetch some initial information for internal caching.
  1028. */
  1029. extern Plugin* createPlugin();
  1030. /** @} */
  1031. // -----------------------------------------------------------------------------------------------------------
  1032. END_NAMESPACE_DISTRHO
  1033. #endif // DISTRHO_PLUGIN_HPP_INCLUDED