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.

bigmeter.cpp 11KB

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