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.

318 lines
8.1KB

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