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.

298 lines
8.4KB

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