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.

747 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. Default constructor for a null parameter.
  246. */
  247. Parameter() noexcept
  248. : hints(0x0),
  249. name(),
  250. symbol(),
  251. unit(),
  252. ranges() {}
  253. /**
  254. Constructor using custom values.
  255. */
  256. Parameter(uint32_t h, const char* n, const char* s, const char* u, float def, float min, float max) noexcept
  257. : hints(h),
  258. name(n),
  259. symbol(s),
  260. unit(u),
  261. ranges(def, min, max) {}
  262. };
  263. /**
  264. MIDI event.
  265. */
  266. struct MidiEvent {
  267. /**
  268. Size of internal data.
  269. */
  270. static const uint32_t kDataSize = 4;
  271. /**
  272. Time offset in frames.
  273. */
  274. uint32_t frame;
  275. /**
  276. Number of bytes used.
  277. */
  278. uint32_t size;
  279. /**
  280. MIDI data.@n
  281. If size > kDataSize, dataExt is used (otherwise null).
  282. */
  283. uint8_t data[kDataSize];
  284. const uint8_t* dataExt;
  285. };
  286. /**
  287. Time position.@n
  288. The @a playing and @a frame values are always valid.@n
  289. BBT values are only valid when @a bbt.valid is true.
  290. This struct is inspired by the JACK Transport API.
  291. */
  292. struct TimePosition {
  293. /**
  294. Wherever the host transport is playing/rolling.
  295. */
  296. bool playing;
  297. /**
  298. Current host transport position in frames.
  299. */
  300. uint64_t frame;
  301. /**
  302. Bar-Beat-Tick time position.
  303. */
  304. struct BarBeatTick {
  305. /**
  306. Wherever the host transport is using BBT.@n
  307. If false you must not read from this struct.
  308. */
  309. bool valid;
  310. /**
  311. Current bar.@n
  312. Should always be > 0.@n
  313. The first bar is bar '1'.
  314. */
  315. int32_t bar;
  316. /**
  317. Current beat within bar.@n
  318. Should always be > 0 and <= @a beatsPerBar.@n
  319. The first beat is beat '1'.
  320. */
  321. int32_t beat;
  322. /**
  323. Current tick within beat.@n
  324. Should always be > 0 and <= @a ticksPerBeat.@n
  325. The first tick is tick '0'.
  326. */
  327. int32_t tick;
  328. /**
  329. Number of ticks that have elapsed between frame 0 and the first beat of the current measure.
  330. */
  331. double barStartTick;
  332. /**
  333. Time signature "numerator".
  334. */
  335. float beatsPerBar;
  336. /**
  337. Time signature "denominator".
  338. */
  339. float beatType;
  340. /**
  341. Number of ticks within a bar.@n
  342. Usually a moderately large integer with many denominators, such as 1920.0.
  343. */
  344. double ticksPerBeat;
  345. /**
  346. Number of beats per minute.
  347. */
  348. double beatsPerMinute;
  349. /**
  350. Default constructor for a null BBT time position.
  351. */
  352. BarBeatTick() noexcept
  353. : valid(false),
  354. bar(0),
  355. beat(0),
  356. tick(0),
  357. barStartTick(0.0),
  358. beatsPerBar(0.0f),
  359. beatType(0.0f),
  360. ticksPerBeat(0.0),
  361. beatsPerMinute(0.0) {}
  362. } bbt;
  363. /**
  364. Default constructor for a time position.
  365. */
  366. TimePosition() noexcept
  367. : playing(false),
  368. frame(0),
  369. bbt() {}
  370. };
  371. /** @} */
  372. /* ------------------------------------------------------------------------------------------------------------
  373. * DPF Plugin */
  374. /**
  375. @defgroup MainClasses Main Classes
  376. @{
  377. */
  378. /**
  379. DPF Plugin class from where plugin instances are created.
  380. The public methods (Host state) are called from the plugin to get or set host information.@n
  381. They can be called from a plugin instance at anytime unless stated otherwise.@n
  382. All other methods are to be implemented by the plugin and will be called by the host.
  383. Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
  384. Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
  385. The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
  386. There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
  387. The buffer size and sample rate values will remain constant between activate and deactivate.@n
  388. Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
  389. Some of this class functions are only available according to some macros.
  390. DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
  391. When enabled you need to implement initProgramName() and loadProgram().
  392. DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
  393. When enabled you need to implement initStateKey() and setState().
  394. The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
  395. When enabled it provides midi input events.
  396. */
  397. class Plugin
  398. {
  399. public:
  400. /**
  401. Plugin class constructor.@n
  402. You must set all parameter values to their defaults, matching ParameterRanges::def.
  403. */
  404. Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
  405. /**
  406. Destructor.
  407. */
  408. virtual ~Plugin();
  409. /* --------------------------------------------------------------------------------------------------------
  410. * Host state */
  411. /**
  412. Get the current buffer size that will probably be used during processing, in frames.@n
  413. This value will remain constant between activate and deactivate.
  414. @note This value is only a hint!@n
  415. Hosts might call run() with a higher or lower number of frames.
  416. @see bufferSizeChanged(uint32_t)
  417. */
  418. uint32_t getBufferSize() const noexcept;
  419. /**
  420. Get the current sample rate that will be used during processing.@n
  421. This value will remain constant between activate and deactivate.
  422. @see sampleRateChanged(double)
  423. */
  424. double getSampleRate() const noexcept;
  425. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  426. /**
  427. Get the current host transport time position.@n
  428. This function should only be called during run().@n
  429. You can call this during other times, but the returned position is not guaranteed to be in sync.
  430. @note TimePosition is not supported in LADSPA and DSSI plugin formats.
  431. */
  432. const TimePosition& getTimePosition() const noexcept;
  433. #endif
  434. #if DISTRHO_PLUGIN_WANT_LATENCY
  435. /**
  436. Change the plugin audio output latency to @a frames.@n
  437. This function should only be called in the constructor, activate() and run().
  438. @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
  439. */
  440. void setLatency(uint32_t frames) noexcept;
  441. #endif
  442. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  443. /**
  444. Write a MIDI output event.@n
  445. This function must only be called during run().@n
  446. Returns false when the host buffer is full, in which case do not call this again until the next run().
  447. @note This function is not implemented yet!@n
  448. It's here so that developers can prepare MIDI plugins in advance.@n
  449. If you plan to use this, please report to DPF authos so it can be implemented.
  450. */
  451. bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
  452. #endif
  453. protected:
  454. /* --------------------------------------------------------------------------------------------------------
  455. * Information */
  456. /**
  457. Get the plugin name.@n
  458. Returns DISTRHO_PLUGIN_NAME by default.
  459. */
  460. virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
  461. /**
  462. Get the plugin label.@n
  463. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  464. */
  465. virtual const char* getLabel() const = 0;
  466. /**
  467. Get an extensive comment/description about the plugin.@n
  468. Optional, returns nothing by default.
  469. */
  470. virtual const char* getDescription() const { return ""; }
  471. /**
  472. Get the plugin author/maker.
  473. */
  474. virtual const char* getMaker() const = 0;
  475. /**
  476. Get the plugin homepage.@n
  477. Optional, returns nothing by default.
  478. */
  479. virtual const char* getHomePage() const { return ""; }
  480. /**
  481. Get the plugin license (a single line of text or a URL).@n
  482. For commercial plugins this should return some short copyright information.
  483. */
  484. virtual const char* getLicense() const = 0;
  485. /**
  486. Get the plugin version, in hexadecimal.
  487. @see d_version()
  488. */
  489. virtual uint32_t getVersion() const = 0;
  490. /**
  491. Get the plugin unique Id.@n
  492. This value is used by LADSPA, DSSI and VST plugin formats.
  493. @see d_cconst()
  494. */
  495. virtual int64_t getUniqueId() const = 0;
  496. /* --------------------------------------------------------------------------------------------------------
  497. * Init */
  498. /**
  499. Initialize the audio port @a index.@n
  500. This function will be called once, shortly after the plugin is created.
  501. */
  502. virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
  503. /**
  504. Initialize the parameter @a index.@n
  505. This function will be called once, shortly after the plugin is created.
  506. */
  507. virtual void initParameter(uint32_t index, Parameter& parameter) = 0;
  508. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  509. /**
  510. Set the name of the program @a index.@n
  511. This function will be called once, shortly after the plugin is created.@n
  512. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  513. */
  514. virtual void initProgramName(uint32_t index, String& programName) = 0;
  515. #endif
  516. #if DISTRHO_PLUGIN_WANT_STATE
  517. /**
  518. Set the state key and default value of @a index.@n
  519. This function will be called once, shortly after the plugin is created.@n
  520. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  521. */
  522. virtual void initState(uint32_t index, String& stateKey, String& defaultStateValue) = 0;
  523. #endif
  524. /* --------------------------------------------------------------------------------------------------------
  525. * Internal data */
  526. /**
  527. Get the current value of a parameter.@n
  528. The host may call this function from any context, including realtime processing.
  529. */
  530. virtual float getParameterValue(uint32_t index) const = 0;
  531. /**
  532. Change a parameter value.@n
  533. The host may call this function from any context, including realtime processing.@n
  534. When a parameter is marked as automable, you must ensure no non-realtime operations are performed.
  535. @note This function will only be called for parameter inputs.
  536. */
  537. virtual void setParameterValue(uint32_t index, float value) = 0;
  538. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  539. /**
  540. Load a program.@n
  541. The host may call this function from any context, including realtime processing.@n
  542. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  543. */
  544. virtual void loadProgram(uint32_t index) = 0;
  545. #endif
  546. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  547. /**
  548. Get the value of an internal state.@n
  549. The host may call this function from any non-realtime context.@n
  550. Must be implemented by your plugin class if DISTRHO_PLUGIN_WANT_PROGRAMS or DISTRHO_PLUGIN_WANT_FULL_STATE is enabled.
  551. @note The use of this function breaks compatibility with the DSSI format.
  552. */
  553. virtual String getState(const char* key) const = 0;
  554. #endif
  555. #if DISTRHO_PLUGIN_WANT_STATE
  556. /**
  557. Change an internal state @a key to @a value.@n
  558. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  559. */
  560. virtual void setState(const char* key, const char* value) = 0;
  561. #endif
  562. /* --------------------------------------------------------------------------------------------------------
  563. * Audio/MIDI Processing */
  564. /**
  565. Activate this plugin.
  566. */
  567. virtual void activate() {}
  568. /**
  569. Deactivate this plugin.
  570. */
  571. virtual void deactivate() {}
  572. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  573. /**
  574. Run/process function for plugins with MIDI input.
  575. @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
  576. */
  577. virtual void run(const float** inputs, float** outputs, uint32_t frames,
  578. const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  579. #else
  580. /**
  581. Run/process function for plugins without MIDI input.
  582. @note Some parameters might be null if there are no audio inputs or outputs.
  583. */
  584. virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
  585. #endif
  586. /* --------------------------------------------------------------------------------------------------------
  587. * Callbacks (optional) */
  588. /**
  589. Optional callback to inform the plugin about a buffer size change.@n
  590. This function will only be called when the plugin is deactivated.
  591. @note This value is only a hint!@n
  592. Hosts might call run() with a higher or lower number of frames.
  593. @see getBufferSize()
  594. */
  595. virtual void bufferSizeChanged(uint32_t newBufferSize);
  596. /**
  597. Optional callback to inform the plugin about a sample rate change.@n
  598. This function will only be called when the plugin is deactivated.
  599. @see getSampleRate()
  600. */
  601. virtual void sampleRateChanged(double newSampleRate);
  602. // -------------------------------------------------------------------------------------------------------
  603. private:
  604. struct PrivateData;
  605. PrivateData* const pData;
  606. friend class PluginExporter;
  607. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  608. };
  609. /** @} */
  610. /* ------------------------------------------------------------------------------------------------------------
  611. * Create plugin, entry point */
  612. /**
  613. @defgroup EntryPoints Entry Points
  614. @{
  615. */
  616. /**
  617. TODO.
  618. */
  619. extern Plugin* createPlugin();
  620. /** @} */
  621. // -----------------------------------------------------------------------------------------------------------
  622. END_NAMESPACE_DISTRHO
  623. #endif // DISTRHO_PLUGIN_HPP_INCLUDED