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.

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