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.

289 lines
8.2KB

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