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.

285 lines
8.0KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2018 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/DPF";
  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 = kParameterIsAutomatable|kParameterIsInteger;
  109. parameter.name = "color";
  110. parameter.symbol = "color";
  111. parameter.enumValues.count = 2;
  112. parameter.enumValues.restrictedMode = true;
  113. {
  114. ParameterEnumerationValue* const values = new ParameterEnumerationValue[2];
  115. parameter.enumValues.values = values;
  116. values[0].label = "Green";
  117. values[0].value = METER_COLOR_GREEN;
  118. values[1].label = "Blue";
  119. values[1].value = METER_COLOR_BLUE;
  120. }
  121. break;
  122. case 1:
  123. parameter.hints = kParameterIsAutomatable|kParameterIsOutput;
  124. parameter.name = "out-left";
  125. parameter.symbol = "out_left";
  126. break;
  127. case 2:
  128. parameter.hints = kParameterIsAutomatable|kParameterIsOutput;
  129. parameter.name = "out-right";
  130. parameter.symbol = "out_right";
  131. break;
  132. }
  133. }
  134. /**
  135. Set a state key and default value.
  136. This function will be called once, shortly after the plugin is created.
  137. */
  138. void initState(uint32_t, String&, String&) override
  139. {
  140. // we are using states but don't want them saved in the host
  141. }
  142. /* --------------------------------------------------------------------------------------------------------
  143. * Internal data */
  144. /**
  145. Get the current value of a parameter.
  146. */
  147. float getParameterValue(uint32_t index) const override
  148. {
  149. switch (index)
  150. {
  151. case 0: return fColor;
  152. case 1: return fOutLeft;
  153. case 2: return fOutRight;
  154. }
  155. return 0.0f;
  156. }
  157. /**
  158. Change a parameter value.
  159. */
  160. void setParameterValue(uint32_t index, float value) override
  161. {
  162. // this is only called for input paramters, and we only have one of those.
  163. if (index != 0) return;
  164. fColor = value;
  165. }
  166. /**
  167. Change an internal state.
  168. */
  169. void setState(const char* key, const char*) override
  170. {
  171. if (std::strcmp(key, "reset") != 0)
  172. return;
  173. fNeedsReset = true;
  174. }
  175. /* --------------------------------------------------------------------------------------------------------
  176. * Process */
  177. /**
  178. Run/process function for plugins without MIDI input.
  179. */
  180. void run(const float** inputs, float** outputs, uint32_t frames) override
  181. {
  182. float tmp;
  183. float tmpLeft = 0.0f;
  184. float tmpRight = 0.0f;
  185. for (uint32_t i=0; i<frames; ++i)
  186. {
  187. // left
  188. tmp = std::abs(inputs[0][i]);
  189. if (tmp > tmpLeft)
  190. tmpLeft = tmp;
  191. // right
  192. tmp = std::abs(inputs[1][i]);
  193. if (tmp > tmpRight)
  194. tmpRight = tmp;
  195. }
  196. if (tmpLeft > 1.0f)
  197. tmpLeft = 1.0f;
  198. if (tmpRight > 1.0f)
  199. tmpRight = 1.0f;
  200. if (fNeedsReset)
  201. {
  202. fOutLeft = tmpLeft;
  203. fOutRight = tmpRight;
  204. fNeedsReset = false;
  205. }
  206. else
  207. {
  208. if (tmpLeft > fOutLeft)
  209. fOutLeft = tmpLeft;
  210. if (tmpRight > fOutRight)
  211. fOutRight = tmpRight;
  212. }
  213. // copy inputs over outputs if needed
  214. if (outputs[0] != inputs[0])
  215. std::memcpy(outputs[0], inputs[0], sizeof(float)*frames);
  216. if (outputs[1] != inputs[1])
  217. std::memcpy(outputs[1], inputs[1], sizeof(float)*frames);
  218. }
  219. // -------------------------------------------------------------------------------------------------------
  220. private:
  221. /**
  222. Parameters.
  223. */
  224. float fColor, fOutLeft, fOutRight;
  225. /**
  226. Boolean used to reset meter values.
  227. The UI will send a "reset" message which sets this as true.
  228. */
  229. volatile bool fNeedsReset;
  230. /**
  231. Set our plugin class as non-copyable and add a leak detector just in case.
  232. */
  233. DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ExamplePluginMeters)
  234. };
  235. /* ------------------------------------------------------------------------------------------------------------
  236. * Plugin entry point, called by DPF to create a new plugin instance. */
  237. Plugin* createPlugin()
  238. {
  239. return new ExamplePluginMeters();
  240. }
  241. // -----------------------------------------------------------------------------------------------------------
  242. END_NAMESPACE_DISTRHO