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.

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