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.

398 lines
12KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2015 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 "CarlaNative.hpp"
  18. #include "CarlaMutex.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaJuceUtils.hpp"
  21. #include "juce_audio_basics.h"
  22. // this one needs to be first
  23. #include "zita-bls1/png2img.cc"
  24. #include "zita-bls1/guiclass.cc"
  25. #include "zita-bls1/hp3filt.cc"
  26. #include "zita-bls1/jclient.cc"
  27. #include "zita-bls1/lfshelf2.cc"
  28. #include "zita-bls1/mainwin.cc"
  29. #include "zita-bls1/rotary.cc"
  30. #include "zita-bls1/shuffler.cc"
  31. #include "zita-bls1/styles.cc"
  32. using juce::FloatVectorOperations;
  33. using juce::ScopedPointer;
  34. // -----------------------------------------------------------------------
  35. // BLS1 Plugin
  36. class BLS1Plugin : public NativePluginClass,
  37. private Mainwin::ValueChangedCallback
  38. {
  39. public:
  40. static const uint32_t kNumInputs = 2;
  41. static const uint32_t kNumOutputs = 2;
  42. enum Parameters {
  43. kParameterINPBAL,
  44. kParameterHPFILT,
  45. kParameterSHGAIN,
  46. kParameterSHFREQ,
  47. kParameterLFFREQ,
  48. kParameterLFGAIN,
  49. kParameterNROTARY
  50. };
  51. BLS1Plugin(const NativeHostDescriptor* const host)
  52. : NativePluginClass(host),
  53. fJackClient(),
  54. xresman(),
  55. jclient(nullptr),
  56. display(nullptr),
  57. rootwin(nullptr),
  58. mainwin(nullptr),
  59. handler(nullptr),
  60. leakDetector_BLS1Plugin()
  61. {
  62. CARLA_SAFE_ASSERT(host != nullptr);
  63. carla_zeroStruct(fJackClient);
  64. fJackClient.clientName = "bls1";
  65. fJackClient.bufferSize = getBufferSize();
  66. fJackClient.sampleRate = getSampleRate();
  67. int argc = 1;
  68. char* argv[] = { (char*)"bls1" };
  69. xresman.init(&argc, argv, (char*)"bls1", nullptr, 0);
  70. jclient = new Jclient(xresman.rname(), &fJackClient);
  71. // set initial values
  72. fParameters[kParameterINPBAL] = 0.0f;
  73. fParameters[kParameterHPFILT] = 40.0f;
  74. fParameters[kParameterSHGAIN] = 15.0f;
  75. fParameters[kParameterSHFREQ] = 5e2f;
  76. fParameters[kParameterLFFREQ] = 80.0f;
  77. fParameters[kParameterLFGAIN] = 0.0f;
  78. jclient->set_inpbal(fParameters[kParameterINPBAL]);
  79. jclient->set_hpfilt(fParameters[kParameterHPFILT]);
  80. jclient->shuffler()->prepare(fParameters[kParameterSHGAIN], fParameters[kParameterSHFREQ]);
  81. jclient->set_loshelf(fParameters[kParameterLFGAIN], fParameters[kParameterLFFREQ]);
  82. }
  83. // -------------------------------------------------------------------
  84. // Plugin parameter calls
  85. uint32_t getParameterCount() const override
  86. {
  87. return kParameterNROTARY;
  88. }
  89. const NativeParameter* getParameterInfo(const uint32_t index) const override
  90. {
  91. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY, nullptr);
  92. static NativeParameter param;
  93. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  94. // reset
  95. param.name = nullptr;
  96. param.unit = nullptr;
  97. param.ranges.def = 0.0f;
  98. param.ranges.min = 0.0f;
  99. param.ranges.max = 1.0f;
  100. param.ranges.step = 1.0f;
  101. param.ranges.stepSmall = 1.0f;
  102. param.ranges.stepLarge = 1.0f;
  103. param.scalePointCount = 0;
  104. param.scalePoints = nullptr;
  105. switch (index)
  106. {
  107. case kParameterINPBAL:
  108. param.name = "Input balance";
  109. //param.unit = "dB";
  110. param.ranges.def = 0.0f;
  111. param.ranges.min = -3.0f;
  112. param.ranges.max = 3.0f;
  113. break;
  114. case kParameterHPFILT:
  115. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  116. param.name = "Highpass filter";
  117. param.ranges.def = 40.0f;
  118. param.ranges.min = 10.0f;
  119. param.ranges.max = 320.0f;
  120. break;
  121. case kParameterSHGAIN:
  122. param.name = "Shuffler gain";
  123. param.ranges.def = 15.0f;
  124. param.ranges.min = 0.0f;
  125. param.ranges.max = 24.0f;
  126. break;
  127. case kParameterSHFREQ:
  128. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  129. param.name = "Shuffler frequency";
  130. param.ranges.def = 5e2f;
  131. param.ranges.min = 125.0f;
  132. param.ranges.max = 2e3f;
  133. break;
  134. case kParameterLFFREQ:
  135. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  136. param.name = "LF shelf filter frequency";
  137. param.ranges.def = 80.0f;
  138. param.ranges.min = 20.0f;
  139. param.ranges.max = 320.0f;
  140. break;
  141. case kParameterLFGAIN:
  142. param.name = "LF shelf filter gain";
  143. param.ranges.def = 0.0f;
  144. param.ranges.min = -9.0f;
  145. param.ranges.max = 9.0f;
  146. break;
  147. }
  148. param.hints = static_cast<NativeParameterHints>(hints);
  149. return &param;
  150. }
  151. float getParameterValue(const uint32_t index) const override
  152. {
  153. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY, 0.0f);
  154. return fParameters[index];
  155. }
  156. // -------------------------------------------------------------------
  157. // Plugin state calls
  158. void setParameterValue(const uint32_t index, const float value) override
  159. {
  160. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY,);
  161. fParameters[index] = value;
  162. switch (index)
  163. {
  164. case kParameterINPBAL:
  165. jclient->set_inpbal(value);
  166. break;
  167. case kParameterHPFILT:
  168. jclient->set_hpfilt(value);
  169. break;
  170. case kParameterSHGAIN:
  171. jclient->shuffler()->prepare(value, fParameters[kParameterSHFREQ]);
  172. break;
  173. case kParameterSHFREQ:
  174. jclient->shuffler()->prepare(fParameters[kParameterSHGAIN], value);
  175. break;
  176. case kParameterLFFREQ:
  177. jclient->set_loshelf(fParameters[kParameterLFGAIN], value);
  178. break;
  179. case kParameterLFGAIN:
  180. jclient->set_loshelf(value, fParameters[kParameterLFFREQ]);
  181. break;
  182. }
  183. }
  184. // -------------------------------------------------------------------
  185. // Plugin process calls
  186. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const, const uint32_t) override
  187. {
  188. if (! fJackClient.active)
  189. {
  190. const int iframes(static_cast<int>(frames));
  191. for (uint32_t i=0; i<kNumInputs; ++i)
  192. FloatVectorOperations::clear(outBuffer[i], iframes);
  193. return;
  194. }
  195. for (uint32_t i=0; i<kNumInputs; ++i)
  196. fJackClient.portsAudioIn[i].buffer = inBuffer[i];
  197. for (uint32_t i=0; i<kNumOutputs; ++i)
  198. fJackClient.portsAudioOut[i].buffer = outBuffer[i];
  199. fJackClient.processCallback(frames, fJackClient.processPtr);
  200. }
  201. // -------------------------------------------------------------------
  202. // Plugin UI calls
  203. void uiShow(const bool show) override
  204. {
  205. if (show)
  206. {
  207. if (display == nullptr)
  208. {
  209. display = new X_display(nullptr);
  210. if (display->dpy() == nullptr)
  211. return hostUiUnavailable();
  212. styles_init(display, &xresman, getResourceDir());
  213. rootwin = new X_rootwin(display);
  214. mainwin = new Mainwin(rootwin, &xresman, 0, 0, jclient, this);
  215. rootwin->handle_event();
  216. mainwin->x_set_title(getUiName());
  217. handler = new X_handler(display, mainwin, EV_X11);
  218. if (const uintptr_t winId = getUiParentId())
  219. XSetTransientForHint(display->dpy(), mainwin->win(), static_cast<Window>(winId));
  220. }
  221. handler->next_event();
  222. XFlush(display->dpy());
  223. }
  224. else
  225. {
  226. handler = nullptr;
  227. mainwin = nullptr;
  228. rootwin = nullptr;
  229. display = nullptr;
  230. }
  231. }
  232. void uiIdle() override
  233. {
  234. if (mainwin == nullptr)
  235. return;
  236. int ev;
  237. for (; (ev = mainwin->process()) == EV_X11;)
  238. {
  239. rootwin->handle_event();
  240. handler->next_event();
  241. }
  242. if (ev == EV_EXIT)
  243. {
  244. handler = nullptr;
  245. mainwin = nullptr;
  246. rootwin = nullptr;
  247. display = nullptr;
  248. uiClosed();
  249. }
  250. }
  251. void uiSetParameterValue(const uint32_t index, const float value) override
  252. {
  253. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY,);
  254. if (mainwin == nullptr)
  255. return;
  256. mainwin->_rotary[index]->set_value(value);
  257. }
  258. // -------------------------------------------------------------------
  259. // Plugin dispatcher calls
  260. void bufferSizeChanged(const uint32_t bufferSize) override
  261. {
  262. fJackClient.bufferSize = bufferSize;
  263. }
  264. void sampleRateChanged(const double sampleRate) override
  265. {
  266. fJackClient.sampleRate = sampleRate;
  267. }
  268. void uiNameChanged(const char* const uiName) override
  269. {
  270. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  271. if (mainwin == nullptr)
  272. return;
  273. mainwin->x_set_title(uiName);
  274. }
  275. // -------------------------------------------------------------------
  276. // Mainwin callbacks
  277. void valueChangedCallback(uint index, double value) override
  278. {
  279. fParameters[index] = value;
  280. uiParameterChanged(index, value);
  281. }
  282. // -------------------------------------------------------------------
  283. private:
  284. // Fake jack client
  285. jack_client_t fJackClient;
  286. // Zita stuff (core)
  287. X_resman xresman;
  288. ScopedPointer<Jclient> jclient;
  289. ScopedPointer<X_display> display;
  290. ScopedPointer<X_rootwin> rootwin;
  291. ScopedPointer<Mainwin> mainwin;
  292. ScopedPointer<X_handler> handler;
  293. float fParameters[kParameterNROTARY];
  294. PluginClassEND(BLS1Plugin)
  295. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(BLS1Plugin)
  296. };
  297. // -----------------------------------------------------------------------
  298. static const NativePluginDescriptor bls1Desc = {
  299. /* category */ NATIVE_PLUGIN_CATEGORY_FILTER,
  300. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  301. |NATIVE_PLUGIN_HAS_UI
  302. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  303. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  304. |NATIVE_PLUGIN_USES_PARENT_ID),
  305. /* supports */ static_cast<NativePluginSupports>(0x0),
  306. /* audioIns */ BLS1Plugin::kNumInputs,
  307. /* audioOuts */ BLS1Plugin::kNumOutputs,
  308. /* midiIns */ 0,
  309. /* midiOuts */ 0,
  310. /* paramIns */ BLS1Plugin::kParameterNROTARY,
  311. /* paramOuts */ 0,
  312. /* name */ "BLS1",
  313. /* label */ "bls1",
  314. /* maker */ "Fons Adriaensen",
  315. /* copyright */ "GPL v2+",
  316. PluginDescriptorFILL(BLS1Plugin)
  317. };
  318. // -----------------------------------------------------------------------
  319. CARLA_EXPORT
  320. void carla_register_native_plugin_zita_bls1();
  321. CARLA_EXPORT
  322. void carla_register_native_plugin_zita_bls1()
  323. {
  324. carla_register_native_plugin(&bls1Desc);
  325. }
  326. // -----------------------------------------------------------------------