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.

637 lines
18KB

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