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-2022 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_AUTOMATABLE;
  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* const* 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 = fInlineDisplay.pending < 0;
  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 != 1 && fInlineDisplay.pending != 2)
  154. {
  155. fInlineDisplay.pending = 1;
  156. hostRequestIdle();
  157. }
  158. }
  159. // -------------------------------------------------------------------
  160. // Plugin dispatcher calls
  161. void idle() override
  162. {
  163. if (fInlineDisplay.pending == 1)
  164. {
  165. fInlineDisplay.pending = 2;
  166. hostQueueDrawInlineDisplay();
  167. }
  168. }
  169. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t rwidth, const uint32_t height) override
  170. {
  171. CARLA_SAFE_ASSERT_RETURN(rwidth > 0 && height > 0, nullptr);
  172. const uint32_t width = rwidth == height ? height / 6 : rwidth;
  173. const size_t stride = width * 4;
  174. const size_t dataSize = stride * height;
  175. uchar* data = fInlineDisplay.data;
  176. if (fInlineDisplay.dataSize < dataSize || data == nullptr)
  177. {
  178. delete[] data;
  179. data = new uchar[dataSize];
  180. std::memset(data, 0, dataSize);
  181. fInlineDisplay.data = data;
  182. fInlineDisplay.dataSize = dataSize;
  183. }
  184. std::memset(data, 0, dataSize);
  185. fInlineDisplay.width = static_cast<int>(width);
  186. fInlineDisplay.height = static_cast<int>(height);
  187. fInlineDisplay.stride = static_cast<int>(stride);
  188. const uint heightValueLeft = static_cast<uint>(fInlineDisplay.lastLeft * static_cast<float>(height));
  189. const uint heightValueRight = static_cast<uint>(fInlineDisplay.lastRight * static_cast<float>(height));
  190. for (uint h=0; h < height; ++h)
  191. {
  192. for (uint w=0; w < width; ++w)
  193. {
  194. // data[h * stride + w * 4 + 0] = 0;
  195. // data[h * stride + w * 4 + 1] = 255;
  196. // data[h * stride + w * 4 + 2] = 0;
  197. data[h * stride + w * 4 + 3] = 160;
  198. }
  199. }
  200. for (uint h=0; h < heightValueLeft; ++h)
  201. {
  202. const uint h2 = height - h - 1;
  203. for (uint w=0; w < width / 2; ++w)
  204. {
  205. data[h2 * stride + w * 4 + 0] = 200;
  206. data[h2 * stride + w * 4 + 1] = 0;
  207. data[h2 * stride + w * 4 + 2] = 0;
  208. data[h2 * stride + w * 4 + 3] = 255;
  209. }
  210. }
  211. for (uint h=0; h < heightValueRight; ++h)
  212. {
  213. const uint h2 = height - h - 1;
  214. for (uint w=width / 2; w < width; ++w)
  215. {
  216. data[h2 * stride + w * 4 + 0] = 200;
  217. data[h2 * stride + w * 4 + 1] = 0;
  218. data[h2 * stride + w * 4 + 2] = 0;
  219. data[h2 * stride + w * 4 + 3] = 255;
  220. }
  221. }
  222. // draw 1px border
  223. for (uint w=0; w < width; ++w)
  224. {
  225. // data[w * 4 + 0] = 0;
  226. // data[w * 4 + 1] = 0;
  227. // data[w * 4 + 2] = 255;
  228. data[w * 4 + 3] = 120;
  229. // data[(height - 1) * stride + w * 4 + 0] = 0;
  230. // data[(height - 1) * stride + w * 4 + 1] = 0;
  231. // data[(height - 1) * stride + w * 4 + 2] = 255;
  232. data[(height - 1) * stride + w * 4 + 3] = 120;
  233. }
  234. for (uint h=0; h < height; ++h)
  235. {
  236. // data[h * stride + 0] = 0;
  237. // data[h * stride + 1] = 0;
  238. // data[h * stride + 2] = 255;
  239. data[h * stride + 3] = 120;
  240. data[h * stride + (width / 2) * 4 + 0] = 0;
  241. data[h * stride + (width / 2) * 4 + 1] = 0;
  242. data[h * stride + (width / 2) * 4 + 2] = 0;
  243. data[h * stride + (width / 2) * 4 + 3] = 160;
  244. // data[h * stride + (width - 1) * 4 + 0] = 0;
  245. // data[h * stride + (width - 1) * 4 + 1] = 0;
  246. // data[h * stride + (width - 1) * 4 + 2] = 255;
  247. data[h * stride + (width - 1) * 4 + 3] = 120;
  248. }
  249. fInlineDisplay.pending = rwidth == height ? -1 : 0;
  250. return (NativeInlineDisplayImageSurface*)(NativeInlineDisplayImageSurfaceCompat*)&fInlineDisplay;
  251. }
  252. private:
  253. int fColor, fStyle;
  254. float fOutLeft, fOutRight;
  255. struct InlineDisplay : NativeInlineDisplayImageSurfaceCompat {
  256. float lastLeft;
  257. float lastRight;
  258. volatile int pending;
  259. InlineDisplay()
  260. : NativeInlineDisplayImageSurfaceCompat(),
  261. lastLeft(0.0f),
  262. lastRight(0.0f),
  263. pending(0) {}
  264. ~InlineDisplay()
  265. {
  266. if (data != nullptr)
  267. {
  268. delete[] data;
  269. data = nullptr;
  270. }
  271. }
  272. CARLA_DECLARE_NON_COPYABLE(InlineDisplay)
  273. CARLA_PREVENT_HEAP_ALLOCATION
  274. } fInlineDisplay;
  275. PluginClassEND(BigMeterPlugin)
  276. CARLA_DECLARE_NON_COPYABLE(BigMeterPlugin)
  277. };
  278. // -----------------------------------------------------------------------
  279. static const NativePluginDescriptor bigmeterDesc = {
  280. /* category */ NATIVE_PLUGIN_CATEGORY_UTILITY,
  281. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  282. |NATIVE_PLUGIN_HAS_INLINE_DISPLAY
  283. |NATIVE_PLUGIN_HAS_UI
  284. |NATIVE_PLUGIN_REQUESTS_IDLE),
  285. /* supports */ NATIVE_PLUGIN_SUPPORTS_NOTHING,
  286. /* audioIns */ 2,
  287. /* audioOuts */ 0,
  288. /* midiIns */ 0,
  289. /* midiOuts */ 0,
  290. /* paramIns */ 2,
  291. /* paramOuts */ 2,
  292. /* name */ "Big Meter",
  293. /* label */ "bigmeter",
  294. /* maker */ "falkTX",
  295. /* copyright */ "GNU GPL v2+",
  296. PluginDescriptorFILL(BigMeterPlugin)
  297. };
  298. // -----------------------------------------------------------------------
  299. CARLA_API_EXPORT
  300. void carla_register_native_plugin_bigmeter();
  301. CARLA_API_EXPORT
  302. void carla_register_native_plugin_bigmeter()
  303. {
  304. carla_register_native_plugin(&bigmeterDesc);
  305. }
  306. // -----------------------------------------------------------------------