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.

274 lines
7.6KB

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