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.

570 lines
13KB

  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 characters, 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. This struct is inspired by the JACK API.
  237. The @a playing and @a frame values are always valid.
  238. BBT values are only valid when @a bbt.valid is true.
  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. TODO.
  323. */
  324. class Plugin
  325. {
  326. public:
  327. /**
  328. TODO.
  329. */
  330. Plugin(const uint32_t parameterCount, const uint32_t programCount, const uint32_t stateCount);
  331. /**
  332. Destructor.
  333. */
  334. virtual ~Plugin();
  335. // -------------------------------------------------------------------
  336. // Host state
  337. /**
  338. TODO.
  339. */
  340. uint32_t d_getBufferSize() const noexcept;
  341. /**
  342. TODO.
  343. */
  344. double d_getSampleRate() const noexcept;
  345. /**
  346. TODO.
  347. */
  348. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  349. /**
  350. TODO.
  351. */
  352. const TimePos& d_getTimePos() const noexcept;
  353. #endif
  354. #if DISTRHO_PLUGIN_WANT_LATENCY
  355. /**
  356. TODO.
  357. */
  358. void d_setLatency(uint32_t frames) noexcept;
  359. #endif
  360. protected:
  361. // -------------------------------------------------------------------
  362. // Information
  363. /**
  364. TODO.
  365. */
  366. virtual const char* d_getName() const noexcept { return DISTRHO_PLUGIN_NAME; }
  367. /**
  368. TODO.
  369. */
  370. virtual const char* d_getLabel() const noexcept = 0;
  371. /**
  372. TODO.
  373. */
  374. virtual const char* d_getMaker() const noexcept = 0;
  375. /**
  376. TODO.
  377. */
  378. virtual const char* d_getLicense() const noexcept = 0;
  379. /**
  380. TODO.
  381. */
  382. virtual uint32_t d_getVersion() const noexcept = 0;
  383. /**
  384. TODO.
  385. */
  386. virtual long d_getUniqueId() const noexcept = 0;
  387. // -------------------------------------------------------------------
  388. // Init
  389. /**
  390. TODO.
  391. */
  392. virtual void d_initParameter(uint32_t index, Parameter& parameter) = 0;
  393. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  394. /**
  395. TODO.
  396. */
  397. virtual void d_initProgramName(uint32_t index, d_string& programName) = 0;
  398. #endif
  399. #if DISTRHO_PLUGIN_WANT_STATE
  400. /**
  401. TODO.
  402. */
  403. virtual void d_initStateKey(uint32_t index, d_string& stateKey) = 0;
  404. #endif
  405. // -------------------------------------------------------------------
  406. // Internal data
  407. /**
  408. TODO.
  409. */
  410. virtual float d_getParameterValue(uint32_t index) const = 0;
  411. /**
  412. TODO.
  413. */
  414. virtual void d_setParameterValue(uint32_t index, float value) = 0;
  415. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  416. /**
  417. TODO.
  418. */
  419. virtual void d_setProgram(uint32_t index) = 0;
  420. #endif
  421. #if DISTRHO_PLUGIN_WANT_STATE
  422. /**
  423. TODO.
  424. */
  425. virtual void d_setState(const char* key, const char* value) = 0;
  426. #endif
  427. // -------------------------------------------------------------------
  428. // Process
  429. /**
  430. TODO.
  431. */
  432. virtual void d_activate() {}
  433. /**
  434. TODO.
  435. */
  436. virtual void d_deactivate() {}
  437. #if DISTRHO_PLUGIN_IS_SYNTH
  438. /**
  439. TODO.
  440. */
  441. virtual void d_run(const float** inputs, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  442. #else
  443. /**
  444. TODO.
  445. */
  446. virtual void d_run(const float** inputs, float** outputs, uint32_t frames) = 0;
  447. #endif
  448. // -------------------------------------------------------------------
  449. // Callbacks (optional)
  450. /**
  451. TODO.
  452. */
  453. virtual void d_bufferSizeChanged(uint32_t newBufferSize);
  454. /**
  455. TODO.
  456. */
  457. virtual void d_sampleRateChanged(double newSampleRate);
  458. // -------------------------------------------------------------------
  459. private:
  460. struct PrivateData;
  461. PrivateData* const pData;
  462. friend class PluginExporter;
  463. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  464. };
  465. /* ------------------------------------------------------------------------------------------------------------
  466. * Create plugin, entry point */
  467. /**
  468. TODO.
  469. */
  470. extern Plugin* createPlugin();
  471. /* ------------------------------------------------------------------------------------------------------------ */
  472. END_NAMESPACE_DISTRHO
  473. #endif // DISTRHO_PLUGIN_HPP_INCLUDED