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.

319 lines
8.2KB

  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 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. const uint32_t PARAMETER_IS_AUTOMABLE = 1 << 0;
  33. const uint32_t PARAMETER_IS_BOOLEAN = 1 << 1;
  34. const uint32_t PARAMETER_IS_INTEGER = 1 << 2;
  35. const uint32_t PARAMETER_IS_LOGARITHMIC = 1 << 3;
  36. const uint32_t PARAMETER_IS_OUTPUT = 1 << 4;
  37. // -----------------------------------------------------------------------
  38. // Parameter Ranges
  39. struct ParameterRanges {
  40. float def;
  41. float min;
  42. float max;
  43. ParameterRanges() noexcept
  44. : def(0.0f),
  45. min(0.0f),
  46. max(1.0f) {}
  47. ParameterRanges(float def, float min, float max) noexcept
  48. {
  49. this->def = def;
  50. this->min = min;
  51. this->max = max;
  52. }
  53. void clear() noexcept
  54. {
  55. def = 0.0f;
  56. min = 0.0f;
  57. max = 1.0f;
  58. }
  59. /*!
  60. * Fix default value within range.
  61. */
  62. void fixDefault() noexcept
  63. {
  64. fixValue(def);
  65. }
  66. /*!
  67. * Fix a value within range.
  68. */
  69. void fixValue(float& value) const noexcept
  70. {
  71. if (value <= min)
  72. value = min;
  73. else if (value > max)
  74. value = max;
  75. }
  76. /*!
  77. * Get a fixed value within range.
  78. */
  79. float getFixedValue(const float& value) const noexcept
  80. {
  81. if (value <= min)
  82. return min;
  83. if (value >= max)
  84. return max;
  85. return value;
  86. }
  87. /*!
  88. * Get a value normalized to 0.0<->1.0.
  89. */
  90. float getNormalizedValue(const float& value) const noexcept
  91. {
  92. const float normValue((value - min) / (max - min));
  93. if (normValue <= 0.0f)
  94. return 0.0f;
  95. if (normValue >= 1.0f)
  96. return 1.0f;
  97. return normValue;
  98. }
  99. /*!
  100. * Get a value normalized to 0.0<->1.0, fixed within range.
  101. */
  102. float getFixedAndNormalizedValue(const float& value) const noexcept
  103. {
  104. if (value <= min)
  105. return 0.0f;
  106. if (value >= max)
  107. return 1.0f;
  108. const float normValue((value - min) / (max - min));
  109. if (normValue <= 0.0f)
  110. return 0.0f;
  111. if (normValue >= 1.0f)
  112. return 1.0f;
  113. return normValue;
  114. }
  115. /*!
  116. * Get a proper value previously normalized to 0.0<->1.0.
  117. */
  118. float getUnnormalizedValue(const float& value) const noexcept
  119. {
  120. return value * (max - min) + min;
  121. }
  122. };
  123. // -----------------------------------------------------------------------
  124. // Parameter
  125. struct Parameter {
  126. uint32_t hints;
  127. d_string name;
  128. d_string symbol;
  129. d_string unit;
  130. ParameterRanges ranges;
  131. Parameter() noexcept
  132. : hints(0x0) {}
  133. void clear() noexcept
  134. {
  135. hints = 0x0;
  136. name = "";
  137. symbol = "";
  138. unit = "";
  139. ranges.clear();
  140. }
  141. };
  142. // -----------------------------------------------------------------------
  143. // MidiEvent
  144. struct MidiEvent {
  145. uint32_t frame;
  146. uint8_t size;
  147. uint8_t buf[4];
  148. void clear() noexcept
  149. {
  150. frame = 0;
  151. size = 0;
  152. buf[0] = 0;
  153. buf[1] = 0;
  154. buf[2] = 0;
  155. buf[3] = 0;
  156. }
  157. };
  158. // -----------------------------------------------------------------------
  159. // TimePos
  160. struct TimePos {
  161. bool playing;
  162. uint64_t frame;
  163. struct BeatBarTick {
  164. bool valid;
  165. int32_t bar; /*!< current bar */
  166. int32_t beat; /*!< current beat-within-bar */
  167. int32_t tick; /*!< current tick-within-beat */
  168. double barStartTick;
  169. float beatsPerBar; /*!< time signature "numerator" */
  170. float beatType; /*!< time signature "denominator" */
  171. double ticksPerBeat;
  172. double beatsPerMinute;
  173. BeatBarTick() noexcept
  174. : valid(false),
  175. bar(0),
  176. beat(0),
  177. tick(0),
  178. barStartTick(0.0),
  179. beatsPerBar(0.0f),
  180. beatType(0.0f),
  181. ticksPerBeat(0.0),
  182. beatsPerMinute(0.0) {}
  183. } bbt;
  184. TimePos() noexcept
  185. : playing(false),
  186. frame(0) {}
  187. };
  188. // -----------------------------------------------------------------------
  189. // Plugin
  190. class Plugin
  191. {
  192. public:
  193. Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
  194. virtual ~Plugin();
  195. // -------------------------------------------------------------------
  196. // Host state
  197. uint32_t d_getBufferSize() const noexcept;
  198. double d_getSampleRate() const noexcept;
  199. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  200. const TimePos& d_getTimePos() const noexcept;
  201. #endif
  202. #if DISTRHO_PLUGIN_WANT_LATENCY
  203. void d_setLatency(uint32_t frames) noexcept;
  204. #endif
  205. protected:
  206. // -------------------------------------------------------------------
  207. // Information
  208. virtual const char* d_getName() const noexcept { return DISTRHO_PLUGIN_NAME; }
  209. virtual const char* d_getLabel() const noexcept = 0;
  210. virtual const char* d_getMaker() const noexcept = 0;
  211. virtual const char* d_getLicense() const noexcept = 0;
  212. virtual uint32_t d_getVersion() const noexcept = 0;
  213. virtual long d_getUniqueId() const noexcept = 0;
  214. // -------------------------------------------------------------------
  215. // Init
  216. virtual void d_initParameter(uint32_t index, Parameter& parameter) = 0;
  217. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  218. virtual void d_initProgramName(uint32_t index, d_string& programName) = 0;
  219. #endif
  220. #if DISTRHO_PLUGIN_WANT_STATE
  221. virtual void d_initStateKey(uint32_t index, d_string& stateKey) = 0;
  222. #endif
  223. // -------------------------------------------------------------------
  224. // Internal data
  225. virtual float d_getParameterValue(uint32_t index) const = 0;
  226. virtual void d_setParameterValue(uint32_t index, float value) = 0;
  227. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  228. virtual void d_setProgram(uint32_t index) = 0;
  229. #endif
  230. #if DISTRHO_PLUGIN_WANT_STATE
  231. virtual void d_setState(const char* key, const char* value) = 0;
  232. #endif
  233. // -------------------------------------------------------------------
  234. // Process
  235. virtual void d_activate() {}
  236. virtual void d_deactivate() {}
  237. #if DISTRHO_PLUGIN_IS_SYNTH
  238. virtual void d_run(const float** inputs, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  239. #else
  240. virtual void d_run(const float** inputs, float** outputs, uint32_t frames) = 0;
  241. #endif
  242. // -------------------------------------------------------------------
  243. // Callbacks (optional)
  244. virtual void d_bufferSizeChanged(uint32_t newBufferSize);
  245. virtual void d_sampleRateChanged(double newSampleRate);
  246. // -------------------------------------------------------------------
  247. private:
  248. struct PrivateData;
  249. PrivateData* const pData;
  250. friend class PluginExporter;
  251. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Plugin)
  252. };
  253. // -----------------------------------------------------------------------
  254. // Create plugin, entry point
  255. extern Plugin* createPlugin();
  256. // -----------------------------------------------------------------------
  257. END_NAMESPACE_DISTRHO
  258. #endif // DISTRHO_PLUGIN_HPP_INCLUDED