DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

710 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 "src/DistrhoPluginChecks.h"
  20. START_NAMESPACE_DISTRHO
  21. /* ------------------------------------------------------------------------------------------------------------
  22. * Audio Port Hints */
  23. /**
  24. @defgroup AudioPortHints Audio Port Hints
  25. Various audio port hints.
  26. @see AudioPort::hints
  27. @{
  28. */
  29. /**
  30. Audio port can be used as control voltage (LV2 only).
  31. */
  32. static const uint32_t kAudioPortIsCV = 0x1;
  33. /**
  34. Audio port should be used as sidechan (LV2 only).
  35. */
  36. static const uint32_t kAudioPortIsSidechain = 0x2;
  37. /** @} */
  38. /* ------------------------------------------------------------------------------------------------------------
  39. * Parameter Hints */
  40. /**
  41. @defgroup ParameterHints Parameter Hints
  42. Various parameter hints.
  43. @see Parameter::hints
  44. @{
  45. */
  46. /**
  47. Parameter is automable (real-time safe).
  48. @see Plugin::setParameterValue(uint32_t, float)
  49. */
  50. static const uint32_t kParameterIsAutomable = 0x01;
  51. /**
  52. Parameter value is boolean.@n
  53. It's always at either minimum or maximum value.
  54. */
  55. static const uint32_t kParameterIsBoolean = 0x02;
  56. /**
  57. Parameter value is integer.
  58. */
  59. static const uint32_t kParameterIsInteger = 0x04;
  60. /**
  61. Parameter value is logarithmic.
  62. */
  63. static const uint32_t kParameterIsLogarithmic = 0x08;
  64. /**
  65. Parameter is of output type.@n
  66. When unset, parameter is assumed to be of input type.
  67. Parameter inputs are changed by the host and must not be changed by the plugin.@n
  68. The only exception being when changing programs, see Plugin::loadProgram().@n
  69. Outputs are changed by the plugin and never modified by the host.
  70. */
  71. static const uint32_t kParameterIsOutput = 0x10;
  72. /** @} */
  73. /* ------------------------------------------------------------------------------------------------------------
  74. * Base Plugin structs */
  75. /**
  76. @defgroup BasePluginStructs Base Plugin Structs
  77. @{
  78. */
  79. /**
  80. Audio Port.
  81. */
  82. struct AudioPort {
  83. /**
  84. Hints describing this audio port.
  85. @see AudioPortHints
  86. */
  87. uint32_t hints;
  88. /**
  89. The name of this audio port.@n
  90. An audio port name can contain any character, but hosts might have a hard time with non-ascii ones.@n
  91. The name doesn't have to be unique within a plugin instance, but it's recommended.
  92. */
  93. String name;
  94. /**
  95. The symbol of this audio port.@n
  96. An audio port symbol is a short restricted name used as a machine and human readable identifier.@n
  97. 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.
  98. @note Audio port and parameter symbols MUST be unique within a plugin instance.
  99. */
  100. String symbol;
  101. /**
  102. Default constructor for a regular audio port.
  103. */
  104. AudioPort() noexcept
  105. : hints(0x0),
  106. name(),
  107. symbol() {}
  108. };
  109. /**
  110. Parameter ranges.@n
  111. This is used to set the default, minimum and maximum values of a parameter.
  112. By default a parameter has 0.0 as minimum, 1.0 as maximum and 0.0 as default.@n
  113. When changing this struct values you must ensure maximum > minimum and default is within range.
  114. */
  115. struct ParameterRanges {
  116. /**
  117. Default value.
  118. */
  119. float def;
  120. /**
  121. Minimum value.
  122. */
  123. float min;
  124. /**
  125. Maximum value.
  126. */
  127. float max;
  128. /**
  129. Default constructor, using 0.0 as minimum, 1.0 as maximum and 0.0 as default.
  130. */
  131. ParameterRanges() noexcept
  132. : def(0.0f),
  133. min(0.0f),
  134. max(1.0f) {}
  135. /**
  136. Constructor using custom values.
  137. */
  138. ParameterRanges(float df, float mn, float mx) noexcept
  139. : def(df),
  140. min(mn),
  141. max(mx) {}
  142. /**
  143. Fix the default value within range.
  144. */
  145. void fixDefault() noexcept
  146. {
  147. fixValue(def);
  148. }
  149. /**
  150. Fix a value within range.
  151. */
  152. void fixValue(float& value) const noexcept
  153. {
  154. if (value < min)
  155. value = min;
  156. else if (value > max)
  157. value = max;
  158. }
  159. /**
  160. Get a fixed value within range.
  161. */
  162. const float& getFixedValue(const float& value) const noexcept
  163. {
  164. if (value <= min)
  165. return min;
  166. if (value >= max)
  167. return max;
  168. return value;
  169. }
  170. /**
  171. Get a value normalized to 0.0<->1.0.
  172. */
  173. float getNormalizedValue(const float& value) const noexcept
  174. {
  175. const float normValue((value - min) / (max - min));
  176. if (normValue <= 0.0f)
  177. return 0.0f;
  178. if (normValue >= 1.0f)
  179. return 1.0f;
  180. return normValue;
  181. }
  182. /**
  183. Get a value normalized to 0.0<->1.0, fixed within range.
  184. */
  185. float getFixedAndNormalizedValue(const float& value) const noexcept
  186. {
  187. if (value <= min)
  188. return 0.0f;
  189. if (value >= max)
  190. return 1.0f;
  191. const float normValue((value - min) / (max - min));
  192. if (normValue <= 0.0f)
  193. return 0.0f;
  194. if (normValue >= 1.0f)
  195. return 1.0f;
  196. return normValue;
  197. }
  198. /**
  199. Get a proper value previously normalized to 0.0<->1.0.
  200. */
  201. float getUnnormalizedValue(const float& value) const noexcept
  202. {
  203. if (value <= 0.0f)
  204. return min;
  205. if (value >= 1.0f)
  206. return max;
  207. return value * (max - min) + min;
  208. }
  209. };
  210. /**
  211. Parameter.
  212. */
  213. struct Parameter {
  214. /**
  215. Hints describing this parameter.
  216. @see ParameterHints
  217. */
  218. uint32_t hints;
  219. /**
  220. The name of this parameter.@n
  221. A parameter name can contain any character, but hosts might have a hard time with non-ascii ones.@n
  222. The name doesn't have to be unique within a plugin instance, but it's recommended.
  223. */
  224. String name;
  225. /**
  226. The symbol of this parameter.@n
  227. A parameter symbol is a short restricted name used as a machine and human readable identifier.@n
  228. 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.
  229. @note Parameter symbols MUST be unique within a plugin instance.
  230. */
  231. String symbol;
  232. /**
  233. The unit of this parameter.@n
  234. This means something like "dB", "kHz" and "ms".@n
  235. Can be left blank if a unit does not apply to this parameter.
  236. */
  237. String unit;
  238. /**
  239. Ranges of this parameter.@n
  240. The ranges describe the default, minimum and maximum values.
  241. */
  242. ParameterRanges ranges;
  243. /**
  244. Default constructor for a null parameter.
  245. */
  246. Parameter() noexcept
  247. : hints(0x0),
  248. name(),
  249. symbol(),
  250. unit(),
  251. ranges() {}
  252. };
  253. /**
  254. MIDI event.
  255. */
  256. struct MidiEvent {
  257. /**
  258. Size of internal data.
  259. */
  260. static const uint32_t kDataSize = 4;
  261. /**
  262. Time offset in frames.
  263. */
  264. uint32_t frame;
  265. /**
  266. Number of bytes used.
  267. */
  268. uint32_t size;
  269. /**
  270. MIDI data.@n
  271. If size > kDataSize, dataExt is used (otherwise null).
  272. */
  273. uint8_t data[kDataSize];
  274. const uint8_t* dataExt;
  275. };
  276. /**
  277. Time position.@n
  278. The @a playing and @a frame values are always valid.@n
  279. BBT values are only valid when @a bbt.valid is true.
  280. This struct is inspired by the JACK Transport API.
  281. */
  282. struct TimePosition {
  283. /**
  284. Wherever the host transport is playing/rolling.
  285. */
  286. bool playing;
  287. /**
  288. Current host transport position in frames.
  289. */
  290. uint64_t frame;
  291. /**
  292. Bar-Beat-Tick time position.
  293. */
  294. struct BarBeatTick {
  295. /**
  296. Wherever the host transport is using BBT.@n
  297. If false you must not read from this struct.
  298. */
  299. bool valid;
  300. /**
  301. Current bar.@n
  302. Should always be > 0.@n
  303. The first bar is bar '1'.
  304. */
  305. int32_t bar;
  306. /**
  307. Current beat within bar.@n
  308. Should always be > 0 and <= @a beatsPerBar.@n
  309. The first beat is beat '1'.
  310. */
  311. int32_t beat;
  312. /**
  313. Current tick within beat.@n
  314. Should always be > 0 and <= @a ticksPerBeat.@n
  315. The first tick is tick '0'.
  316. */
  317. int32_t tick;
  318. /**
  319. Number of ticks that have elapsed between frame 0 and the first beat of the current measure.
  320. */
  321. double barStartTick;
  322. /**
  323. Time signature "numerator".
  324. */
  325. float beatsPerBar;
  326. /**
  327. Time signature "denominator".
  328. */
  329. float beatType;
  330. /**
  331. Number of ticks within a bar.@n
  332. Usually a moderately large integer with many denominators, such as 1920.0.
  333. */
  334. double ticksPerBeat;
  335. /**
  336. Number of beats per minute.
  337. */
  338. double beatsPerMinute;
  339. /**
  340. Default constructor for a null BBT time position.
  341. */
  342. BarBeatTick() noexcept
  343. : valid(false),
  344. bar(0),
  345. beat(0),
  346. tick(0),
  347. barStartTick(0.0),
  348. beatsPerBar(0.0f),
  349. beatType(0.0f),
  350. ticksPerBeat(0.0),
  351. beatsPerMinute(0.0) {}
  352. } bbt;
  353. /**
  354. Default constructor for a time position.
  355. */
  356. TimePosition() noexcept
  357. : playing(false),
  358. frame(0),
  359. bbt() {}
  360. };
  361. /** @} */
  362. /* ------------------------------------------------------------------------------------------------------------
  363. * DPF Plugin */
  364. /**
  365. @defgroup MainClasses Main Classes
  366. @{
  367. */
  368. /**
  369. DPF Plugin class from where plugin instances are created.
  370. The public methods (Host state) are called from the plugin to get or set host information.@n
  371. They can be called from a plugin instance at anytime unless stated otherwise.@n
  372. All other methods are to be implemented by the plugin and will be called by the host.
  373. Shortly after a plugin instance is created, the various init* functions will be called by the host.@n
  374. Host will call activate() before run(), and deactivate() before the plugin instance is destroyed.@n
  375. The host may call deactivate right after activate and vice-versa, but never activate/deactivate consecutively.@n
  376. There is no limit on how many times run() is called, only that activate/deactivate will be called in between.
  377. The buffer size and sample rate values will remain constant between activate and deactivate.@n
  378. Buffer size is only a hint though, the host might call run() with a higher or lower number of frames.
  379. Some of this class functions are only available according to some macros.
  380. DISTRHO_PLUGIN_WANT_PROGRAMS activates program related features.@n
  381. When enabled you need to implement initProgramName() and loadProgram().
  382. DISTRHO_PLUGIN_WANT_STATE activates internal state features.@n
  383. When enabled you need to implement initStateKey() and setState().
  384. The process function run() changes wherever DISTRHO_PLUGIN_WANT_MIDI_INPUT is enabled or not.@n
  385. When enabled it provides midi input events.
  386. */
  387. class Plugin
  388. {
  389. public:
  390. /**
  391. Plugin class constructor.@n
  392. You must set all parameter values to their defaults, matching ParameterRanges::def.
  393. */
  394. Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
  395. /**
  396. Destructor.
  397. */
  398. virtual ~Plugin();
  399. /* --------------------------------------------------------------------------------------------------------
  400. * Host state */
  401. /**
  402. Get the current buffer size that will probably be used during processing, in frames.@n
  403. This value will remain constant between activate and deactivate.
  404. @note This value is only a hint!@n
  405. Hosts might call run() with a higher or lower number of frames.
  406. @see bufferSizeChanged(uint32_t)
  407. */
  408. uint32_t getBufferSize() const noexcept;
  409. /**
  410. Get the current sample rate that will be used during processing.@n
  411. This value will remain constant between activate and deactivate.
  412. @see sampleRateChanged(double)
  413. */
  414. double getSampleRate() const noexcept;
  415. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  416. /**
  417. Get the current host transport time position.@n
  418. This function should only be called during run().@n
  419. You can call this during other times, but the returned position is not guaranteed to be in sync.
  420. @note TimePosition is not supported in LADSPA and DSSI plugin formats.
  421. */
  422. const TimePosition& getTimePosition() const noexcept;
  423. #endif
  424. #if DISTRHO_PLUGIN_WANT_LATENCY
  425. /**
  426. Change the plugin audio output latency to @a frames.@n
  427. This function should only be called in the constructor, activate() and run().
  428. @note This function is only available if DISTRHO_PLUGIN_WANT_LATENCY is enabled.
  429. */
  430. void setLatency(uint32_t frames) noexcept;
  431. #endif
  432. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  433. /**
  434. Write a MIDI output event.@n
  435. This function must only be called during run().@n
  436. Returns false when the host buffer is full, in which case do not call this again until the next run().
  437. */
  438. bool writeMidiEvent(const MidiEvent& midiEvent) noexcept;
  439. #endif
  440. protected:
  441. /* --------------------------------------------------------------------------------------------------------
  442. * Information */
  443. /**
  444. Get the plugin name.@n
  445. Returns DISTRHO_PLUGIN_NAME by default.
  446. */
  447. virtual const char* getName() const { return DISTRHO_PLUGIN_NAME; }
  448. /**
  449. Get the plugin label.@n
  450. This label is a short restricted name consisting of only _, a-z, A-Z and 0-9 characters.
  451. */
  452. virtual const char* getLabel() const = 0;
  453. /**
  454. Get the plugin author/maker.
  455. */
  456. virtual const char* getMaker() const = 0;
  457. /**
  458. Get the plugin license name (a single line of text).@n
  459. For commercial plugins this should return some short copyright information.
  460. */
  461. virtual const char* getLicense() const = 0;
  462. /**
  463. Get the plugin version, in hexadecimal.@n
  464. TODO format to be defined
  465. */
  466. virtual uint32_t getVersion() const = 0;
  467. /**
  468. Get the plugin unique Id.@n
  469. This value is used by LADSPA, DSSI and VST plugin formats.
  470. */
  471. virtual int64_t getUniqueId() const = 0;
  472. /* --------------------------------------------------------------------------------------------------------
  473. * Init */
  474. /**
  475. Initialize the audio port @a index.@n
  476. This function will be called once, shortly after the plugin is created.
  477. */
  478. virtual void initAudioPort(bool input, uint32_t index, AudioPort& port);
  479. /**
  480. Initialize the parameter @a index.@n
  481. This function will be called once, shortly after the plugin is created.
  482. */
  483. virtual void initParameter(uint32_t index, Parameter& parameter) = 0;
  484. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  485. /**
  486. Set the name of the program @a index.@n
  487. This function will be called once, shortly after the plugin is created.@n
  488. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  489. */
  490. virtual void initProgramName(uint32_t index, String& programName) = 0;
  491. #endif
  492. #if DISTRHO_PLUGIN_WANT_STATE
  493. /**
  494. Set the state key and default value of @a index.@n
  495. This function will be called once, shortly after the plugin is created.@n
  496. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  497. */
  498. virtual void initState(uint32_t index, String& stateKey, String& defaultStateValue) = 0;
  499. #endif
  500. /* --------------------------------------------------------------------------------------------------------
  501. * Internal data */
  502. /**
  503. Get the current value of a parameter.@n
  504. The host may call this function from any context, including realtime processing.
  505. */
  506. virtual float getParameterValue(uint32_t index) const = 0;
  507. /**
  508. Change a parameter value.@n
  509. The host may call this function from any context, including realtime processing.@n
  510. When a parameter is marked as automable, you must ensure no non-realtime operations are performed.
  511. @note This function will only be called for parameter inputs.
  512. */
  513. virtual void setParameterValue(uint32_t index, float value) = 0;
  514. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  515. /**
  516. Load a program.@n
  517. The host may call this function from any context, including realtime processing.@n
  518. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_PROGRAMS is enabled.
  519. */
  520. virtual void loadProgram(uint32_t index) = 0;
  521. #endif
  522. #if DISTRHO_PLUGIN_WANT_STATE
  523. /**
  524. Change an internal state @a key to @a value.@n
  525. Must be implemented by your plugin class only if DISTRHO_PLUGIN_WANT_STATE is enabled.
  526. */
  527. virtual void setState(const String& key, const String& value) = 0;
  528. #endif
  529. /* --------------------------------------------------------------------------------------------------------
  530. * Audio/MIDI Processing */
  531. /**
  532. Activate this plugin.
  533. */
  534. virtual void activate() {}
  535. /**
  536. Deactivate this plugin.
  537. */
  538. virtual void deactivate() {}
  539. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  540. /**
  541. Run/process function for plugins with MIDI input.
  542. @note Some parameters might be null if there are no audio inputs/outputs or MIDI events.
  543. */
  544. virtual void run(const float** inputs, float** outputs, uint32_t frames,
  545. const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  546. #else
  547. /**
  548. Run/process function for plugins without MIDI input.
  549. @note Some parameters might be null if there are no audio inputs or outputs.
  550. */
  551. virtual void run(const float** inputs, float** outputs, uint32_t frames) = 0;
  552. #endif
  553. /* --------------------------------------------------------------------------------------------------------
  554. * Callbacks (optional) */
  555. /**
  556. Optional callback to inform the plugin about a buffer size change.@n
  557. This function will only be called when the plugin is deactivated.
  558. @note This value is only a hint!@n
  559. Hosts might call run() with a higher or lower number of frames.
  560. @see getBufferSize()
  561. */
  562. virtual void bufferSizeChanged(uint32_t newBufferSize);
  563. /**
  564. Optional callback to inform the plugin about a sample rate change.@n
  565. This function will only be called when the plugin is deactivated.
  566. @see getSampleRate()
  567. */
  568. virtual void sampleRateChanged(double newSampleRate);
  569. // -------------------------------------------------------------------------------------------------------
  570. private:
  571. struct PrivateData;
  572. PrivateData* const pData;
  573. friend class PluginExporter;
  574. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  575. };
  576. /** @} */
  577. /* ------------------------------------------------------------------------------------------------------------
  578. * Create plugin, entry point */
  579. /**
  580. @defgroup EntryPoints Entry Points
  581. @{
  582. */
  583. /**
  584. TODO.
  585. */
  586. extern Plugin* createPlugin();
  587. /** @} */
  588. // -----------------------------------------------------------------------------------------------------------
  589. END_NAMESPACE_DISTRHO
  590. #endif // DISTRHO_PLUGIN_HPP_INCLUDED