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.

zita-rev1.cpp 18KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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-rev1/guiclass.cc"
  23. #include "zita-rev1/jclient.cc"
  24. #include "zita-rev1/mainwin.cc"
  25. #include "zita-rev1/pareq.cc"
  26. #include "zita-rev1/png2img.cc"
  27. #include "zita-rev1/reverb.cc"
  28. #include "zita-rev1/rotary.cc"
  29. #include "zita-rev1/styles.cc"
  30. using juce::FloatVectorOperations;
  31. using juce::ScopedPointer;
  32. using namespace REV1;
  33. // -----------------------------------------------------------------------
  34. // REV1 Plugin
  35. class REV1Plugin : public NativePluginClass,
  36. public X_handler_thread<Mainwin>::SetValueCallback,
  37. private Mainwin::ValueChangedCallback
  38. {
  39. public:
  40. enum Parameters {
  41. kParameterDELAY,
  42. kParameterXOVER,
  43. kParameterRTLOW,
  44. kParameterRTMID,
  45. kParameterFDAMP,
  46. kParameterEQ1FR,
  47. kParameterEQ1GN,
  48. kParameterEQ2FR,
  49. kParameterEQ2GN,
  50. kParameterOPMIXorRGXYZ,
  51. kParameterNROTARY
  52. };
  53. REV1Plugin(const NativeHostDescriptor* const host, const bool isAmbisonic)
  54. : NativePluginClass(host),
  55. kIsAmbisonic(isAmbisonic),
  56. kNumInputs(2),
  57. kNumOutputs(isAmbisonic ? 4 : 2),
  58. fJackClient(),
  59. xresman(),
  60. jclient(nullptr),
  61. display(nullptr),
  62. rootwin(nullptr),
  63. mainwin(nullptr),
  64. handler(nullptr),
  65. handlerThread(this),
  66. leakDetector_REV1Plugin()
  67. {
  68. CARLA_SAFE_ASSERT(host != nullptr);
  69. carla_zeroStruct(fJackClient);
  70. fJackClient.clientName = "rev1";
  71. fJackClient.bufferSize = getBufferSize();
  72. fJackClient.sampleRate = getSampleRate();
  73. int argc = 1;
  74. char* argv[] = { (char*)"rev1" };
  75. xresman.init(&argc, argv, (char*)"rev1", nullptr, 0);
  76. jclient = new Jclient(xresman.rname(), &fJackClient, isAmbisonic);
  77. // set initial values
  78. fParameters[kParameterDELAY] = 0.04f;
  79. fParameters[kParameterXOVER] = 200.0f;
  80. fParameters[kParameterRTLOW] = 3.0f;
  81. fParameters[kParameterRTMID] = 2.0f;
  82. fParameters[kParameterFDAMP] = 6.0e3;
  83. fParameters[kParameterEQ1FR] = 160.0f;
  84. fParameters[kParameterEQ1GN] = 0.0f;
  85. fParameters[kParameterEQ2FR] = 2.5e3;
  86. fParameters[kParameterEQ2GN] = 0.0f;
  87. if (isAmbisonic)
  88. fParameters[kParameterOPMIXorRGXYZ] = 0.0f;
  89. else
  90. fParameters[kParameterOPMIXorRGXYZ] = 0.5f;
  91. Reverb* const reverb(jclient->reverb());
  92. reverb->set_delay(fParameters[kParameterDELAY]);
  93. reverb->set_xover(fParameters[kParameterXOVER]);
  94. reverb->set_rtlow(fParameters[kParameterRTLOW]);
  95. reverb->set_rtmid(fParameters[kParameterRTMID]);
  96. reverb->set_fdamp(fParameters[kParameterFDAMP]);
  97. if (isAmbisonic)
  98. {
  99. reverb->set_opmix(0.5);
  100. reverb->set_rgxyz(fParameters[kParameterOPMIXorRGXYZ]);
  101. }
  102. else
  103. {
  104. reverb->set_opmix(fParameters[kParameterOPMIXorRGXYZ]);
  105. reverb->set_rgxyz(0.0);
  106. }
  107. reverb->set_eq1(fParameters[kParameterEQ1FR], fParameters[kParameterEQ1GN]);
  108. reverb->set_eq2(fParameters[kParameterEQ2FR], fParameters[kParameterEQ2GN]);
  109. }
  110. // -------------------------------------------------------------------
  111. // Plugin parameter calls
  112. uint32_t getParameterCount() const override
  113. {
  114. return kParameterNROTARY;
  115. }
  116. const NativeParameter* getParameterInfo(const uint32_t index) const override
  117. {
  118. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY, nullptr);
  119. static NativeParameter param;
  120. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  121. // reset
  122. param.name = nullptr;
  123. param.unit = nullptr;
  124. param.ranges.def = 0.0f;
  125. param.ranges.min = 0.0f;
  126. param.ranges.max = 1.0f;
  127. param.ranges.step = 1.0f;
  128. param.ranges.stepSmall = 1.0f;
  129. param.ranges.stepLarge = 1.0f;
  130. param.scalePointCount = 0;
  131. param.scalePoints = nullptr;
  132. switch (index)
  133. {
  134. case kParameterDELAY:
  135. param.name = "Delay";
  136. param.ranges.def = 0.04f;
  137. param.ranges.min = 0.02f;
  138. param.ranges.max = 0.100f;
  139. break;
  140. case kParameterXOVER:
  141. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  142. param.name = "Crossover";
  143. param.ranges.def = 200.0f;
  144. param.ranges.min = 50.0f;
  145. param.ranges.max = 1000.0f;
  146. break;
  147. case kParameterRTLOW:
  148. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  149. param.name = "RT60 Low";
  150. param.ranges.def = 3.0f;
  151. param.ranges.min = 1.0f;
  152. param.ranges.max = 8.0f;
  153. break;
  154. case kParameterRTMID:
  155. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  156. param.name = "RT60 Mid";
  157. param.ranges.def = 2.0f;
  158. param.ranges.min = 1.0f;
  159. param.ranges.max = 8.0f;
  160. break;
  161. case kParameterFDAMP:
  162. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  163. param.name = "HF Damping";
  164. param.ranges.def = 6.0e3;
  165. param.ranges.min = 1.5e3;
  166. param.ranges.max = 24.0e3;
  167. break;
  168. case kParameterEQ1FR:
  169. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  170. param.name = "Eq1 Frequency";
  171. param.ranges.def = 160.0f;
  172. param.ranges.min = 40.0f;
  173. param.ranges.max = 2.5e3;
  174. break;
  175. case kParameterEQ1GN:
  176. param.name = "Eq1 Gain";
  177. param.ranges.def = 0.0f;
  178. param.ranges.min = -15.0;
  179. param.ranges.max = 15.0f;
  180. break;
  181. case kParameterEQ2FR:
  182. hints |= NATIVE_PARAMETER_IS_LOGARITHMIC;
  183. param.name = "Eq2 Frequency";
  184. param.ranges.def = 2.5e3;
  185. param.ranges.min = 160.0;
  186. param.ranges.max = 10e3;
  187. break;
  188. case kParameterEQ2GN:
  189. param.name = "Eq2 Gain";
  190. param.ranges.def = 0.0f;
  191. param.ranges.min = -15.0;
  192. param.ranges.max = 15.0f;
  193. break;
  194. case kParameterOPMIXorRGXYZ:
  195. if (kIsAmbisonic)
  196. {
  197. param.name = "XYZ gain";
  198. param.ranges.def = 0.0f;
  199. param.ranges.min = -9.0f;
  200. param.ranges.max = 9.0f;
  201. }
  202. else
  203. {
  204. param.name = "Dry/wet mix";
  205. param.ranges.def = 0.5f;
  206. param.ranges.min = 0.0f;
  207. param.ranges.max = 1.0f;
  208. }
  209. break;
  210. }
  211. param.hints = static_cast<NativeParameterHints>(hints);
  212. return &param;
  213. }
  214. float getParameterValue(const uint32_t index) const override
  215. {
  216. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY, 0.0f);
  217. return fParameters[index];
  218. }
  219. // -------------------------------------------------------------------
  220. // Plugin state calls
  221. void setParameterValue(const uint32_t index, const float value) override
  222. {
  223. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY,);
  224. Reverb* const reverb(jclient->reverb());
  225. fParameters[index] = value;
  226. switch (index)
  227. {
  228. case kParameterDELAY:
  229. reverb->set_delay(value);
  230. break;
  231. case kParameterXOVER:
  232. reverb->set_xover(value);
  233. break;
  234. case kParameterRTLOW:
  235. reverb->set_rtlow(value);
  236. break;
  237. case kParameterRTMID:
  238. reverb->set_rtmid(value);
  239. break;
  240. case kParameterFDAMP:
  241. reverb->set_fdamp(value);
  242. break;
  243. case kParameterEQ1FR:
  244. reverb->set_eq1(value, fParameters[kParameterEQ1GN]);
  245. break;
  246. case kParameterEQ1GN:
  247. reverb->set_eq1(fParameters[kParameterEQ1FR], value);
  248. break;
  249. case kParameterEQ2FR:
  250. reverb->set_eq2(value, fParameters[kParameterEQ2GN]);
  251. break;
  252. case kParameterEQ2GN:
  253. reverb->set_eq2(fParameters[kParameterEQ2FR], value);
  254. break;
  255. case kParameterOPMIXorRGXYZ:
  256. if (kIsAmbisonic)
  257. reverb->set_rgxyz(value);
  258. else
  259. reverb->set_opmix(value);
  260. break;
  261. }
  262. }
  263. // -------------------------------------------------------------------
  264. // Plugin process calls
  265. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const, const uint32_t) override
  266. {
  267. if (! fJackClient.active)
  268. {
  269. const int iframes(static_cast<int>(frames));
  270. for (uint32_t i=0; i<kNumInputs; ++i)
  271. FloatVectorOperations::clear(outBuffer[i], iframes);
  272. return;
  273. }
  274. for (uint32_t i=0; i<kNumInputs; ++i)
  275. fJackClient.portsAudioIn[i].buffer.audio = inBuffer[i];
  276. for (uint32_t i=0; i<kNumOutputs; ++i)
  277. fJackClient.portsAudioOut[i].buffer.audio = outBuffer[i];
  278. fJackClient.processCallback(frames, fJackClient.processPtr);
  279. }
  280. // -------------------------------------------------------------------
  281. // Plugin UI calls
  282. void uiShow(const bool show) override
  283. {
  284. if (show)
  285. {
  286. if (display != nullptr)
  287. return;
  288. display = new X_display(nullptr);
  289. if (display->dpy() == nullptr)
  290. return hostUiUnavailable();
  291. styles_init(display, &xresman, getResourceDir());
  292. rootwin = new X_rootwin(display);
  293. mainwin = new Mainwin(rootwin, &xresman, 0, 0, jclient, this);
  294. rootwin->handle_event();
  295. mainwin->x_set_title(getUiName());
  296. handler = new X_handler(display, mainwin, EV_X11);
  297. if (const uintptr_t winId = getUiParentId())
  298. XSetTransientForHint(display->dpy(), mainwin->win(), static_cast<Window>(winId));
  299. handler->next_event();
  300. XFlush(display->dpy());
  301. handlerThread.setupAndRun(handler, rootwin, mainwin);
  302. }
  303. else
  304. {
  305. if (handlerThread.isThreadRunning())
  306. handlerThread.stopThread();
  307. handler = nullptr;
  308. mainwin = nullptr;
  309. rootwin = nullptr;
  310. display = nullptr;
  311. }
  312. }
  313. void uiIdle() override
  314. {
  315. if (mainwin == nullptr)
  316. return;
  317. if (handlerThread.wasClosed())
  318. {
  319. {
  320. const CarlaMutexLocker cml(handlerThread.getLock());
  321. handler = nullptr;
  322. mainwin = nullptr;
  323. rootwin = nullptr;
  324. display = nullptr;
  325. }
  326. uiClosed();
  327. }
  328. }
  329. void uiSetParameterValue(const uint32_t index, const float value) override
  330. {
  331. CARLA_SAFE_ASSERT_RETURN(index < kParameterNROTARY,);
  332. if (mainwin == nullptr)
  333. return;
  334. handlerThread.setParameterValueLater(index, value);
  335. }
  336. // -------------------------------------------------------------------
  337. // Plugin dispatcher calls
  338. void bufferSizeChanged(const uint32_t bufferSize) override
  339. {
  340. fJackClient.bufferSize = bufferSize;
  341. }
  342. void sampleRateChanged(const double sampleRate) override
  343. {
  344. fJackClient.sampleRate = sampleRate;
  345. }
  346. void uiNameChanged(const char* const uiName) override
  347. {
  348. CARLA_SAFE_ASSERT_RETURN(uiName != nullptr && uiName[0] != '\0',);
  349. if (mainwin == nullptr)
  350. return;
  351. const CarlaMutexLocker cml(handlerThread.getLock());
  352. mainwin->x_set_title(uiName);
  353. }
  354. // -------------------------------------------------------------------
  355. // Mainwin callbacks
  356. void valueChangedCallback(uint rindex, double value) override
  357. {
  358. uint32_t index = rindex;
  359. if (kIsAmbisonic && rindex == kParameterNROTARY)
  360. index = kParameterOPMIXorRGXYZ;
  361. fParameters[index] = value;
  362. uiParameterChanged(index, value);
  363. }
  364. // -------------------------------------------------------------------
  365. // X_handler_thread callbacks
  366. void setParameterValueFromHandlerThread(uint32_t index, float value) override
  367. {
  368. CARLA_SAFE_ASSERT_RETURN(mainwin != nullptr,);
  369. uint32_t rindex = index;
  370. if (kIsAmbisonic && index == kParameterOPMIXorRGXYZ)
  371. rindex += 1;
  372. mainwin->_rotary[rindex]->set_value(value);
  373. }
  374. // -------------------------------------------------------------------
  375. private:
  376. const bool kIsAmbisonic;
  377. const uint32_t kNumInputs;
  378. const uint32_t kNumOutputs;
  379. // Fake jack client
  380. jack_client_t fJackClient;
  381. // Zita stuff (core)
  382. X_resman xresman;
  383. ScopedPointer<Jclient> jclient;
  384. ScopedPointer<X_display> display;
  385. ScopedPointer<X_rootwin> rootwin;
  386. ScopedPointer<Mainwin> mainwin;
  387. ScopedPointer<X_handler> handler;
  388. X_handler_thread<Mainwin> handlerThread;
  389. float fParameters[kParameterNROTARY];
  390. public:
  391. static NativePluginHandle _instantiateAmbisonic(const NativeHostDescriptor* host)
  392. {
  393. return (host != nullptr) ? new REV1Plugin(host, true) : nullptr;
  394. }
  395. static NativePluginHandle _instantiateStereo(const NativeHostDescriptor* host)
  396. {
  397. return (host != nullptr) ? new REV1Plugin(host, false) : nullptr;
  398. }
  399. static void _cleanup(NativePluginHandle handle)
  400. {
  401. delete (REV1Plugin*)handle;
  402. }
  403. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(REV1Plugin)
  404. };
  405. // -----------------------------------------------------------------------
  406. static const NativePluginDescriptor rev1AmbisonicDesc = {
  407. /* category */ NATIVE_PLUGIN_CATEGORY_DELAY,
  408. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  409. |NATIVE_PLUGIN_HAS_UI
  410. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  411. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  412. |NATIVE_PLUGIN_USES_PARENT_ID),
  413. /* supports */ static_cast<NativePluginSupports>(0x0),
  414. /* audioIns */ 2,
  415. /* audioOuts */ 4,
  416. /* midiIns */ 0,
  417. /* midiOuts */ 0,
  418. /* paramIns */ REV1Plugin::kParameterNROTARY,
  419. /* paramOuts */ 0,
  420. /* name */ "REV1 (Ambisonic)",
  421. /* label */ "rev1-ambisonic",
  422. /* maker */ "falkTX, Fons Adriaensen",
  423. /* copyright */ "GPL v2+",
  424. REV1Plugin::_instantiateAmbisonic,
  425. REV1Plugin::_cleanup,
  426. REV1Plugin::_get_parameter_count,
  427. REV1Plugin::_get_parameter_info,
  428. REV1Plugin::_get_parameter_value,
  429. REV1Plugin::_get_parameter_text,
  430. REV1Plugin::_get_midi_program_count,
  431. REV1Plugin::_get_midi_program_info,
  432. REV1Plugin::_set_parameter_value,
  433. REV1Plugin::_set_midi_program,
  434. REV1Plugin::_set_custom_data,
  435. REV1Plugin::_ui_show,
  436. REV1Plugin::_ui_idle,
  437. REV1Plugin::_ui_set_parameter_value,
  438. REV1Plugin::_ui_set_midi_program,
  439. REV1Plugin::_ui_set_custom_data,
  440. REV1Plugin::_activate,
  441. REV1Plugin::_deactivate,
  442. REV1Plugin::_process,
  443. REV1Plugin::_get_state,
  444. REV1Plugin::_set_state,
  445. REV1Plugin::_dispatcher
  446. };
  447. static const NativePluginDescriptor rev1StereoDesc = {
  448. /* category */ NATIVE_PLUGIN_CATEGORY_DELAY,
  449. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_RTSAFE
  450. |NATIVE_PLUGIN_HAS_UI
  451. |NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS
  452. |NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD
  453. |NATIVE_PLUGIN_USES_PARENT_ID),
  454. /* supports */ static_cast<NativePluginSupports>(0x0),
  455. /* audioIns */ 2,
  456. /* audioOuts */ 2,
  457. /* midiIns */ 0,
  458. /* midiOuts */ 0,
  459. /* paramIns */ REV1Plugin::kParameterNROTARY,
  460. /* paramOuts */ 0,
  461. /* name */ "REV1 (Stereo)",
  462. /* label */ "rev1-stereo",
  463. /* maker */ "falkTX, Fons Adriaensen",
  464. /* copyright */ "GPL v2+",
  465. REV1Plugin::_instantiateStereo,
  466. REV1Plugin::_cleanup,
  467. REV1Plugin::_get_parameter_count,
  468. REV1Plugin::_get_parameter_info,
  469. REV1Plugin::_get_parameter_value,
  470. REV1Plugin::_get_parameter_text,
  471. REV1Plugin::_get_midi_program_count,
  472. REV1Plugin::_get_midi_program_info,
  473. REV1Plugin::_set_parameter_value,
  474. REV1Plugin::_set_midi_program,
  475. REV1Plugin::_set_custom_data,
  476. REV1Plugin::_ui_show,
  477. REV1Plugin::_ui_idle,
  478. REV1Plugin::_ui_set_parameter_value,
  479. REV1Plugin::_ui_set_midi_program,
  480. REV1Plugin::_ui_set_custom_data,
  481. REV1Plugin::_activate,
  482. REV1Plugin::_deactivate,
  483. REV1Plugin::_process,
  484. REV1Plugin::_get_state,
  485. REV1Plugin::_set_state,
  486. REV1Plugin::_dispatcher
  487. };
  488. // -----------------------------------------------------------------------
  489. CARLA_EXPORT
  490. void carla_register_native_plugin_zita_rev1();
  491. CARLA_EXPORT
  492. void carla_register_native_plugin_zita_rev1()
  493. {
  494. carla_register_native_plugin(&rev1AmbisonicDesc);
  495. carla_register_native_plugin(&rev1StereoDesc);
  496. }
  497. // -----------------------------------------------------------------------