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.

276 lines
7.3KB

  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. void fixValue(float& value) const noexcept
  59. {
  60. if (value < min)
  61. value = min;
  62. else if (value > max)
  63. value = max;
  64. }
  65. float getFixedValue(const float& value) const noexcept
  66. {
  67. if (value < min)
  68. return min;
  69. else if (value > max)
  70. return max;
  71. return value;
  72. }
  73. float getNormalizedValue(const float& value) const noexcept
  74. {
  75. const float newValue((value - min) / (max - min));
  76. if (newValue <= 0.0f)
  77. return 0.0f;
  78. if (newValue >= 1.0f)
  79. return 1.0f;
  80. return newValue;
  81. }
  82. float getUnnormalizedValue(const float& value) const noexcept
  83. {
  84. return value * (max - min) + min;
  85. }
  86. };
  87. // -----------------------------------------------------------------------
  88. // Parameter
  89. struct Parameter {
  90. uint32_t hints;
  91. d_string name;
  92. d_string symbol;
  93. d_string unit;
  94. ParameterRanges ranges;
  95. Parameter()
  96. : hints(0x0) {}
  97. void clear() noexcept
  98. {
  99. hints = 0x0;
  100. name = "";
  101. symbol = "";
  102. unit = "";
  103. ranges.clear();
  104. }
  105. };
  106. // -----------------------------------------------------------------------
  107. // MidiEvent
  108. struct MidiEvent {
  109. uint32_t frame;
  110. uint8_t size;
  111. uint8_t buf[4];
  112. void clear() noexcept
  113. {
  114. frame = 0;
  115. size = 0;
  116. buf[0] = 0;
  117. buf[1] = 0;
  118. buf[2] = 0;
  119. buf[3] = 0;
  120. }
  121. };
  122. // -----------------------------------------------------------------------
  123. // TimePos
  124. struct TimePos {
  125. bool playing;
  126. uint64_t frame;
  127. struct BeatBarTick {
  128. bool valid;
  129. int32_t bar; /*!< current bar */
  130. int32_t beat; /*!< current beat-within-bar */
  131. int32_t tick; /*!< current tick-within-beat */
  132. double barStartTick;
  133. float beatsPerBar; /*!< time signature "numerator" */
  134. float beatType; /*!< time signature "denominator" */
  135. double ticksPerBeat;
  136. double beatsPerMinute;
  137. BeatBarTick() noexcept
  138. : valid(false),
  139. bar(0),
  140. beat(0),
  141. tick(0),
  142. barStartTick(0.0),
  143. beatsPerBar(0.0f),
  144. beatType(0.0f),
  145. ticksPerBeat(0.0),
  146. beatsPerMinute(0.0) {}
  147. } bbt;
  148. TimePos() noexcept
  149. : playing(false),
  150. frame(0) {}
  151. };
  152. // -----------------------------------------------------------------------
  153. // Plugin
  154. class Plugin
  155. {
  156. public:
  157. Plugin(uint32_t parameterCount, uint32_t programCount, uint32_t stateCount);
  158. virtual ~Plugin();
  159. // -------------------------------------------------------------------
  160. // Host state
  161. uint32_t d_getBufferSize() const noexcept;
  162. double d_getSampleRate() const noexcept;
  163. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  164. const TimePos& d_getTimePos() const noexcept;
  165. #endif
  166. #if DISTRHO_PLUGIN_WANT_LATENCY
  167. void d_setLatency(uint32_t frames) noexcept;
  168. #endif
  169. protected:
  170. // -------------------------------------------------------------------
  171. // Information
  172. virtual const char* d_getName() const noexcept { return DISTRHO_PLUGIN_NAME; }
  173. virtual const char* d_getLabel() const noexcept = 0;
  174. virtual const char* d_getMaker() const noexcept = 0;
  175. virtual const char* d_getLicense() const noexcept = 0;
  176. virtual uint32_t d_getVersion() const noexcept = 0;
  177. virtual long d_getUniqueId() const noexcept = 0;
  178. // -------------------------------------------------------------------
  179. // Init
  180. virtual void d_initParameter(uint32_t index, Parameter& parameter) = 0;
  181. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  182. virtual void d_initProgramName(uint32_t index, d_string& programName) = 0;
  183. #endif
  184. #if DISTRHO_PLUGIN_WANT_STATE
  185. virtual void d_initStateKey(uint32_t index, d_string& stateKey) = 0;
  186. #endif
  187. // -------------------------------------------------------------------
  188. // Internal data
  189. virtual float d_getParameterValue(uint32_t index) const = 0;
  190. virtual void d_setParameterValue(uint32_t index, float value) = 0;
  191. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  192. virtual void d_setProgram(uint32_t index) = 0;
  193. #endif
  194. #if DISTRHO_PLUGIN_WANT_STATE
  195. virtual void d_setState(const char* key, const char* value) = 0;
  196. #endif
  197. // -------------------------------------------------------------------
  198. // Process
  199. virtual void d_activate() {}
  200. virtual void d_deactivate() {}
  201. #if DISTRHO_PLUGIN_IS_SYNTH
  202. virtual void d_run(float** inputs, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount) = 0;
  203. #else
  204. virtual void d_run(float** inputs, float** outputs, uint32_t frames) = 0;
  205. #endif
  206. // -------------------------------------------------------------------
  207. // Callbacks (optional)
  208. virtual void d_bufferSizeChanged(uint32_t newBufferSize);
  209. virtual void d_sampleRateChanged(double newSampleRate);
  210. // -------------------------------------------------------------------
  211. private:
  212. struct PrivateData;
  213. PrivateData* const pData;
  214. friend class PluginExporter;
  215. };
  216. // -----------------------------------------------------------------------
  217. // Create plugin, entry point
  218. extern Plugin* createPlugin();
  219. // -----------------------------------------------------------------------
  220. END_NAMESPACE_DISTRHO
  221. #endif // DISTRHO_PLUGIN_HPP_INCLUDED