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.

721 lines
20KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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. */
  448. bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
  449. #endif
  450. protected:
  451. /* --------------------------------------------------------------------------------------------------------
  452. * Information */
  453. /**
  454. Get the plugin name.@n
  455. Returns DISTRHO_PLUGIN_NAME by default.
  456. */
  457. virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
  458. /**
  459. Get the plugin label.@n
  460. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  461. */
  462. virtual const char* getLabel() const = 0;
  463. /**
  464. Get the plugin author/maker.
  465. */
  466. virtual const char* getMaker() const = 0;
  467. /**
  468. Get the plugin license name (a single line of text).@n
  469. For commercial plugins this should return some short copyright information.
  470. */
  471. virtual const char* getLicense() const = 0;
  472. /**
  473. Get the plugin version, in hexadecimal.@n
  474. TODO format to be defined
  475. */
  476. virtual uint32_t getVersion() const = 0;
  477. /**
  478. Get the plugin unique Id.@n
  479. This value is used by LADSPA, DSSI and VST plugin formats.
  480. */
  481. virtual int64_t getUniqueId() const = 0;
  482. /* --------------------------------------------------------------------------------------------------------
  483. * Init */
  484. /**
  485. Initialize the audio port @a index.@n
  486. This function will be called once, shortly after the plugin is created.
  487. */
  488. virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
  489. /**
  490. Initialize the parameter @a index.@n
  491. This function will be called once, shortly after the plugin is created.
  492. */
  493. virtual void initParameter(uint32_t index, Parameter& parameter) = 0;
  494. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  495. /**
  496. Set the name of the program @a index.@n
  497. This function will be called once, shortly after the plugin is created.@n
  498. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  499. */
  500. virtual void initProgramName(uint32_t index, String& programName) = 0;
  501. #endif
  502. #if DISTRHO_PLUGIN_WANT_STATE
  503. /**
  504. Set the state key and default value of @a index.@n
  505. This function will be called once, shortly after the plugin is created.@n
  506. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  507. */
  508. virtual void initState(uint32_t index, String& stateKey, String& defaultStateValue) = 0;
  509. #endif
  510. /* --------------------------------------------------------------------------------------------------------
  511. * Internal data */
  512. /**
  513. Get the current value of a parameter.@n
  514. The host may call this function from any context, including realtime processing.
  515. */
  516. virtual float getParameterValue(uint32_t index) const = 0;
  517. /**
  518. Change a parameter value.@n
  519. The host may call this function from any context, including realtime processing.@n
  520. When a parameter is marked as automable, you must ensure no non-realtime operations are performed.
  521. @note This function will only be called for parameter inputs.
  522. */
  523. virtual void setParameterValue(uint32_t index, float value) = 0;
  524. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  525. /**
  526. Load a program.@n
  527. The host may call this function from any context, including realtime processing.@n
  528. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  529. */
  530. virtual void loadProgram(uint32_t index) = 0;
  531. #endif
  532. #if DISTRHO_PLUGIN_WANT_STATE
  533. /**
  534. Change an internal state @a key to @a value.@n
  535. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  536. */
  537. virtual void setState(const char* key, const char* value) = 0;
  538. #endif
  539. /* --------------------------------------------------------------------------------------------------------
  540. * Audio/MIDI Processing */
  541. /**
  542. Activate this plugin.
  543. */
  544. virtual void activate() {}
  545. /**
  546. Deactivate this plugin.
  547. */
  548. virtual void deactivate() {}
  549. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  550. /**
  551. Run/process function for plugins with MIDI input.
  552. @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
  553. */
  554. virtual void run(const float** inputs, float** outputs, uint32_t frames,
  555. const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  556. #else
  557. /**
  558. Run/process function for plugins without MIDI input.
  559. @note Some parameters might be null if there are no audio inputs or outputs.
  560. */
  561. virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
  562. #endif
  563. /* --------------------------------------------------------------------------------------------------------
  564. * Callbacks (optional) */
  565. /**
  566. Optional callback to inform the plugin about a buffer size change.@n
  567. This function will only be called when the plugin is deactivated.
  568. @note This value is only a hint!@n
  569. Hosts might call run() with a higher or lower number of frames.
  570. @see getBufferSize()
  571. */
  572. virtual void bufferSizeChanged(uint32_t newBufferSize);
  573. /**
  574. Optional callback to inform the plugin about a sample rate change.@n
  575. This function will only be called when the plugin is deactivated.
  576. @see getSampleRate()
  577. */
  578. virtual void sampleRateChanged(double newSampleRate);
  579. // -------------------------------------------------------------------------------------------------------
  580. private:
  581. struct PrivateData;
  582. PrivateData* const pData;
  583. friend class PluginExporter;
  584. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  585. };
  586. /** @} */
  587. /* ------------------------------------------------------------------------------------------------------------
  588. * Create plugin, entry point */
  589. /**
  590. @defgroup EntryPoints Entry Points
  591. @{
  592. */
  593. /**
  594. TODO.
  595. */
  596. extern Plugin* createPlugin();
  597. /** @} */
  598. // -----------------------------------------------------------------------------------------------------------
  599. END_NAMESPACE_DISTRHO
  600. #endif // DISTRHO_PLUGIN_HPP_INCLUDED