DPF Plugin examples
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.

258 lines
7.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. #include "DistrhoPlugin.hpp"
  17. START_NAMESPACE_DISTRHO
  18. // -----------------------------------------------------------------------------------------------------------
  19. /**
  20. Plugin to demonstrate parameter outputs using meters.
  21. */
  22. class ExamplePluginMeters : public Plugin
  23. {
  24. public:
  25. ExamplePluginMeters()
  26. : Plugin(3, 0, 0), // 3 parameters, 0 programs, 0 states
  27. fColor(0.0f),
  28. fOutLeft(0.0f),
  29. fOutRight(0.0f),
  30. fNeedsReset(true)
  31. {
  32. }
  33. protected:
  34. /* --------------------------------------------------------------------------------------------------------
  35. * Information */
  36. /**
  37. Get the plugin label.
  38. A plugin label follows the same rules as Parameter::symbol, with the exception that it can start with numbers.
  39. */
  40. const char* getLabel() const override
  41. {
  42. return "meters";
  43. }
  44. /**
  45. Get the plugin author/maker.
  46. */
  47. const char* getMaker() const override
  48. {
  49. return "DISTRHO";
  50. }
  51. /**
  52. Get the plugin license name (a single line of text).
  53. */
  54. const char* getLicense() const override
  55. {
  56. return "ISC";
  57. }
  58. /**
  59. Get the plugin version, in hexadecimal.
  60. TODO format to be defined
  61. */
  62. uint32_t getVersion() const override
  63. {
  64. return 0x1000;
  65. }
  66. /**
  67. Get the plugin unique Id.
  68. This value is used by LADSPA, DSSI and VST plugin formats.
  69. */
  70. int64_t getUniqueId() const override
  71. {
  72. return d_cconst('d', 'M', 't', 'r');
  73. }
  74. /* --------------------------------------------------------------------------------------------------------
  75. * Init */
  76. /**
  77. Initialize the parameter @a index.
  78. This function will be called once, shortly after the plugin is created.
  79. */
  80. void initParameter(uint32_t index, Parameter& parameter) override
  81. {
  82. /**
  83. All parameters in this plugin have the same ranges.
  84. */
  85. parameter.ranges.min = 0.0f;
  86. parameter.ranges.max = 1.0f;
  87. parameter.ranges.def = 0.0f;
  88. /**
  89. Set parameter data.
  90. */
  91. switch (index)
  92. {
  93. case 0:
  94. parameter.hints = kParameterIsAutomable|kParameterIsInteger;
  95. parameter.name = "color";
  96. parameter.symbol = "color";
  97. break;
  98. case 1:
  99. parameter.hints = kParameterIsAutomable|kParameterIsOutput;
  100. parameter.name = "out-left";
  101. parameter.symbol = "out_left";
  102. break;
  103. case 2:
  104. parameter.hints = kParameterIsAutomable|kParameterIsOutput;
  105. parameter.name = "out-right";
  106. parameter.symbol = "out_right";
  107. break;
  108. }
  109. }
  110. /**
  111. Set a state key and default value.
  112. This function will be called once, shortly after the plugin is created.
  113. */
  114. void initState(uint32_t, String&, String&) override
  115. {
  116. // we are using states but don't want them saved in the host
  117. }
  118. /* --------------------------------------------------------------------------------------------------------
  119. * Internal data */
  120. /**
  121. Get the current value of a parameter.
  122. */
  123. float getParameterValue(uint32_t index) const override
  124. {
  125. switch (index)
  126. {
  127. case 0: return fColor;
  128. case 1: return fOutLeft;
  129. case 2: return fOutRight;
  130. }
  131. return 0.0f;
  132. }
  133. /**
  134. Change a parameter value.
  135. */
  136. void setParameterValue(uint32_t index, float value) override
  137. {
  138. // this is only called for input paramters, and we only have one of those.
  139. if (index != 0) return;
  140. fColor = value;
  141. }
  142. /**
  143. Change an internal state.
  144. */
  145. void setState(const char* key, const char*) override
  146. {
  147. if (std::strcmp(key, "reset") != 0)
  148. return;
  149. fNeedsReset = true;
  150. }
  151. /* --------------------------------------------------------------------------------------------------------
  152. * Process */
  153. /**
  154. Run/process function for plugins without MIDI input.
  155. */
  156. void run(const float** inputs, float** outputs, uint32_t frames) override
  157. {
  158. float tmp;
  159. float tmpLeft = 0.0f;
  160. float tmpRight = 0.0f;
  161. for (uint32_t i=0; i<frames; ++i)
  162. {
  163. // left
  164. tmp = std::abs(inputs[0][i]);
  165. if (tmp > tmpLeft)
  166. tmpLeft = tmp;
  167. // right
  168. tmp = std::abs(inputs[1][i]);
  169. if (tmp > tmpRight)
  170. tmpRight = tmp;
  171. }
  172. if (tmpLeft > 1.0f)
  173. tmpLeft = 1.0f;
  174. if (tmpRight > 1.0f)
  175. tmpRight = 1.0f;
  176. if (fNeedsReset)
  177. {
  178. fOutLeft = tmpLeft;
  179. fOutRight = tmpRight;
  180. fNeedsReset = false;
  181. }
  182. else
  183. {
  184. if (tmpLeft > fOutLeft)
  185. fOutLeft = tmpLeft;
  186. if (tmpRight > fOutRight)
  187. fOutRight = tmpRight;
  188. }
  189. // copy inputs over outputs if needed
  190. if (outputs[0] != inputs[0])
  191. std::memcpy(outputs[0], inputs[0], sizeof(float)*frames);
  192. if (outputs[1] != inputs[1])
  193. std::memcpy(outputs[1], inputs[1], sizeof(float)*frames);
  194. }
  195. // -------------------------------------------------------------------------------------------------------
  196. private:
  197. /**
  198. Parameters.
  199. */
  200. float fColor, fOutLeft, fOutRight;
  201. /**
  202. Boolean used to reset meter values.
  203. The UI will send a "reset" message which sets this as true.
  204. */
  205. volatile bool fNeedsReset;
  206. /**
  207. Set our plugin class as non-copyable and add a leak detector just in case.
  208. */
  209. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginMeters)
  210. };
  211. /* ------------------------------------------------------------------------------------------------------------
  212. * Plugin entry point, called by DPF to create a new plugin instance. */
  213. Plugin* createPlugin()
  214. {
  215. return new ExamplePluginMeters();
  216. }
  217. // -----------------------------------------------------------------------------------------------------------
  218. END_NAMESPACE_DISTRHO