Audio plugin host https://kx.studio/carla
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.

364 lines
11KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaDefines.h"
  18. #include "CarlaMathUtils.hpp"
  19. #include "CarlaNativeExtUI.hpp"
  20. #include "water/maths/MathsFunctions.h"
  21. using water::roundToIntAccurate;
  22. // -----------------------------------------------------------------------
  23. class BigMeterPlugin : public NativePluginAndUiClass
  24. {
  25. public:
  26. BigMeterPlugin(const NativeHostDescriptor* const host)
  27. : NativePluginAndUiClass(host, "bigmeter-ui"),
  28. fColor(1),
  29. fStyle(1),
  30. fOutLeft(0.0f),
  31. fOutRight(0.0f),
  32. fInlineDisplay() {}
  33. protected:
  34. // -------------------------------------------------------------------
  35. // Plugin parameter calls
  36. uint32_t getParameterCount() const override
  37. {
  38. return 4;
  39. }
  40. const NativeParameter* getParameterInfo(const uint32_t index) const override
  41. {
  42. CARLA_SAFE_ASSERT_RETURN(index < 4, nullptr);
  43. static NativeParameter param;
  44. static NativeParameterScalePoint scalePoints[3];
  45. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  46. param.name = nullptr;
  47. param.unit = nullptr;
  48. param.ranges.def = 0.0f;
  49. param.ranges.min = 0.0f;
  50. param.ranges.max = 1.0f;
  51. param.ranges.step = 1.0f;
  52. param.ranges.stepSmall = 1.0f;
  53. param.ranges.stepLarge = 1.0f;
  54. param.scalePointCount = 0;
  55. param.scalePoints = nullptr;
  56. switch (index)
  57. {
  58. case 0:
  59. hints |= NATIVE_PARAMETER_IS_INTEGER|NATIVE_PARAMETER_USES_SCALEPOINTS;
  60. param.name = "Color";
  61. param.ranges.def = 1.0f;
  62. param.ranges.min = 1.0f;
  63. param.ranges.max = 2.0f;
  64. scalePoints[0].value = 1.0f;
  65. scalePoints[0].label = "Green";
  66. scalePoints[1].value = 2.0f;
  67. scalePoints[1].label = "Blue";
  68. param.scalePointCount = 2;
  69. param.scalePoints = scalePoints;
  70. break;
  71. case 1:
  72. hints |= NATIVE_PARAMETER_IS_INTEGER|NATIVE_PARAMETER_USES_SCALEPOINTS;
  73. param.name = "Style";
  74. param.ranges.def = 1.0f;
  75. param.ranges.min = 1.0f;
  76. param.ranges.max = 3.0f;
  77. scalePoints[0].value = 1.0f;
  78. scalePoints[0].label = "Default";
  79. scalePoints[1].value = 2.0f;
  80. scalePoints[1].label = "OpenAV";
  81. scalePoints[2].value = 3.0f;
  82. scalePoints[2].label = "RNCBC";
  83. param.scalePointCount = 3;
  84. param.scalePoints = scalePoints;
  85. break;
  86. case 2:
  87. hints |= NATIVE_PARAMETER_IS_OUTPUT;
  88. param.name = "Out Left";
  89. break;
  90. case 3:
  91. hints |= NATIVE_PARAMETER_IS_OUTPUT;
  92. param.name = "Out Right";
  93. break;
  94. }
  95. param.hints = static_cast<NativeParameterHints>(hints);
  96. return &param;
  97. }
  98. float getParameterValue(const uint32_t index) const override
  99. {
  100. switch (index)
  101. {
  102. case 0:
  103. return float(fColor);
  104. case 1:
  105. return float(fStyle);
  106. case 2:
  107. return fOutLeft;
  108. case 3:
  109. return fOutRight;
  110. default:
  111. return 0.0f;
  112. }
  113. }
  114. // -------------------------------------------------------------------
  115. // Plugin state calls
  116. void setParameterValue(const uint32_t index, const float value) override
  117. {
  118. switch (index)
  119. {
  120. case 0:
  121. fColor = roundToIntAccurate(value);
  122. break;
  123. case 1:
  124. fStyle = roundToIntAccurate(value);
  125. break;
  126. default:
  127. break;
  128. }
  129. }
  130. // -------------------------------------------------------------------
  131. // Plugin process calls
  132. void activate() override
  133. {
  134. fOutLeft = 0.0f;
  135. fOutRight = 0.0f;
  136. }
  137. void process(const float** inputs, float**, const uint32_t frames,
  138. const NativeMidiEvent* const, const uint32_t) override
  139. {
  140. fOutLeft = carla_findMaxNormalizedFloat(inputs[0], frames);
  141. fOutRight = carla_findMaxNormalizedFloat(inputs[1], frames);
  142. bool needsInlineRender = false;
  143. if (carla_isNotEqual(fOutLeft, fInlineDisplay.lastLeft))
  144. {
  145. fInlineDisplay.lastLeft = fOutLeft;
  146. needsInlineRender = true;
  147. }
  148. if (carla_isNotEqual(fOutRight, fInlineDisplay.lastRight))
  149. {
  150. fInlineDisplay.lastRight = fOutRight;
  151. needsInlineRender = true;
  152. }
  153. if (needsInlineRender && ! fInlineDisplay.pending)
  154. hostQueueDrawInlineDisplay();
  155. }
  156. // -------------------------------------------------------------------
  157. // Plugin dispatcher calls
  158. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width_, const uint32_t height) override
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(width_ > 0 && height > 0, nullptr);
  161. const uint32_t width = width_ < height ? width_ : height;
  162. const size_t stride = width * 4;
  163. const size_t dataSize = stride * height;
  164. if (fInlineDisplay.size < dataSize || fInlineDisplay.data == nullptr)
  165. {
  166. fInlineDisplay.size = dataSize;
  167. delete[] fInlineDisplay.data;
  168. fInlineDisplay.data = new uchar[dataSize];
  169. }
  170. uchar* const data = fInlineDisplay.data;
  171. std::memset(data, 0, fInlineDisplay.size);
  172. const uint heightValueLeft = static_cast<uint>(fInlineDisplay.lastLeft * static_cast<float>(height));
  173. const uint heightValueRight = static_cast<uint>(fInlineDisplay.lastRight * static_cast<float>(height));
  174. for (uint h=0; h < height; ++h)
  175. {
  176. for (uint w=0; w < width; ++w)
  177. {
  178. // data[h * stride + w * 4 + 0] = 0;
  179. // data[h * stride + w * 4 + 1] = 255;
  180. // data[h * stride + w * 4 + 2] = 0;
  181. data[h * stride + w * 4 + 3] = 160;
  182. }
  183. }
  184. for (uint h=0; h < heightValueLeft; ++h)
  185. {
  186. const uint h2 = height - h - 1;
  187. for (uint w=0; w < width / 2; ++w)
  188. {
  189. data[h2 * stride + w * 4 + 0] = 200;
  190. data[h2 * stride + w * 4 + 1] = 0;
  191. data[h2 * stride + w * 4 + 2] = 0;
  192. data[h2 * stride + w * 4 + 3] = 255;
  193. }
  194. }
  195. for (uint h=0; h < heightValueRight; ++h)
  196. {
  197. const uint h2 = height - h - 1;
  198. for (uint w=width / 2; w < width; ++w)
  199. {
  200. data[h2 * stride + w * 4 + 0] = 200;
  201. data[h2 * stride + w * 4 + 1] = 0;
  202. data[h2 * stride + w * 4 + 2] = 0;
  203. data[h2 * stride + w * 4 + 3] = 255;
  204. }
  205. }
  206. // draw 1px border
  207. for (uint w=0; w < width; ++w)
  208. {
  209. // data[w * 4 + 0] = 0;
  210. // data[w * 4 + 1] = 0;
  211. // data[w * 4 + 2] = 255;
  212. data[w * 4 + 3] = 120;
  213. // data[(height - 1) * stride + w * 4 + 0] = 0;
  214. // data[(height - 1) * stride + w * 4 + 1] = 0;
  215. // data[(height - 1) * stride + w * 4 + 2] = 255;
  216. data[(height - 1) * stride + w * 4 + 3] = 120;
  217. }
  218. for (uint h=0; h < height; ++h)
  219. {
  220. // data[h * stride + 0] = 0;
  221. // data[h * stride + 1] = 0;
  222. // data[h * stride + 2] = 255;
  223. data[h * stride + 3] = 120;
  224. data[h * stride + (width / 2) * 4 + 0] = 0;
  225. data[h * stride + (width / 2) * 4 + 1] = 0;
  226. data[h * stride + (width / 2) * 4 + 2] = 0;
  227. data[h * stride + (width / 2) * 4 + 3] = 160;
  228. // data[h * stride + (width - 1) * 4 + 0] = 0;
  229. // data[h * stride + (width - 1) * 4 + 1] = 0;
  230. // data[h * stride + (width - 1) * 4 + 2] = 255;
  231. data[h * stride + (width - 1) * 4 + 3] = 120;
  232. }
  233. static const NativeInlineDisplayImageSurface nidims = {
  234. data,
  235. static_cast<int>(width),
  236. static_cast<int>(height),
  237. static_cast<int>(stride),
  238. };
  239. static uint lastWidth = 0;
  240. if (width != lastWidth)
  241. {
  242. carla_stdout("rendering at %u", width);
  243. lastWidth = width;
  244. }
  245. fInlineDisplay.pending = false;
  246. return &nidims;
  247. }
  248. private:
  249. int fColor, fStyle;
  250. float fOutLeft, fOutRight;
  251. struct InlineDisplay {
  252. uchar* data;
  253. size_t size;
  254. float lastLeft;
  255. float lastRight;
  256. bool pending;
  257. InlineDisplay()
  258. : data(nullptr),
  259. size(0),
  260. lastLeft(0.0f),
  261. lastRight(0.0f),
  262. pending(false) {}
  263. ~InlineDisplay()
  264. {
  265. if (data != nullptr)
  266. {
  267. delete[] data;
  268. data = nullptr;
  269. }
  270. }
  271. CARLA_DECLARE_NON_COPY_STRUCT(InlineDisplay)
  272. CARLA_PREVENT_HEAP_ALLOCATION
  273. } fInlineDisplay;
  274. PluginClassEND(BigMeterPlugin)
  275. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BigMeterPlugin)
  276. };
  277. // -----------------------------------------------------------------------
  278. static const NativePluginDescriptor bigmeterDesc = {
  279. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  280. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  281. |NATIVE_PLUGIN_HAS_INLINE_DISPLAY
  282. |NATIVE_PLUGIN_HAS_UI
  283. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS),
  284. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  285. /* audioIns */ 2,
  286. /* audioOuts */ 0,
  287. /* midiIns */ 0,
  288. /* midiOuts */ 0,
  289. /* paramIns */ 2,
  290. /* paramOuts */ 2,
  291. /* name */ "Big Meter",
  292. /* label */ "bigmeter",
  293. /* maker */ "falkTX",
  294. /* copyright */ "GNU GPL v2+",
  295. PluginDescriptorFILL(BigMeterPlugin)
  296. };
  297. // -----------------------------------------------------------------------
  298. CARLA_EXPORT
  299. void carla_register_native_plugin_bigmeter();
  300. CARLA_EXPORT
  301. void carla_register_native_plugin_bigmeter()
  302. {
  303. carla_register_native_plugin(&bigmeterDesc);
  304. }
  305. // -----------------------------------------------------------------------